Skip to content

Commit

Permalink
dont inherit producers
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-qnz committed Jan 16, 2024
1 parent da7f2b2 commit 79cbf73
Show file tree
Hide file tree
Showing 15 changed files with 586 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected static void validateParameterCount(AnnotatedCallable<?> callable) {
}
if (callable.getParameters().size() != parameterTypes.length) {
// For enums, BackedAnnotatedConstructor sets parameters to an empty list, so we shouldn't throw the DefinitionException
Class<?> declaringClass = callable.getDeclaringType().getJavaClass();
Class<?> declaringClass = callable.getJavaMember().getDeclaringClass();
if (!declaringClass.isEnum() && !declaringClass.isMemberClass()) {
throw ReflectionLogger.LOG.incorrectNumberOfAnnotatedParametersMethod(callable.getParameters().size(), callable,
callable.getParameters(), Arrays.asList(parameterTypes));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,12 @@ protected EnhancedAnnotatedTypeImpl(SlimAnnotatedType<T> annotatedType,
EnhancedAnnotatedField<?, ? super T> weldField = EnhancedAnnotatedFieldImpl.of(annotatedField, this,
classTransformer);
fieldsTemp.add(weldField);
if (annotatedField.getDeclaringType().getJavaClass().equals(javaClass)) {
if (annotatedField.getJavaMember().getDeclaringClass().equals(javaClass)) {
declaredFieldsTemp.add(weldField);
}
for (Annotation annotation : weldField.getAnnotations()) {
annotatedFields.put(annotation.annotationType(), weldField);
if (annotatedField.getDeclaringType().getJavaClass().equals(javaClass)) {
if (annotatedField.getJavaMember().getDeclaringClass().equals(javaClass)) {
declaredAnnotatedFields.put(annotation.annotationType(), weldField);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Objects;
Expand Down Expand Up @@ -64,13 +65,13 @@ private BackedAnnotatedType(Class<X> rawType, Type baseType, SharedObjectCache s
ReflectionCache reflectionCache, String contextId,
String bdaId, String suffix) {
super(baseType, sharedObjectCache);
this.identifier = AnnotatedTypeIdentifier.forBackedAnnotatedType(contextId, rawType, baseType, bdaId, suffix);
this.javaClass = rawType;
this.sharedObjectCache = sharedObjectCache;
this.reflectionCache = reflectionCache;
this.constructors = new BackedAnnotatedConstructors();
this.fields = new BackedAnnotatedFields();
this.methods = new BackedAnnotatedMethods();
this.identifier = AnnotatedTypeIdentifier.forBackedAnnotatedType(contextId, rawType, baseType, bdaId, suffix);
}

@Override
Expand Down Expand Up @@ -186,7 +187,7 @@ protected Set<AnnotatedField<? super X>> computeValue() {
Class<? super X> clazz = javaClass;
while (clazz != Object.class && clazz != null) {
for (Field field : SecurityActions.getDeclaredFields(clazz)) {
fields.add(BackedAnnotatedField.of(field, BackedAnnotatedType.this, sharedObjectCache));
fields.add(BackedAnnotatedField.of(field, getDeclaringAnnotatedType(field), sharedObjectCache));
}
clazz = clazz.getSuperclass();
}
Expand All @@ -201,22 +202,32 @@ protected Set<AnnotatedMethod<? super X>> computeValue() {
Class<? super X> clazz = javaClass;
while (clazz != Object.class && clazz != null) {
for (Method method : SecurityActions.getDeclaredMethods(clazz)) {
methods.add(BackedAnnotatedMethod.of(method, BackedAnnotatedType.this, sharedObjectCache));
methods.add(BackedAnnotatedMethod.of(method, getDeclaringAnnotatedType(method), sharedObjectCache));
}
clazz = clazz.getSuperclass();
}
// Also add default methods
for (Class<?> interfaceClazz : Reflections.getInterfaceClosure(javaClass)) {
for (Method method : SecurityActions.getDeclaredMethods(interfaceClazz)) {
if (Reflections.isDefault(method)) {
methods.add(BackedAnnotatedMethod.of(method, BackedAnnotatedType.this, sharedObjectCache));
methods.add(BackedAnnotatedMethod.of(method, getDeclaringAnnotatedType(method), sharedObjectCache));
}
}
}
return methods.build();
}
}

private <T> BackedAnnotatedType<T> getDeclaringAnnotatedType(Member member) {
Class<T> declaringClass = Reflections.<Class<T>> cast(member.getDeclaringClass());
if (declaringClass.equals(getJavaClass())) {
return cast(this);
} else {
return BackedAnnotatedType.of(declaringClass, sharedObjectCache, reflectionCache, identifier.getSuffix(),
identifier.getBdaId());
}
}

public ReflectionCache getReflectionCache() {
return reflectionCache;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ protected boolean initScopeFromStereotype() {
Class<?> declaringClass;
if (annotated instanceof EnhancedAnnotatedMember) {
EnhancedAnnotatedMember<?, ?, ?> member = (EnhancedAnnotatedMember<?, ?, ?>) annotated;
declaringClass = member.getDeclaringType().getJavaClass();
declaringClass = member.getJavaMember().getDeclaringClass();
stack = "\n at " + Formats.formatAsStackTraceElement(member.getJavaMember());
} else {
declaringClass = annotated.getJavaClass();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public ObserverMethodConfigurator<T> read(AnnotatedMethod<?> method) {
if (priority != null) {
priority(priority.value());
}
beanClass(eventParameter.getDeclaringCallable().getDeclaringType().getJavaClass());
beanClass(eventParameter.getDeclaringCallable().getJavaMember().getDeclaringClass());
observedType(eventParameter.getBaseType());
qualifiers(Configurators.getQualifiers(eventParameter));
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public ProducerMethodProducer(EnhancedAnnotatedMethod<T, ? super X> enhancedAnno
super(enhancedAnnotatedMethod, disposalMethod);
// Note that for producer method injection points the declaring bean is the producer method itself
this.method = InjectionPointFactory.instance().createMethodInjectionPoint(MethodInjectionPointType.PRODUCER,
enhancedAnnotatedMethod, getBean(), enhancedAnnotatedMethod.getDeclaringType().getJavaClass(), null,
enhancedAnnotatedMethod, getBean(), enhancedAnnotatedMethod.getJavaMember().getDeclaringClass(), null,
getBeanManager());
checkProducerMethod(enhancedAnnotatedMethod);
checkDelegateInjectionPoints();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1445,7 +1445,7 @@ public <T, X> Bean<T> createBean(BeanAttributes<T> attributes, Class<X> beanClas

private <X> FieldInjectionPointAttributes<?, X> createFieldInjectionPoint(AnnotatedField<X> field) {
EnhancedAnnotatedField<?, X> enhancedField = services.get(MemberTransformer.class).loadEnhancedMember(field, getId());
return InferringFieldInjectionPointAttributes.of(enhancedField, null, field.getDeclaringType().getJavaClass(), this);
return InferringFieldInjectionPointAttributes.of(enhancedField, null, field.getJavaMember().getDeclaringClass(), this);
}

@Override
Expand All @@ -1454,7 +1454,7 @@ private <X> FieldInjectionPointAttributes<?, X> createFieldInjectionPoint(Annota
EnhancedAnnotatedParameter<?, ?> enhancedParameter = services.get(MemberTransformer.class)
.loadEnhancedParameter(parameter, getId());
return validateInjectionPoint(InferringParameterInjectionPointAttributes.of(enhancedParameter, null,
parameter.getDeclaringCallable().getDeclaringType().getJavaClass(), this));
parameter.getDeclaringCallable().getJavaMember().getDeclaringClass(), this));
}

private <T extends InjectionPoint> T validateInjectionPoint(T injectionPoint) {
Expand Down Expand Up @@ -1543,13 +1543,13 @@ public <T> WeldInjectionTargetFactory<T> getInjectionTargetFactory(AnnotatedType

@Override
public <X> FieldProducerFactory<X> getProducerFactory(AnnotatedField<? super X> field, Bean<X> declaringBean) {
BeanManagerImpl manager = BeanManagerLookupService.lookupBeanManager(field.getDeclaringType().getJavaClass(), this);
BeanManagerImpl manager = BeanManagerLookupService.lookupBeanManager(field.getJavaMember().getDeclaringClass(), this);
return new FieldProducerFactory<X>(field, declaringBean, manager);
}

@Override
public <X> MethodProducerFactory<X> getProducerFactory(AnnotatedMethod<? super X> method, Bean<X> declaringBean) {
BeanManagerImpl manager = BeanManagerLookupService.lookupBeanManager(method.getDeclaringType().getJavaClass(), this);
BeanManagerImpl manager = BeanManagerLookupService.lookupBeanManager(method.getJavaMember().getDeclaringClass(), this);
return new MethodProducerFactory<X>(method, declaringBean, manager);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -616,23 +616,23 @@ public static String formatAnnotatedConstructor(AnnotatedConstructor<?> construc
return Formats.formatSimpleClassName(constructor) + " "
+ Formats.addSpaceIfNeeded(Formats.formatAnnotations(constructor.getAnnotations()))
+ Formats.addSpaceIfNeeded(Formats.formatModifiers(constructor.getJavaMember().getModifiers()))
+ constructor.getDeclaringType().getJavaClass().getName()
+ constructor.getJavaMember().getDeclaringClass().getName()
+ Formats.formatAsFormalParameterList(constructor.getParameters());
}

public static String formatAnnotatedField(AnnotatedField<?> field) {
return Formats.formatSimpleClassName(field) + " "
+ Formats.addSpaceIfNeeded(Formats.formatAnnotations(field.getAnnotations()))
+ Formats.addSpaceIfNeeded(Formats.formatModifiers(field.getJavaMember().getModifiers()))
+ field.getDeclaringType().getJavaClass().getName() + "."
+ field.getJavaMember().getDeclaringClass().getName() + "."
+ field.getJavaMember().getName();
}

public static String formatAnnotatedMethod(AnnotatedMethod<?> method) {
return Formats.formatSimpleClassName(method) + " "
+ Formats.addSpaceIfNeeded(Formats.formatAnnotations(method.getAnnotations()))
+ Formats.addSpaceIfNeeded(Formats.formatModifiers(method.getJavaMember().getModifiers()))
+ method.getDeclaringType().getJavaClass().getName() + "."
+ method.getJavaMember().getDeclaringClass().getName() + "."
+ method.getJavaMember().getName() + Formats.formatAsFormalParameterList(method.getParameters());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@
*/
package org.jboss.weld.tests.annotatedType;

import jakarta.enterprise.context.Dependent;

@Dependent
public class Child extends Parent {
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,20 @@ public static Archive<?> deploy() {
@Test
public void testInheritance() {
AnnotatedType<Child> type = beanManager.createAnnotatedType(Child.class);

Assert.assertEquals(1, type.getConstructors().size());

Assert.assertEquals(1, type.getFields().size());
for (AnnotatedField<? super Child> field : type.getFields()) {
if (field.getJavaMember().getName().equals("parent")) {
Assert.assertEquals(Parent.class, field.getJavaMember().getDeclaringClass()); // OK - Returns Parent
// this assertion is commented out because the spec is not clear which type to return and the flat type actually makes more sense
// Assert.assertEquals(Parent.class, field.getDeclaringType().getJavaClass()); // FAIL - Returns Child
} else {
Assert.fail("Unknown field " + field.getJavaMember());
}
}
AnnotatedField<? super Child> field = type.getFields().stream().findAny().get();
Assert.assertEquals("parent", field.getJavaMember().getName());
Assert.assertEquals(Parent.class, field.getJavaMember().getDeclaringClass());
Assert.assertEquals(Parent.class, field.getDeclaringType().getJavaClass());

Assert.assertEquals(1, type.getMethods().size());
for (AnnotatedMethod<? super Child> method : type.getMethods()) {
if (method.getJavaMember().getName().equals("parentMethod")) {
Assert.assertEquals(Parent.class, method.getJavaMember().getDeclaringClass()); // OK - Returns Parent
// this assertion is commented out because the spec is not clear which type to return and the flat type actually makes more sense
// Assert.assertEquals(Parent.class, method.getDeclaringType().getJavaClass()); // FAIL - Returns Child
} else {
Assert.fail("Unknown method " + method.getJavaMember());
}
}
AnnotatedMethod<? super Child> method = type.getMethods().stream().findAny().get();
Assert.assertEquals("parentMethod", method.getJavaMember().getName());
Assert.assertEquals(Parent.class, method.getJavaMember().getDeclaringClass());
Assert.assertEquals(Parent.class, method.getDeclaringType().getJavaClass());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
package org.jboss.weld.tests.annotatedType;

import jakarta.enterprise.context.Dependent;

@Dependent
public class Parent {
int parent;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ public void testDisposedParameter() {
assertTrue(parameter.isAnnotationPresent(Zero.class));
assertEquals(int.class, parameter.getBaseType());
assertEquals(0, parameter.getPosition());
assertEquals(Producer.class, parameter.getDeclaringCallable().getDeclaringType().getJavaClass());
assertEquals(Producer.class, parameter.getDeclaringCallable().getJavaMember().getDeclaringClass());
}
}
Loading

0 comments on commit 79cbf73

Please sign in to comment.