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.jar.JarEntry;
27  import java.util.jar.JarFile;
28  import java.util.jar.Manifest;
29  
30  import org.codehaus.plexus.languages.java.version.JavaVersion;
31  
32  abstract class AbstractBinaryModuleInfoParser implements ModuleInfoParser {
33      @Override
34      public JavaModuleDescriptor getModuleDescriptor(Path modulePath) throws IOException {
35          return getModuleDescriptor(modulePath, JavaVersion.JAVA_SPECIFICATION_VERSION);
36      }
37  
38      @Override
39      public JavaModuleDescriptor getModuleDescriptor(Path modulePath, JavaVersion jdkVersion) throws IOException {
40          JavaModuleDescriptor descriptor;
41          if (Files.isDirectory(modulePath)) {
42              Path moduleInfo = modulePath.resolve("module-info.class");
43              if (!Files.exists(moduleInfo)) {
44                  Path versionedModuleInfo = findVersionedModuleInfo(modulePath, jdkVersion);
45                  if (versionedModuleInfo != null) {
46                      moduleInfo = versionedModuleInfo;
47                  }
48              }
49              try (InputStream in = Files.newInputStream(moduleInfo)) {
50                  descriptor = parse(in);
51              }
52          } else {
53              try (JarFile jarFile = new JarFile(modulePath.toFile())) {
54                  JarEntry moduleInfo;
55                  if (modulePath.toString().toLowerCase().endsWith(".jmod")) {
56                      moduleInfo = jarFile.getJarEntry("classes/module-info.class");
57                  } else {
58                      moduleInfo = jarFile.getJarEntry("module-info.class");
59  
60                      if (moduleInfo == null) {
61                          Manifest manifest = jarFile.getManifest();
62  
63                          if (manifest != null
64                                  && "true"
65                                          .equalsIgnoreCase(
66                                                  manifest.getMainAttributes().getValue("Multi-Release"))) {
67                              int javaVersion = jdkVersion.getMajorVersion();
68  
69                              for (int version = javaVersion; version >= 9; version--) {
70                                  String resource = "META-INF/versions/" + version + "/module-info.class";
71                                  JarEntry entry = jarFile.getJarEntry(resource);
72                                  if (entry != null) {
73                                      moduleInfo = entry;
74                                      break;
75                                  }
76                              }
77                          }
78                      }
79                  }
80  
81                  if (moduleInfo != null) {
82                      descriptor = parse(jarFile.getInputStream(moduleInfo));
83                  } else {
84                      descriptor = null;
85                  }
86              }
87          }
88          return descriptor;
89      }
90  
91      /**
92       * Finds the module descriptor of a multi-release output directory, which has no {@code module-info.class} at its
93       * root but under {@code META-INF/versions/<N>}, the way a multi-release jar does.
94       *
95       * @param directory the output directory
96       * @param jdkVersion the highest version to consider
97       * @return the path of the highest applicable versioned descriptor, or {@code null} if there is none
98       */
99      static Path findVersionedModuleInfo(Path directory, JavaVersion jdkVersion) {
100         int javaVersion = jdkVersion.getMajorVersion();
101 
102         for (int version = javaVersion; version >= 9; version--) {
103             Path moduleInfo = directory.resolve("META-INF/versions/" + version + "/module-info.class");
104             if (Files.exists(moduleInfo)) {
105                 return moduleInfo;
106             }
107         }
108         return null;
109     }
110 
111     abstract JavaModuleDescriptor parse(InputStream in) throws IOException;
112 }