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
24import java.io.InputStream;
25import java.io.IOException;
26import java.lang.module.ModuleReader;
27import java.lang.module.ModuleReference;
28import java.net.URI;
29import java.nio.file.Files;
30import java.nio.file.Path;
31import java.nio.file.Paths;
32import jdk.internal.module.ClassFileConstants;
33import jdk.internal.module.ClassFileAttributes;
34import jdk.internal.module.ClassFileAttributes.ModuleTargetAttribute;
35import jdk.internal.org.objectweb.asm.Attribute;
36import jdk.internal.org.objectweb.asm.ClassReader;
37import jdk.internal.org.objectweb.asm.ClassVisitor;
38import jdk.internal.org.objectweb.asm.Opcodes;
39
40public 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}
97