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.IOException;
23  import java.io.InputStream;
24  import java.nio.file.Files;
25  import java.nio.file.Path;
26  import java.util.Arrays;
27  import java.util.Enumeration;
28  import java.util.HashSet;
29  import java.util.jar.JarEntry;
30  import java.util.jar.JarFile;
31  import java.util.jar.Manifest;
32  import java.util.regex.Pattern;
33  
34  import org.objectweb.asm.ClassReader;
35  import org.objectweb.asm.ClassVisitor;
36  import org.objectweb.asm.ModuleVisitor;
37  import org.objectweb.asm.Opcodes;
38  
39  /**
40   * Extract information from module with ASM
41   * 
42   * @author Robert Scholte
43   * @since 1.0.0
44   */
45  public class AsmModuleInfoParser
46      implements ModuleInfoParser
47  {
48      private static final Pattern MRJAR_DESCRIPTOR = Pattern.compile( "META-INF/versions/[^/]+/module-info.class" );
49  
50      @Override
51      public JavaModuleDescriptor getModuleDescriptor( Path modulePath )
52          throws IOException
53      {
54          JavaModuleDescriptor descriptor;
55          if ( Files.isDirectory( modulePath ) )
56          {
57              try ( InputStream in = Files.newInputStream( modulePath.resolve( "module-info.class" ) ) )
58              {
59                  descriptor = parse( in );
60              }
61          }
62          else
63          {
64              try ( JarFile jarFile = new JarFile( modulePath.toFile() ) )
65              {
66                  JarEntry moduleInfo;
67                  if ( modulePath.toString().toLowerCase().endsWith( ".jmod" ) )
68                  {
69                      moduleInfo = jarFile.getJarEntry( "classes/module-info.class" );
70                  }
71                  else
72                  {
73                      moduleInfo = jarFile.getJarEntry( "module-info.class" );
74  
75                      if ( moduleInfo == null )
76                      {
77                          Manifest manifest =  jarFile.getManifest();
78  
79                          if ( manifest != null && "true".equalsIgnoreCase( manifest.getMainAttributes().getValue( "Multi-Release" ) ) ) 
80                          {
81                              // look for multirelease descriptor
82                              Enumeration<JarEntry> entryIter = jarFile.entries();
83                              while ( entryIter.hasMoreElements() )
84                              {
85                                  JarEntry entry = entryIter.nextElement();
86                                  if ( MRJAR_DESCRIPTOR.matcher( entry.getName() ).matches() )
87                                  {
88                                      moduleInfo = entry;
89                                      break;
90                                  }
91                              }
92                          }
93                      }
94                  }
95  
96                  if ( moduleInfo != null )
97                  {
98                      descriptor = parse( jarFile.getInputStream( moduleInfo ) );
99                  }
100                 else
101                 {
102                     descriptor = null;
103                 }
104             }
105         }
106         return descriptor;
107     }
108 
109     private JavaModuleDescriptor parse( InputStream in )
110         throws IOException
111     {
112         final JavaModuleDescriptorWrapper wrapper = new JavaModuleDescriptorWrapper();
113 
114         ClassReader reader = new ClassReader( in );
115         reader.accept( new ClassVisitor( Opcodes.ASM6 )
116         {
117             @Override
118             public ModuleVisitor visitModule( String name, int arg1, String arg2 )
119             {
120                 wrapper.builder = JavaModuleDescriptor.newModule( name );
121 
122                 return new ModuleVisitor( Opcodes.ASM6 )
123                 {
124                     @Override
125                     public void visitRequire( String module, int access, String version )
126                     {
127                         wrapper.builder.requires( module );
128                     }
129 
130                     @Override
131                     public void visitExport( String pn, int ms, String... targets )
132                     {
133                         if ( targets == null || targets.length == 0 )
134                         {
135                             wrapper.builder.exports( pn.replace( '/', '.' ) );
136                         }
137                         else
138                         {
139                             wrapper.builder.exports( pn.replace( '/', '.' ), new HashSet<>( Arrays.asList( targets ) ) );
140                         }
141                     }
142                 };
143             }
144         }, 0 );
145         return wrapper.builder.build();
146     }
147 
148     private static class JavaModuleDescriptorWrapper
149     {
150         private JavaModuleDescriptor.Builder builder;
151     }
152 }