Search in sources :

Example 1 with AnnotatedField

use of jakarta.enterprise.inject.spi.AnnotatedField in project cdi-tck by eclipse-ee4j.

the class DynamicInjectionPointTest method testInjectionPointGetAnnotated.

@SuppressWarnings("rawtypes")
@Test
@SpecAssertion(section = INJECTION_POINT, id = "dab")
public void testInjectionPointGetAnnotated() {
    Annotated fooFieldAnnotated = bar.getFoo().getInjectionPoint().getAnnotated();
    assertTrue(fooFieldAnnotated instanceof AnnotatedField);
    assertEquals(((AnnotatedField) fooFieldAnnotated).getJavaMember().getName(), "fooInstance");
    assertTrue(fooFieldAnnotated.isAnnotationPresent(Any.class));
    Annotated fooInitializerAnnnotated = bar.getInitializerFoo().getInjectionPoint().getAnnotated();
    assertTrue(fooInitializerAnnnotated instanceof AnnotatedParameter);
    assertEquals(((AnnotatedParameter) fooInitializerAnnnotated).getPosition(), 0);
    Annotated fooConstructorAnnnotated = bar.getConstructorInjectionFoo().getInjectionPoint().getAnnotated();
    assertTrue(fooConstructorAnnnotated instanceof AnnotatedParameter);
    assertEquals(((AnnotatedParameter) fooConstructorAnnnotated).getPosition(), 0);
}
Also used : Annotated(jakarta.enterprise.inject.spi.Annotated) AnnotatedParameter(jakarta.enterprise.inject.spi.AnnotatedParameter) AnnotatedField(jakarta.enterprise.inject.spi.AnnotatedField) Any(jakarta.enterprise.inject.Any) Test(org.testng.annotations.Test) AbstractTest(org.jboss.cdi.tck.AbstractTest) SpecAssertion(org.jboss.test.audit.annotations.SpecAssertion)

Example 2 with AnnotatedField

use of jakarta.enterprise.inject.spi.AnnotatedField in project cdi-tck by eclipse-ee4j.

the class EventMetadataInjectionPointTest method testGetAnnotatedType.

@SuppressWarnings("rawtypes")
@Test
@SpecAssertions({ @SpecAssertion(section = INJECTION_POINT, id = "daa"), @SpecAssertion(section = EVENT_METADATA, id = "b") })
public void testGetAnnotatedType() {
    Annotated lastAnnotated = null;
    notifier.fireInfoEvent();
    lastAnnotated = infoObserver.getLastAnnotated();
    assertTrue(lastAnnotated instanceof AnnotatedField);
    assertEquals(((AnnotatedField) lastAnnotated).getJavaMember().getName(), "infoEvent");
    assertTrue(lastAnnotated.isAnnotationPresent(Inject.class));
    notifier.fireInitializerInfoEvent();
    lastAnnotated = infoObserver.getLastAnnotated();
    assertTrue(lastAnnotated instanceof AnnotatedParameter);
    assertEquals(((AnnotatedParameter) lastAnnotated).getPosition(), 0);
    notifier.fireConstructorInfoEvent();
    lastAnnotated = infoObserver.getLastAnnotated();
    assertTrue(lastAnnotated instanceof AnnotatedParameter);
    assertEquals(((AnnotatedParameter) lastAnnotated).getPosition(), 0);
    assertTrue(lastAnnotated.isAnnotationPresent(Nice.class));
}
Also used : Inject(jakarta.inject.Inject) Annotated(jakarta.enterprise.inject.spi.Annotated) AnnotatedParameter(jakarta.enterprise.inject.spi.AnnotatedParameter) AnnotatedField(jakarta.enterprise.inject.spi.AnnotatedField) SpecAssertions(org.jboss.test.audit.annotations.SpecAssertions) Test(org.testng.annotations.Test) AbstractTest(org.jboss.cdi.tck.AbstractTest)

Example 3 with AnnotatedField

use of jakarta.enterprise.inject.spi.AnnotatedField in project core by weld.

the class Validator method validateInjectionPointForDefinitionErrors.

/**
 * Checks for definition errors associated with a given {@link InjectionPoint}
 */
public void validateInjectionPointForDefinitionErrors(InjectionPoint ij, Bean<?> bean, BeanManagerImpl beanManager) {
    if (ij.getType() instanceof TypeVariable<?>) {
        throw ValidatorLogger.LOG.injectionPointWithTypeVariable(ij, Formats.formatAsStackTraceElement(ij));
    }
    // WELD-1739
    if (ij.getMember() instanceof Executable && ij.getAnnotated().isAnnotationPresent(Named.class) && ij.getAnnotated().getAnnotation(Named.class).value().equals("")) {
        Executable executable = (Executable) ij.getMember();
        AnnotatedParameter<?> annotatedParameter = (AnnotatedParameter<?>) ij.getAnnotated();
        if (!executable.getParameters()[annotatedParameter.getPosition()].isNamePresent()) {
            // No parameters info available
            throw ValidatorLogger.LOG.nonFieldInjectionPointCannotUseNamed(ij, Formats.formatAsStackTraceElement(ij));
        }
    }
    if (ij.getAnnotated().isAnnotationPresent(Produces.class)) {
        if (bean != null) {
            throw BeanLogger.LOG.injectedFieldCannotBeProducer(ij.getAnnotated(), bean);
        } else {
            throw BeanLogger.LOG.injectedFieldCannotBeProducer(ij.getAnnotated(), Reflections.<AnnotatedField<?>>cast(ij.getAnnotated()).getDeclaringType());
        }
    }
    checkScopeAnnotations(ij, beanManager.getServices().get(MetaAnnotationStore.class));
    checkFacadeInjectionPoint(ij, Instance.class);
    checkFacadeInjectionPoint(ij, Event.class);
    if (InterceptionFactory.class.equals(Reflections.getRawType(ij.getType())) && !(bean instanceof ProducerMethod<?, ?>)) {
        throw ValidatorLogger.LOG.invalidInterceptionFactoryInjectionPoint(ij, Formats.formatAsStackTraceElement(ij));
    }
    for (PlugableValidator validator : plugableValidators) {
        validator.validateInjectionPointForDefinitionErrors(ij, bean, beanManager);
    }
}
Also used : PlugableValidator(org.jboss.weld.module.PlugableValidator) Named(jakarta.inject.Named) TypeVariable(java.lang.reflect.TypeVariable) AnnotatedParameter(jakarta.enterprise.inject.spi.AnnotatedParameter) MetaAnnotationStore(org.jboss.weld.metadata.cache.MetaAnnotationStore) InterceptionFactory(jakarta.enterprise.inject.spi.InterceptionFactory) Executable(java.lang.reflect.Executable) AnnotatedField(jakarta.enterprise.inject.spi.AnnotatedField)

Example 4 with AnnotatedField

use of jakarta.enterprise.inject.spi.AnnotatedField in project core by weld.

the class Formats method formatAsStackTraceElement.

/**
 * See also WELD-1454.
 *
 * @param ij
 * @return the formatted string
 */
public static String formatAsStackTraceElement(InjectionPoint ij) {
    Member member;
    if (ij.getAnnotated() instanceof AnnotatedField) {
        AnnotatedField<?> annotatedField = (AnnotatedField<?>) ij.getAnnotated();
        member = annotatedField.getJavaMember();
    } else if (ij.getAnnotated() instanceof AnnotatedParameter<?>) {
        AnnotatedParameter<?> annotatedParameter = (AnnotatedParameter<?>) ij.getAnnotated();
        member = annotatedParameter.getDeclaringCallable().getJavaMember();
    } else {
        // Throwing an exception here would hide the original exception.
        return "-";
    }
    return formatAsStackTraceElement(member);
}
Also used : AnnotatedParameter(jakarta.enterprise.inject.spi.AnnotatedParameter) AnnotatedField(jakarta.enterprise.inject.spi.AnnotatedField) Member(java.lang.reflect.Member)

Example 5 with AnnotatedField

use of jakarta.enterprise.inject.spi.AnnotatedField in project core by weld.

the class JsonObjectsTest method getFooAnnotatedMethod.

private static AnnotatedMethod<Foo> getFooAnnotatedMethod(String name) {
    final Method method = findDeclaredFooMethod(name);
    return new AnnotatedMethod<Foo>() {

        @Override
        public List<AnnotatedParameter<Foo>> getParameters() {
            Type[] paramTypes = method.getGenericParameterTypes();
            if (paramTypes.length == 0) {
                return Collections.emptyList();
            }
            List<AnnotatedParameter<Foo>> params = new ArrayList<>();
            for (int i = 0; i < paramTypes.length; i++) {
                final Type baseType = paramTypes[i];
                final Annotation[] annotations = method.getParameterAnnotations()[i];
                params.add(new AnnotatedParameter<Foo>() {

                    @Override
                    public Type getBaseType() {
                        return baseType;
                    }

                    @Override
                    public Set<Type> getTypeClosure() {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public Set<Annotation> getAnnotations() {
                        return new HashSet<>(Arrays.asList(annotations));
                    }

                    @Override
                    public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public int getPosition() {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public AnnotatedCallable<Foo> getDeclaringCallable() {
                        throw new UnsupportedOperationException();
                    }
                });
            }
            return params;
        }

        @Override
        public boolean isStatic() {
            throw new UnsupportedOperationException();
        }

        @Override
        public AnnotatedType<Foo> getDeclaringType() {
            return new AnnotatedType<Foo>() {

                @Override
                public Type getBaseType() {
                    throw new UnsupportedOperationException();
                }

                @Override
                public Set<Type> getTypeClosure() {
                    throw new UnsupportedOperationException();
                }

                @Override
                public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
                    throw new UnsupportedOperationException();
                }

                @Override
                public Set<Annotation> getAnnotations() {
                    throw new UnsupportedOperationException();
                }

                @Override
                public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
                    throw new UnsupportedOperationException();
                }

                @Override
                public Class<Foo> getJavaClass() {
                    return Foo.class;
                }

                @Override
                public Set<AnnotatedConstructor<Foo>> getConstructors() {
                    throw new UnsupportedOperationException();
                }

                @Override
                public Set<AnnotatedMethod<? super Foo>> getMethods() {
                    throw new UnsupportedOperationException();
                }

                @Override
                public Set<AnnotatedField<? super Foo>> getFields() {
                    throw new UnsupportedOperationException();
                }
            };
        }

        @Override
        public Type getBaseType() {
            throw new UnsupportedOperationException();
        }

        @Override
        public Set<Type> getTypeClosure() {
            throw new UnsupportedOperationException();
        }

        @Override
        public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Set<Annotation> getAnnotations() {
            return new HashSet<>(Arrays.asList(method.getAnnotations()));
        }

        @Override
        public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Method getJavaMember() {
            return method;
        }
    };
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) AnnotatedMethod(jakarta.enterprise.inject.spi.AnnotatedMethod) AnnotatedParameter(jakarta.enterprise.inject.spi.AnnotatedParameter) ArrayList(java.util.ArrayList) AnnotatedCallable(jakarta.enterprise.inject.spi.AnnotatedCallable) AnnotatedConstructor(jakarta.enterprise.inject.spi.AnnotatedConstructor) HashSet(java.util.HashSet) AnnotatedMethod(jakarta.enterprise.inject.spi.AnnotatedMethod) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) ElementType(java.lang.annotation.ElementType) Type(java.lang.reflect.Type) AnnotatedType(jakarta.enterprise.inject.spi.AnnotatedType) AnnotatedType(jakarta.enterprise.inject.spi.AnnotatedType) AnnotatedField(jakarta.enterprise.inject.spi.AnnotatedField)

Aggregations

AnnotatedField (jakarta.enterprise.inject.spi.AnnotatedField)8 AnnotatedParameter (jakarta.enterprise.inject.spi.AnnotatedParameter)7 Annotated (jakarta.enterprise.inject.spi.Annotated)5 Member (java.lang.reflect.Member)3 Method (java.lang.reflect.Method)3 AbstractTest (org.jboss.cdi.tck.AbstractTest)3 Test (org.testng.annotations.Test)3 AnnotatedMethod (jakarta.enterprise.inject.spi.AnnotatedMethod)2 Constructor (java.lang.reflect.Constructor)2 SpecAssertions (org.jboss.test.audit.annotations.SpecAssertions)2 Any (jakarta.enterprise.inject.Any)1 AnnotatedCallable (jakarta.enterprise.inject.spi.AnnotatedCallable)1 AnnotatedConstructor (jakarta.enterprise.inject.spi.AnnotatedConstructor)1 AnnotatedType (jakarta.enterprise.inject.spi.AnnotatedType)1 InterceptionFactory (jakarta.enterprise.inject.spi.InterceptionFactory)1 ProcessObserverMethod (jakarta.enterprise.inject.spi.ProcessObserverMethod)1 Inject (jakarta.inject.Inject)1 Named (jakarta.inject.Named)1 Annotation (java.lang.annotation.Annotation)1 ElementType (java.lang.annotation.ElementType)1