1 6 package org.jfox.ioc.depend; 7 8 import org.jfox.ioc.Registry; 9 import org.jfox.ioc.exception.ComponentException; 10 import org.jfox.ioc.util.Classes; 11 12 18 19 public class ObjectDependency implements Dependency { 20 21 private String literalValue = null; 22 private Object value = null; 23 private Class type = null; 24 private String stype = null; 25 26 public ObjectDependency(Object obj) { 27 this.value = obj; 28 this.type = obj.getClass(); 29 } 30 31 37 public ObjectDependency(Object value, Class type) { 38 this.value = value; 39 this.type = type; 40 } 41 42 public ObjectDependency(String literalValue, String type) { 43 this.literalValue = literalValue; 44 this.stype = type; 45 } 46 47 public ObjectDependency(String literalValue, Class type) { 48 this.literalValue = literalValue; 49 this.type = type; 50 } 51 52 55 public Class getType() { 56 return type; 57 } 58 59 public String getLiteralValue() { 60 if(literalValue == null) { 61 literalValue = value.toString(); 62 } 63 return literalValue; 64 65 } 66 67 82 public Object resolveValue(Registry registry) throws ComponentException { 83 if(type == null && stype != null) { 84 if(Classes.isPrimitiveClass(stype)) { 86 type = Classes.loadPrimitiveClass(stype); 87 } 88 else { 89 try { 90 type = registry.loadClass(stype); 92 } 93 catch(Exception e) { 94 throw new ComponentException("load type class " + stype + " error", e); 95 } 96 } 97 } 98 101 if(value == null) { 102 try { 103 value = Classes.newObject(type,literalValue); 104 value = type.getConstructor(new Class []{String .class}).newInstance(new Object []{literalValue}); 105 } 106 catch(Exception e) { 107 throw new ComponentException("resolve component parameter value error", e); 108 } 109 } 110 return value; 111 } 112 113 public String toString() { 114 return "ComponentInstanceParameter{" + 115 "type=" + type + 116 ", value=" + (value == null ? "NOT RESOVLED" : value.toString()) + 117 "}"; 118 } 119 120 public static void main(String [] args) { 121 122 } 123 124 } 125 126 | Popular Tags |