2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.vm4brwsr;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.HashMap;
24 import org.apidesign.bck2brwsr.core.ExtraJavaScript;
25 import org.apidesign.vm4brwsr.ByteCodeParser.AnnotationParser;
26 import org.apidesign.vm4brwsr.ByteCodeParser.ClassData;
27 import org.apidesign.vm4brwsr.ByteCodeParser.FieldData;
28 import org.apidesign.vm4brwsr.ByteCodeParser.MethodData;
30 @ExtraJavaScript(processByteCode = false, resource="")
31 final class ExportedSymbols {
32 private final Bck2Brwsr.Resources resources;
33 private final StringArray exported;
34 private final Map<Object, Boolean> isMarkedAsExportedCache;
36 ExportedSymbols(final Bck2Brwsr.Resources resources, StringArray explicitlyExported) {
37 this.resources = resources;
38 this.exported = explicitlyExported;
40 isMarkedAsExportedCache = new HashMap<Object, Boolean>();
43 boolean isExported(ClassData classData) throws IOException {
44 if (exported.contains(classData.getClassName())) {
47 return classData.isPublic() && isMarkedAsExportedPackage(
48 classData.getPkgName())
49 || isMarkedAsExported(classData);
52 boolean isExported(MethodData methodData) throws IOException {
53 return isAccessible(methodData.access) && isExported(methodData.cls)
54 || isMarkedAsExported(methodData);
57 boolean isExported(FieldData fieldData) throws IOException {
59 isAccessible(fieldData.access) &&
60 isExported(fieldData.cls) || isMarkedAsExported(fieldData)
65 fieldData.isStatic() && fieldData.getName().equals("$VALUES") &&
66 "java/lang/Enum".equals(fieldData.cls.getSuperClassName())
68 // enum values need to be exported
74 private boolean isMarkedAsExportedPackage(String pkgName) {
75 if (pkgName == null) {
78 if (pkgName.startsWith("java/")) {
82 final Boolean cachedValue = isMarkedAsExportedCache.get(pkgName);
83 if (cachedValue != null) {
87 final boolean newValue = resolveIsMarkedAsExportedPackage(pkgName);
88 isMarkedAsExportedCache.put(pkgName, newValue);
93 private boolean isMarkedAsExported(ClassData classData)
95 final Boolean cachedValue = isMarkedAsExportedCache.get(classData);
96 if (cachedValue != null) {
100 final boolean newValue =
101 isMarkedAsExported(classData.findAnnotationData(true),
103 isMarkedAsExportedCache.put(classData, newValue);
108 private boolean isMarkedAsExported(MethodData methodData)
110 return isMarkedAsExported(methodData.findAnnotationData(true),
114 private boolean isMarkedAsExported(FieldData fieldData)
116 return isMarkedAsExported(fieldData.findAnnotationData(true),
120 private boolean resolveIsMarkedAsExportedPackage(String pkgName) {
121 if (exported.contains(pkgName + '/')) {
125 final InputStream is =
126 resources.get(pkgName + "/package-info.class");
132 final ClassData pkgInfoClass = new ClassData(is);
133 return isMarkedAsExported(
134 pkgInfoClass.findAnnotationData(true),
139 } catch (final IOException e) {
144 static boolean isMarkedAsExported(byte[] arrData, ClassData cd)
146 if (arrData == null) {
150 final boolean[] found = { false };
151 final AnnotationParser annotationParser =
152 new AnnotationParser(false, false) {
154 protected void visitAnnotationStart(
157 if (top && type.equals("Lorg/apidesign/bck2brwsr"
158 + "/core/Exported;")) {
163 annotationParser.parse(arrData, cd);
167 private static boolean isAccessible(int access) {
168 return (access & (ByteCodeParser.ACC_PUBLIC
169 | ByteCodeParser.ACC_PROTECTED)) != 0;