View Javadoc
1   package org.codehaus.plexus.languages.java.jpms;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.nio.file.Path;
24  import java.nio.file.Paths;
25  import java.util.Arrays;
26  import java.util.Collection;
27  import java.util.Collections;
28  
29  /**
30   * Contains all information required to analyze the project
31   *
32   * @author Robert Scholte
33   * @since 1.0.0
34   */
35  public abstract class ResolvePathsRequest<T> {
36      private Path jdkHome;
37  
38      private Path mainModuleDescriptor;
39  
40      private Collection<T> pathElements;
41  
42      private Collection<String> additionalModules;
43  
44      private boolean includeAllProviders;
45  
46      private JavaModuleDescriptor resolvedMainModuleDescriptor;
47  
48      private boolean includeStatic;
49  
50      private ResolvePathsRequest() {}
51  
52      /**
53       * @deprecated use {@link #ofFiles(Collection)} instead
54       */
55      @Deprecated
56      public static ResolvePathsRequest<File> withFiles(Collection<File> files) {
57          return ofFiles(files);
58      }
59  
60      public static ResolvePathsRequest<File> ofFiles(File... files) {
61          return ofFiles(Arrays.asList(files));
62      }
63  
64      public static ResolvePathsRequest<File> ofFiles(Collection<File> files) {
65          ResolvePathsRequest<File> request = new ResolvePathsRequest<File>() {
66              @Override
67              protected Path toPath(File t) {
68                  return t.toPath();
69              }
70          };
71  
72          request.pathElements = files;
73          return request;
74      }
75  
76      /**
77       * @deprecated use {@link #ofPaths(Collection)} instead
78       */
79      @Deprecated
80      public static ResolvePathsRequest<Path> withPaths(Collection<Path> paths) {
81          return ofPaths(paths);
82      }
83  
84      public static ResolvePathsRequest<Path> ofPaths(Path... paths) {
85          return ofPaths(Arrays.asList(paths));
86      }
87  
88      public static ResolvePathsRequest<Path> ofPaths(Collection<Path> paths) {
89          ResolvePathsRequest<Path> request = new ResolvePathsRequest<Path>() {
90              @Override
91              protected Path toPath(Path t) {
92                  return t;
93              }
94          };
95          request.pathElements = paths;
96          return request;
97      }
98  
99      /**
100      * @deprecated use {@link #ofStrings(Collection)} instead
101      */
102     @Deprecated
103     public static ResolvePathsRequest<String> withStrings(Collection<String> strings) {
104         return ofStrings(strings);
105     }
106 
107     public static ResolvePathsRequest<String> ofStrings(String... strings) {
108         return ofStrings(Arrays.asList(strings));
109     }
110 
111     public static ResolvePathsRequest<String> ofStrings(Collection<String> strings) {
112         ResolvePathsRequest<String> request = new ResolvePathsRequest<String>() {
113             @Override
114             protected Path toPath(String t) {
115                 return Paths.get(t);
116             }
117         };
118         request.pathElements = strings;
119         return request;
120     }
121 
122     protected abstract Path toPath(T t);
123 
124     final ResolvePathsResult<T> createResult() {
125         return new ResolvePathsResult<>();
126     }
127 
128     public Path getMainModuleDescriptor() {
129         return mainModuleDescriptor;
130     }
131 
132     public JavaModuleDescriptor getModuleDescriptor() {
133         return resolvedMainModuleDescriptor;
134     }
135 
136     /**
137      * Must be either {@code module-info.java} or {@code module-info.class}
138      *
139      * @param mainModuleDescriptor
140      * @return this request
141      */
142     public ResolvePathsRequest<T> setMainModuleDescriptor(T mainModuleDescriptor) {
143         this.mainModuleDescriptor = toPath(mainModuleDescriptor);
144         return this;
145     }
146 
147     /***
148      * Provide a resolved module descriptor
149      *
150      * @param mainModuleDescriptor
151      * @return this request
152      */
153     public ResolvePathsRequest<T> setModuleDescriptor(JavaModuleDescriptor mainModuleDescriptor) {
154         this.resolvedMainModuleDescriptor = mainModuleDescriptor;
155         return this;
156     }
157 
158     public Collection<T> getPathElements() {
159         return pathElements;
160     }
161 
162     /**
163      * In case the JRE is Java 8 or before, this jdkHome is used to extract the module name.
164      *
165      * @param jdkHome
166      * @return this request
167      */
168     public ResolvePathsRequest<T> setJdkHome(T jdkHome) {
169         this.jdkHome = toPath(jdkHome);
170         return this;
171     }
172 
173     public Path getJdkHome() {
174         return jdkHome;
175     }
176 
177     /**
178      * The module names that are usually passed with {@code --add-modules}
179      *
180      * @param additionalModules
181      * @return this request
182      */
183     public ResolvePathsRequest<T> setAdditionalModules(Collection<String> additionalModules) {
184         this.additionalModules = additionalModules;
185         return this;
186     }
187 
188     public Collection<String> getAdditionalModules() {
189         if (additionalModules == null) {
190             additionalModules = Collections.emptyList();
191         }
192         return additionalModules;
193     }
194 
195     /**
196      * Will also include all modules that contain providers for used services, should only be used at runtime (not during compile nor test)
197      *
198      * @param includeAllProviders
199      * @return this request
200      */
201     public ResolvePathsRequest<T> setIncludeAllProviders(boolean includeAllProviders) {
202         this.includeAllProviders = includeAllProviders;
203         return this;
204     }
205 
206     public boolean isIncludeAllProviders() {
207         return includeAllProviders;
208     }
209 
210     /**
211      *
212      * @return <code>true</code> if the result will include all static dependencies
213      * @since 1.0.5
214      */
215     public boolean isIncludeStatic() {
216         return includeStatic;
217     }
218 
219     /**
220      *
221      * @param includeStatic <code>true</code> if the result must include all static dependencies
222      * @return this request
223      * @since 1.0.5
224      */
225     public ResolvePathsRequest<T> setIncludeStatic(boolean includeStatic) {
226         this.includeStatic = includeStatic;
227         return this;
228     }
229 }