Changeset 73

Show
Ignore:
Timestamp:
09/18/08 13:48:58 (4 months ago)
Author:
daniel
Message:

added enum functionallity to XJConf

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/java/net/schst/XJConf/ObjectValueConverter.java

    r65 r73  
    11package net.schst.XJConf; 
    22 
    3 import java.lang.reflect.Constructor; 
    43import java.lang.reflect.Modifier; 
    54 
     5import net.schst.XJConf.Converter.ClassConverter; 
     6import net.schst.XJConf.Converter.EnumConverter; 
     7import net.schst.XJConf.Converter.TypeConverter; 
    68import net.schst.XJConf.exceptions.ValueConversionException; 
    79 
     
    1012 *  
    1113 * @author Stephan Schmidt <stephan.schmidt@schlund.de> 
     14 * @author Daniel Jahnke <daniel.jahnke@1und1.de> 
    1215 */ 
    1316public class ObjectValueConverter implements ValueConverter { 
     
    3639    public Object convertValue(Object[] values, Class[] types, ClassLoader loader) 
    3740        throws ValueConversionException { 
    38         Class instanceClass; 
    39         Object instance
     41        Class<?> instanceClass; 
     42        TypeConverter typeConverter
    4043         
    4144        // try to get the class object 
     
    4548            throw new ValueConversionException("Class " + this.className + " does not exist", e); 
    4649        } 
    47          
     50         
    4851        if (instanceClass.isInterface()) { 
    4952            throw new ValueConversionException("Class " + this.className + " is an interface."); 
     
    5558            throw new ValueConversionException("Class " + this.className + " is an annotation."); 
    5659        } 
    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(); 
    5964        } 
    6065         
    61         // try to create a new instance 
     66        /* 
     67         * after recognizing type, make 
     68         * the object alive and set values  
     69         */ 
    6270        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); 
    8475        } 
    85         return instance; 
     76             
     77         
     78         
     79        return typeConverter.getInstance();         
    8680    } 
    8781