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"
lubomir@1084
     1
/**
lubomir@1084
     2
 * Back 2 Browser Bytecode Translator
lubomir@1084
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
lubomir@1084
     4
 *
lubomir@1084
     5
 * This program is free software: you can redistribute it and/or modify
lubomir@1084
     6
 * it under the terms of the GNU General Public License as published by
lubomir@1084
     7
 * the Free Software Foundation, version 2 of the License.
lubomir@1084
     8
 *
lubomir@1084
     9
 * This program is distributed in the hope that it will be useful,
lubomir@1084
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
lubomir@1084
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
lubomir@1084
    12
 * GNU General Public License for more details.
lubomir@1084
    13
 *
lubomir@1084
    14
 * You should have received a copy of the GNU General Public License
lubomir@1084
    15
 * along with this program. Look for COPYING file in the top folder.
lubomir@1084
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
lubomir@1084
    17
 */
lubomir@1084
    18
package org.apidesign.vm4brwsr;
lubomir@1084
    19
lubomir@1084
    20
import java.io.IOException;
lubomir@1084
    21
import java.io.InputStream;
lubomir@1084
    22
import java.util.HashMap;
lubomir@1084
    23
import java.util.Map;
lubomir@1084
    24
import org.apidesign.bck2brwsr.core.ExtraJavaScript;
lubomir@1084
    25
import org.apidesign.vm4brwsr.ByteCodeParser.ClassData;
lubomir@1085
    26
import org.apidesign.vm4brwsr.ByteCodeParser.FieldData;
lubomir@1085
    27
import org.apidesign.vm4brwsr.ByteCodeParser.MethodData;
lubomir@1084
    28
lubomir@1084
    29
@ExtraJavaScript(processByteCode = false, resource="")
lubomir@1084
    30
final class ClassDataCache {
lubomir@1084
    31
    private static final Object MISSING_CLASS = new Object();
lubomir@1084
    32
lubomir@1084
    33
    private final Bck2Brwsr.Resources resources;
lubomir@1084
    34
    private final Map<String, Object> classDataMap;
lubomir@1084
    35
lubomir@1084
    36
    ClassDataCache(final Bck2Brwsr.Resources resources) {
lubomir@1084
    37
        this.resources = resources;
lubomir@1084
    38
lubomir@1084
    39
        classDataMap = new HashMap<String, Object>();
lubomir@1084
    40
    }
lubomir@1084
    41
lubomir@1085
    42
    ClassData getClassData(String className) throws IOException {
lubomir@1085
    43
        if (className.startsWith("[")) {
lubomir@1085
    44
            // required for accessVirtualMethod, shouldn't be problematic for
lubomir@1085
    45
            // calls from other sources
lubomir@1085
    46
            className = "java/lang/Object";
lubomir@1085
    47
        }
lubomir@1084
    48
        Object cacheEntry = classDataMap.get(className);
lubomir@1084
    49
        if (cacheEntry == null) {
lubomir@1084
    50
            final InputStream is = loadClass(resources, className);
lubomir@1084
    51
            cacheEntry = (is != null) ? new ClassData(is) : MISSING_CLASS;
lubomir@1084
    52
            classDataMap.put(className, cacheEntry);
lubomir@1084
    53
        }
lubomir@1084
    54
lubomir@1084
    55
        return (cacheEntry != MISSING_CLASS) ? (ClassData) cacheEntry : null;
lubomir@1084
    56
    }
lubomir@1084
    57
lubomir@1085
    58
    MethodData findMethod(final String startingClass,
lubomir@1085
    59
                          final String name,
lubomir@1085
    60
                          final String signature) throws IOException {
lubomir@1085
    61
        return findMethod(getClassData(startingClass), name, signature);
lubomir@1085
    62
    }
lubomir@1085
    63
lubomir@1085
    64
    FieldData findField(final String startingClass,
lubomir@1085
    65
                        final String name,
lubomir@1085
    66
                        final String signature) throws IOException {
lubomir@1085
    67
        return findField(getClassData(startingClass), name, signature);
lubomir@1085
    68
    }
lubomir@1085
    69
lubomir@1085
    70
    MethodData findMethod(ClassData currentClass,
lubomir@1085
    71
                          final String name,
lubomir@1085
    72
                          final String signature) throws IOException {
lubomir@1085
    73
        while (currentClass != null) {
lubomir@1085
    74
            final MethodData methodData =
lubomir@1085
    75
                    currentClass.findMethod(name, signature);
lubomir@1085
    76
            if (methodData != null) {
lubomir@1085
    77
                return methodData;
lubomir@1085
    78
            }
lubomir@1085
    79
lubomir@1085
    80
            final String superClassName = currentClass.getSuperClassName();
lubomir@1085
    81
            if (superClassName == null) {
lubomir@1085
    82
                break;
lubomir@1085
    83
            }
lubomir@1085
    84
            currentClass = getClassData(superClassName);
lubomir@1085
    85
        }
lubomir@1085
    86
lubomir@1085
    87
        return null;
lubomir@1085
    88
    }
lubomir@1085
    89
lubomir@1085
    90
    FieldData findField(ClassData currentClass,
lubomir@1085
    91
                        final String name,
lubomir@1085
    92
                        final String signature) throws IOException {
lubomir@1085
    93
        while (currentClass != null) {
lubomir@1085
    94
            final FieldData fieldData =
lubomir@1085
    95
                    currentClass.findField(name, signature);
lubomir@1085
    96
            if (fieldData != null) {
lubomir@1085
    97
                return fieldData;
lubomir@1085
    98
            }
lubomir@1085
    99
lubomir@1085
   100
            final String superClassName = currentClass.getSuperClassName();
lubomir@1085
   101
            if (superClassName == null) {
lubomir@1085
   102
                break;
lubomir@1085
   103
            }
lubomir@1085
   104
            currentClass = getClassData(superClassName);
lubomir@1085
   105
        }
lubomir@1085
   106
lubomir@1085
   107
        return null;
lubomir@1085
   108
    }
lubomir@1085
   109
lubomir@1084
   110
    private static InputStream loadClass(Bck2Brwsr.Resources l, String name)
lubomir@1084
   111
            throws IOException {
lubomir@1084
   112
        return l.get(name + ".class"); // NOI18N
lubomir@1084
   113
    }
lubomir@1084
   114
}