Author: aslak
Date: 2009-12-27 07:52:06 -0500 (Sun, 27 Dec 2009)
New Revision: 5368
Modified:
test/trunk/core/src/main/java/org/jboss/weld/test/core/TestCore.java
test/trunk/junit/src/test/java/org/jboss/weld/test/junit/ModelTestCase.java
test/trunk/testng/src/test/java/org/jboss/weld/test/testng/ModelTestCase.java
Log:
WELDX-63 Changed to use non-contextual-injection
Modified: test/trunk/core/src/main/java/org/jboss/weld/test/core/TestCore.java
===================================================================
--- test/trunk/core/src/main/java/org/jboss/weld/test/core/TestCore.java 2009-12-24
14:29:14 UTC (rev 5367)
+++ test/trunk/core/src/main/java/org/jboss/weld/test/core/TestCore.java 2009-12-27
12:52:06 UTC (rev 5368)
@@ -1,11 +1,11 @@
package org.jboss.weld.test.core;
import java.lang.annotation.Annotation;
-import java.lang.reflect.Field;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.InjectionTarget;
import javax.inject.Qualifier;
import org.jboss.weld.test.spi.Bootstrap;
@@ -26,6 +26,20 @@
boostrap.stop();
}
+ public void injectFields(Object target) throws Exception
+ {
+ injectNonContextualInstance(manager, target);
+ }
+
+ @SuppressWarnings("unchecked")
+ private void injectNonContextualInstance(BeanManager manager, Object instance)
+ {
+ CreationalContext<Object> creationalContext =
manager.createCreationalContext(null);
+ InjectionTarget<Object> injectionTarget = (InjectionTarget<Object>)
manager.createInjectionTarget(
+ manager.createAnnotatedType(instance.getClass()));
+ injectionTarget.inject(instance, creationalContext);
+ }
+
public <T> T getInstanceByType(Class<T> type, Annotation... bindings)
{
return getInstanceByType(manager, type, bindings);
@@ -40,34 +54,6 @@
return (T) manager.getReference(bean, type, cc);
}
- public void injectFields(Object target) throws Exception
- {
- Field[] fields = target.getClass().getDeclaredFields();
- for (Field field : fields)
- {
- injectField(field, target);
- }
- }
-
- private void injectField(Field field, Object target) throws Exception
- {
- if (!hasBindTypeAnnotation(field.getAnnotations()))
- {
- return;
- }
- if (!field.isAccessible())
- {
- field.setAccessible(true);
- }
- Object injectable = getInstanceByType(field.getType(), field
- .getAnnotations());
-
- if (injectable != null)
- {
- field.set(target, injectable);
- }
- }
-
public static boolean hasBindTypeAnnotation(Annotation[] annotations)
{
for (Annotation annotation : annotations)
Modified: test/trunk/junit/src/test/java/org/jboss/weld/test/junit/ModelTestCase.java
===================================================================
--- test/trunk/junit/src/test/java/org/jboss/weld/test/junit/ModelTestCase.java 2009-12-24
14:29:14 UTC (rev 5367)
+++ test/trunk/junit/src/test/java/org/jboss/weld/test/junit/ModelTestCase.java 2009-12-27
12:52:06 UTC (rev 5368)
@@ -1,5 +1,7 @@
package org.jboss.weld.test.junit;
+import javax.inject.Inject;
+
import junit.framework.Assert;
import org.jboss.weld.test.model.Language;
@@ -15,8 +17,8 @@
// Will not be injected, no BindingType
private static String userName = "Aslak";
- private @Language(LanguageType.NO)
- WelcomeService service;
+ @Inject @Language(LanguageType.NO)
+ private WelcomeService service;
private User user = new User(userName);
Modified: test/trunk/testng/src/test/java/org/jboss/weld/test/testng/ModelTestCase.java
===================================================================
---
test/trunk/testng/src/test/java/org/jboss/weld/test/testng/ModelTestCase.java 2009-12-24
14:29:14 UTC (rev 5367)
+++
test/trunk/testng/src/test/java/org/jboss/weld/test/testng/ModelTestCase.java 2009-12-27
12:52:06 UTC (rev 5368)
@@ -1,5 +1,7 @@
package org.jboss.weld.test.testng;
+import javax.inject.Inject;
+
import org.jboss.weld.test.model.Language;
import org.jboss.weld.test.model.LanguageType;
import org.jboss.weld.test.model.User;
@@ -12,8 +14,8 @@
// Will not be injected, no BindingType
private static String userName = "Aslak";
- private @Language(LanguageType.NO)
- WelcomeService service;
+ @Inject @Language(LanguageType.NO)
+ private WelcomeService service;
private User user = new User(userName);
@@ -26,3 +28,4 @@
}
}
+