001// Copyright 2011 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007// http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.internal.plastic;
016
017import org.apache.tapestry5.plastic.MethodDescription;
018import org.apache.tapestry5.plastic.PlasticUtils;
019
020/**
021 * A cache of translations from type names to internal names and descriptors, as well as a cache from MethodDescription
022 * to method descriptor.
023 */
024@SuppressWarnings("rawtypes")
025public class NameCache
026{
027    private final Cache<String, String> class2internal = new Cache<String, String>()
028    {
029
030        @Override
031        protected String convert(String className)
032        {
033            return PlasticInternalUtils.toInternalName(className);
034        }
035    };
036
037    private final Cache<Class, String> type2internal = new Cache<Class, String>()
038    {
039        @Override
040        protected String convert(Class input)
041        {
042            return toInternalName(input.getName());
043        }
044    };
045
046    private final Cache<MethodDescription, String> md2desc = new Cache<MethodDescription, String>()
047    {
048        @Override
049        protected String convert(MethodDescription methodDescription)
050        {
051            return toMethodDescriptor(methodDescription.returnType, methodDescription.argumentTypes);
052        }
053    };
054
055    private final Cache<String, String> type2desc = new Cache<String, String>()
056    {
057        @Override
058        protected String convert(String typeName)
059        {
060            return PlasticInternalUtils.toDescriptor(typeName);
061        }
062    };
063
064    private final Cache<Class, String> typeToTypeName = new Cache<Class, String>()
065    {
066        @Override
067        protected String convert(Class type)
068        {
069            return PlasticUtils.toTypeName(type);
070        }
071    };
072
073    public String toInternalName(String className)
074    {
075        return class2internal.get(className);
076    }
077
078    public String toInternalName(Class type)
079    {
080        return type2internal.get(type);
081    }
082
083    public String toDesc(MethodDescription md)
084    {
085        return md2desc.get(md);
086    }
087
088    public String toDesc(String typeName)
089    {
090        return type2desc.get(typeName);
091    }
092
093    public String toTypeName(Class type)
094    {
095        return typeToTypeName.get(type);
096    }
097
098    public String[] toTypeNames(Class... types)
099    {
100        String[] result = new String[types.length];
101
102        for (int i = 0; i < result.length; i++)
103            result[i] = toTypeName(types[i]);
104
105        return result;
106    }
107
108    public String toMethodDescriptor(Class returnType, Class... argumentTypes)
109    {
110        return toMethodDescriptor(toTypeName(returnType), toTypeNames(argumentTypes));
111    }
112
113    public String toMethodDescriptor(String returnType, String... argumentTypes)
114    {
115        StringBuilder builder = new StringBuilder("(");
116
117        for (String argumentType : argumentTypes)
118        {
119            builder.append(toDesc(argumentType));
120        }
121
122        builder.append(')').append(toDesc(returnType));
123
124        return builder.toString();
125    }
126}