Author: swd847
Date: 2010-06-27 02:06:05 -0400 (Sun, 27 Jun 2010)
New Revision: 6578
Added:
extensions/trunk/src/test/java/org/jboss/weld/extensions/test/core/
extensions/trunk/src/test/java/org/jboss/weld/extensions/test/core/BorderCollie.java
extensions/trunk/src/test/java/org/jboss/weld/extensions/test/core/CoreTest.java
extensions/trunk/src/test/java/org/jboss/weld/extensions/test/core/Dog.java
extensions/trunk/src/test/java/org/jboss/weld/extensions/test/core/Greyhound.java
extensions/trunk/src/test/java/org/jboss/weld/extensions/test/core/RaceTrack.java
Modified:
extensions/trunk/src/main/java/org/jboss/weld/extensions/core/CoreExtension.java
extensions/trunk/src/main/java/org/jboss/weld/extensions/core/Exact.java
extensions/trunk/src/main/java/org/jboss/weld/extensions/util/Reflections.java
Log:
Changes to the way @Exact is implemented, and tests to make sure it works
Modified:
extensions/trunk/src/main/java/org/jboss/weld/extensions/core/CoreExtension.java
===================================================================
---
extensions/trunk/src/main/java/org/jboss/weld/extensions/core/CoreExtension.java 2010-06-24
11:51:42 UTC (rev 6577)
+++
extensions/trunk/src/main/java/org/jboss/weld/extensions/core/CoreExtension.java 2010-06-27
06:06:05 UTC (rev 6578)
@@ -16,19 +16,18 @@
*/
package org.jboss.weld.extensions.core;
-import static org.jboss.weld.extensions.util.Reflections.getAnnotationsWithMetatype;
-import static org.jboss.weld.extensions.util.Reflections.getMemberType;
-
import java.lang.annotation.Annotation;
import java.lang.reflect.Member;
import java.util.ArrayList;
import java.util.Collection;
-import java.util.Set;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
import javax.enterprise.inject.spi.AnnotatedConstructor;
+import javax.enterprise.inject.spi.AnnotatedField;
+import javax.enterprise.inject.spi.AnnotatedMethod;
+import javax.enterprise.inject.spi.AnnotatedParameter;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
@@ -36,15 +35,11 @@
import javax.enterprise.inject.spi.ProcessAnnotatedType;
import javax.inject.Inject;
import javax.inject.Named;
-import javax.inject.Qualifier;
import org.jboss.weld.extensions.annotated.AnnotatedTypeBuilder;
import org.jboss.weld.extensions.annotated.AnnotationBuilder;
import org.jboss.weld.extensions.annotated.MemberAnnotationRedefiner;
-import org.jboss.weld.extensions.annotated.Parameter;
-import org.jboss.weld.extensions.annotated.ParameterAnnotationRedefiner;
import org.jboss.weld.extensions.bean.BeanBuilder;
-import org.jboss.weld.extensions.core.Exact.ExactLiteral;
/**
* Extension to install the "core" extensions. Core extensions are those that
@@ -74,7 +69,7 @@
return;
}
- AnnotatedTypeBuilder<X> builder =
AnnotatedTypeBuilder.newInstance(pat.getAnnotatedType()).readAnnotationsFromUnderlyingType();
+ AnnotatedTypeBuilder<X> builder =
AnnotatedTypeBuilder.newInstance(pat.getAnnotatedType()).mergeAnnotations(pat.getAnnotatedType(),
true);
// support for @Named packages
Package pkg = pat.getAnnotatedType().getJavaClass().getPackage();
@@ -84,7 +79,6 @@
builder.redefineMembers(Named.class, new
MemberAnnotationRedefiner<Named>()
{
-
public Named redefine(Named annotation, Member member, AnnotationBuilder
annotations)
{
if (annotations.isAnnotationPresent(Produces.class))
@@ -111,45 +105,39 @@
}
// support for @Exact
- Set<Annotation> qualfiers =
getAnnotationsWithMetatype(pat.getAnnotatedType().getAnnotations(), Qualifier.class);
- if (qualfiers.isEmpty() || (qualfiers.size() == 1 &&
qualfiers.iterator().next().annotationType() == Named.class))
+ // fields
+ for (AnnotatedField<? super X> f : pat.getAnnotatedType().getFields())
{
- builder.addToClass(DefaultLiteral.INSTANCE);
+ if (f.isAnnotationPresent(Exact.class))
+ {
+ Class<?> type = f.getAnnotation(Exact.class).value();
+ builder.overrideFieldType(f.getJavaMember(), type);
+ }
}
- builder.addToClass(new Exact.ExactLiteral(pat.getAnnotatedType().getJavaClass()));
- builder.redefineMembers(Exact.class, new MemberAnnotationRedefiner<Exact>()
+ // method parameters
+ for (AnnotatedMethod<? super X> m : pat.getAnnotatedType().getMethods())
{
-
- public Exact redefine(Exact annotation, Member annotated, AnnotationBuilder
annotations)
+ for (AnnotatedParameter<? super X> p : m.getParameters())
{
- if (annotation.value() == void.class)
+ if (p.isAnnotationPresent(Exact.class))
{
- return new ExactLiteral(getMemberType(annotated));
+ Class<?> type = p.getAnnotation(Exact.class).value();
+ builder.overrideMethodParameterType(m.getJavaMember(), type,
p.getPosition());
}
- else
- {
- return annotation;
- }
}
- });
-
- builder.redefineMemberParameters(Exact.class, new
ParameterAnnotationRedefiner<Exact>()
+ }
+ // constructor parameters
+ for (AnnotatedConstructor<X> c : pat.getAnnotatedType().getConstructors())
{
-
- public Exact redefine(Exact annotation, Parameter annotated, AnnotationBuilder
annotations)
+ for (AnnotatedParameter<? super X> p : c.getParameters())
{
- if (annotation.value() == void.class)
+ if (p.isAnnotationPresent(Exact.class))
{
- return new ExactLiteral(getMemberType(annotated.getDeclaringMember()));
+ Class<?> type = p.getAnnotation(Exact.class).value();
+ builder.overrideConstructorParameterType(c.getJavaMember(), type,
p.getPosition());
}
- else
- {
- return annotation;
- }
}
-
- });
-
+ }
pat.setAnnotatedType(builder.create());
// support for @Constructs
@@ -157,7 +145,7 @@
{
if (constructor.isAnnotationPresent(Constructs.class))
{
- AnnotatedTypeBuilder<X> annotatedTypeBuilder =
AnnotatedTypeBuilder.newInstance(pat.getAnnotatedType()).readAnnotationsFromUnderlyingType();
+ AnnotatedTypeBuilder<X> annotatedTypeBuilder =
AnnotatedTypeBuilder.newInstance(pat.getAnnotatedType()).mergeAnnotations(pat.getAnnotatedType(),
true);
// remove class-level @Named annotation
annotatedTypeBuilder.removeFromClass(Named.class);
// remove bean constructors annotated @Inject
Modified: extensions/trunk/src/main/java/org/jboss/weld/extensions/core/Exact.java
===================================================================
--- extensions/trunk/src/main/java/org/jboss/weld/extensions/core/Exact.java 2010-06-24
11:51:42 UTC (rev 6577)
+++ extensions/trunk/src/main/java/org/jboss/weld/extensions/core/Exact.java 2010-06-27
06:06:05 UTC (rev 6578)
@@ -27,7 +27,6 @@
import java.lang.annotation.Target;
import javax.enterprise.util.AnnotationLiteral;
-import javax.inject.Qualifier;
/**
* An injection point qualifier that may be used to select the exact bean to be
@@ -39,7 +38,6 @@
@Retention(RUNTIME)
@Target( { METHOD, TYPE, FIELD, PARAMETER })
@Documented
-@Qualifier
public @interface Exact
{
Class<?> value() default void.class;
Modified: extensions/trunk/src/main/java/org/jboss/weld/extensions/util/Reflections.java
===================================================================
---
extensions/trunk/src/main/java/org/jboss/weld/extensions/util/Reflections.java 2010-06-24
11:51:42 UTC (rev 6577)
+++
extensions/trunk/src/main/java/org/jboss/weld/extensions/util/Reflections.java 2010-06-27
06:06:05 UTC (rev 6578)
@@ -26,6 +26,7 @@
import javax.enterprise.inject.spi.AnnotatedField;
import javax.enterprise.inject.spi.AnnotatedType;
+import javax.enterprise.inject.spi.BeanManager;
/**
* class that provides a way of retrieving all methods and fields from a class
@@ -96,7 +97,20 @@
return set;
}
+ public static Set<Annotation> getQualifiers(Set<Annotation> annotations,
BeanManager beanManager)
+ {
+ Set<Annotation> set = new HashSet<Annotation>();
+ for (Annotation annotation : annotations)
+ {
+ if (beanManager.isQualifier(annotation.annotationType()))
+ {
+ set.add(annotation);
+ }
+ }
+ return set;
+ }
+
public static boolean methodExists(Class<?> parent, String name)
{
Class<?> p = parent;
Added:
extensions/trunk/src/test/java/org/jboss/weld/extensions/test/core/BorderCollie.java
===================================================================
--- extensions/trunk/src/test/java/org/jboss/weld/extensions/test/core/BorderCollie.java
(rev 0)
+++
extensions/trunk/src/test/java/org/jboss/weld/extensions/test/core/BorderCollie.java 2010-06-27
06:06:05 UTC (rev 6578)
@@ -0,0 +1,22 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jboss.weld.extensions.test.core;
+
+public class BorderCollie extends Dog
+{
+
+}
Added: extensions/trunk/src/test/java/org/jboss/weld/extensions/test/core/CoreTest.java
===================================================================
--- extensions/trunk/src/test/java/org/jboss/weld/extensions/test/core/CoreTest.java
(rev 0)
+++
extensions/trunk/src/test/java/org/jboss/weld/extensions/test/core/CoreTest.java 2010-06-27
06:06:05 UTC (rev 6578)
@@ -0,0 +1,38 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jboss.weld.extensions.test.core;
+
+import org.jboss.testharness.impl.packaging.Artifact;
+import org.jboss.weld.test.AbstractWeldTest;
+import org.testng.annotations.Test;
+
+/**
+ * @author Stuart Douglas
+ *
+ */
+@Artifact
+public class CoreTest extends AbstractWeldTest
+{
+ @Test
+ public void testExact()
+ {
+ // TODO: This needs to be split up into lots of little tests
+ RaceTrack bean = getReference(RaceTrack.class);
+ assert bean.dog instanceof Greyhound;
+ }
+
+}
Added: extensions/trunk/src/test/java/org/jboss/weld/extensions/test/core/Dog.java
===================================================================
--- extensions/trunk/src/test/java/org/jboss/weld/extensions/test/core/Dog.java
(rev 0)
+++ extensions/trunk/src/test/java/org/jboss/weld/extensions/test/core/Dog.java 2010-06-27
06:06:05 UTC (rev 6578)
@@ -0,0 +1,22 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jboss.weld.extensions.test.core;
+
+public class Dog
+{
+
+}
Added: extensions/trunk/src/test/java/org/jboss/weld/extensions/test/core/Greyhound.java
===================================================================
--- extensions/trunk/src/test/java/org/jboss/weld/extensions/test/core/Greyhound.java
(rev 0)
+++
extensions/trunk/src/test/java/org/jboss/weld/extensions/test/core/Greyhound.java 2010-06-27
06:06:05 UTC (rev 6578)
@@ -0,0 +1,22 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jboss.weld.extensions.test.core;
+
+public class Greyhound extends Dog
+{
+
+}
Added: extensions/trunk/src/test/java/org/jboss/weld/extensions/test/core/RaceTrack.java
===================================================================
--- extensions/trunk/src/test/java/org/jboss/weld/extensions/test/core/RaceTrack.java
(rev 0)
+++
extensions/trunk/src/test/java/org/jboss/weld/extensions/test/core/RaceTrack.java 2010-06-27
06:06:05 UTC (rev 6578)
@@ -0,0 +1,28 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jboss.weld.extensions.test.core;
+
+import javax.inject.Inject;
+
+import org.jboss.weld.extensions.core.Exact;
+
+public class RaceTrack
+{
+ @Inject
+ @Exact(Greyhound.class)
+ Dog dog;
+}