Weld SVN: r6786 - extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic.
by weld-commits@lists.jboss.org
Author: pete.muir(a)jboss.org
Date: 2010-07-23 18:16:42 -0400 (Fri, 23 Jul 2010)
New Revision: 6786
Modified:
extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericBeanInjectionTargetWrapper.java
extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/InjectConfiguration.java
Log:
use Reflections
Modified: extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericBeanInjectionTargetWrapper.java
===================================================================
--- extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericBeanInjectionTargetWrapper.java 2010-07-23 22:12:08 UTC (rev 6785)
+++ extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericBeanInjectionTargetWrapper.java 2010-07-23 22:16:42 UTC (rev 6786)
@@ -16,13 +16,11 @@
*/
package org.jboss.weld.extensions.bean.generic;
+import static org.jboss.weld.extensions.util.Reflections.getAllFields;
import static org.jboss.weld.extensions.util.Reflections.getField;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
-import java.util.Arrays;
-import java.util.HashSet;
-import java.util.Set;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.AnnotatedField;
@@ -41,18 +39,6 @@
public class GenericBeanInjectionTargetWrapper<T> extends ForwardingInjectionTarget<T>
{
- private static Set<Field> getFields(Class<?> clazz)
- {
- Set<Field> fields = new HashSet<Field>();
- fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
- Class<?> superClass = clazz.getSuperclass();
- if (superClass != Object.class)
- {
- fields.addAll(getFields(superClass));
- }
- return fields;
- }
-
private final InjectionTarget<T> delegate;
private final Annotation annotation;
private final AnnotatedType<T> annotatedType;
@@ -73,7 +59,7 @@
@Override
public void inject(T instance, CreationalContext<T> ctx)
{
- for (Field f : getFields(instance.getClass()))
+ for (Field f : getAllFields(instance.getClass()))
{
if (annotation.annotationType().isAssignableFrom(f.getType()))
Modified: extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/InjectConfiguration.java
===================================================================
--- extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/InjectConfiguration.java 2010-07-23 22:12:08 UTC (rev 6785)
+++ extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/InjectConfiguration.java 2010-07-23 22:16:42 UTC (rev 6786)
@@ -22,7 +22,7 @@
import javax.enterprise.util.AnnotationLiteral;
/**
- * synthetic qualifier that is added to Generic beans to signify configuration annotation injection points
+ * Annotation used to indicate {@link GenericConfiguration} injection points
*
* @author Stuart Douglas <stuart(a)baileyroberts.com.au>
*
13 years, 10 months
Weld SVN: r6785 - in extensions/trunk/impl/src: test/java/org/jboss/weld/extensions/test/bean/generic and 1 other directory.
by weld-commits@lists.jboss.org
Author: pete.muir(a)jboss.org
Date: 2010-07-23 18:12:08 -0400 (Fri, 23 Jul 2010)
New Revision: 6785
Added:
extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericConfiguration.java
Removed:
extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericAnnotation.java
Modified:
extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericBeanExtension.java
extensions/trunk/impl/src/test/java/org/jboss/weld/extensions/test/bean/generic/Garply.java
Log:
rename GenericAnnotation to GenericConfiguration
Deleted: extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericAnnotation.java
===================================================================
--- extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericAnnotation.java 2010-07-23 22:05:15 UTC (rev 6784)
+++ extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericAnnotation.java 2010-07-23 22:12:08 UTC (rev 6785)
@@ -1,38 +0,0 @@
-/*
- * 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.bean.generic;
-
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
-
-/**
- * Must be applied to any annotation that is used as a value in @Generic, this
- * may not be needed in the future
- *
- * TODO Remove
- *
- * @author Stuart Douglas <stuart(a)baileyroberts.com.au>
- *
- */
-(a)Retention(RetentionPolicy.RUNTIME)
-@Target( { ElementType.TYPE })
-public @interface GenericAnnotation
-{
-
-}
Modified: extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericBeanExtension.java
===================================================================
--- extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericBeanExtension.java 2010-07-23 22:05:15 UTC (rev 6784)
+++ extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericBeanExtension.java 2010-07-23 22:12:08 UTC (rev 6785)
@@ -103,7 +103,7 @@
{
for (Annotation annotation : field.getAnnotations())
{
- if (annotation.annotationType().isAnnotationPresent(GenericAnnotation.class))
+ if (annotation.annotationType().isAnnotationPresent(GenericConfiguration.class))
{
if (!producerFields.containsKey(type.getJavaClass()))
{
@@ -127,7 +127,7 @@
{
for (Annotation annotation : method.getAnnotations())
{
- if (annotation.annotationType().isAnnotationPresent(GenericAnnotation.class))
+ if (annotation.annotationType().isAnnotationPresent(GenericConfiguration.class))
{
if (!producerFields.containsKey(type.getJavaClass()))
{
Copied: extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericConfiguration.java (from rev 6781, extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericAnnotation.java)
===================================================================
--- extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericConfiguration.java (rev 0)
+++ extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericConfiguration.java 2010-07-23 22:12:08 UTC (rev 6785)
@@ -0,0 +1,36 @@
+/*
+ * 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.bean.generic;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * A meta annotation used to indicate that the annotation is used for
+ * configuration of a generic bean
+ *
+ * @author Stuart Douglas <stuart(a)baileyroberts.com.au>
+ *
+ */
+(a)Retention(RetentionPolicy.RUNTIME)
+@Target( { ElementType.TYPE })
+public @interface GenericConfiguration
+{
+
+}
Modified: extensions/trunk/impl/src/test/java/org/jboss/weld/extensions/test/bean/generic/Garply.java
===================================================================
--- extensions/trunk/impl/src/test/java/org/jboss/weld/extensions/test/bean/generic/Garply.java 2010-07-23 22:05:15 UTC (rev 6784)
+++ extensions/trunk/impl/src/test/java/org/jboss/weld/extensions/test/bean/generic/Garply.java 2010-07-23 22:12:08 UTC (rev 6785)
@@ -25,7 +25,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
-import org.jboss.weld.extensions.bean.generic.GenericAnnotation;
+import org.jboss.weld.extensions.bean.generic.GenericConfiguration;
/**
* The annotation used to configure a generic bean
@@ -36,7 +36,7 @@
@Retention(RUNTIME)
@Target({METHOD, FIELD, PARAMETER, TYPE})
-@GenericAnnotation
+@GenericConfiguration
public @interface Garply
{
String value();
13 years, 10 months
Weld SVN: r6784 - in extensions/trunk/impl/src/main/java/org/jboss/weld/extensions: util and 1 other directory.
by weld-commits@lists.jboss.org
Author: pete.muir(a)jboss.org
Date: 2010-07-23 18:05:15 -0400 (Fri, 23 Jul 2010)
New Revision: 6784
Added:
extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/Synthetic.java
Removed:
extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/SyntheticQualifier.java
extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/SyntheticQualifierManager.java
Modified:
extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericBeanExtension.java
extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/ProducerFieldInjectionTarget.java
Log:
make Synthetic a generic utility, use AnnotationLiteral
Modified: extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericBeanExtension.java
===================================================================
--- extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericBeanExtension.java 2010-07-23 21:42:50 UTC (rev 6783)
+++ extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericBeanExtension.java 2010-07-23 22:05:15 UTC (rev 6784)
@@ -45,6 +45,7 @@
import org.jboss.weld.extensions.annotated.AnnotatedTypeBuilder;
import org.jboss.weld.extensions.bean.BeanBuilder;
+import org.jboss.weld.extensions.util.Synthetic;
import org.jboss.weld.extensions.util.properties.Properties;
import org.jboss.weld.extensions.util.properties.Property;
@@ -65,19 +66,19 @@
// A map of a generic annotation type to all instances of that type found on beans
private final Map<Class<?>, Set<Annotation>> concreteGenerics;
- private final SyntheticQualifierManager syntheticQualifierManager;
+ private final Synthetic.Provider syntheticProvider;
GenericBeanExtension()
{
this.genericBeans = new HashMap<Class<?>, Set<AnnotatedType<?>>>();
this.producerFields = new HashMap<Class<?>, Map<Member, Annotation>>();
this.concreteGenerics = new HashMap<Class<?>, Set<Annotation>>();
- this.syntheticQualifierManager = new SyntheticQualifierManager();
+ this.syntheticProvider = new Synthetic.Provider("org.jboss.weld.extensions.bean.generic");
}
void beforeBeanDiscovery(@Observes BeforeBeanDiscovery event)
{
- event.addQualifier(SyntheticQualifier.class);
+ event.addQualifier(Synthetic.class);
}
void processAnnotatedType(@Observes ProcessAnnotatedType<?> event)
@@ -160,7 +161,7 @@
Property<Object> property = Properties.createProperty(field);
setters.add(property);
}
- ProducerFieldInjectionTarget<T> it = new ProducerFieldInjectionTarget<T>(event.getInjectionTarget(), beanManager, setters, producers, syntheticQualifierManager);
+ ProducerFieldInjectionTarget<T> it = new ProducerFieldInjectionTarget<T>(event.getInjectionTarget(), beanManager, setters, producers, syntheticProvider);
event.setInjectionTarget(it);
}
@@ -189,7 +190,7 @@
private <X> Bean<X> redefineType(AnnotatedType<X> at, Annotation conc, BeanManager beanManager)
{
- SyntheticQualifier newQualifier = syntheticQualifierManager.getQualifierForGeneric(conc);
+ Synthetic newQualifier = syntheticProvider.get(conc);
AnnotatedTypeBuilder<X> builder = AnnotatedTypeBuilder.newInstance(at).readAnnotationsFromUnderlyingType();
builder.addToClass(newQualifier);
Modified: extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/ProducerFieldInjectionTarget.java
===================================================================
--- extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/ProducerFieldInjectionTarget.java 2010-07-23 21:42:50 UTC (rev 6783)
+++ extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/ProducerFieldInjectionTarget.java 2010-07-23 22:05:15 UTC (rev 6784)
@@ -28,6 +28,7 @@
import javax.enterprise.inject.spi.InjectionTarget;
import org.jboss.weld.extensions.bean.ForwardingInjectionTarget;
+import org.jboss.weld.extensions.util.Synthetic;
import org.jboss.weld.extensions.util.properties.Property;
/**
@@ -44,15 +45,15 @@
private final BeanManager beanManager;
private final List<Property<Object>> properties;
private final Map<Member, Annotation> producers;
- private final SyntheticQualifierManager syntheticQualifierManager;
+ private final Synthetic.Provider syntheticProvider;
- public ProducerFieldInjectionTarget(InjectionTarget<T> delegate, BeanManager beanManager, List<Property<Object>> properties, Map<Member, Annotation> producers, SyntheticQualifierManager syntheticQualifierManager)
+ public ProducerFieldInjectionTarget(InjectionTarget<T> delegate, BeanManager beanManager, List<Property<Object>> properties, Map<Member, Annotation> producers, Synthetic.Provider syntheticProvider)
{
this.delegate = delegate;
this.beanManager = beanManager;
this.properties = properties;
this.producers = producers;
- this.syntheticQualifierManager = syntheticQualifierManager;
+ this.syntheticProvider = syntheticProvider;
}
@Override
@@ -66,7 +67,7 @@
{
for (Property<Object> property: properties)
{
- SyntheticQualifier qualifier = syntheticQualifierManager.getQualifierForGeneric(producers.get(property.getMember()));
+ Synthetic qualifier = syntheticProvider.get(producers.get(property.getMember()));
Bean<?> bean = beanManager.resolve(beanManager.getBeans(property.getBaseType(), qualifier));
if (bean == null)
{
Deleted: extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/SyntheticQualifier.java
===================================================================
--- extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/SyntheticQualifier.java 2010-07-23 21:42:50 UTC (rev 6783)
+++ extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/SyntheticQualifier.java 2010-07-23 22:05:15 UTC (rev 6784)
@@ -1,37 +0,0 @@
-/*
- * 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.bean.generic;
-
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-
-import javax.inject.Qualifier;
-
-/**
- * qualifier that is added to Generic beans to that the correct one is injected
- *
- * @author Stuart Douglas <stuart(a)baileyroberts.com.au>
- *
- */
-(a)Retention(RetentionPolicy.RUNTIME)
-// even though this is not in a bean archive this is still needed to make
-// BeanImpl work
-@Qualifier
-public @interface SyntheticQualifier
-{
- long value();
-}
Deleted: extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/SyntheticQualifierManager.java
===================================================================
--- extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/SyntheticQualifierManager.java 2010-07-23 21:42:50 UTC (rev 6783)
+++ extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/SyntheticQualifierManager.java 2010-07-23 22:05:15 UTC (rev 6784)
@@ -1,38 +0,0 @@
-package org.jboss.weld.extensions.bean.generic;
-
-import java.lang.annotation.Annotation;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-
-import org.jboss.weld.extensions.util.AnnotationInstanceProvider;
-
-class SyntheticQualifierManager
-{
-
-
- private final AnnotationInstanceProvider annotationProvider;
-
- //Map of generic Annotation instance to a SyntheticQualifier
- private final Map<Annotation, SyntheticQualifier> qualifierMap;
-
- private long count;
-
- SyntheticQualifierManager()
- {
- this.qualifierMap = new HashMap<Annotation, SyntheticQualifier>();
- this.annotationProvider = new AnnotationInstanceProvider();
- this.count = 0;
- }
-
- SyntheticQualifier getQualifierForGeneric(Annotation annotation)
- {
- if (!qualifierMap.containsKey(annotation))
- {
- SyntheticQualifier qualifier = annotationProvider.get(SyntheticQualifier.class, Collections.singletonMap("value", count++));
- qualifierMap.put(annotation, qualifier);
- }
- return qualifierMap.get(annotation);
- }
-
-}
Copied: extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/Synthetic.java (from rev 6781, extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/SyntheticQualifier.java)
===================================================================
--- extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/Synthetic.java (rev 0)
+++ extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/Synthetic.java 2010-07-23 22:05:15 UTC (rev 6784)
@@ -0,0 +1,98 @@
+/*
+ * 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.util;
+
+import java.lang.annotation.Annotation;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicLong;
+
+import javax.enterprise.util.AnnotationLiteral;
+import javax.inject.Qualifier;
+
+/**
+ * A synthetic qualifier that can be used to replace other user-supplied configuration at deployment
+ *
+ * @author Stuart Douglas <stuart(a)baileyroberts.com.au>
+ * @author Pete Muir
+ *
+ */
+(a)Retention(RetentionPolicy.RUNTIME)
+// even though this is not in a bean archive this is still needed to make
+// BeanImpl work
+@Qualifier
+public @interface Synthetic
+{
+
+ long index();
+
+ String namespace();
+
+ public static class SyntheticLiteral extends AnnotationLiteral<Synthetic> implements Synthetic
+ {
+
+ private final Long index;
+
+ private final String namespace;
+
+ public SyntheticLiteral(String namespace, Long index)
+ {
+ this.namespace = namespace;
+ this.index = index;
+ }
+
+ public long index()
+ {
+ return index;
+ }
+
+ public String namespace()
+ {
+ return namespace;
+ }
+
+ }
+
+ public static class Provider
+ {
+
+ //Map of generic Annotation instance to a SyntheticQualifier
+ private final Map<Annotation, Synthetic> synthetics;
+ private final String namespace;
+
+ private AtomicLong count;
+
+ public Provider(String namespace)
+ {
+ this.synthetics = new HashMap<Annotation, Synthetic>();
+ this.namespace = namespace;
+ this.count = new AtomicLong();
+ }
+
+ public Synthetic get(Annotation annotation)
+ {
+ if (!synthetics.containsKey(annotation))
+ {
+ synthetics.put(annotation, new Synthetic.SyntheticLiteral(namespace, count.getAndIncrement()));
+ }
+ return synthetics.get(annotation);
+ }
+ }
+
+}
13 years, 10 months
Weld SVN: r6783 - in extensions/trunk/impl/src/main/java/org/jboss/weld/extensions: bean/generic and 2 other directories.
by weld-commits@lists.jboss.org
Author: pete.muir(a)jboss.org
Date: 2010-07-23 17:42:50 -0400 (Fri, 23 Jul 2010)
New Revision: 6783
Added:
extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericBeanExtension.java
extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/SyntheticQualifierManager.java
Removed:
extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/FieldSetter.java
extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericExtension.java
Modified:
extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/annotated/AnnotatedTypeBuilder.java
extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/ProducerFieldInjectionTarget.java
extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/AnnotationInstanceProvider.java
extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/AnnotationInvocationHandler.java
extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/properties/FieldPropertyImpl.java
extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/properties/MethodPropertyImpl.java
extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/properties/Properties.java
extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/properties/Property.java
Log:
Initial refactor of GenericBean onto Property
Modified: extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/annotated/AnnotatedTypeBuilder.java
===================================================================
--- extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/annotated/AnnotatedTypeBuilder.java 2010-07-23 21:41:46 UTC (rev 6782)
+++ extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/annotated/AnnotatedTypeBuilder.java 2010-07-23 21:42:50 UTC (rev 6783)
@@ -100,7 +100,7 @@
typeAnnotations.add(annotation);
}
- for (Field field : Reflections.getFields(underlyingType))
+ for (Field field : Reflections.getAllFields(underlyingType))
{
AnnotationBuilder annotationBuilder = new AnnotationBuilder();
fields.put(field, annotationBuilder);
@@ -111,7 +111,7 @@
}
}
- for (Method method : Reflections.getMethods(underlyingType))
+ for (Method method : Reflections.getAllMethods(underlyingType))
{
AnnotationBuilder annotationBuilder = new AnnotationBuilder();
method.setAccessible(true);
Deleted: extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/FieldSetter.java
===================================================================
--- extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/FieldSetter.java 2010-07-23 21:41:46 UTC (rev 6782)
+++ extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/FieldSetter.java 2010-07-23 21:42:50 UTC (rev 6783)
@@ -1,69 +0,0 @@
-/*
- * 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.bean.generic;
-
-import java.lang.reflect.Field;
-
-import javax.enterprise.context.spi.CreationalContext;
-import javax.enterprise.inject.UnsatisfiedResolutionException;
-import javax.enterprise.inject.spi.Bean;
-import javax.enterprise.inject.spi.BeanManager;
-
-/**
- * Class that is responsible for setting the values of generic producer fields
- *
- * @author Stuart Douglas <stuart(a)baileyroberts.com.au>
- *
- */
-class FieldSetter
-{
- private final Field field;
- private final SyntheticQualifier qualifier;
- private final BeanManager beanManager;
-
- FieldSetter(BeanManager beanManager, Field field, SyntheticQualifier qualifier)
- {
- this.field = field;
- this.field.setAccessible(true);
- this.qualifier = qualifier;
- this.beanManager = beanManager;
- }
-
- public void set(Object instance, CreationalContext<?> ctx)
- {
- Bean<?> bean = beanManager.resolve(beanManager.getBeans(field.getType(), qualifier));
- if (bean == null)
- {
- throw new UnsatisfiedResolutionException("Could not resolve bean for Generic Producer field " + field.getDeclaringClass() + "." + field.getName() + " Type: " + field.getType() + " Qualifiers:" + qualifier);
- }
- Object value = beanManager.getReference(bean, field.getType(), ctx);
- try
- {
- field.set(instance, value);
- }
- catch (IllegalArgumentException e)
- {
- throw new RuntimeException(e);
- }
- catch (IllegalAccessException e)
- {
- throw new RuntimeException(e);
- }
-
- }
-
-}
Copied: extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericBeanExtension.java (from rev 6772, extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericExtension.java)
===================================================================
--- extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericBeanExtension.java (rev 0)
+++ extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericBeanExtension.java 2010-07-23 21:42:50 UTC (rev 6783)
@@ -0,0 +1,299 @@
+/*
+ * 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.bean.generic;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Member;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.Map.Entry;
+
+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;
+import javax.enterprise.inject.spi.BeforeBeanDiscovery;
+import javax.enterprise.inject.spi.Extension;
+import javax.enterprise.inject.spi.InjectionTarget;
+import javax.enterprise.inject.spi.ProcessAnnotatedType;
+import javax.enterprise.inject.spi.ProcessInjectionTarget;
+import javax.inject.Inject;
+
+import org.jboss.weld.extensions.annotated.AnnotatedTypeBuilder;
+import org.jboss.weld.extensions.bean.BeanBuilder;
+import org.jboss.weld.extensions.util.properties.Properties;
+import org.jboss.weld.extensions.util.properties.Property;
+
+/**
+ * Extension that wires in Generic Beans
+ *
+ * @author Pete Muir
+ * @author Stuart Douglas <stuart(a)baileyroberts.com.au>
+ *
+ */
+class GenericBeanExtension implements Extension
+{
+
+ private final Map<Class<?>, Set<AnnotatedType<?>>> genericBeans;
+
+ private final Map<Class<?>, Map<Member, Annotation>> producerFields;
+
+ // A map of a generic annotation type to all instances of that type found on beans
+ private final Map<Class<?>, Set<Annotation>> concreteGenerics;
+
+ private final SyntheticQualifierManager syntheticQualifierManager;
+
+ GenericBeanExtension()
+ {
+ this.genericBeans = new HashMap<Class<?>, Set<AnnotatedType<?>>>();
+ this.producerFields = new HashMap<Class<?>, Map<Member, Annotation>>();
+ this.concreteGenerics = new HashMap<Class<?>, Set<Annotation>>();
+ this.syntheticQualifierManager = new SyntheticQualifierManager();
+ }
+
+ void beforeBeanDiscovery(@Observes BeforeBeanDiscovery event)
+ {
+ event.addQualifier(SyntheticQualifier.class);
+ }
+
+ void processAnnotatedType(@Observes ProcessAnnotatedType<?> event)
+ {
+ AnnotatedType<?> type = event.getAnnotatedType();
+ if (type.isAnnotationPresent(Generic.class))
+ {
+ Generic generic = type.getAnnotation(Generic.class);
+ if (!genericBeans.containsKey(generic.value()))
+ {
+ genericBeans.put(generic.value(), new HashSet<AnnotatedType<?>>());
+ }
+ genericBeans.get(generic.value()).add(type);
+ // we will install (multiple copies of) this bean later
+ event.veto();
+
+ }
+ // make note of any producer fields that produce generic beans
+ for (AnnotatedField<?> field : type.getFields())
+ {
+ if (field.isAnnotationPresent(Produces.class))
+ {
+ for (Annotation annotation : field.getAnnotations())
+ {
+ if (annotation.annotationType().isAnnotationPresent(GenericAnnotation.class))
+ {
+ if (!producerFields.containsKey(type.getJavaClass()))
+ {
+ producerFields.put(type.getJavaClass(), new HashMap<Member, Annotation>());
+ }
+ if (!concreteGenerics.containsKey(annotation.annotationType()))
+ {
+ concreteGenerics.put(annotation.annotationType(), new HashSet<Annotation>());
+ }
+ producerFields.get(type.getJavaClass()).put(field.getJavaMember(), annotation);
+ concreteGenerics.get(annotation.annotationType()).add(annotation);
+ }
+ }
+ }
+ }
+
+ // make note of any producer method that produce generic beans
+ for (AnnotatedMethod<?> method : type.getMethods())
+ {
+ if (method.isAnnotationPresent(Produces.class))
+ {
+ for (Annotation annotation : method.getAnnotations())
+ {
+ if (annotation.annotationType().isAnnotationPresent(GenericAnnotation.class))
+ {
+ if (!producerFields.containsKey(type.getJavaClass()))
+ {
+ producerFields.put(type.getJavaClass(), new HashMap<Member, Annotation>());
+ }
+ if (!concreteGenerics.containsKey(annotation.annotationType()))
+ {
+ concreteGenerics.put(annotation.annotationType(), new HashSet<Annotation>());
+ }
+ producerFields.get(type.getJavaClass()).put(method.getJavaMember(), annotation);
+ concreteGenerics.get(annotation.annotationType()).add(annotation);
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * wraps InjectionTarget to initialise producer fields that produce generic
+ * beans
+ */
+ <T> void processInjectionTarget(@Observes ProcessInjectionTarget<T> event, BeanManager beanManager)
+ {
+ Class<?> javaClass = event.getAnnotatedType().getJavaClass();
+ if (producerFields.containsKey(javaClass))
+ {
+ Map<Member, Annotation> producers = producerFields.get(javaClass);
+ List<Property<Object>> setters = new ArrayList<Property<Object>>();
+ for (Member field : producers.keySet())
+ {
+ Property<Object> property = Properties.createProperty(field);
+ setters.add(property);
+ }
+ ProducerFieldInjectionTarget<T> it = new ProducerFieldInjectionTarget<T>(event.getInjectionTarget(), beanManager, setters, producers, syntheticQualifierManager);
+ event.setInjectionTarget(it);
+ }
+
+ }
+
+ /**
+ * Installs the generic beans.
+ */
+ public void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager beanManager)
+ {
+ for (Entry<Class<?>, Set<AnnotatedType<?>>> entry : genericBeans.entrySet())
+ {
+ Set<Annotation> concretes = concreteGenerics.get(entry.getKey());
+ if (concretes != null)
+ {
+ for (AnnotatedType<?> type : entry.getValue())
+ {
+ for (Annotation conc : concretes)
+ {
+ abd.addBean(redefineType(type, conc, beanManager));
+ }
+ }
+ }
+ }
+ }
+
+ private <X> Bean<X> redefineType(AnnotatedType<X> at, Annotation conc, BeanManager beanManager)
+ {
+ SyntheticQualifier newQualifier = syntheticQualifierManager.getQualifierForGeneric(conc);
+
+ AnnotatedTypeBuilder<X> builder = AnnotatedTypeBuilder.newInstance(at).readAnnotationsFromUnderlyingType();
+ builder.addToClass(newQualifier);
+ for (AnnotatedField<? super X> f : at.getFields())
+ {
+ if (f.isAnnotationPresent(Inject.class))
+ {
+ // if this is a configuration injection point
+ if (conc.annotationType().isAssignableFrom(f.getJavaMember().getType()))
+ {
+ builder.removeFromField(f.getJavaMember(), Inject.class);
+ builder.addToField(f.getJavaMember(), InjectConfiguration.INSTANCE);
+ }
+ else
+ {
+ // check to see if we should be injecting a generic bean
+ // we do this by checking if there are any beans that can be
+ // injected into this point
+ // if there is not then we assume it is a generic injection
+ // point
+ // this has the downside that if it is actually a deployment
+ // error then it will confuse the user
+ Annotation[] qls = getQualifiers(f.getAnnotations(), beanManager);
+ Set<Bean<?>> beans = beanManager.getBeans(f.getJavaMember().getType(), qls);
+ if (beans.isEmpty())
+ {
+ builder.addToField(f.getJavaMember(), newQualifier);
+ }
+ }
+ }
+ else if (f.isAnnotationPresent(Produces.class))
+ {
+ // TODO: register a producer with the appropriate qualifier
+ }
+ }
+ for (AnnotatedMethod<?> m : at.getMethods())
+ {
+ // TODO: need to properly handle Observer methods and Disposal
+ // methods
+ if (m.isAnnotationPresent(Produces.class))
+ {
+ // TODO: we need to register the producer bean, so this is not
+ // very useful at the moment
+ for (AnnotatedParameter<?> pm : m.getParameters())
+ {
+ Class<?> paramType = m.getJavaMember().getParameterTypes()[pm.getPosition()];
+
+ // check to see if we should be injecting a generic bean
+ // we do this by checking if there are any beans that can be
+ // injected into this point
+ // if there is not then we assume it is a generic injection
+ // point
+ // this has the downside that if it is actually a deployment
+ // error then it will confuse the user
+ Annotation[] qls = getQualifiers(pm.getAnnotations(), beanManager);
+ Set<Bean<?>> beans = beanManager.getBeans(paramType, qls);
+ if (beans.isEmpty())
+ {
+ builder.addToMethod(m.getJavaMember(), newQualifier);
+ }
+ }
+ }
+ }
+
+ for (AnnotatedConstructor<X> m : at.getConstructors())
+ {
+ if (m.isAnnotationPresent(Inject.class))
+ {
+ for (AnnotatedParameter<X> pm : m.getParameters())
+ {
+ Class<?> paramType = m.getJavaMember().getParameterTypes()[pm.getPosition()];
+ Annotation[] qls = getQualifiers(pm.getAnnotations(), beanManager);
+ Set<Bean<?>> beans = beanManager.getBeans(paramType, qls);
+ if (beans.isEmpty())
+ {
+ builder.addToConstructorParameter(m.getJavaMember(), pm.getPosition(), newQualifier);
+ }
+ }
+ }
+ }
+ AnnotatedType<X> newAnnotatedType = builder.create();
+ InjectionTarget<X> it = beanManager.createInjectionTarget(newAnnotatedType);
+
+ it = new GenericBeanInjectionTargetWrapper<X>(newAnnotatedType, it, conc);
+ BeanBuilder<X> beanBuilder = new BeanBuilder<X>(newAnnotatedType, beanManager).defineBeanFromAnnotatedType().setInjectionTarget(it);
+ return beanBuilder.create();
+ }
+
+ static Annotation[] getQualifiers(Set<Annotation> annotations, BeanManager manager)
+ {
+ List<Annotation> qualifiers = new ArrayList<Annotation>();
+ for (Annotation a : annotations)
+ {
+ if (manager.isQualifier(a.annotationType()))
+ {
+ qualifiers.add(a);
+ }
+ }
+ Annotation[] qls = new Annotation[qualifiers.size()];
+ for (int j = 0; j < qls.length; ++j)
+ {
+ qls[j] = qualifiers.get(j);
+ }
+ return qls;
+ }
+
+}
Deleted: extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericExtension.java
===================================================================
--- extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericExtension.java 2010-07-23 21:41:46 UTC (rev 6782)
+++ extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericExtension.java 2010-07-23 21:42:50 UTC (rev 6783)
@@ -1,280 +0,0 @@
-/*
- * 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.bean.generic;
-
-import java.lang.annotation.Annotation;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.Map.Entry;
-
-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;
-import javax.enterprise.inject.spi.BeforeBeanDiscovery;
-import javax.enterprise.inject.spi.Extension;
-import javax.enterprise.inject.spi.InjectionTarget;
-import javax.enterprise.inject.spi.ProcessAnnotatedType;
-import javax.enterprise.inject.spi.ProcessInjectionTarget;
-import javax.inject.Inject;
-
-import org.jboss.weld.extensions.annotated.AnnotatedTypeBuilder;
-import org.jboss.weld.extensions.bean.BeanBuilder;
-import org.jboss.weld.extensions.util.AnnotationInstanceProvider;
-
-public class GenericExtension implements Extension
-{
-
- private AnnotationInstanceProvider annotationProvider = new AnnotationInstanceProvider();
-
- private Map<Class<?>, Set<AnnotatedType<?>>> genericBeans = new HashMap<Class<?>, Set<AnnotatedType<?>>>();
-
- private Map<Class<?>, Map<AnnotatedField<?>, Annotation>> producerFields = new HashMap<Class<?>, Map<AnnotatedField<?>, Annotation>>();
-
- /**
- * map of a generic annotation type to all instances of that type found on
- * beans
- */
- private Map<Class<?>, Set<Annotation>> concreteGenerics = new HashMap<Class<?>, Set<Annotation>>();
-
- /**
- * Map of generic Annotation instance to a SyntheticQualifier
- */
- private Map<Annotation, SyntheticQualifier> qualifierMap = new HashMap<Annotation, SyntheticQualifier>();
-
- long count = 0;
-
- public void beforeBeanDiscovery(@Observes BeforeBeanDiscovery event)
- {
- event.addQualifier(SyntheticQualifier.class);
- }
-
- public void processAnnotatedType(@Observes ProcessAnnotatedType<?> event)
- {
- AnnotatedType<?> type = event.getAnnotatedType();
- if (type.isAnnotationPresent(Generic.class))
- {
- Generic an = type.getAnnotation(Generic.class);
- if (!genericBeans.containsKey(an.value()))
- {
- genericBeans.put(an.value(), new HashSet<AnnotatedType<?>>());
- }
- genericBeans.get(an.value()).add(type);
- // we will install (multiple copies of) this bean later
- event.veto();
-
- }
- // make note of any producer fields that produce generic beans
- for (Object f : type.getFields())
- {
- AnnotatedField<?> field = (AnnotatedField<?>) f;
- if (field.isAnnotationPresent(Produces.class))
- {
- for (Annotation a : field.getAnnotations())
- {
- if (a.annotationType().isAnnotationPresent(GenericAnnotation.class))
- {
- if (!producerFields.containsKey(type.getJavaClass()))
- {
- producerFields.put(type.getJavaClass(), new HashMap<AnnotatedField<?>, Annotation>());
- }
- if (!concreteGenerics.containsKey(a.annotationType()))
- {
- concreteGenerics.put(a.annotationType(), new HashSet<Annotation>());
- }
- producerFields.get(type.getJavaClass()).put(field, a);
- concreteGenerics.get(a.annotationType()).add(a);
- }
- }
- }
- }
- }
-
- /**
- * wraps InjectionTarget to initialise producer fields that produce generic
- * beans
- */
- public <T> void processInjectionTarget(@Observes ProcessInjectionTarget<T> event, BeanManager beanManager)
- {
- Class<?> javaClass = event.getAnnotatedType().getJavaClass();
- if (producerFields.containsKey(javaClass))
- {
- Map<AnnotatedField<?>, Annotation> producers = producerFields.get(javaClass);
- List<FieldSetter> setters = new ArrayList<FieldSetter>();
- for (AnnotatedField<?> a : producers.keySet())
- {
- SyntheticQualifier qual = this.getQualifierForGeneric(producers.get(a));
- FieldSetter f = new FieldSetter(beanManager, a.getJavaMember(), qual);
- setters.add(f);
- }
- ProducerFieldInjectionTarget<T> it = new ProducerFieldInjectionTarget<T>(event.getInjectionTarget(), setters);
- event.setInjectionTarget(it);
- }
- }
-
- /**
- * Installs the generic beans.
- */
- public void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager beanManager)
- {
- for (Entry<Class<?>, Set<AnnotatedType<?>>> entry : genericBeans.entrySet())
- {
- Set<Annotation> concretes = concreteGenerics.get(entry.getKey());
- if (concretes != null)
- {
- for (AnnotatedType<?> type : entry.getValue())
- {
- for (Annotation conc : concretes)
- {
- abd.addBean(redefineType(type, conc, beanManager));
- }
- }
- }
- }
- }
-
- private <X> Bean<X> redefineType(AnnotatedType<X> at, Annotation conc, BeanManager beanManager)
- {
- SyntheticQualifier newQualifier = getQualifierForGeneric(conc);
-
- AnnotatedTypeBuilder<X> builder = AnnotatedTypeBuilder.newInstance(at).readAnnotationsFromUnderlyingType();
- builder.addToClass(newQualifier);
- for (AnnotatedField<? super X> f : at.getFields())
- {
- if (f.isAnnotationPresent(Inject.class))
- {
- // if this is a configuration injection point
- if (conc.annotationType().isAssignableFrom(f.getJavaMember().getType()))
- {
- builder.removeFromField(f.getJavaMember(), Inject.class);
- builder.addToField(f.getJavaMember(), InjectConfiguration.INSTANCE);
- }
- else
- {
- // check to see if we should be injecting a generic bean
- // we do this by checking if there are any beans that can be
- // injected into this point
- // if there is not then we assume it is a generic injection
- // point
- // this has the downside that if it is actually a deployment
- // error then it will confuse the user
- Annotation[] qls = getQualifiers(f.getAnnotations(), beanManager);
- Set<Bean<?>> beans = beanManager.getBeans(f.getJavaMember().getType(), qls);
- if (beans.isEmpty())
- {
- builder.addToField(f.getJavaMember(), newQualifier);
- }
- }
- }
- else if (f.isAnnotationPresent(Produces.class))
- {
- // TODO: register a producer with the appropriate qualifier
- }
- }
- for (AnnotatedMethod<?> m : at.getMethods())
- {
- // TODO: need to properly handle Observer methods and Disposal
- // methods
- if (m.isAnnotationPresent(Produces.class))
- {
- // TODO: we need to register the producer bean, so this is not
- // very useful at the moment
- for (AnnotatedParameter<?> pm : m.getParameters())
- {
- Class<?> paramType = m.getJavaMember().getParameterTypes()[pm.getPosition()];
-
- // check to see if we should be injecting a generic bean
- // we do this by checking if there are any beans that can be
- // injected into this point
- // if there is not then we assume it is a generic injection
- // point
- // this has the downside that if it is actually a deployment
- // error then it will confuse the user
- Annotation[] qls = getQualifiers(pm.getAnnotations(), beanManager);
- Set<Bean<?>> beans = beanManager.getBeans(paramType, qls);
- if (beans.isEmpty())
- {
- builder.addToMethod(m.getJavaMember(), newQualifier);
- }
- }
- }
- }
-
- for (AnnotatedConstructor<X> m : at.getConstructors())
- {
- if (m.isAnnotationPresent(Inject.class))
- {
- for (AnnotatedParameter<X> pm : m.getParameters())
- {
- Class<?> paramType = m.getJavaMember().getParameterTypes()[pm.getPosition()];
- Annotation[] qls = getQualifiers(pm.getAnnotations(), beanManager);
- Set<Bean<?>> beans = beanManager.getBeans(paramType, qls);
- if (beans.isEmpty())
- {
- builder.addToConstructorParameter(m.getJavaMember(), pm.getPosition(), newQualifier);
- }
- }
- }
- }
- AnnotatedType<X> newAnnotatedType = builder.create();
- InjectionTarget<X> it = beanManager.createInjectionTarget(newAnnotatedType);
-
- it = new GenericBeanInjectionTargetWrapper<X>(newAnnotatedType, it, conc);
- BeanBuilder<X> beanBuilder = new BeanBuilder<X>(newAnnotatedType, beanManager).defineBeanFromAnnotatedType().setInjectionTarget(it);
- return beanBuilder.create();
- }
-
- public SyntheticQualifier getQualifierForGeneric(Annotation a)
- {
- if (!qualifierMap.containsKey(a))
- {
- SyntheticQualifier qualifier = annotationProvider.get(SyntheticQualifier.class, (Map) Collections.singletonMap("value", count++));
- qualifierMap.put(a, qualifier);
- }
- return qualifierMap.get(a);
- }
-
- static Annotation[] getQualifiers(Set<Annotation> annotations, BeanManager manager)
- {
- List<Annotation> qualifiers = new ArrayList<Annotation>();
- for (Annotation a : annotations)
- {
- if (manager.isQualifier(a.annotationType()))
- {
- qualifiers.add(a);
- }
- }
- Annotation[] qls = new Annotation[qualifiers.size()];
- for (int j = 0; j < qls.length; ++j)
- {
- qls[j] = qualifiers.get(j);
- }
- return qls;
- }
-
-}
Modified: extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/ProducerFieldInjectionTarget.java
===================================================================
--- extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/ProducerFieldInjectionTarget.java 2010-07-23 21:41:46 UTC (rev 6782)
+++ extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/ProducerFieldInjectionTarget.java 2010-07-23 21:42:50 UTC (rev 6783)
@@ -16,12 +16,19 @@
*/
package org.jboss.weld.extensions.bean.generic;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Member;
import java.util.List;
+import java.util.Map;
import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.UnsatisfiedResolutionException;
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.InjectionTarget;
import org.jboss.weld.extensions.bean.ForwardingInjectionTarget;
+import org.jboss.weld.extensions.util.properties.Property;
/**
* {@link InjectionTarget} wrapper used for beans that have generic producer fields
@@ -33,12 +40,19 @@
public class ProducerFieldInjectionTarget<T> extends ForwardingInjectionTarget<T>
{
private final InjectionTarget<T> delegate;
- private final List<FieldSetter> fieldSetters;
+
+ private final BeanManager beanManager;
+ private final List<Property<Object>> properties;
+ private final Map<Member, Annotation> producers;
+ private final SyntheticQualifierManager syntheticQualifierManager;
- public ProducerFieldInjectionTarget(InjectionTarget<T> delegate, List<FieldSetter> fieldSetters)
+ public ProducerFieldInjectionTarget(InjectionTarget<T> delegate, BeanManager beanManager, List<Property<Object>> properties, Map<Member, Annotation> producers, SyntheticQualifierManager syntheticQualifierManager)
{
this.delegate = delegate;
- this.fieldSetters = fieldSetters;
+ this.beanManager = beanManager;
+ this.properties = properties;
+ this.producers = producers;
+ this.syntheticQualifierManager = syntheticQualifierManager;
}
@Override
@@ -50,9 +64,16 @@
@Override
public void inject(T instance, CreationalContext<T> ctx)
{
- for (FieldSetter f : fieldSetters)
+ for (Property<Object> property: properties)
{
- f.set(instance, ctx);
+ SyntheticQualifier qualifier = syntheticQualifierManager.getQualifierForGeneric(producers.get(property.getMember()));
+ Bean<?> bean = beanManager.resolve(beanManager.getBeans(property.getBaseType(), qualifier));
+ if (bean == null)
+ {
+ throw new UnsatisfiedResolutionException("Could not resolve bean for Generic Producer " + property.toString() + ". Type: " + property.getJavaClass() + " Qualifiers:" + qualifier);
+ }
+ Object value = beanManager.getReference(bean, property.getBaseType(), ctx);
+ property.setValue(instance, value);
}
delegate().inject(instance, ctx);
}
Added: extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/SyntheticQualifierManager.java
===================================================================
--- extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/SyntheticQualifierManager.java (rev 0)
+++ extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/SyntheticQualifierManager.java 2010-07-23 21:42:50 UTC (rev 6783)
@@ -0,0 +1,38 @@
+package org.jboss.weld.extensions.bean.generic;
+
+import java.lang.annotation.Annotation;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.jboss.weld.extensions.util.AnnotationInstanceProvider;
+
+class SyntheticQualifierManager
+{
+
+
+ private final AnnotationInstanceProvider annotationProvider;
+
+ //Map of generic Annotation instance to a SyntheticQualifier
+ private final Map<Annotation, SyntheticQualifier> qualifierMap;
+
+ private long count;
+
+ SyntheticQualifierManager()
+ {
+ this.qualifierMap = new HashMap<Annotation, SyntheticQualifier>();
+ this.annotationProvider = new AnnotationInstanceProvider();
+ this.count = 0;
+ }
+
+ SyntheticQualifier getQualifierForGeneric(Annotation annotation)
+ {
+ if (!qualifierMap.containsKey(annotation))
+ {
+ SyntheticQualifier qualifier = annotationProvider.get(SyntheticQualifier.class, Collections.singletonMap("value", count++));
+ qualifierMap.put(annotation, qualifier);
+ }
+ return qualifierMap.get(annotation);
+ }
+
+}
Property changes on: extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/SyntheticQualifierManager.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Modified: extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/AnnotationInstanceProvider.java
===================================================================
--- extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/AnnotationInstanceProvider.java 2010-07-23 21:41:46 UTC (rev 6782)
+++ extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/AnnotationInstanceProvider.java 2010-07-23 21:42:50 UTC (rev 6783)
@@ -46,7 +46,7 @@
* Returns an instance of the given annotation type with member values
* specified in the map.
*/
- public <T extends Annotation> T get(Class<T> annotation, Map<String, Object> values)
+ public <T extends Annotation> T get(Class<T> annotation, Map<String, ?> values)
{
if (annotation == null)
{
Modified: extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/AnnotationInvocationHandler.java
===================================================================
--- extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/AnnotationInvocationHandler.java 2010-07-23 21:41:46 UTC (rev 6782)
+++ extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/AnnotationInvocationHandler.java 2010-07-23 21:42:50 UTC (rev 6783)
@@ -57,10 +57,10 @@
private static class SerializationProxy
{
- private final Map<String, Object> valueMap;
+ private final Map<String, ?> valueMap;
private final Class<? extends Annotation> annotationType;
- private SerializationProxy(Map<String, Object> valueMap, Class<? extends Annotation> annotationType)
+ private SerializationProxy(Map<String, ?> valueMap, Class<? extends Annotation> annotationType)
{
this.valueMap = valueMap;
this.annotationType = annotationType;
@@ -81,7 +81,7 @@
private final Method[] members;
- AnnotationInvocationHandler(Map<String, Object> values, Class<? extends Annotation> annotationType)
+ AnnotationInvocationHandler(Map<String, ?> values, Class<? extends Annotation> annotationType)
{
this.valueMap = new HashMap<String, Object>();
valueMap.putAll(values);
Modified: extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/properties/FieldPropertyImpl.java
===================================================================
--- extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/properties/FieldPropertyImpl.java 2010-07-23 21:41:46 UTC (rev 6782)
+++ extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/properties/FieldPropertyImpl.java 2010-07-23 21:42:50 UTC (rev 6783)
@@ -4,6 +4,7 @@
package org.jboss.weld.extensions.util.properties;
import java.lang.reflect.Field;
+import java.lang.reflect.Member;
import java.lang.reflect.Type;
/**
@@ -46,6 +47,11 @@
{
return field;
}
+
+ public Member getMember()
+ {
+ return field;
+ }
@SuppressWarnings("unchecked")
public Class<V> getJavaClass()
@@ -100,4 +106,22 @@
{
return false;
}
+
+ @Override
+ public String toString()
+ {
+ return field.toString();
+ }
+
+ @Override
+ public int hashCode()
+ {
+ return field.hashCode();
+ }
+
+ @Override
+ public boolean equals(Object obj)
+ {
+ return field.equals(obj);
+ }
}
\ No newline at end of file
Modified: extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/properties/MethodPropertyImpl.java
===================================================================
--- extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/properties/MethodPropertyImpl.java 2010-07-23 21:41:46 UTC (rev 6782)
+++ extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/properties/MethodPropertyImpl.java 2010-07-23 21:42:50 UTC (rev 6783)
@@ -3,8 +3,10 @@
*/
package org.jboss.weld.extensions.util.properties;
+import static org.jboss.weld.extensions.util.Reflections.invokeMethod;
+
import java.beans.Introspector;
-import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
@@ -59,6 +61,11 @@
return getterMethod;
}
+ public Member getMember()
+ {
+ return getterMethod;
+ }
+
public V getValue(Object instance)
{
return getJavaClass().cast(invokeMethod(getterMethod, instance));
@@ -69,46 +76,6 @@
invokeMethod(setterMethod, instance, value);
}
- private static String buildInvokeMethodErrorMessage(Method method, Object obj, Object... args)
- {
- StringBuilder message = new StringBuilder(String.format("Exception invoking method [%s] on object [%s], using arguments [", method.getName(), obj));
- if (args != null)
- for (int i = 0; i < args.length; i++)
- message.append((i > 0 ? "," : "") + args[i]);
- message.append("]");
- return message.toString();
- }
-
- private static Object invokeMethod(Method method, Object obj, Object... args)
- {
- try
- {
- return method.invoke(obj, args);
- }
- catch (IllegalAccessException ex)
- {
- throw new RuntimeException(buildInvokeMethodErrorMessage(method, obj, args), ex);
- }
- catch (IllegalArgumentException ex)
- {
- throw new IllegalArgumentException(buildInvokeMethodErrorMessage(method, obj, args), ex.getCause());
- }
- catch (InvocationTargetException ex)
- {
- throw new RuntimeException(buildInvokeMethodErrorMessage(method, obj, args), ex);
- }
- catch (NullPointerException ex)
- {
- NullPointerException ex2 = new NullPointerException(buildInvokeMethodErrorMessage(method, obj, args));
- ex2.initCause(ex.getCause());
- throw ex2;
- }
- catch (ExceptionInInitializerError e)
- {
- throw new RuntimeException(buildInvokeMethodErrorMessage(method, obj, args), e);
- }
- }
-
private static Method getSetterMethod(Class<?> clazz, String name)
{
Method[] methods = clazz.getMethods();
@@ -161,5 +128,47 @@
{
return setterMethod == null;
}
+
+ @Override
+ public String toString()
+ {
+ StringBuilder builder = new StringBuilder();
+ if (isReadOnly())
+ {
+ builder.append("read-only ").append(setterMethod.toString()).append("; ");
+ }
+ builder.append(getterMethod.toString());
+ return builder.toString();
+ }
+
+ @Override
+ public int hashCode()
+ {
+ int hash = 1;
+ hash = hash * 31 + (setterMethod == null ? 0 : setterMethod.hashCode());
+ hash = hash * 31 + getterMethod.hashCode();
+ return hash;
+ }
+
+ @Override
+ public boolean equals(Object obj)
+ {
+ if (obj instanceof MethodPropertyImpl<?>)
+ {
+ MethodPropertyImpl<?> that = (MethodPropertyImpl<?>) obj;
+ if (this.setterMethod == null)
+ {
+ return that.setterMethod == null && this.getterMethod.equals(that.getterMethod);
+ }
+ else
+ {
+ return this.setterMethod.equals(that.setterMethod) && this.getterMethod.equals(that.getterMethod);
+ }
+ }
+ else
+ {
+ return false;
+ }
+ }
}
\ No newline at end of file
Modified: extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/properties/Properties.java
===================================================================
--- extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/properties/Properties.java 2010-07-23 21:41:46 UTC (rev 6782)
+++ extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/properties/Properties.java 2010-07-23 21:42:50 UTC (rev 6783)
@@ -1,6 +1,7 @@
package org.jboss.weld.extensions.util.properties;
import java.lang.reflect.Field;
+import java.lang.reflect.Member;
import java.lang.reflect.Method;
/**
@@ -38,4 +39,28 @@
{
return new MethodPropertyImpl<V>(method);
}
+
+ /**
+ * Create a JavaBean style property from the specified member
+ *
+ * @param <V>
+ * @param member
+ * @return
+ * @throws IllegalArgumentException if the method does not match JavaBean conventions
+ */
+ public static <V> Property<V> createProperty(Member member)
+ {
+ if (member instanceof Method)
+ {
+ return new MethodPropertyImpl<V>(Method.class.cast(member));
+ }
+ else if (member instanceof Field)
+ {
+ return new FieldPropertyImpl<V>(Field.class.cast(member));
+ }
+ else
+ {
+ throw new IllegalArgumentException("Cannot make a property of " + member + " - it is neither a method or a field");
+ }
+ }
}
Modified: extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/properties/Property.java
===================================================================
--- extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/properties/Property.java 2010-07-23 21:41:46 UTC (rev 6782)
+++ extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/properties/Property.java 2010-07-23 21:42:50 UTC (rev 6783)
@@ -1,6 +1,7 @@
package org.jboss.weld.extensions.util.properties;
import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.Member;
import java.lang.reflect.Type;
/**
@@ -40,13 +41,20 @@
public Class<V> getJavaClass();
/**
- * Get the element backing the property
+ * Get the element responsible for retrieving the property value
*
* @return
*/
public AnnotatedElement getAnnotatedElement();
/**
+ * Get the member responsible for retrieving the property value
+ *
+ * @return
+ */
+ public Member getMember();
+
+ /**
* Returns the property value for the specified bean. The property to be
* returned is either a field or getter method.
*
13 years, 10 months
Weld SVN: r6782 - extensions/trunk/impl.
by weld-commits@lists.jboss.org
Author: pete.muir(a)jboss.org
Date: 2010-07-23 17:41:46 -0400 (Fri, 23 Jul 2010)
New Revision: 6782
Modified:
extensions/trunk/impl/
Log:
ignores
Property changes on: extensions/trunk/impl
___________________________________________________________________
Name: svn:ignore
- target
.settings
.classpath
.project
+ target
.settings
.classpath
.project
test-output
13 years, 10 months
Weld SVN: r6781 - extensions/trunk/impl.
by weld-commits@lists.jboss.org
Author: pete.muir(a)jboss.org
Date: 2010-07-23 17:40:49 -0400 (Fri, 23 Jul 2010)
New Revision: 6781
Modified:
extensions/trunk/impl/pom.xml
Log:
remove testharness junk
Modified: extensions/trunk/impl/pom.xml
===================================================================
--- extensions/trunk/impl/pom.xml 2010-07-23 19:12:50 UTC (rev 6780)
+++ extensions/trunk/impl/pom.xml 2010-07-23 21:40:49 UTC (rev 6781)
@@ -9,7 +9,7 @@
<groupId>org.jboss.weld</groupId>
<artifactId>weld-extensions-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
- <relativePath>../pom.xml</relativePath>
+ <relativePath>../po.xml</relativePath>
</parent>
<name>Weld Extensions</name>
@@ -155,51 +155,6 @@
</dependency>
</dependencies>
</profile>
-<!--
- <profile>
- <id>write-artifacts-to-disk</id>
- <activation>
- <property>
- <name>dumpArtifacts</name>
- </property>
- </activation>
- <build>
- <plugins>
- <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>exec-maven-plugin</artifactId>
- <executions>
- <execution>
- <id>generate-test-artifacts</id>
- <phase>generate-test-sources</phase>
- <goals>
- <goal>java</goal>
- </goals>
- </execution>
- </executions>
- <configuration>
- <mainClass>org.jboss.testharness.api.TCK</mainClass>
- <classpathScope>test</classpathScope>
- <systemProperties>
- <systemProperty>
- <key>dumpArtifacts</key>
- <value>true</value>
- </systemProperty>
- <systemProperty>
- <key>org.jboss.testharness.outputDirectory</key>
- <value>target/test-artifacts</value>
- </systemProperty>
- <systemProperty>
- <key>org.jboss.testharness.libraryDirectory</key>
- <value>target/dependency/lib</value>
- </systemProperty>
- </systemProperties>
- </configuration>
- </plugin>
- </plugins>
- </build>
- </profile>
--->
</profiles>
<scm>
13 years, 10 months
Weld SVN: r6780 - extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic.
by weld-commits@lists.jboss.org
Author: pete.muir(a)jboss.org
Date: 2010-07-23 15:12:50 -0400 (Fri, 23 Jul 2010)
New Revision: 6780
Modified:
extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/ProducerFieldInjectionTarget.java
Log:
Use ForwardingInjectionTarget
Modified: extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/ProducerFieldInjectionTarget.java
===================================================================
--- extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/ProducerFieldInjectionTarget.java 2010-07-23 19:11:17 UTC (rev 6779)
+++ extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/ProducerFieldInjectionTarget.java 2010-07-23 19:12:50 UTC (rev 6780)
@@ -17,20 +17,20 @@
package org.jboss.weld.extensions.bean.generic;
import java.util.List;
-import java.util.Set;
import javax.enterprise.context.spi.CreationalContext;
-import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.InjectionTarget;
+import org.jboss.weld.extensions.bean.ForwardingInjectionTarget;
+
/**
- * injection target wrapper used for beans that have generic producer fields
+ * {@link InjectionTarget} wrapper used for beans that have generic producer fields
*
* @author Stuart Douglas <stuart(a)baileyroberts.com.au>
*
* @param <T>
*/
-public class ProducerFieldInjectionTarget<T> implements InjectionTarget<T>
+public class ProducerFieldInjectionTarget<T> extends ForwardingInjectionTarget<T>
{
private final InjectionTarget<T> delegate;
private final List<FieldSetter> fieldSetters;
@@ -40,39 +40,21 @@
this.delegate = delegate;
this.fieldSetters = fieldSetters;
}
+
+ @Override
+ protected InjectionTarget<T> delegate()
+ {
+ return delegate;
+ }
+ @Override
public void inject(T instance, CreationalContext<T> ctx)
{
for (FieldSetter f : fieldSetters)
{
f.set(instance, ctx);
}
- delegate.inject(instance, ctx);
+ delegate().inject(instance, ctx);
}
- public void postConstruct(T instance)
- {
- delegate.postConstruct(instance);
- }
-
- public void preDestroy(T instance)
- {
- delegate.preDestroy(instance);
- }
-
- public void dispose(T instance)
- {
- delegate.dispose(instance);
- }
-
- public Set<InjectionPoint> getInjectionPoints()
- {
- return delegate.getInjectionPoints();
- }
-
- public T produce(CreationalContext<T> ctx)
- {
- return delegate.produce(ctx);
- }
-
}
13 years, 10 months
Weld SVN: r6779 - extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util.
by weld-commits@lists.jboss.org
Author: pete.muir(a)jboss.org
Date: 2010-07-23 15:11:17 -0400 (Fri, 23 Jul 2010)
New Revision: 6779
Modified:
extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/Reflections.java
Log:
Tidy up
Modified: extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/Reflections.java
===================================================================
--- extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/Reflections.java 2010-07-23 18:45:13 UTC (rev 6778)
+++ extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/util/Reflections.java 2010-07-23 19:11:17 UTC (rev 6779)
@@ -19,6 +19,7 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.util.HashSet;
@@ -29,9 +30,10 @@
import javax.enterprise.inject.spi.BeanManager;
/**
- * class that provides a way of retrieving all methods and fields from a class
+ * Utility class for working with Java Reflection
*
* @author stuart
+ * @author Pete Muir
*
*/
public class Reflections
@@ -41,39 +43,59 @@
{
}
- public static Set<Field> getFields(Class<?> clazz)
+ /**
+ * Get all the declared fields on the class hierarchy. This <b>will</b>
+ * return overridden fields.
+ *
+ * @param clazz The class to search
+ * @return the set of all declared fields or an empty set if there are none
+ */
+ public static Set<Field> getAllFields(Class<?> clazz)
{
- HashSet<Field> ret = new HashSet<Field>();
- Class<?> p = clazz;
- while (p != null && p != Object.class)
+ HashSet<Field> fields = new HashSet<Field>();
+ for (Class<?> c = clazz; c != null && c != Object.class; c = c.getSuperclass())
{
- for (Field a : p.getDeclaredFields())
+ for (Field a : c.getDeclaredFields())
{
- ret.add(a);
+ fields.add(a);
}
- p = p.getSuperclass();
}
- return ret;
+ return fields;
}
- public static Field getField(Class<?> parent, String name)
+ /**
+ * Search the class hierarchy for a field with the given name. Will return
+ * the nearest match, starting with the class specified and searching up the
+ * hierarchy.
+ *
+ * @param clazz The class to search
+ * @param name The name of the field to search for
+ * @return The field found, or null if no field is found
+ */
+ public static Field getField(Class<?> clazz, String name)
{
- Class<?> p = parent;
- while (p != null && p != Object.class)
+ for (Class<?> c = clazz; c != null && c != Object.class; c = c.getSuperclass())
{
try
{
- return p.getDeclaredField(name);
+ return c.getDeclaredField(name);
}
- catch (Exception e1)
+ catch (NoSuchFieldException e)
{
-
+ // No-op, we continue looking up the class hierarchy
}
- p = p.getSuperclass();
}
return null;
}
+ /**
+ * Search the annotatedType for the field, returning the
+ * {@link AnnotatedField}
+ *
+ * @param annotatedType The annotatedType to search
+ * @param field the field to search for
+ * @return The {@link AnnotatedField} found, or null if no field is found
+ */
public static <X> AnnotatedField<? super X> getField(AnnotatedType<X> annotatedType, Field field)
{
for (AnnotatedField<? super X> annotatedField : annotatedType.getFields())
@@ -86,7 +108,15 @@
return null;
}
- public static Set<Annotation> getAnnotationsWithMetatype(Set<Annotation> annotations, Class<? extends Annotation> metaAnnotationType)
+ /**
+ * Search for annotations with the specified meta annotation type
+ *
+ * @param annotations The annotation set to search
+ * @param metaAnnotationType The type of the meta annotation to search for
+ * @return The set of annotations with the specified meta annotation, or an
+ * empty set if none are found
+ */
+ public static Set<Annotation> getAnnotationsWithMetaAnnotation(Set<Annotation> annotations, Class<? extends Annotation> metaAnnotationType)
{
Set<Annotation> set = new HashSet<Annotation>();
for (Annotation annotation : annotations)
@@ -99,6 +129,15 @@
return set;
}
+ /**
+ * Extract any qualifiers from the set of annotations
+ *
+ * @param annotations The set of annotations to search
+ * @param beanManager The beanManager to use to establish if an annotation is
+ * a qualifier
+ * @return The qualifiers present in the set, or an empty set if there are
+ * none
+ */
public static Set<Annotation> getQualifiers(Set<Annotation> annotations, BeanManager beanManager)
{
Set<Annotation> set = new HashSet<Annotation>();
@@ -112,89 +151,128 @@
return set;
}
- public static boolean methodExists(Class<?> parent, String name)
+ /**
+ * Determine if a method exists in a specified class hierarchy
+ *
+ * @param clazz The class to search
+ * @param name The name of the method
+ * @return true if a method is found, otherwise false
+ */
+ public static boolean methodExists(Class<?> clazz, String name)
{
- Class<?> p = parent;
- while (p != null && p != Object.class)
+ for (Class<?> c = clazz; c != null && c != Object.class; c = c.getSuperclass())
{
- for (Method m : p.getDeclaredMethods())
+ for (Method m : c.getDeclaredMethods())
{
if (m.getName().equals(name))
{
return true;
}
}
- p = p.getSuperclass();
}
return false;
}
- public static Set<Method> getMethods(Class<?> clazz)
+ /**
+ * Get all the declared methods on the class hierarchy. This <b>will</b>
+ * return overridden methods.
+ *
+ * @param clazz The class to search
+ * @return the set of all declared methods or an empty set if there are none
+ */
+ public static Set<Method> getAllMethods(Class<?> clazz)
{
- HashSet<Method> ret = new HashSet<Method>();
- Class<?> p = clazz;
- while (p != null && p != Object.class)
+ HashSet<Method> methods = new HashSet<Method>();
+ for (Class<?> c = clazz; c != null && c != Object.class; c = c.getSuperclass())
{
- for (Method a : p.getDeclaredMethods())
+ for (Method a : c.getDeclaredMethods())
{
- ret.add(a);
+ methods.add(a);
}
- p = p.getSuperclass();
}
- return ret;
+ return methods;
}
- public static Method getMethod(Class<?> parent, String name, Class<?>... args)
+ /**
+ * Search the class hierarchy for a method with the given name and arguments.
+ * Will return the nearest match, starting with the class specified and
+ * searching up the hierarchy.
+ *
+ * @param clazz The class to search
+ * @param name The name of the method to search for
+ * @param args The arguments of the method to search for
+ * @return The method found, or null if no method is found
+ */
+ public static Method getMethod(Class<?> clazz, String name, Class<?>... args)
{
- Class<?> p = parent;
- while (p != null && p != Object.class)
+ for (Class<?> c = clazz; c != null && c != Object.class; c = c.getSuperclass())
{
try
{
- return p.getDeclaredMethod(name, args);
+ return c.getDeclaredMethod(name, args);
}
- catch (Exception e1)
+ catch (NoSuchMethodException e)
{
-
+ // No-op, continue the search
}
- p = p.getSuperclass();
}
return null;
}
- public static Constructor<?> getConstructor(Class<?> parent, Class<?>... args)
+ /**
+ * Search the class hierarchy for a constructor with the given arguments.
+ * Will return the nearest match, starting with the class specified and
+ * searching up the hierarchy.
+ *
+ * @param clazz The class to search
+ * @param args The arguments of the constructor to search for
+ * @return The constructor found, or null if no constructor is found
+ */
+ public static Constructor<?> getConstructor(Class<?> clazz, Class<?>... args)
{
- Class<?> p = parent;
- while (p != null && p != Object.class)
+ for (Class<?> c = clazz; c != null && c != Object.class; c = c.getSuperclass())
{
try
{
- return p.getDeclaredConstructor(args);
+ return c.getDeclaredConstructor(args);
}
- catch (Exception e1)
+ catch (NoSuchMethodException e)
{
-
+ // No-op, continue the search
}
- p = p.getSuperclass();
}
return null;
}
- public static Set<Constructor<?>> getConstructors(Class<?> clazz)
+ /**
+ * Get all the declared constructors on the class hierarchy. This <b>will</b>
+ * return overridden constructors.
+ *
+ * @param clazz The class to search
+ * @return the set of all declared constructors or an empty set if there are
+ * none
+ */
+ public static Set<Constructor<?>> getAllConstructors(Class<?> clazz)
{
- HashSet<Constructor<?>> ret = new HashSet<Constructor<?>>();
- Class<?> p = clazz;
- while (p != null && p != Object.class)
+ HashSet<Constructor<?>> constructors = new HashSet<Constructor<?>>();
+ for (Class<?> c = clazz; c != null && c != Object.class; c = c.getSuperclass())
{
- for (Constructor<?> c : p.getDeclaredConstructors())
+ for (Constructor<?> constructor : c.getDeclaredConstructors())
{
- ret.add(c);
+ constructors.add(constructor);
}
- p = p.getSuperclass();
}
- return ret;
+ return constructors;
}
+ /**
+ * Get the type of the member
+ *
+ * @param member The member
+ * @return The type of the member
+ * @throws UnsupportedOperationException if the member is not a field,
+ * method, or constructor
+ */
public static Class<?> getMemberType(Member member)
{
if (member instanceof Field)
@@ -215,7 +293,17 @@
}
}
- public static Class classForName(String name) throws ClassNotFoundException
+ /**
+ * Load a class for the given name.
+ *
+ * If the Thread Context Class Loader is available, it will be used,
+ * otherwise the classloader used to load {@link Reflections} will be used
+ *
+ * @param name The name of the class to load
+ * @return The class object
+ * @throws ClassNotFoundException if the class cannot be found
+ */
+ public static Class<?> classForName(String name) throws ClassNotFoundException
{
if (Thread.currentThread().getContextClassLoader() != null)
{
@@ -226,24 +314,44 @@
return Class.forName(name);
}
}
-
- public static Object invokeAndWrap(Method method, Object target, Object... args)
+ private static String buildInvokeMethodErrorMessage(Method method, Object obj, Object... args)
{
+ StringBuilder message = new StringBuilder(String.format("Exception invoking method [%s] on object [%s], using arguments [", method.getName(), obj));
+ if (args != null)
+ for (int i = 0; i < args.length; i++)
+ message.append((i > 0 ? "," : "") + args[i]);
+ message.append("]");
+ return message.toString();
+ }
+
+ public static Object invokeMethod(Method method, Object obj, Object... args)
+ {
try
{
- return method.invoke(target, args);
+ return method.invoke(obj, args);
}
- catch (Exception e)
+ catch (IllegalAccessException ex)
{
- if (e instanceof RuntimeException)
- {
- throw (RuntimeException) e;
- }
- else
- {
- throw new RuntimeException("exception invoking: " + method.getName(), e);
- }
+ throw new RuntimeException(buildInvokeMethodErrorMessage(method, obj, args), ex);
}
+ catch (IllegalArgumentException ex)
+ {
+ throw new IllegalArgumentException(buildInvokeMethodErrorMessage(method, obj, args), ex.getCause());
+ }
+ catch (InvocationTargetException ex)
+ {
+ throw new RuntimeException(buildInvokeMethodErrorMessage(method, obj, args), ex);
+ }
+ catch (NullPointerException ex)
+ {
+ NullPointerException ex2 = new NullPointerException(buildInvokeMethodErrorMessage(method, obj, args));
+ ex2.initCause(ex.getCause());
+ throw ex2;
+ }
+ catch (ExceptionInInitializerError e)
+ {
+ throw new RuntimeException(buildInvokeMethodErrorMessage(method, obj, args), e);
+ }
}
}
13 years, 10 months
Weld SVN: r6778 - extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic.
by weld-commits@lists.jboss.org
Author: pete.muir(a)jboss.org
Date: 2010-07-23 14:45:13 -0400 (Fri, 23 Jul 2010)
New Revision: 6778
Modified:
extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericBeanInjectionTargetWrapper.java
Log:
use ForwaingInjectionTarget
Modified: extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericBeanInjectionTargetWrapper.java
===================================================================
--- extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericBeanInjectionTargetWrapper.java 2010-07-23 18:42:46 UTC (rev 6777)
+++ extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/generic/GenericBeanInjectionTargetWrapper.java 2010-07-23 18:45:13 UTC (rev 6778)
@@ -27,9 +27,10 @@
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.AnnotatedField;
import javax.enterprise.inject.spi.AnnotatedType;
-import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.InjectionTarget;
+import org.jboss.weld.extensions.bean.ForwardingInjectionTarget;
+
/**
* injection target wrapper that injects the configuration for generic beans
*
@@ -37,7 +38,7 @@
*
* @param <T>
*/
-public class GenericBeanInjectionTargetWrapper<T> implements InjectionTarget<T>
+public class GenericBeanInjectionTargetWrapper<T> extends ForwardingInjectionTarget<T>
{
private static Set<Field> getFields(Class<?> clazz)
@@ -62,7 +63,14 @@
this.delegate = delegate;
this.annotatedType = annotatedType;
}
+
+ @Override
+ protected InjectionTarget<T> delegate()
+ {
+ return delegate;
+ }
+ @Override
public void inject(T instance, CreationalContext<T> ctx)
{
for (Field f : getFields(instance.getClass()))
@@ -86,32 +94,7 @@
}
}
- delegate.inject(instance, ctx);
+ delegate().inject(instance, ctx);
}
- public void postConstruct(T instance)
- {
- delegate.postConstruct(instance);
- }
-
- public void preDestroy(T instance)
- {
- delegate.preDestroy(instance);
- }
-
- public void dispose(T instance)
- {
- delegate.dispose(instance);
- }
-
- public Set<InjectionPoint> getInjectionPoints()
- {
- return delegate.getInjectionPoints();
- }
-
- public T produce(CreationalContext<T> ctx)
- {
- return delegate.produce(ctx);
- }
-
}
13 years, 10 months
Weld SVN: r6777 - extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean.
by weld-commits@lists.jboss.org
Author: pete.muir(a)jboss.org
Date: 2010-07-23 14:42:46 -0400 (Fri, 23 Jul 2010)
New Revision: 6777
Added:
extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/ForwardingInjectionTarget.java
Log:
Add ForwardingInjectionTarget
Added: extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/ForwardingInjectionTarget.java
===================================================================
--- extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/ForwardingInjectionTarget.java (rev 0)
+++ extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/ForwardingInjectionTarget.java 2010-07-23 18:42:46 UTC (rev 6777)
@@ -0,0 +1,44 @@
+package org.jboss.weld.extensions.bean;
+
+import java.util.Set;
+
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.InjectionPoint;
+import javax.enterprise.inject.spi.InjectionTarget;
+
+public abstract class ForwardingInjectionTarget<T> implements InjectionTarget<T>
+{
+
+ protected abstract InjectionTarget<T> delegate();
+
+ public void inject(T instance, CreationalContext<T> ctx)
+ {
+ delegate().inject(instance, ctx);
+ }
+
+ public void postConstruct(T instance)
+ {
+ delegate().postConstruct(instance);
+ }
+
+ public void preDestroy(T instance)
+ {
+ delegate().preDestroy(instance);
+ }
+
+ public void dispose(T instance)
+ {
+ delegate().dispose(instance);
+ }
+
+ public Set<InjectionPoint> getInjectionPoints()
+ {
+ return delegate().getInjectionPoints();
+ }
+
+ public T produce(CreationalContext<T> ctx)
+ {
+ return delegate().produce(ctx);
+ }
+
+}
Property changes on: extensions/trunk/impl/src/main/java/org/jboss/weld/extensions/bean/ForwardingInjectionTarget.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
13 years, 10 months