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.ClassData;
26 import org.apidesign.vm4brwsr.ByteCodeParser.FieldData;
27 import org.apidesign.vm4brwsr.ByteCodeParser.MethodData;
29 @ExtraJavaScript(processByteCode = false, resource="")
30 final class ClassDataCache {
31 private static final Object MISSING_CLASS = new Object();
33 private final Bck2Brwsr.Resources resources;
34 private final Map<String, Object> classDataMap;
36 ClassDataCache(final Bck2Brwsr.Resources resources) {
37 this.resources = resources;
39 classDataMap = new HashMap<String, Object>();
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";
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);
55 return (cacheEntry != MISSING_CLASS) ? (ClassData) cacheEntry : null;
58 MethodData findMethod(final String startingClass,
60 final String signature) throws IOException {
61 return findMethod(getClassData(startingClass), name, signature);
64 FieldData findField(final String startingClass,
66 final String signature) throws IOException {
67 return findField(getClassData(startingClass), name, signature);
70 MethodData findMethod(ClassData currentClass,
72 final String signature) throws IOException {
73 while (currentClass != null) {
74 final MethodData methodData =
75 currentClass.findMethod(name, signature);
76 if (methodData != null) {
80 final String superClassName = currentClass.getSuperClassName();
81 if (superClassName == null) {
84 currentClass = getClassData(superClassName);
90 FieldData findField(ClassData currentClass,
92 final String signature) throws IOException {
93 while (currentClass != null) {
94 final FieldData fieldData =
95 currentClass.findField(name, signature);
96 if (fieldData != null) {
100 final String superClassName = currentClass.getSuperClassName();
101 if (superClassName == null) {
104 currentClass = getClassData(superClassName);
110 private static InputStream loadClass(Bck2Brwsr.Resources l, String name)
112 return l.get(name + ".class"); // NOI18N