Red Hat Application Migration Toolkit
package org.exolab.castor.mapping.loader; import java.io.InputStream; import java.io.Serializable; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.math.BigDecimal; import java.math.BigInteger; import java.sql.Time; import java.sql.Timestamp; import java.util.Date; import java.util.Locale; import java.util.Vector; import org.castor.core.util.Messages; import org.exolab.castor.types.Duration; public class Types { private static final Vector ENUMS = new Vector(); private static final Vector CONVERTIBLE = new Vector(); static Types.TypeInfo[] _typeInfos; public static Class typeFromName(ClassLoader loader, String typeName) throws ClassNotFoundException { for(int aClass = 0; aClass < _typeInfos.length; ++aClass) { if(typeName.equals(_typeInfos[aClass]._shortName)) { return _typeInfos[aClass]._primitive != null?_typeInfos[aClass]._primitive:_typeInfos[aClass]._javaType; } } if(loader != null) { Class var3 = Class.forName(typeName, false, loader); return var3; } else { return Class.forName(typeName); } } public static Object getDefault(Class type) { for(int i = 0; i < _typeInfos.length; ++i) { if(_typeInfos[i]._primitive == type || _typeInfos[i]._javaType == type) { return _typeInfos[i]._defaultValue; } } return null; } public static Class typeFromPrimitive(Class type) { if(type != null && type.isArray() && !type.getComponentType().isPrimitive()) { return typeFromPrimitive(type.getComponentType()); } else { for(int i = 0; i < _typeInfos.length; ++i) { if(_typeInfos[i]._primitive == type) { return _typeInfos[i]._javaType; } } return type; } } public static boolean isSimpleType(Class type) { for(int i = 0; i < _typeInfos.length; ++i) { if(_typeInfos[i]._javaType == type || _typeInfos[i]._primitive == type) { return true; } } return false; } public static boolean isPrimitiveType(Class type) { for(int i = 0; i < _typeInfos.length; ++i) { if(_typeInfos[i]._primitive == type || _typeInfos[i]._javaType == type && _typeInfos[i]._primitive != null) { return true; } } return false; } public static void addEnumType(Class type) { ENUMS.add(type); } public static boolean isEnumType(Class type) { return ENUMS.contains(type); } public static void addConvertibleType(Class type) { CONVERTIBLE.add(type); } public static boolean isConvertibleType(Class type) { return CONVERTIBLE.contains(type); } public static Object newInstance(Class type) throws IllegalStateException { try { return type.newInstance(); } catch (IllegalAccessException var2) { throw new IllegalStateException(Messages.format("mapping.schemaNotConstructable", type.getName(), var2.getMessage())); } catch (InstantiationException var3) { throw new IllegalStateException(Messages.format("mapping.schemaNotConstructable", type.getName(), var3.getMessage())); } } public static Object newInstance(Class type, Object[] args) throws IllegalStateException { if(args != null && args.length != 0) { try { Constructor except = findConstructor(type, args); return except.newInstance(args); } catch (NoSuchMethodException var3) { throw new IllegalStateException(Messages.format("mapping.constructorNotFound", type.getName(), var3.getMessage())); } catch (InvocationTargetException var4) { throw new IllegalStateException(Messages.format("mapping.schemaNotConstructable", type.getName(), var4.getMessage())); } catch (IllegalAccessException var5) { throw new IllegalStateException(Messages.format("mapping.schemaNotConstructable", type.getName(), var5.getMessage())); } catch (InstantiationException var6) { throw new IllegalStateException(Messages.format("mapping.schemaNotConstructable", type.getName(), var6.getMessage())); } } else { return newInstance(type); } } public static boolean isConstructable(Class type) { return isConstructable(type, false); } public static boolean isConstructable(Class type, boolean allowAbstractOrInterface) { try { if((type.getModifiers() & 1) == 0) { return false; } if(!allowAbstractOrInterface && (type.getModifiers() & 1536) != 0) { return false; } if((type.getConstructor(new Class[0]).getModifiers() & 1) != 0) { return true; } } catch (NoSuchMethodException var3) { ; } catch (SecurityException var4) { ; } return false; } public static boolean isSerializable(Class type) { return Serializable.class.isAssignableFrom(type); } public static boolean isImmutable(Class type) { for(int i = 0; i < _typeInfos.length; ++i) { if(_typeInfos[i]._javaType == type || _typeInfos[i]._primitive == type) { return _typeInfos[i]._immutable; } } return false; } public static boolean isCloneable(Class type) { return Cloneable.class.isAssignableFrom(type); } private static Constructor findConstructor(Class type, Object[] args) throws NoSuchMethodException { Constructor[] constructors = type.getConstructors(); Constructor cons = null; int rank = 0; for(int c = 0; c < constructors.length; ++c) { Class[] paramTypes = constructors[c].getParameterTypes(); if(paramTypes.length == args.length) { int tmpRank = 0; boolean matches = true; int p = 0; while(p < paramTypes.length) { label57: { if(args[p] == null) { if(paramTypes[p].isPrimitive()) { matches = false; break; } } else if(paramTypes[p] == args[p].getClass()) { ++tmpRank; } else if(!paramTypes[p].isAssignableFrom(args[p].getClass())) { if(!paramTypes[p].isPrimitive()) { break label57; } Class pType = typeFromPrimitive(paramTypes[p]); if(!pType.isAssignableFrom(args[p].getClass())) { break label57; } } ++p; continue; } matches = false; break; } if(matches) { if(tmpRank == paramTypes.length) { return constructors[c]; } if(cons == null || tmpRank > rank) { cons = constructors[c]; rank = tmpRank; } } } } if(cons == null) { throw new NoSuchMethodException(); } else { return cons; } } private static final Class getClobClass() { Class type = null; try { type = Class.forName("java.sql.Clob"); } catch (ClassNotFoundException var2) { ; } return type; } static { _typeInfos = new Types.TypeInfo[]{new Types.TypeInfo("other", (Class)null, Object.class, false, (Object)null), new Types.TypeInfo("string", (Class)null, String.class, true, (Object)null), new Types.TypeInfo("integer", Integer.TYPE, Integer.class, true, new Integer(0)), new Types.TypeInfo("int", Integer.TYPE, Integer.TYPE, true, new Integer(0)), new Types.TypeInfo("long", Long.TYPE, Long.class, true, new Long(0L)), new Types.TypeInfo("big-integer", (Class)null, BigInteger.class, true, BigInteger.valueOf(0L)), new Types.TypeInfo("boolean", Boolean.TYPE, Boolean.class, true, Boolean.FALSE), new Types.TypeInfo("double", Double.TYPE, Double.class, true, new Double(0.0D)), new Types.TypeInfo("float", Float.TYPE, Float.class, true, new Float(0.0F)), new Types.TypeInfo("big-decimal", (Class)null, BigDecimal.class, true, new BigDecimal(0.0D)), new Types.TypeInfo("byte", Byte.TYPE, Byte.class, true, new Byte(0)), new Types.TypeInfo("date", (Class)null, Date.class, true, (Object)null), new Types.TypeInfo("timestamp", (Class)null, Timestamp.class, true, (Object)null), new Types.TypeInfo("sqldate", (Class)null, java.sql.Date.class, true, (Object)null), new Types.TypeInfo("sqltime", (Class)null, Time.class, true, (Object)null), new Types.TypeInfo("short", Short.TYPE, Short.class, true, new Short(0)), new Types.TypeInfo("char", Character.TYPE, Character.class, true, new Character('\u0000')), new Types.TypeInfo("bytes", (Class)null, byte[].class, false, (Object)null), new Types.TypeInfo("chars", (Class)null, char[].class, false, (Object)null), new Types.TypeInfo("strings", (Class)null, String[].class, false, (Object)null), new Types.TypeInfo("locale", (Class)null, Locale.class, true, (Object)null), new Types.TypeInfo("stream", (Class)null, InputStream.class, true, (Object)null), new Types.TypeInfo("clob", (Class)null, getClobClass(), true, (Object)null), new Types.TypeInfo("serializable", (Class)null, Serializable.class, false, (Object)null), new Types.TypeInfo("[Lbyte;", (Class)null, byte[].class, false, (Object)null), new Types.TypeInfo("[Lchar;", (Class)null, char[].class, false, (Object)null), new Types.TypeInfo("[Ldouble;", (Class)null, double[].class, false, (Object)null), new Types.TypeInfo("[Lfloat;", (Class)null, float[].class, false, (Object)null), new Types.TypeInfo("[Lint;", (Class)null, int[].class, false, (Object)null), new Types.TypeInfo("[Llong;", (Class)null, long[].class, false, (Object)null), new Types.TypeInfo("[Lshort;", (Class)null, int[].class, false, (Object)null), new Types.TypeInfo("[Lboolean;", (Class)null, int[].class, false, (Object)null), new Types.TypeInfo("duration", (Class)null, Duration.class, false, new Duration(0L)), new Types.TypeInfo("xml-date", (Class)null, org.exolab.castor.types.Date.class, false, new org.exolab.castor.types.Date(0L)), new Types.TypeInfo("xml-time", (Class)null, org.exolab.castor.types.Time.class, false, new org.exolab.castor.types.Time(0L))}; } static class TypeInfo { final String _shortName; final Class _primitive; final Class _javaType; final boolean _immutable; final Object _defaultValue; TypeInfo(String shortName, Class primitive, Class javaType, boolean immutable, Object defaultValue) { this._shortName = shortName; this._primitive = primitive; this._javaType = javaType; this._immutable = immutable; this._defaultValue = defaultValue; } } }