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);
}
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));
}
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);
}
}
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);
}
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;
}
};
}
Aggregations