rt/vm/src/main/java/org/apidesign/vm4brwsr/ClassDataCache.java
author Lubomir Nerad <lubomir.nerad@oracle.com>
Mon, 06 May 2013 18:06:08 +0200
branchclosure
changeset 1084 f5c9934a252c
child 1085 6a4ef883e233
permissions -rw-r--r--
Overridable class member access type
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@1084
    26
lubomir@1084
    27
@ExtraJavaScript(processByteCode = false, resource="")
lubomir@1084
    28
final class ClassDataCache {
lubomir@1084
    29
    private static final Object MISSING_CLASS = new Object();
lubomir@1084
    30
lubomir@1084
    31
    private final Bck2Brwsr.Resources resources;
lubomir@1084
    32
    private final Map<String, Object> classDataMap;
lubomir@1084
    33
lubomir@1084
    34
    ClassDataCache(final Bck2Brwsr.Resources resources) {
lubomir@1084
    35
        this.resources = resources;
lubomir@1084
    36
lubomir@1084
    37
        classDataMap = new HashMap<String, Object>();
lubomir@1084
    38
    }
lubomir@1084
    39
lubomir@1084
    40
    ClassData getClassData(final String className) throws IOException {
lubomir@1084
    41
        Object cacheEntry = classDataMap.get(className);
lubomir@1084
    42
        if (cacheEntry == null) {
lubomir@1084
    43
            final InputStream is = loadClass(resources, className);
lubomir@1084
    44
            cacheEntry = (is != null) ? new ClassData(is) : MISSING_CLASS;
lubomir@1084
    45
            classDataMap.put(className, cacheEntry);
lubomir@1084
    46
        }
lubomir@1084
    47
lubomir@1084
    48
        return (cacheEntry != MISSING_CLASS) ? (ClassData) cacheEntry : null;
lubomir@1084
    49
    }
lubomir@1084
    50
lubomir@1084
    51
    private static InputStream loadClass(Bck2Brwsr.Resources l, String name)
lubomir@1084
    52
            throws IOException {
lubomir@1084
    53
        return l.get(name + ".class"); // NOI18N
lubomir@1084
    54
    }
lubomir@1084
    55
}