1 package jfun.yan.xml.nuts; 2 3 4 import java.lang.reflect.Field ; 5 6 import jfun.yan.Component; 7 import jfun.yan.Components; 8 import jfun.yan.Functions; 9 import jfun.yan.util.ReflectionUtil; 10 import jfun.yan.xml.NutsUtils; 11 12 13 19 public class FieldNut extends DelegatingNut { 20 private Class hostclass; 21 private String fldname; 22 private boolean private_access = false; 23 24 25 public boolean isPrivate_access() { 26 return private_access; 27 } 28 public void setPrivate_access(boolean private_access) { 29 this.private_access = private_access; 30 } 31 public void setClass(Class type){ 32 this.hostclass = type; 34 } 35 public void setName(String name){ 36 fldname = name.trim(); 37 } 38 private static Component getField1(Class hostclass, Component c, String name, 39 boolean private_access){ 40 if(c!=null){ 41 if(hostclass!=null){ 42 final Field fld = ReflectionUtil.getField(hostclass, name, 43 private_access); 44 return c.field(fld); 45 } 46 else 47 return c.field(name, private_access); 48 } 49 else{ 50 return Components.fun( 51 Functions.static_field(hostclass, name, private_access)); 52 } 53 54 } 55 private static Component getFieldComponent(Class type, Component c, 56 String name, boolean private_access){ 57 if(name.indexOf('.')<0) 58 return getField1(type, c, name.trim(), private_access); 59 final String [] parts = NutsUtils.split(name, "."); 60 c = getField1(type, c, parts[0], private_access); 61 for(int i=1; i<parts.length; i++){ 62 c = c.field(parts[i].trim(), private_access); 63 } 64 return c; 65 } 66 public Component eval(){ 67 final Component cc = getComponent(); 68 checkMandatory("class", hostclass, "component", cc); 69 checkMandatory("name", fldname); 70 return getFieldComponent(hostclass, cc, fldname, private_access); 71 84 } 85 } 86 | Popular Tags |