Changeset 25

Show
Ignore:
Timestamp:
07/26/05 10:18:38 (3 years ago)
Author:
schst
Message:

Fixed bug #7: Definitions are able to return their type so the values can be null

Files:

Legend:

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

    r23 r25  
    101101    } 
    102102     
     103    /** 
     104     * Get the type of the attribute 
     105     *  
     106     * @return  Class object 
     107     */ 
     108    public Class getValueType(Tag tag) { 
     109        try { 
     110            return Class.forName(this.type); 
     111        } catch (ClassNotFoundException e) { 
     112            throw new RuntimeException("Could not return type."); 
     113        } 
     114    } 
     115 
    103116    /** 
    104117     * Get the type of the attribute 
  • trunk/src/net/schst/XJConf/CDataDefinition.java

    r21 r25  
    6767        String value = tag.getData(); 
    6868        Object instance = null; 
     69        Constructor co = null; 
    6970        try { 
    7071            Class cl = Class.forName(this.type, true,loader); 
    71             Class paramTypes[] = {value.getClass()}; 
     72            Class paramTypes[] = {String.class}; 
    7273             
    73             Constructor co = cl.getConstructor(paramTypes); 
     74            co = cl.getConstructor(paramTypes); 
    7475            String params[] = {value}; 
    7576            instance = co.newInstance(params); 
     
    8889 
    8990    /** 
     91     * Get the type of the cdata 
     92     *  
     93     * @return  Class object 
     94     */ 
     95    public Class getValueType(Tag tag) { 
     96        try { 
     97            return Class.forName(this.type); 
     98        } catch (ClassNotFoundException e) { 
     99            throw new RuntimeException("Could not return type."); 
     100        } 
     101    } 
     102     
     103    /** 
    90104     * Get the setter method, which is setData() by default 
    91105     */ 
  • trunk/src/net/schst/XJConf/ChildDefinition.java

    r21 r25  
    3838        Tag child = tag.getChild(this.getName()); 
    3939        if (child == null) { 
    40             return null
     40            throw new RuntimeException("Child element " + this.getName() + " does not exist")
    4141        } 
    4242        return child.getConvertedValue(loader); 
    4343    } 
    4444 
     45    /** 
     46     * Get the type of the attribute 
     47     *  
     48     * @return  Class object 
     49     */ 
     50    public Class getValueType(Tag tag) { 
     51        Tag child = tag.getChild(this.getName()); 
     52        if (child == null) { 
     53            throw new RuntimeException("Child element " + this.getName() + " does not exist"); 
     54        } 
     55        return child.getValueType(tag); 
     56    } 
     57     
    4558    /** 
    4659     * This does not provide a setter method. 
  • trunk/src/net/schst/XJConf/ConstructorDefinition.java

    r21 r25  
    4848 
    4949    /** 
     50     * Get the type of the constructor 
     51     *  
     52     * @return  Always returns null  
     53     */ 
     54    public Class getValueType(Tag tag) { 
     55        return null; 
     56    } 
     57     
     58     
     59    /** 
    5060     * Get the setter method 
    5161     */ 
  • trunk/src/net/schst/XJConf/Definition.java

    r21 r25  
    2828     
    2929    /** 
     30     * Get the type of the converted value 
     31     * @param tag TODO 
     32     *  
     33     * @return 
     34     */ 
     35    public Class getValueType(Tag tag); 
     36     
     37    /** 
    3038     * Get the name of the setter method 
    3139     *  
  • trunk/src/net/schst/XJConf/Examples/TestConstructor.java

    r21 r25  
    2626        ConstructorColor color = (ConstructorColor)conf.getConfigValue("color"); 
    2727        System.out.println(color); 
     28        color = (ConstructorColor)conf.getConfigValue("color-no-atts"); 
     29        System.out.println(color); 
    2830        color = (ConstructorColor)conf.getConfigValue("color2"); 
    2931        System.out.println(color); 
  • trunk/src/net/schst/XJConf/Tag.java

    r21 r25  
    163163    } 
    164164 
     165    /** 
     166     * Get the type of the attribute 
     167     *  
     168     * @return  Class object 
     169     */ 
     170    public Class getValueType(Tag tag) { 
     171        return this.def.getValueType(tag); 
     172    } 
     173     
    165174   /** 
    166175    * Get the key, under which the tag will be stored in 
  • trunk/src/net/schst/XJConf/TagDefinition.java

    r24 r25  
    135135        return this.type; 
    136136    } 
    137      
     137 
     138    /** 
     139     * Get the type of the tag 
     140     *  
     141     * @return  Class object 
     142     */ 
     143    public Class getValueType(Tag tag) { 
     144        try { 
     145            return Class.forName(this.type); 
     146        } catch (ClassNotFoundException e) { 
     147            throw new RuntimeException("Could not return type."); 
     148        } 
     149    } 
     150     
    138151    /** 
    139152     * Set the setter method 
     
    209222            paramDef = (Definition)conParams.get(i); 
    210223            Object val = paramDef.convertValue(tag,loader); 
    211             if (val == null) { 
    212                 //TODO schst Definition interface braucht noch type method 
    213                 throw new RuntimeException("argument for constructor must be available!!"); 
    214             } 
    215             cParamTypes[i] = val.getClass(); 
     224            cParamTypes[i] = paramDef.getValueType(tag); 
    216225            cParams[i] = val; 
    217226        } 
     
    242251            try { 
    243252                methodName = att.getSetterMethod(); 
    244                 Class meParamTypes[] = {val.getClass()}; 
     253                Class meParamTypes[] = {att.getValueType(tag)}; 
    245254                 
    246255                Method me = cl.getMethod(methodName, meParamTypes); 
     
    277286                if (instance instanceof java.util.Properties) { 
    278287                    String key = (String)child.getKey();  
    279                     Class childParamTypes[] = {Class.forName("java.lang.String"), Class.forName("java.lang.String")}; 
     288                    Class childParamTypes[] = {String.class, String.class}; 
    280289                    Method childMethod = cl.getMethod("setProperty", childParamTypes); 
    281290                    String params[] = {key, (String)childValue}; 
     
    285294                } else if (instance instanceof java.util.AbstractMap) { 
    286295                    Object name = (Object)child.getKey();  
    287                     Class childParamTypes[] = {Class.forName("java.lang.Object"), Class.forName("java.lang.Object")}; 
     296                    Class childParamTypes[] = {Object.class, Object.class}; 
    288297                    Method childMethod = cl.getMethod("put", childParamTypes); 
    289298                     
    290299                    Object params[] = {name, childValue}; 
    291300                    childMethod.invoke(instance, params); 
     301                     
    292302                // Check, whether the current instance is a collection 
    293303                } else if (methodName == null && instance instanceof java.util.AbstractCollection) { 
    294                     Class childParamTypes[] = {Class.forName("java.lang.Object")}; 
     304                    Class childParamTypes[] = {Object.class}; 
    295305                    Method childMethod = cl.getMethod("add", childParamTypes); 
    296306                    childMethod.invoke(instance, childParams); 
     307                     
    297308                // instance is any generic object 
    298309                } else { 
    299                     Class childParamTypes[] = {childValue.getClass()}; 
     310                    Class childParamTypes[] = {child.getValueType(child)}; 
    300311                    Method childMethod = null; 
    301312                     
     
    303314                        childMethod = cl.getMethod(methodName, childParamTypes); 
    304315                    } catch (NoSuchMethodException e) { 
    305                         Class interfaces[] = (Class[])  this.determineAllInterfaces(new ArrayList(), childValue.getClass()).toArray(new Class[0]); 
     316                        Class interfaces[] = (Class[])  this.determineAllInterfaces(new ArrayList(), child.getValueType(child)).toArray(new Class[0]); 
    306317                        for (int j = 0; j < interfaces.length; j++) { 
    307318                            try { 
  • trunk/xml/defines-constructor.xml

    r21 r25  
    11<defines> 
    2     <tag name="color" type="net.schst.XJConf.Examples.ConstructorColor"
     2    <tag name="color" type="net.schst.XJConf.Examples.ConstructorColor" keyAttribute="id"
    33        <constructor> 
    44            <attribute name="red" type="java.lang.Integer"/> 
  • trunk/xml/test-constructor.xml

    r21 r25  
    11<configuration> 
    2     <color red="100" green="25" blue="10"/> 
     2    <color id="color" red="100" green="25" blue="10"/> 
     3    <color id="color-no-atts"/> 
    34     
    45    <!-- The cdata is the third parameter in the constructor --> 
  • trunk/xml/test.xml

    r21 r25  
    55        <item>bar</item> 
    66    </array> 
    7  
    87    <foo>tomato</foo> 
    98    <zahl>124</zahl>