Changeset 73
- Timestamp:
- 09/18/08 13:48:58 (4 months ago)
- Files:
-
- trunk/src/main/java/net/schst/XJConf/Converter (added)
- trunk/src/main/java/net/schst/XJConf/Converter/ClassConverter.java (added)
- trunk/src/main/java/net/schst/XJConf/Converter/EnumConverter.java (added)
- trunk/src/main/java/net/schst/XJConf/Converter/TypeConverter.java (added)
- trunk/src/main/java/net/schst/XJConf/ObjectValueConverter.java (modified) (5 diffs)
- trunk/src/test/java/net/schst/XJConf/Examples/Planet.java (added)
- trunk/src/test/java/net/schst/XJConf/Examples/PlanetInfo.java (added)
- trunk/src/test/java/net/schst/XJConf/Examples/TestEnum.java (added)
- trunk/src/test/resources/xml/defines-enum.xml (added)
- trunk/src/test/resources/xml/test-enum-planet.xml (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/java/net/schst/XJConf/ObjectValueConverter.java
r65 r73 1 1 package net.schst.XJConf; 2 2 3 import java.lang.reflect.Constructor;4 3 import java.lang.reflect.Modifier; 5 4 5 import net.schst.XJConf.Converter.ClassConverter; 6 import net.schst.XJConf.Converter.EnumConverter; 7 import net.schst.XJConf.Converter.TypeConverter; 6 8 import net.schst.XJConf.exceptions.ValueConversionException; 7 9 … … 10 12 * 11 13 * @author Stephan Schmidt <stephan.schmidt@schlund.de> 14 * @author Daniel Jahnke <daniel.jahnke@1und1.de> 12 15 */ 13 16 public class ObjectValueConverter implements ValueConverter { … … 36 39 public Object convertValue(Object[] values, Class[] types, ClassLoader loader) 37 40 throws ValueConversionException { 38 Class instanceClass;39 Object instance;41 Class<?> instanceClass; 42 TypeConverter typeConverter; 40 43 41 44 // try to get the class object … … 45 48 throw new ValueConversionException("Class " + this.className + " does not exist", e); 46 49 } 47 50 48 51 if (instanceClass.isInterface()) { 49 52 throw new ValueConversionException("Class " + this.className + " is an interface."); … … 55 58 throw new ValueConversionException("Class " + this.className + " is an annotation."); 56 59 } 57 else if (instanceClass.isEnum()) { 58 throw new ValueConversionException("Class " + this.className + " is an enum."); 60 else if (instanceClass.isEnum()) { 61 typeConverter = new EnumConverter(); 62 } else { 63 typeConverter = new ClassConverter(); 59 64 } 60 65 61 // try to create a new instance 66 /* 67 * after recognizing type, make 68 * the object alive and set values 69 */ 62 70 try { 63 Constructor co; 64 try { 65 co = instanceClass.getConstructor(types); 66 } catch (NoSuchMethodException e) { 67 // try to convert the values to a string 68 for (int i = 0; i < types.length; i++) { 69 types[i] = String.class; 70 values[i] = values[0].toString(); 71 } 72 co = instanceClass.getConstructor(types); 73 } 74 instance = co.newInstance(values); 75 } catch (Exception e){ 76 try { 77 // no matching constructor has been found 78 // try to instantiate the class without using 79 // a constructor 80 instance = instanceClass.newInstance(); 81 } catch (Exception e2) { 82 throw new ValueConversionException("Could not create instance of " + this.className, e2); 83 } 71 typeConverter.doInstantiate(this.className, loader); 72 typeConverter.setValues(values, types); 73 } catch (Exception e) { 74 throw new ValueConversionException("Trying to create an Object of Class " + this.className + " faild!", e); 84 75 } 85 return instance; 76 77 78 79 return typeConverter.getInstance(); 86 80 } 87 81
