2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 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 Map<Object, Boolean> isMarkedAsExportedCache;
35 ExportedSymbols(final Bck2Brwsr.Resources resources) {
36 this.resources = resources;
38 isMarkedAsExportedCache = new HashMap<Object, Boolean>();
41 boolean isExported(ClassData classData) throws IOException {
42 return classData.isPublic() && isMarkedAsExportedPackage(
43 classData.getPkgName())
44 || isMarkedAsExported(classData);
47 boolean isExported(MethodData methodData) throws IOException {
48 return isAccessible(methodData.access) && isExported(methodData.cls)
49 || isMarkedAsExported(methodData);
52 boolean isExported(FieldData fieldData) throws IOException {
53 return isAccessible(fieldData.access) && isExported(fieldData.cls)
54 || isMarkedAsExported(fieldData);
57 private boolean isMarkedAsExportedPackage(String pkgName) {
58 if (pkgName == null) {
62 final Boolean cachedValue = isMarkedAsExportedCache.get(pkgName);
63 if (cachedValue != null) {
67 final boolean newValue = resolveIsMarkedAsExportedPackage(pkgName);
68 isMarkedAsExportedCache.put(pkgName, newValue);
73 private boolean isMarkedAsExported(ClassData classData)
75 final Boolean cachedValue = isMarkedAsExportedCache.get(classData);
76 if (cachedValue != null) {
80 final boolean newValue =
81 isMarkedAsExported(classData.findAnnotationData(true),
83 isMarkedAsExportedCache.put(classData, newValue);
88 private boolean isMarkedAsExported(MethodData methodData)
90 return isMarkedAsExported(methodData.findAnnotationData(true),
94 private boolean isMarkedAsExported(FieldData fieldData)
96 return isMarkedAsExported(fieldData.findAnnotationData(true),
100 private boolean resolveIsMarkedAsExportedPackage(String pkgName) {
102 final InputStream is =
103 resources.get(pkgName + "/package-info.class");
109 final ClassData pkgInfoClass = new ClassData(is);
110 return isMarkedAsExported(
111 pkgInfoClass.findAnnotationData(true),
116 } catch (final IOException e) {
121 private boolean isMarkedAsExported(byte[] arrData, ClassData cd)
123 if (arrData == null) {
127 final boolean[] found = { false };
128 final AnnotationParser annotationParser =
129 new AnnotationParser(false, false) {
131 protected void visitAnnotationStart(
134 if (top && type.equals("Lorg/apidesign/bck2brwsr"
135 + "/core/Exported;")) {
140 annotationParser.parse(arrData, cd);
144 private static boolean isAccessible(int access) {
145 return (access & (ByteCodeParser.ACC_PUBLIC
146 | ByteCodeParser.ACC_PROTECTED)) != 0;