1 8 14 package jfun.yan; 15 16 import jfun.util.Misc; 17 import jfun.util.StringUtils; 18 import jfun.yan.factory.Factory; 19 import jfun.yan.util.ReflectionUtil; 20 21 27 final class ProxyComponent extends DelegatingComponent { 28 private final Class [] itfs; 29 private final ClassLoader cl; 30 private transient Class proxyType; 31 ProxyComponent(Component cc, final Class [] itfs, 32 final ClassLoader cl) { 33 super(cc); 34 this.itfs = itfs; 35 this.cl = cl; 36 this.proxyType = InstanceProxy.getProxyClass(cl, itfs); 37 } 38 private void readObject(java.io.ObjectInputStream in) 39 throws java.io.IOException , ClassNotFoundException { 40 in.defaultReadObject(); 41 this.proxyType = InstanceProxy.getProxyClass(this.cl, this.itfs); 42 } 43 public boolean isConcrete(){ 44 return true; 45 } 46 public Class getType(){ 47 return proxyType; 48 } 49 public String toString(){ 50 return "proxy <" + getDelegateTarget() +">"; 51 } 52 public Object create(final Dependency dep){ 53 return InstanceProxy.instance(cl, itfs, new Factory(){ 54 public Object create(){ 55 return getDelegateTarget().create(dep); 56 } 57 }); 58 } 59 public Class verify(final Dependency dep){ 60 return getType(); 63 } 64 public Component singleton(){ 65 return Components.singleton(this); 66 99 } 100 136 140 public Component proxy(){ 141 return this; 142 } 143 public Component proxy(Class itf){ 144 return proxy(new Class []{itf}); 145 } 146 public Component proxy(Class [] itfs2){ 147 boolean act = false; 148 for(int i=0; i<itfs2.length; i++){ 149 final Class itf2 = itfs2[i]; 150 final int among = contains(itfs, itf2); 151 if(among < 0){ throw new IllegalArgumentException ("type " 153 + Misc.getTypeName(itf2) 154 + " is not a super type of any of " 155 + StringUtils.listString("[", ",", "]", itfs)); 156 } 157 if(among > 0) act = true; 159 } 160 return act?Components.proxy(this, itfs2): this; 162 } 163 164 private int contains(Class [] types, Class type){ 165 for(int i=0; i<types.length; i++){ 166 if(type.equals(types[i])){ 167 return 0; 168 } 169 if(ReflectionUtil.isAssignableFrom(type, types[i])){ 170 return 1; 171 } 172 } 173 return -1; 174 } 175 } 176 | Popular Tags |