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.Collection;
26  import java.util.Collections;
27  
28  /**
29   * Contains all information required to analyze the project
30   * 
31   * @author Robert Scholte
32   * @since 1.0.0
33   */
34  public abstract class ResolvePathsRequest<T>
35  {
36      private Path jdkHome;
37  
38      private Path mainModuleDescriptor;
39  
40      private Collection<T> pathElements;
41  
42      private Collection<String> additionalModules;
43  
44      private ResolvePathsRequest()
45      {
46      }
47  
48      /**
49       * @deprecated use {@link #ofFiles(Collection)} instead
50       */
51      @Deprecated
52      public static ResolvePathsRequest<File> withFiles( Collection<File> files )
53      {
54          return ofFiles( files );
55      }
56      
57      public static ResolvePathsRequest<File> ofFiles( Collection<File> files )
58      {
59          ResolvePathsRequest<File> request = new ResolvePathsRequest<File>()
60          {
61              @Override
62              protected Path toPath( File t )
63              {
64                  return t.toPath();
65              }
66          };
67  
68          request.pathElements = files;
69          return request;
70      }
71  
72      /**
73       * @deprecated use {@link #ofPaths(Collection)} instead
74       */
75      @Deprecated
76      public static ResolvePathsRequest<Path> withPaths( Collection<Path> paths )
77      {
78          return ofPaths( paths );
79      }
80      
81      public static ResolvePathsRequest<Path> ofPaths( Collection<Path> paths )
82      {
83          ResolvePathsRequest<Path> request = new ResolvePathsRequest<Path>() {
84              @Override
85              protected Path toPath( Path t )
86              {
87                  return t;
88              }
89          };
90          request.pathElements = paths;
91          return request;
92      }
93  
94      /**
95       * @deprecated use {@link #ofStrings(Collection)} instead
96       */
97      @Deprecated
98      public static ResolvePathsRequest<String> withStrings( Collection<String> strings )
99      {
100         return ofStrings( strings );
101     }
102     
103     public static ResolvePathsRequest<String> ofStrings( Collection<String> strings )
104     {
105         ResolvePathsRequest<String> request = new ResolvePathsRequest<String>() {
106             @Override
107             protected Path toPath( String t )
108             {
109                 return Paths.get( t );
110             }
111         };
112         request.pathElements = strings;
113         return request;
114     }
115 
116     protected abstract Path toPath( T t );
117 
118     final ResolvePathsResult<T> createResult() {
119         return new ResolvePathsResult<>();
120     }
121 
122     public Path getMainModuleDescriptor()
123     {
124         return mainModuleDescriptor;
125     }
126 
127     /**
128      * Must be either {@code module-info.java} or {@code module-info.class} 
129      * 
130      * @param mainModuleDescriptor
131      * @return this request
132      */
133     public ResolvePathsRequest<T> setMainModuleDescriptor( T mainModuleDescriptor )
134     {
135         this.mainModuleDescriptor = toPath( mainModuleDescriptor );
136         return this;
137     }
138 
139     public Collection<T> getPathElements()
140     {
141         return pathElements;
142     }
143 
144     /**
145      * In case the JRE is Java 8 or before, this jdkHome is used to extract the module name.
146      * 
147      * @param jdkHome
148      * @return this request
149      */
150     public ResolvePathsRequest<T> setJdkHome( T jdkHome )
151     {
152         this.jdkHome = toPath( jdkHome );
153         return this;
154     }
155 
156     public Path getJdkHome()
157     {
158         return jdkHome;
159     }
160 
161     /**
162      * The module names that are usually passed with {@code --add-modules}
163      * 
164      * @param additionalModules
165      * @return this request
166      */
167     public ResolvePathsRequest<T> setAdditionalModules( Collection<String> additionalModules )
168     {
169         this.additionalModules = additionalModules;
170         return this;
171     }
172 
173     public Collection<String> getAdditionalModules()
174     {
175         if ( additionalModules == null )
176         {
177             additionalModules = Collections.emptyList();
178         }
179         return additionalModules;
180     }
181 }