1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  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 osName, osArch;
  45 
  46         public ModuleTarget(String osName, String osArch) {
  47             this.osName = osName;
  48             this.osArch = osArch;
  49         }
  50 
  51         public String osName() {
  52             return osName;
  53         }
  54 
  55         public String osArch() {
  56             return osArch;
  57         }
  58     }
  59 
  60     public static ModuleTarget getJavaBaseTarget() throws IOException {
  61         Path p = Paths.get(URI.create("jrt:/modules/java.base/module-info.class"));
  62         try (InputStream in = Files.newInputStream(p)) {
  63             return read(in);
  64         }
  65     }
  66 
  67     public static ModuleTarget read(InputStream in) throws IOException {
  68         ModuleTargetAttribute[] modTargets = new ModuleTargetAttribute[1];
  69         ClassVisitor cv = new ClassVisitor(Opcodes.ASM5) {
  70             @Override
  71             public void visitAttribute(Attribute attr) {
  72                 if (attr instanceof ModuleTargetAttribute) {
  73                     modTargets[0] = (ModuleTargetAttribute)attr;
  74                 }
  75             }
  76         };
  77 
  78         // prototype of attributes that should be parsed
  79         Attribute[] attrs = new Attribute[] {
  80             new ModuleTargetAttribute()
  81         };
  82 
  83         // parse module-info.class
  84         ClassReader cr = new ClassReader(in);
  85         cr.accept(cv, attrs, 0);
  86         if (modTargets[0] != null) {
  87             return new ModuleTarget(modTargets[0].osName(), modTargets[0].osArch());
  88         }
  89 
  90         return null;
  91     }
  92 
  93     public static ModuleTarget read(ModuleReference modRef) throws IOException {
  94         ModuleReader reader = modRef.open();
  95         try (InputStream in = reader.open("module-info.class").get()) {
  96             return read(in);
  97         } finally {
  98             reader.close();
  99         }
 100     }
 101 }