Search in sources :

Example 6 with AnnotatedField

use of javax.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(javax.enterprise.inject.spi.AnnotatedParameter) AnnotatedField(javax.enterprise.inject.spi.AnnotatedField) Member(java.lang.reflect.Member)

Example 7 with AnnotatedField

use of javax.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(javax.enterprise.inject.spi.AnnotatedMethod) AnnotatedParameter(javax.enterprise.inject.spi.AnnotatedParameter) ArrayList(java.util.ArrayList) AnnotatedCallable(javax.enterprise.inject.spi.AnnotatedCallable) AnnotatedConstructor(javax.enterprise.inject.spi.AnnotatedConstructor) HashSet(java.util.HashSet) Method(java.lang.reflect.Method) AnnotatedMethod(javax.enterprise.inject.spi.AnnotatedMethod) Annotation(java.lang.annotation.Annotation) AnnotatedType(javax.enterprise.inject.spi.AnnotatedType) Type(java.lang.reflect.Type) AnnotatedType(javax.enterprise.inject.spi.AnnotatedType) AnnotatedField(javax.enterprise.inject.spi.AnnotatedField)

Example 8 with AnnotatedField

use of javax.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.getAnnotated().getAnnotation(New.class) != null && ij.getQualifiers().size() > 1) {
        throw ValidatorLogger.LOG.newWithQualifiers(ij, Formats.formatAsStackTraceElement(ij));
    }
    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());
        }
    }
    boolean newBean = (bean instanceof NewBean);
    if (!newBean) {
        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(javax.inject.Named) TypeVariable(java.lang.reflect.TypeVariable) AnnotatedParameter(javax.enterprise.inject.spi.AnnotatedParameter) MetaAnnotationStore(org.jboss.weld.metadata.cache.MetaAnnotationStore) NewBean(org.jboss.weld.bean.NewBean) InterceptionFactory(javax.enterprise.inject.spi.InterceptionFactory) Executable(java.lang.reflect.Executable) AnnotatedField(javax.enterprise.inject.spi.AnnotatedField)

Example 9 with AnnotatedField

use of javax.enterprise.inject.spi.AnnotatedField in project deltaspike by apache.

the class BeanBuilderTest method assertNonNullInjectionPointsWhenOverriding.

@Test
public void assertNonNullInjectionPointsWhenOverriding() {
    final BeanBuilder beanBuilder = new BeanBuilder(beanManager);
    final AnnotatedType<?> at = beanManager.createAnnotatedType(WithInjectionPoint.class);
    beanBuilder.readFromType(at);
    // It's not easy to actually create these in the state we need, so we have to get the InjectionPonits,
    // create new ones with the correct info, null out the bean, set them as the new injection points
    // then move on.
    final Set<InjectionPoint> origInjectionPoints = beanBuilder.getInjectionPoints();
    beanBuilder.injectionPoints(beanBuilder.getInjectionPoints());
    final Set<InjectionPoint> newInjectionPoints = new HashSet<InjectionPoint>();
    for (InjectionPoint ip : origInjectionPoints) {
        newInjectionPoints.add(new ImmutableInjectionPoint((AnnotatedField) ip.getAnnotated(), ip.getQualifiers(), null, ip.isTransient(), ip.isDelegate()));
    }
    beanBuilder.injectionPoints(newInjectionPoints);
    final Bean<?> newInjectionBean = beanBuilder.create();
    for (final InjectionPoint ip : newInjectionBean.getInjectionPoints()) {
        Assert.assertNotNull(ip);
    }
}
Also used : BeanBuilder(org.apache.deltaspike.core.util.bean.BeanBuilder) ImmutableInjectionPoint(org.apache.deltaspike.core.util.metadata.builder.ImmutableInjectionPoint) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) AnnotatedField(javax.enterprise.inject.spi.AnnotatedField) ImmutableInjectionPoint(org.apache.deltaspike.core.util.metadata.builder.ImmutableInjectionPoint) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

AnnotatedField (javax.enterprise.inject.spi.AnnotatedField)9 AnnotatedParameter (javax.enterprise.inject.spi.AnnotatedParameter)4 HashSet (java.util.HashSet)3 Annotation (java.lang.annotation.Annotation)2 Field (java.lang.reflect.Field)2 Member (java.lang.reflect.Member)2 Method (java.lang.reflect.Method)2 Type (java.lang.reflect.Type)2 ArrayList (java.util.ArrayList)2 Annotated (javax.enterprise.inject.spi.Annotated)2 AnnotatedMethod (javax.enterprise.inject.spi.AnnotatedMethod)2 AnnotatedType (javax.enterprise.inject.spi.AnnotatedType)2 InjectionPoint (javax.enterprise.inject.spi.InjectionPoint)2 Named (javax.inject.Named)2 ConfigPropertyModel (fish.payara.microprofile.config.cdi.model.ConfigPropertyModel)1 Executable (java.lang.reflect.Executable)1 TypeVariable (java.lang.reflect.TypeVariable)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1 WeakHashMap (java.util.WeakHashMap)1