rt/vm/src/main/java/org/apidesign/vm4brwsr/ExportedSymbols.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 22 May 2014 15:29:40 +0200
branchclosure
changeset 1586 d4ee65642d8d
parent 1583 89b6b369c13d
child 1592 f0433f790aaa
permissions -rw-r--r--
Tests pass OK in full obfuscation mode
     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         if (
    59             isAccessible(fieldData.access) && 
    60             isExported(fieldData.cls) || isMarkedAsExported(fieldData)
    61         ) {
    62             return true;
    63         }
    64         if (
    65             fieldData.isStatic() && fieldData.getName().equals("$VALUES") &&
    66             "java/lang/Enum".equals(fieldData.cls.getSuperClassName())
    67         ) {
    68             // enum values need to be exported
    69             return true;
    70         }
    71         return false;
    72     }
    73 
    74     private boolean isMarkedAsExportedPackage(String pkgName) {
    75         if (pkgName == null) {
    76             return false;
    77         }
    78 
    79         final Boolean cachedValue = isMarkedAsExportedCache.get(pkgName);
    80         if (cachedValue != null) {
    81             return cachedValue;
    82         }
    83 
    84         final boolean newValue = resolveIsMarkedAsExportedPackage(pkgName);
    85         isMarkedAsExportedCache.put(pkgName, newValue);
    86 
    87         return newValue;
    88     }
    89 
    90     private boolean isMarkedAsExported(ClassData classData)
    91             throws IOException {
    92         final Boolean cachedValue = isMarkedAsExportedCache.get(classData);
    93         if (cachedValue != null) {
    94             return cachedValue;
    95         }
    96 
    97         final boolean newValue =
    98                 isMarkedAsExported(classData.findAnnotationData(true),
    99                                    classData);
   100         isMarkedAsExportedCache.put(classData, newValue);
   101 
   102         return newValue;
   103     }
   104 
   105     private boolean isMarkedAsExported(MethodData methodData)
   106             throws IOException {
   107         return isMarkedAsExported(methodData.findAnnotationData(true),
   108                                   methodData.cls);
   109     }
   110 
   111     private boolean isMarkedAsExported(FieldData fieldData)
   112             throws IOException {
   113         return isMarkedAsExported(fieldData.findAnnotationData(true),
   114                                   fieldData.cls);
   115     }
   116 
   117     private boolean resolveIsMarkedAsExportedPackage(String pkgName) {
   118         if (exported.contains(pkgName + '/')) {
   119             return true;
   120         }
   121         try {
   122             final InputStream is =
   123                     resources.get(pkgName + "/package-info.class");
   124             if (is == null) {
   125                 return false;
   126             }
   127 
   128             try {
   129                 final ClassData pkgInfoClass = new ClassData(is);
   130                 return isMarkedAsExported(
   131                                pkgInfoClass.findAnnotationData(true),
   132                                pkgInfoClass);
   133             } finally {
   134                 is.close();
   135             }
   136         } catch (final IOException e) {
   137             return false;
   138         }
   139     }
   140 
   141     static boolean isMarkedAsExported(byte[] arrData, ClassData cd)
   142             throws IOException {
   143         if (arrData == null) {
   144             return false;
   145         }
   146 
   147         final boolean[] found = { false };
   148         final AnnotationParser annotationParser =
   149                 new AnnotationParser(false, false) {
   150                     @Override
   151                     protected void visitAnnotationStart(
   152                             String type,
   153                             boolean top) {
   154                         if (top && type.equals("Lorg/apidesign/bck2brwsr"
   155                                                    + "/core/Exported;")) {
   156                             found[0] = true;
   157                         }
   158                     }
   159                 };
   160         annotationParser.parse(arrData, cd);
   161         return found[0];
   162     }
   163 
   164     private static boolean isAccessible(int access) {
   165         return (access & (ByteCodeParser.ACC_PUBLIC
   166                               | ByteCodeParser.ACC_PROTECTED)) != 0;
   167     }
   168 }