001: // Copyright 2006 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:
015: package org.apache.tapestry.services;
016:
017: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newMap;
018:
019: import java.util.Map;
020:
021: /**
022: * Support code for generating code (used when transforming component classes).
023: */
024: public final class TransformUtils {
025: private static final Map<String, PrimitiveTypeInfo> _nameToInfo = newMap();
026:
027: private static final Map<Class, PrimitiveTypeInfo> _classToInfo = newMap();
028:
029: static class PrimitiveTypeInfo {
030: private final Class _wrapperType;
031:
032: private final String _unwrapperMethodName;
033:
034: private final String _defaultValue;
035:
036: public PrimitiveTypeInfo(Class wrapperType,
037: String unwrapperMethodName, String defaultValue) {
038: _wrapperType = wrapperType;
039: _unwrapperMethodName = unwrapperMethodName;
040: _defaultValue = defaultValue;
041: }
042:
043: public String getUnwrapperMethodName() {
044: return _unwrapperMethodName;
045: }
046:
047: public String getDefaultValue() {
048: return _defaultValue;
049: }
050:
051: public Class getWrapperType() {
052: return _wrapperType;
053: }
054: }
055:
056: static {
057: add(boolean.class, Boolean.class, "booleanValue", "false");
058: add(byte.class, Byte.class, "byteValue", "0");
059: add(char.class, Character.class, "charValue", "0");
060: add(short.class, Short.class, "shortValue", "0");
061: add(int.class, Integer.class, "intValue", "0");
062: add(long.class, Long.class, "longValue", "0L");
063: add(float.class, Float.class, "floatValue", "0.0f");
064: add(double.class, Double.class, "doubleValue", "0.0d");
065: }
066:
067: private TransformUtils() {
068: }
069:
070: private static void add(Class primitiveType, Class wrapperType,
071: String unwrapperMethodName, String defaultValue) {
072: PrimitiveTypeInfo info = new PrimitiveTypeInfo(wrapperType,
073: unwrapperMethodName, defaultValue);
074:
075: _classToInfo.put(primitiveType, info);
076: _nameToInfo.put(primitiveType.getName(), info);
077: }
078:
079: /**
080: * Returns true if the specified type is a primitive type.
081: */
082: public static boolean isPrimitive(String type) {
083: return _nameToInfo.containsKey(type);
084: }
085:
086: /**
087: * Returns the name of wrapper type for a given input type. For primitive types, returns the
088: * wrapper type. For other types, returns the input type name.
089: *
090: * @param type
091: * primitive type name, or fully qualified class name
092: */
093: public static String getWrapperTypeName(String type) {
094: PrimitiveTypeInfo info = _nameToInfo.get(type);
095:
096: return info == null ? type : info.getWrapperType().getName();
097: }
098:
099: /**
100: * For primitive types, returns the method on the <em>wrapper type</em> that converts back to
101: * the primitive.
102: *
103: * @param type
104: * the primitive type
105: * @return the method of the corresponding wrapper type, or null if type is not a primitive type
106: */
107: public static String getUnwrapperMethodName(String type) {
108: PrimitiveTypeInfo info = _nameToInfo.get(type);
109:
110: return info == null ? null : info.getUnwrapperMethodName();
111: }
112:
113: /**
114: * Returns the wrapper type for a given input type. For primitive types, returns the wrapper
115: * type. For other types, returns the type itself.
116: *
117: * @param type
118: * primitive or object type
119: */
120: public static Class getWrapperType(Class type) {
121: PrimitiveTypeInfo info = _classToInfo.get(type);
122:
123: return info == null ? type : info.getWrapperType();
124: }
125:
126: /**
127: * Returns the default value for a type. This is the string "null" for most types, or a literal
128: * value for primtive types.
129: */
130: public static String getDefaultValue(String type) {
131: PrimitiveTypeInfo info = _nameToInfo.get(type);
132:
133: return info == null ? "null" : info.getDefaultValue();
134: }
135: }
|