1 package org.apache.maven.plugin.compiler.module;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
32
33
34
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
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
86 }
87 catch ( NoSuchMethodException e )
88 {
89 e.printStackTrace();
90 }
91 catch ( SecurityException e )
92 {
93
94 }
95 catch ( IllegalAccessException e )
96 {
97
98 }
99 catch ( IllegalArgumentException e )
100 {
101
102 }
103 catch ( InvocationTargetException e )
104 {
105
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 }