All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.jandex.NameTable Maven / Gradle / Ivy

There is a newer version: 1.3.3
Show newest version
/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2013 Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jboss.jandex;

import java.util.HashMap;
import java.util.Map;

/**
 * A collection of intern pools.
 *
 * @author Jason T. Greene
 */
class NameTable {
    private StrongInternPool stringPool = new StrongInternPool();
    private StrongInternPool typePool = new StrongInternPool();
    private StrongInternPool typeListPool = new StrongInternPool();
    private StrongInternPool bytePool = new StrongInternPool();
    private StrongInternPool methodPool = new StrongInternPool();
    private StrongInternPool fieldPool = new StrongInternPool();
    private Map names = new HashMap();

    DotName convertToName(String name) {
        return convertToName(name, '.');
    }

    DotName convertToName(String name, char delim) {
        DotName result = names.get(name);
        if (result != null)
            return result;

        int loc = lastIndexOf(name, delim, '$');
        String local = intern(name.substring(loc + 1));
        DotName prefix = loc < 1 ? null : convertToName(intern(name.substring(0, loc)), delim);
        result = new DotName(prefix, local, true, loc > 0 && name.charAt(loc) == '$');

        names.put(name, result);

        return result;
    }

    private int lastIndexOf(String name, char delim1, char delim2) {
        int pos = name.length();
        while (--pos >= 0) {
            char c = name.charAt(pos);
            if (c == delim1 || c == delim2) {
                break;
            }
        }

        return pos;
    }

    DotName wrap(DotName prefix, String local, boolean inner) {
        DotName name = new DotName(prefix, intern(local), true, true);

        return intern(name, '.');
    }

    String intern(String string) {
        return stringPool.intern(string);
    }

    int positionOf(String string) {
        return stringPool.index().positionOf(string);
    }

    Type intern(Type type) {
        return typePool.intern(type);
    }

    Type[] intern(Type[] types) {
        return typeListPool.intern(types);
    }

    byte[] intern(byte[] bytes) {
        return bytePool.intern(bytes);
    }

    int positionOf(byte[] type) {
        return bytePool.index().positionOf(type);
    }

    MethodInternal intern(MethodInternal methodInternal) {
        return methodPool.intern(methodInternal);
    }

    int positionOf(MethodInternal methodInternal) {
        return methodPool.index().positionOf(methodInternal);
    }

    FieldInternal intern(FieldInternal fieldInternal) {
        return fieldPool.intern(fieldInternal);
    }

    int positionOf(FieldInternal fieldInternal) {
        return fieldPool.index().positionOf(fieldInternal);
    }

    StrongInternPool stringPool() {
        return stringPool;
    }

    StrongInternPool bytePool() {
        return bytePool;
    }

    StrongInternPool methodPool() {
        return methodPool;
    }

    StrongInternPool fieldPool() {
        return fieldPool;
    }

    DotName intern(DotName dotName, char delim) {
        String name = dotName.toString(delim);
        DotName old = names.get(name);
        if (old == null) {
            old = dotName;
            names.put(name, dotName);
        }

        return old;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy