1 package jfun.yan.xml.nuts; 2 3 import java.lang.reflect.Array ; 4 5 import jfun.util.Misc; 6 import jfun.util.StringUtils; 7 import jfun.yan.Component; 8 import jfun.yan.Components; 9 import jfun.yan.SimpleComponent; 10 import jfun.yan.util.ReflectionUtil; 11 12 13 19 public class ArrayNut extends CollectionNut { 20 private void checkTypeMatch(Class expected, Class actual){ 21 if(!ReflectionUtil.isAssignableFrom(expected, actual)){ 22 throw raise(Misc.getTypeName(actual) 23 + " is not subtype of "+Misc.getTypeName(expected)); 24 } 25 } 26 public void setType(Class type){ 27 if(!type.isArray()){ 28 raise(Misc.getTypeName(type)+" is not an array."); 29 } 30 final Class etype = type.getComponentType(); 31 final Class of = getOf(); 32 if(of==null){ 33 super.setOf(etype); 34 } 35 else{ 36 checkTypeMatch(etype, of); 37 } 38 super.setType(type); 39 } 40 public void setOf(Class oftype){ 41 final Class of = getOf(); 42 if(of==null){ 43 super.setType(Misc.getArrayType(oftype)); 44 } 45 else{ 46 checkTypeMatch(of, oftype); 47 } 48 super.setOf(oftype); 49 } 50 70 public Component eval(){ 71 final Component[] elements = getMandatoryElements(); 72 final Class type = getType(Object [].class); 73 final Class oftype = getOf(); 74 final Class etype = oftype==null?Object .class:oftype; 75 final Component step1 = new SimpleComponent(type){ 76 public Object create(){ 77 return Array.newInstance(etype, elements.length); 78 } 79 public String toString(){ 80 return StringUtils.listArray("[",",","]",elements); 81 } 82 }; 83 return Components.storeArray(step1, elements); 84 91 } 92 } 93 | Popular Tags |