rt/vm/src/main/java/org/apidesign/vm4brwsr/ExportedSymbols.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 26 Apr 2014 21:30:06 +0200
branchclosure
changeset 1491 4a1398eff4fb
parent 1029 b1fe994d4267
child 1561 f6200b8decc4
permissions -rw-r--r--
Different meaning of root vs. added classes. Ability to explicitly enumerate classes that should be exported and available with fully qualified name.
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 package org.apidesign.vm4brwsr;
    19 
    20 import java.io.IOException;
    21 import java.io.InputStream;
    22 import java.util.HashMap;
    23 import java.util.Map;
    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;
    29 
    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;
    35 
    36     ExportedSymbols(final Bck2Brwsr.Resources resources, StringArray explicitlyExported) {
    37         this.resources = resources;
    38         this.exported = explicitlyExported;
    39 
    40         isMarkedAsExportedCache = new HashMap<Object, Boolean>();
    41     }
    42 
    43     boolean isExported(ClassData classData) throws IOException {
    44         if (exported.contains(classData.getClassName())) {
    45             return true;
    46         }
    47         return classData.isPublic() && isMarkedAsExportedPackage(
    48                                            classData.getPkgName())
    49                    || isMarkedAsExported(classData);
    50     }
    51 
    52     boolean isExported(MethodData methodData) throws IOException {
    53         return isAccessible(methodData.access) && isExported(methodData.cls)
    54                    || isMarkedAsExported(methodData);
    55     }
    56 
    57     boolean isExported(FieldData fieldData) throws IOException {
    58         return isAccessible(fieldData.access) && isExported(fieldData.cls)
    59                    || isMarkedAsExported(fieldData);
    60     }
    61 
    62     private boolean isMarkedAsExportedPackage(String pkgName) {
    63         if (pkgName == null) {
    64             return false;
    65         }
    66 
    67         final Boolean cachedValue = isMarkedAsExportedCache.get(pkgName);
    68         if (cachedValue != null) {
    69             return cachedValue;
    70         }
    71 
    72         final boolean newValue = resolveIsMarkedAsExportedPackage(pkgName);
    73         isMarkedAsExportedCache.put(pkgName, newValue);
    74 
    75         return newValue;
    76     }
    77 
    78     private boolean isMarkedAsExported(ClassData classData)
    79             throws IOException {
    80         final Boolean cachedValue = isMarkedAsExportedCache.get(classData);
    81         if (cachedValue != null) {
    82             return cachedValue;
    83         }
    84 
    85         final boolean newValue =
    86                 isMarkedAsExported(classData.findAnnotationData(true),
    87                                    classData);
    88         isMarkedAsExportedCache.put(classData, newValue);
    89 
    90         return newValue;
    91     }
    92 
    93     private boolean isMarkedAsExported(MethodData methodData)
    94             throws IOException {
    95         return isMarkedAsExported(methodData.findAnnotationData(true),
    96                                   methodData.cls);
    97     }
    98 
    99     private boolean isMarkedAsExported(FieldData fieldData)
   100             throws IOException {
   101         return isMarkedAsExported(fieldData.findAnnotationData(true),
   102                                   fieldData.cls);
   103     }
   104 
   105     private boolean resolveIsMarkedAsExportedPackage(String pkgName) {
   106         try {
   107             final InputStream is =
   108                     resources.get(pkgName + "/package-info.class");
   109             if (is == null) {
   110                 return false;
   111             }
   112 
   113             try {
   114                 final ClassData pkgInfoClass = new ClassData(is);
   115                 return isMarkedAsExported(
   116                                pkgInfoClass.findAnnotationData(true),
   117                                pkgInfoClass);
   118             } finally {
   119                 is.close();
   120             }
   121         } catch (final IOException e) {
   122             return false;
   123         }
   124     }
   125 
   126     private boolean isMarkedAsExported(byte[] arrData, ClassData cd)
   127             throws IOException {
   128         if (arrData == null) {
   129             return false;
   130         }
   131 
   132         final boolean[] found = { false };
   133         final AnnotationParser annotationParser =
   134                 new AnnotationParser(false, false) {
   135                     @Override
   136                     protected void visitAnnotationStart(
   137                             String type,
   138                             boolean top) {
   139                         if (top && type.equals("Lorg/apidesign/bck2brwsr"
   140                                                    + "/core/Exported;")) {
   141                             found[0] = true;
   142                         }
   143                     }
   144                 };
   145         annotationParser.parse(arrData, cd);
   146         return found[0];
   147     }
   148 
   149     private static boolean isAccessible(int access) {
   150         return (access & (ByteCodeParser.ACC_PUBLIC
   151                               | ByteCodeParser.ACC_PROTECTED)) != 0;
   152     }
   153 }