diff options
author | bdaniel <bdaniel@13f79535-47bb-0310-9956-ffa450edef68> | 2010-08-18 13:52:28 +0000 |
---|---|---|
committer | bdaniel <bdaniel@13f79535-47bb-0310-9956-ffa450edef68> | 2010-08-18 13:52:28 +0000 |
commit | a91e13d3e237ea8dcd91a1eafaecf47203e50768 (patch) | |
tree | f17c4e7f31b56669616602633589615ec0ea8623 /sca-java-2.x/trunk/modules | |
parent | 748a4395399afcca8c8a3b448a75b6a114991981 (diff) |
TUSCANY-3652 Add methods to Injectors to get underlying Types and to inject null
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@986676 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/trunk/modules')
3 files changed, 45 insertions, 2 deletions
diff --git a/sca-java-2.x/trunk/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/FieldInjector.java b/sca-java-2.x/trunk/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/FieldInjector.java index dd454be7c5..4d97e72bd0 100644 --- a/sca-java-2.x/trunk/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/FieldInjector.java +++ b/sca-java-2.x/trunk/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/FieldInjector.java @@ -19,6 +19,7 @@ package org.apache.tuscany.sca.implementation.java.injection; import java.lang.reflect.Field; +import java.lang.reflect.Type; import java.security.AccessController; import java.security.PrivilegedAction; @@ -58,10 +59,28 @@ public class FieldInjector<T> implements Injector<T> { * Inject a new value on the given instance */ public void inject(T instance) throws ObjectCreationException { + inject(instance, objectFactory.getInstance()); + } + + public void inject(T instance, Object value) { try { - field.set(instance, objectFactory.getInstance()); + field.set(instance, value); } catch (IllegalAccessException e) { throw new ObjectCreationException("Field is not accessible [" + field + "]", e); } } + + public Class<?> getType() { + return field.getType(); + } + + public Type getGenericType() { + return field.getGenericType(); + } + + public void injectNull(T instance) throws ObjectCreationException { + inject(instance, null); + } + + } diff --git a/sca-java-2.x/trunk/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/Injector.java b/sca-java-2.x/trunk/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/Injector.java index 4d062859b9..f0208c4e71 100644 --- a/sca-java-2.x/trunk/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/Injector.java +++ b/sca-java-2.x/trunk/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/Injector.java @@ -18,6 +18,8 @@ */ package org.apache.tuscany.sca.implementation.java.injection; +import java.lang.reflect.Type; + import org.apache.tuscany.sca.core.factory.ObjectCreationException; /** @@ -31,5 +33,8 @@ public interface Injector<T> { * Inject a value on the given instance */ void inject(T instance) throws ObjectCreationException; + void injectNull(T instance) throws ObjectCreationException; + Class<?> getType(); + Type getGenericType(); } diff --git a/sca-java-2.x/trunk/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/MethodInjector.java b/sca-java-2.x/trunk/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/MethodInjector.java index baf9eb9cc1..cd43a60ab6 100644 --- a/sca-java-2.x/trunk/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/MethodInjector.java +++ b/sca-java-2.x/trunk/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/MethodInjector.java @@ -20,6 +20,7 @@ package org.apache.tuscany.sca.implementation.java.injection; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.lang.reflect.Type; import java.security.AccessController; import java.security.PrivilegedAction; @@ -51,8 +52,12 @@ public class MethodInjector<T> implements Injector<T> { } public void inject(T instance) throws ObjectCreationException { + inject(instance, objectFactory.getInstance()); + } + + private void inject(T instance, Object value) { try { - method.invoke(instance, objectFactory.getInstance()); + method.invoke(instance, value); } catch (IllegalAccessException e) { throw new ObjectCreationException("Method is not accessible [" + method + "]", e); } catch (IllegalArgumentException e) { @@ -61,4 +66,18 @@ public class MethodInjector<T> implements Injector<T> { throw new ObjectCreationException("Exception thrown by setter: " + method.getName(), e); } } + + public Class<?> getType() { + return method.getParameterTypes()[0]; + } + + public Type getGenericType() { + return method.getGenericParameterTypes()[0]; + } + + public void injectNull(T instance) throws ObjectCreationException { + inject(instance, null); + } + + } |