1 10 11 package org.picocontainer.defaults; 12 13 import org.picocontainer.ComponentAdapter; 14 import org.picocontainer.Parameter; 15 import org.picocontainer.PicoContainer; 16 import org.picocontainer.PicoException; 17 import org.picocontainer.PicoIntrospectionException; 18 import org.picocontainer.PicoVisitor; 19 20 import java.io.Serializable ; 21 import java.lang.reflect.Field ; 22 23 24 35 public class ConstantParameter 36 implements Parameter, Serializable { 37 38 private final Object value; 39 40 public ConstantParameter(Object value) { 41 this.value = value; 42 } 43 44 public Object resolveInstance(PicoContainer container, ComponentAdapter adapter, Class expectedType) { 45 return value; 46 } 47 48 public boolean isResolvable(PicoContainer container, ComponentAdapter adapter, Class expectedType) { 49 try { 50 verify(container, adapter, expectedType); 51 return true; 52 } catch(final PicoIntrospectionException e) { 53 return false; 54 } 55 } 56 57 63 public void verify(PicoContainer container, ComponentAdapter adapter, Class expectedType) throws PicoException { 64 if (!checkPrimitive(expectedType) && !expectedType.isInstance(value)) { 65 throw new PicoIntrospectionException(expectedType.getClass().getName() 66 + " is not assignable from " 67 + value.getClass().getName()); 68 } 69 } 70 71 76 public void accept(final PicoVisitor visitor) { 77 visitor.visitParameter(this); 78 } 79 80 private boolean checkPrimitive(Class expectedType) { 81 try { 82 if (expectedType.isPrimitive()) { 83 final Field field = value.getClass().getField("TYPE"); 84 final Class type = (Class ) field.get(value); 85 return expectedType.isAssignableFrom(type); 86 } 87 } catch (NoSuchFieldException e) { 88 } catch (IllegalAccessException e) { 89 } 90 return false; 91 } 92 93 } 94 | Popular Tags |