< prev index next >

test/tools/jlink/plugins/SystemModuleDescriptors/src/m1/p1/Main.java

Print this page

        

@@ -21,10 +21,11 @@
  * questions.
  */
 
 package p1;
 
+import java.io.InputStream;
 import java.io.IOException;
 import java.lang.module.ModuleDescriptor;
 import java.lang.reflect.Layer;
 import java.lang.reflect.Module;
 import java.net.URI;

@@ -33,25 +34,57 @@
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.Collections;
 import java.util.Set;
 
+import jdk.internal.module.ClassFileAttributes;
+import jdk.internal.module.ClassFileAttributes.ModuleTargetAttribute;
+import jdk.internal.module.ClassFileConstants;
+import jdk.internal.org.objectweb.asm.Attribute;
+import jdk.internal.org.objectweb.asm.ClassReader;
+import jdk.internal.org.objectweb.asm.ClassVisitor;
+import jdk.internal.org.objectweb.asm.Opcodes;
+
 public class Main {
+    private static boolean hasModuleTarget(InputStream in) throws IOException {
+        ModuleTargetAttribute[] modTargets = new ModuleTargetAttribute[1];
+        ClassVisitor cv = new ClassVisitor(Opcodes.ASM5) {
+            @Override
+            public void visitAttribute(Attribute attr) {
+                if (attr instanceof ModuleTargetAttribute) {
+                    modTargets[0] = (ModuleTargetAttribute)attr;
+                }
+            }
+        };
+
+        // prototype of attributes that should be parsed
+        Attribute[] attrs = new Attribute[] {
+            new ModuleTargetAttribute()
+        };
+
+        // parse module-info.class
+        ClassReader cr = new ClassReader(in);
+        cr.accept(cv, attrs, 0);
+        return modTargets[0] != null &&
+            (modTargets[0].osName() != null || modTargets[0].osArch() != null);
+    }
+
     public static void main(String... args) throws Exception {
         // load another package
         p2.T.test();
 
         // validate the module descriptor
         validate(Main.class.getModule());
 
         // validate the Moduletarget attribute for java.base
-        ModuleDescriptor md = Layer.boot().findModule("java.base").get()
-                                   .getDescriptor();
-        if (!md.osName().isPresent() || !md.osArch().isPresent() ||
-                !md.osVersion().isPresent()) {
-            throw new RuntimeException("java.base: " + md.osName() + " " +
-                    md.osArch() + " " + md.osVersion());
+        FileSystem fs = FileSystems.newFileSystem(URI.create("jrt:/"),
+                                                  Collections.emptyMap());
+        Path path = fs.getPath("/", "modules", "java.base", "module-info.class");
+        try (InputStream in = Files.newInputStream(path)) {
+            if (! hasModuleTarget(in)) {
+                throw new RuntimeException("Missing ModuleTarget for java.base");
+            }
         }
     }
 
     static void validate(Module module) throws IOException {
         ModuleDescriptor md = module.getDescriptor();

@@ -65,23 +98,22 @@
 
         // check the module descriptor of a system module and read from jimage
         checkPackages(md.packages(), "p1", "p2");
         checkPackages(md1.packages(), "p1", "p2");
 
-        // check ModuleTarget attribute
-        checkModuleTargetAttribute(md);
-        checkModuleTargetAttribute(md1);
+        try (InputStream in = Files.newInputStream(path)) {
+            checkModuleTargetAttribute(in, "p1");
+        }
     }
 
     static void checkPackages(Set<String> pkgs, String... expected) {
         if (!pkgs.equals(Set.of(expected))) {
             throw new RuntimeException(pkgs + " expected: " + Set.of(expected));
         }
     }
 
-    static void checkModuleTargetAttribute(ModuleDescriptor md) {
-        if (md.osName().isPresent() || md.osArch().isPresent() ||
-                md.osVersion().isPresent()) {
-            throw new RuntimeException(md.osName() + " " + md.osArch() + " " + md.osVersion());
+    static void checkModuleTargetAttribute(InputStream in, String modName) throws IOException {
+        if (hasModuleTarget(in)) {
+            throw new RuntimeException("ModuleTarget present for " + modName);
         }
     }
 }
< prev index next >