View Javadoc
1   package org.apache.maven.plugin.compiler.module;
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.io.IOException;
24  import java.lang.reflect.InvocationTargetException;
25  import java.lang.reflect.Method;
26  import java.util.Set;
27  
28  import org.codehaus.plexus.component.annotations.Component;
29  
30  /**
31   * This class is could be replaced with a Java 9 MultiRelease implementation 
32   * 
33   * @author Robert Scholte
34   * @since 3.7.0
35   */
36  @Component( role = ModuleInfoParser.class, hint = "reflect" )
37  public class ReflectModuleInfoParser implements ModuleInfoParser
38  {
39      @Override
40      public Type getType()
41      {
42          return Type.CLASS;
43      }
44  
45      @Override
46      public JavaModuleDescriptor getModuleDescriptor( File modulePath )
47          throws IOException
48      {
49          JavaModuleDescriptor moduleDescriptor = null;
50          
51          try
52          {
53              // Use Java9 code to get moduleName, don't try to do it better with own implementation
54              Class moduleFinderClass = Class.forName( "java.lang.module.ModuleFinder" );
55  
56              java.nio.file.Path path = modulePath.toPath();
57  
58              Method ofMethod = moduleFinderClass.getMethod( "of", java.nio.file.Path[].class );
59              Object moduleFinderInstance = ofMethod.invoke( null, new Object[] { new java.nio.file.Path[] { path } } );
60  
61              Method findAllMethod = moduleFinderClass.getMethod( "findAll" );
62              Set<Object> moduleReferences = (Set<Object>) findAllMethod.invoke( moduleFinderInstance );
63  
64              Object moduleReference = moduleReferences.iterator().next();
65              Method descriptorMethod = moduleReference.getClass().getMethod( "descriptor" );
66              Object moduleDescriptorInstance = descriptorMethod.invoke( moduleReference );
67  
68              JavaModuleDescriptor.Builder builder = getBuilder( moduleDescriptorInstance );
69              
70              Method requiresMethod = moduleDescriptorInstance.getClass().getMethod( "requires" );
71              Set<Object> requires = (Set<Object>) requiresMethod.invoke( moduleDescriptorInstance );
72              
73              for ( Object requiresInstance : requires )
74              {
75                  Method nameMethod = requiresInstance.getClass().getMethod( "name" );
76                  String name = (String) nameMethod.invoke( requiresInstance );
77                  
78                  builder.requires( name );
79              }
80              
81              moduleDescriptor = builder.build();
82          }
83          catch ( ClassNotFoundException e )
84          {
85              // do nothing
86          }
87          catch ( NoSuchMethodException e )
88          {
89              e.printStackTrace();
90          }
91          catch ( SecurityException e )
92          {
93              // do nothing
94          }
95          catch ( IllegalAccessException e )
96          {
97              // do nothing
98          }
99          catch ( IllegalArgumentException e )
100         {
101             // do nothing
102         }
103         catch ( InvocationTargetException e )
104         {
105             // do nothing
106         }
107         return moduleDescriptor;
108     }
109 
110     private JavaModuleDescriptor.Builder getBuilder( Object moduleDescriptorInstance )
111         throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
112     {
113         JavaModuleDescriptor.Builder builder;
114         Method nameMethod = moduleDescriptorInstance.getClass().getMethod( "name" );
115         String name = (String) nameMethod.invoke( moduleDescriptorInstance );
116         
117         Method isAutomaticMethod = moduleDescriptorInstance.getClass().getMethod( "isAutomatic" );
118         boolean automatic = (Boolean) isAutomaticMethod.invoke( moduleDescriptorInstance );
119 
120         if ( automatic )
121         {
122             builder = JavaModuleDescriptor.newAutomaticModule( name );
123         }
124         else
125         {
126             builder = JavaModuleDescriptor.newModule( name );
127         }
128         return builder;
129     }
130 
131 }