rt/vm/src/main/java/org/apidesign/vm4brwsr/ClassDataCache.java
author Lubomir Nerad <lubomir.nerad@oracle.com>
Tue, 07 May 2013 19:01:14 +0200
branchclosure
changeset 1085 6a4ef883e233
parent 1084 f5c9934a252c
child 1087 d868b5a67b9b
permissions -rw-r--r--
Access non-final exported methods through "invoker"
     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.ClassData;
    26 import org.apidesign.vm4brwsr.ByteCodeParser.FieldData;
    27 import org.apidesign.vm4brwsr.ByteCodeParser.MethodData;
    28 
    29 @ExtraJavaScript(processByteCode = false, resource="")
    30 final class ClassDataCache {
    31     private static final Object MISSING_CLASS = new Object();
    32 
    33     private final Bck2Brwsr.Resources resources;
    34     private final Map<String, Object> classDataMap;
    35 
    36     ClassDataCache(final Bck2Brwsr.Resources resources) {
    37         this.resources = resources;
    38 
    39         classDataMap = new HashMap<String, Object>();
    40     }
    41 
    42     ClassData getClassData(String className) throws IOException {
    43         if (className.startsWith("[")) {
    44             // required for accessVirtualMethod, shouldn't be problematic for
    45             // calls from other sources
    46             className = "java/lang/Object";
    47         }
    48         Object cacheEntry = classDataMap.get(className);
    49         if (cacheEntry == null) {
    50             final InputStream is = loadClass(resources, className);
    51             cacheEntry = (is != null) ? new ClassData(is) : MISSING_CLASS;
    52             classDataMap.put(className, cacheEntry);
    53         }
    54 
    55         return (cacheEntry != MISSING_CLASS) ? (ClassData) cacheEntry : null;
    56     }
    57 
    58     MethodData findMethod(final String startingClass,
    59                           final String name,
    60                           final String signature) throws IOException {
    61         return findMethod(getClassData(startingClass), name, signature);
    62     }
    63 
    64     FieldData findField(final String startingClass,
    65                         final String name,
    66                         final String signature) throws IOException {
    67         return findField(getClassData(startingClass), name, signature);
    68     }
    69 
    70     MethodData findMethod(ClassData currentClass,
    71                           final String name,
    72                           final String signature) throws IOException {
    73         while (currentClass != null) {
    74             final MethodData methodData =
    75                     currentClass.findMethod(name, signature);
    76             if (methodData != null) {
    77                 return methodData;
    78             }
    79 
    80             final String superClassName = currentClass.getSuperClassName();
    81             if (superClassName == null) {
    82                 break;
    83             }
    84             currentClass = getClassData(superClassName);
    85         }
    86 
    87         return null;
    88     }
    89 
    90     FieldData findField(ClassData currentClass,
    91                         final String name,
    92                         final String signature) throws IOException {
    93         while (currentClass != null) {
    94             final FieldData fieldData =
    95                     currentClass.findField(name, signature);
    96             if (fieldData != null) {
    97                 return fieldData;
    98             }
    99 
   100             final String superClassName = currentClass.getSuperClassName();
   101             if (superClassName == null) {
   102                 break;
   103             }
   104             currentClass = getClassData(superClassName);
   105         }
   106 
   107         return null;
   108     }
   109 
   110     private static InputStream loadClass(Bck2Brwsr.Resources l, String name)
   111             throws IOException {
   112         return l.get(name + ".class"); // NOI18N
   113     }
   114 }