< prev index next >

test/jdk/lib/testlibrary/ModuleTargetHelper.java

Print this page
rev 47454 : [mq]: jdk-new-asm-test.patch


  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.InputStream;
  25 import java.io.IOException;
  26 import java.lang.module.ModuleReader;
  27 import java.lang.module.ModuleReference;
  28 import java.net.URI;
  29 import java.nio.file.Files;
  30 import java.nio.file.Path;
  31 import java.nio.file.Paths;
  32 import jdk.internal.module.ClassFileConstants;
  33 import jdk.internal.module.ClassFileAttributes;
  34 import jdk.internal.module.ClassFileAttributes.ModuleTargetAttribute;
  35 import jdk.internal.org.objectweb.asm.Attribute;
  36 import jdk.internal.org.objectweb.asm.ClassReader;
  37 import jdk.internal.org.objectweb.asm.ClassVisitor;
  38 import jdk.internal.org.objectweb.asm.Opcodes;
  39 



  40 public class ModuleTargetHelper {
  41     private ModuleTargetHelper() {}
  42 
  43     public static final class ModuleTarget {
  44         private String targetPlatform;
  45 
  46         public ModuleTarget(String targetPlatform) {
  47             this.targetPlatform = targetPlatform;
  48         }
  49 
  50         public String targetPlatform() {
  51             return targetPlatform;
  52         }
  53     }
  54 
  55     public static ModuleTarget getJavaBaseTarget() throws IOException {
  56         Path p = Paths.get(URI.create("jrt:/modules/java.base/module-info.class"));
  57         try (InputStream in = Files.newInputStream(p)) {
  58             return read(in);
  59         }
  60     }
  61 
  62     public static ModuleTarget read(InputStream in) throws IOException {
  63         ModuleTargetAttribute[] modTargets = new ModuleTargetAttribute[1];
  64         ClassVisitor cv = new ClassVisitor(Opcodes.ASM5) {
  65             @Override
  66             public void visitAttribute(Attribute attr) {
  67                 if (attr instanceof ModuleTargetAttribute) {
  68                     modTargets[0] = (ModuleTargetAttribute)attr;
  69                 }
  70             }
  71         };
  72 
  73         // prototype of attributes that should be parsed
  74         Attribute[] attrs = new Attribute[] {
  75             new ModuleTargetAttribute()
  76         };
  77 
  78         // parse module-info.class
  79         ClassReader cr = new ClassReader(in);
  80         cr.accept(cv, attrs, 0);
  81         if (modTargets[0] != null) {
  82             return new ModuleTarget(modTargets[0].targetPlatform());
  83         }
  84 
  85         return null;
  86     }
  87 
  88     public static ModuleTarget read(ModuleReference modRef) throws IOException {
  89         ModuleReader reader = modRef.open();
  90         try (InputStream in = reader.open("module-info.class").get()) {
  91             return read(in);
  92         } finally {
  93             reader.close();
  94         }
  95     }
  96 }


  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.InputStream;
  25 import java.io.IOException;
  26 import java.lang.module.ModuleReader;
  27 import java.lang.module.ModuleReference;
  28 import java.net.URI;
  29 import java.nio.file.Files;
  30 import java.nio.file.Path;
  31 import java.nio.file.Paths;







  32 
  33 import jdk.internal.module.ModuleInfo;
  34 import jdk.internal.module.ModuleInfo.Attributes;
  35 
  36 public class ModuleTargetHelper {
  37     private ModuleTargetHelper() {}
  38 
  39     public static final class ModuleTarget {
  40         private String targetPlatform;
  41 
  42         public ModuleTarget(String targetPlatform) {
  43             this.targetPlatform = targetPlatform;
  44         }
  45 
  46         public String targetPlatform() {
  47             return targetPlatform;
  48         }
  49     }
  50 
  51     public static ModuleTarget getJavaBaseTarget() throws IOException {
  52         Path p = Paths.get(URI.create("jrt:/modules/java.base/module-info.class"));
  53         try (InputStream in = Files.newInputStream(p)) {
  54             return read(in);
  55         }
  56     }
  57 
  58     public static ModuleTarget read(InputStream in) throws IOException {
  59         ModuleInfo.Attributes attrs = ModuleInfo.read(in, null);
  60         if (attrs.target() != null) {
  61             return new ModuleTarget(attrs.target().targetPlatform());
  62         } else {
  63             return null;

  64         }
  65     }

  66 















  67     public static ModuleTarget read(ModuleReference modRef) throws IOException {
  68         ModuleReader reader = modRef.open();
  69         try (InputStream in = reader.open("module-info.class").get()) {
  70             return read(in);
  71         } finally {
  72             reader.close();
  73         }
  74     }
  75 }
< prev index next >