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.FileInputStream;
24  import java.io.FileNotFoundException;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.util.jar.JarEntry;
28  import java.util.jar.JarFile;
29  
30  import org.codehaus.plexus.component.annotations.Component;
31  import org.objectweb.asm.ClassReader;
32  import org.objectweb.asm.ClassVisitor;
33  //import org.objectweb.asm.ModuleVisitor;
34  import org.objectweb.asm.Opcodes;
35  
36  /**
37   * Extract information from module with ASM
38   * 
39   * @author Robert Scholte
40   * @since 3.6
41   */
42  @Component( role = ModuleInfoParser.class, hint = "asm" )
43  public class AsmModuleInfoParser
44      implements ModuleInfoParser
45  {
46      @Override
47      public Type getType()
48      {
49          return Type.CLASS;
50      }
51  
52      
53      @Override
54      public JavaModuleDescriptor getModuleDescriptor( File modulePath )
55          throws IOException
56      {
57          final JavaModuleDescriptorWrapper wrapper = new JavaModuleDescriptorWrapper();
58  
59          InputStream in = getModuleInfoClass( modulePath );
60  
61          if ( in != null )
62          {
63              ClassReader reader = new ClassReader( in );
64              reader.accept( new ClassVisitor( Opcodes.ASM6 )
65              {
66  //  REQUIRES ASM 6.0_ALPHA2
67                  
68  //                @Override
69  //                public ModuleVisitor visitModule( String name, int arg1, String arg2 )
70  //                {
71  //                    wrapper.builder = JavaModuleDescriptor.newModule( name );
72  //
73  //                    return new ModuleVisitor( Opcodes.ASM6 )
74  //                    {
75  //                        @Override
76  //                        public void visitRequire( String module, int access, String version )
77  //                        {
78  //                            wrapper.builder.requires( module );
79  //                        }
80  //                    };
81  //                }
82              }, 0 );
83  
84              in.close();
85          }
86          else
87          {
88              wrapper.builder = JavaModuleDescriptor.newAutomaticModule( null );
89          }
90  
91          return wrapper.builder.build();
92      }
93  
94      private InputStream getModuleInfoClass( File modulePath )
95          throws FileNotFoundException, IOException
96      {
97          InputStream in;
98          if ( modulePath.isDirectory() )
99          {
100             in = new FileInputStream( new File( modulePath, "module-info.class" ) );
101         }
102         else
103         {
104             // 
105             JarFile jarFile = new JarFile( modulePath );
106             JarEntry moduleInfo = jarFile.getJarEntry( "/module-info.class" );
107             
108             if ( moduleInfo != null )
109             {
110                 in = jarFile.getInputStream( moduleInfo );
111             }
112             else
113             {
114                 in = null;
115             }
116         }
117         return in;
118     }
119     
120     private static class JavaModuleDescriptorWrapper 
121     {
122         private JavaModuleDescriptor.Builder builder;
123     }
124 }