001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.com
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package org.huihoo.jfox.jmx.loading;
009:
010: /**
011: * change primitive type to Class
012: * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
013: */
014:
015: public class PrimitiveClassLoader {
016:
017: private static PrimitiveClassLoader me = new PrimitiveClassLoader();
018:
019: private PrimitiveClassLoader() {
020:
021: }
022:
023: public static PrimitiveClassLoader getInstance() {
024: return me;
025: }
026:
027: // convert primitive type class to primitive type, ex: java.lang.Integer => int
028: public Class loadClass(String primitiveName) {
029: if (primitiveName.equals(Boolean.TYPE.getName()))
030: return Boolean.TYPE;
031: if (primitiveName.equals(Character.TYPE.getName()))
032: return Character.TYPE;
033: if (primitiveName.equals(Byte.TYPE.getName()))
034: return Byte.TYPE;
035: if (primitiveName.equals(Short.TYPE.getName()))
036: return Short.TYPE;
037: if (primitiveName.equals(Integer.TYPE.getName()))
038: return Integer.TYPE;
039: if (primitiveName.equals(Long.TYPE.getName()))
040: return Long.TYPE;
041: if (primitiveName.equals(Float.TYPE.getName()))
042: return Float.TYPE;
043: if (primitiveName.equals(Double.TYPE.getName()))
044: return Double.TYPE;
045:
046: return null;
047: }
048:
049: // whill setAttribute or operation with primitive parameter
050: public Class object2PrimitiveClass(Object obj) {
051: if (obj instanceof Boolean)
052: return Boolean.TYPE;
053: if (obj instanceof Character)
054: return Character.TYPE;
055: if (obj instanceof Byte)
056: return Byte.TYPE;
057: if (obj instanceof Short)
058: return Short.TYPE;
059: if (obj instanceof Integer)
060: return Integer.TYPE;
061: if (obj instanceof Long)
062: return Long.TYPE;
063: if (obj instanceof Float)
064: return Float.TYPE;
065: if (obj instanceof Double)
066: return Double.TYPE;
067:
068: return null;
069: }
070:
071: /**
072: * create a Object by it's value and className , only check primitive class
073: * @param value
074: * @param className
075: * @return primitive Object or itself
076: */
077: public Object createObject(String value, String className) {
078: Object obj = value;
079:
080: if (className.equals(Boolean.TYPE.getName())
081: || className.equals("java.lang.Boolean")) {
082: obj = Boolean.valueOf(value);
083: } else if (className.equals(Character.TYPE.getName())
084: || className.equals("java.lang.Character")) {
085: obj = new Character(value.charAt(0));
086: } else if (className.equals(Byte.TYPE.getName())
087: || className.equals("java.lang.Byte")) {
088: obj = Byte.valueOf(value);
089: } else if (className.equals(Short.TYPE.getName())
090: || className.equals("java.lang.Short")) {
091: obj = Short.valueOf(value);
092: } else if (className.equals(Integer.TYPE.getName())
093: || className.equals("java.lang.Integer")) {
094: obj = Integer.valueOf(value);
095: } else if (className.equals(Long.TYPE.getName())
096: || className.equals("java.lang.Long")) {
097: obj = Long.valueOf(value);
098: } else if (className.equals(Float.TYPE.getName())
099: || className.equals("java.lang.Float")) {
100: obj = Float.valueOf(value);
101: } else if (className.equals(Double.TYPE.getName())
102: || className.equals("java.lang.Double")) {
103: obj = Double.valueOf(value);
104: }
105:
106: return obj;
107:
108: }
109:
110: public boolean isAssignable(Class primitiveClass, Class objClass) {
111: boolean assignable = false;
112: if (objClass == null || primitiveClass == null) {
113: assignable = false;
114: } else if (objClass == boolean.class
115: && primitiveClass == Boolean.class) {
116: assignable = true;
117: } else if (objClass == byte.class
118: && primitiveClass == Byte.class) {
119: assignable = true;
120: } else if (objClass == char.class
121: && primitiveClass == Character.class) {
122: assignable = true;
123: } else if (objClass == short.class
124: && primitiveClass == Short.class) {
125: assignable = true;
126: } else if (objClass == int.class
127: && primitiveClass == Integer.class) {
128: assignable = true;
129: } else if (objClass == long.class
130: && primitiveClass == Long.class) {
131: assignable = true;
132: } else if (objClass == float.class
133: && primitiveClass == Float.class) {
134: assignable = true;
135: } else if (objClass == double.class
136: && primitiveClass == Double.class) {
137: assignable = true;
138: }
139: return assignable;
140: }
141:
142: public boolean isPrimitiveType(String type) {
143: if (type.equals(Boolean.TYPE.getName())
144: || type.equals(Character.TYPE.getName())
145: || type.equals(Byte.TYPE.getName())
146: || type.equals(Short.TYPE.getName())
147: || type.equals(Integer.TYPE.getName())
148: || type.equals(Long.TYPE.getName())
149: || type.equals(Float.TYPE.getName())
150: || type.equals(Double.TYPE.getName())) {
151: return true;
152: }
153: return false;
154: }
155:
156: }
|