Search in sources :

Example 1 with Member

use of java.lang.reflect.Member in project jersey by jersey.

the class CdiComponentProvider method processInjectionTarget.

@SuppressWarnings("unused")
private void processInjectionTarget(@Observes final ProcessInjectionTarget event) {
    final InjectionTarget it = event.getInjectionTarget();
    final Class<?> componentClass = event.getAnnotatedType().getJavaClass();
    final Set<InjectionPoint> cdiInjectionPoints = filterHk2InjectionPointsOut(it.getInjectionPoints());
    for (final InjectionPoint injectionPoint : cdiInjectionPoints) {
        final Member member = injectionPoint.getMember();
        if (member instanceof Field) {
            addInjecteeToSkip(componentClass, fieldsToSkip, (Field) member);
        } else if (member instanceof Method) {
            addInjecteeToSkip(componentClass, methodsToSkip, (Method) member);
        }
    }
    InjectionManagerInjectedCdiTarget target = null;
    if (isJerseyOrDependencyType(componentClass)) {
        target = new InjectionManagerInjectedCdiTarget(it) {

            @Override
            public Set<InjectionPoint> getInjectionPoints() {
                // CDI will not treat these classes as CDI beans (as they are not).
                return Collections.emptySet();
            }
        };
    } else if (isJaxRsComponentType(componentClass) || jaxrsInjectableTypes.contains(event.getAnnotatedType().getBaseType())) {
        target = new InjectionManagerInjectedCdiTarget(it) {

            @Override
            public Set<InjectionPoint> getInjectionPoints() {
                // Inject CDI beans into JAX-RS resources/providers/application.
                return cdiInjectionPoints;
            }
        };
    }
    if (target != null) {
        notify(target);
        //noinspection unchecked
        event.setInjectionTarget(target);
    }
}
Also used : Field(java.lang.reflect.Field) Set(java.util.Set) HashSet(java.util.HashSet) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) InjectionTarget(javax.enterprise.inject.spi.InjectionTarget) ProcessInjectionTarget(javax.enterprise.inject.spi.ProcessInjectionTarget) Method(java.lang.reflect.Method) Member(java.lang.reflect.Member)

Example 2 with Member

use of java.lang.reflect.Member in project qi4j-sdk by Qi4j.

the class NamedAssociationModel method bind.

@Override
public void bind(Resolution resolution) throws BindingException {
    builderInfo = new AssociationInfo() {

        @Override
        public boolean isImmutable() {
            return false;
        }

        @Override
        public QualifiedName qualifiedName() {
            return qualifiedName;
        }

        @Override
        public Type type() {
            return type;
        }

        @Override
        public void checkConstraints(Object value) throws ConstraintViolationException {
            NamedAssociationModel.this.checkConstraints(value);
        }
    };
    if (type instanceof TypeVariable) {
        Class mainType = first(resolution.model().types());
        type = Classes.resolveTypeVariable((TypeVariable) type, ((Member) accessor).getDeclaringClass(), mainType);
    }
}
Also used : Type(java.lang.reflect.Type) GenericAssociationInfo(org.qi4j.api.association.GenericAssociationInfo) TypeVariable(java.lang.reflect.TypeVariable) QualifiedName(org.qi4j.api.common.QualifiedName) ConstraintViolationException(org.qi4j.api.constraint.ConstraintViolationException) AccessibleObject(java.lang.reflect.AccessibleObject) Member(java.lang.reflect.Member)

Example 3 with Member

use of java.lang.reflect.Member in project Entitas-Java by Rubentxu.

the class TypeResolver method populateLambdaArgs.

/**
   * Populates the {@code map} with variable/argument pairs for the {@code functionalInterface}.
   */
private static void populateLambdaArgs(Class<?> functionalInterface, final Class<?> lambdaType, Map<TypeVariable<?>, Type> map) {
    if (RESOLVES_LAMBDAS) {
        // Find SAM
        for (Method m : functionalInterface.getMethods()) {
            if (!isDefaultMethod(m) && !Modifier.isStatic(m.getModifiers()) && !m.isBridge()) {
                // Skip methods that override Object.class
                Method objectMethod = OBJECT_METHODS.get(m.getName());
                if (objectMethod != null && Arrays.equals(m.getTypeParameters(), objectMethod.getTypeParameters()))
                    continue;
                // Get functional interface's type params
                Type returnTypeVar = m.getGenericReturnType();
                Type[] paramTypeVars = m.getGenericParameterTypes();
                Member member = getMemberRef(lambdaType);
                if (member == null)
                    return;
                // Populate return type argument
                if (returnTypeVar instanceof TypeVariable) {
                    Class<?> returnType = member instanceof Method ? ((Method) member).getReturnType() : ((Constructor<?>) member).getDeclaringClass();
                    returnType = wrapPrimitives(returnType);
                    if (!returnType.equals(Void.class))
                        map.put((TypeVariable<?>) returnTypeVar, returnType);
                }
                Class<?>[] arguments = member instanceof Method ? ((Method) member).getParameterTypes() : ((Constructor<?>) member).getParameterTypes();
                // Populate object type from arbitrary object method reference
                int paramOffset = 0;
                if (paramTypeVars.length > 0 && paramTypeVars[0] instanceof TypeVariable && paramTypeVars.length == arguments.length + 1) {
                    Class<?> instanceType = member.getDeclaringClass();
                    map.put((TypeVariable<?>) paramTypeVars[0], instanceType);
                    paramOffset = 1;
                }
                // Handle additional arguments that are captured from the lambda's enclosing scope
                int argOffset = 0;
                if (paramTypeVars.length < arguments.length) {
                    argOffset = arguments.length - paramTypeVars.length;
                }
                // Populate type arguments
                for (int i = 0; i + argOffset < arguments.length; i++) {
                    if (paramTypeVars[i] instanceof TypeVariable)
                        map.put((TypeVariable<?>) paramTypeVars[i + paramOffset], wrapPrimitives(arguments[i + argOffset]));
                }
                return;
            }
        }
    }
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) Method(java.lang.reflect.Method) Member(java.lang.reflect.Member)

Example 4 with Member

use of java.lang.reflect.Member in project spring-framework by spring-projects.

the class ObjectToObjectConverter method convert.

@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
    if (source == null) {
        return null;
    }
    Class<?> sourceClass = sourceType.getType();
    Class<?> targetClass = targetType.getType();
    Member member = getValidatedMember(targetClass, sourceClass);
    try {
        if (member instanceof Method) {
            Method method = (Method) member;
            ReflectionUtils.makeAccessible(method);
            if (!Modifier.isStatic(method.getModifiers())) {
                return method.invoke(source);
            } else {
                return method.invoke(null, source);
            }
        } else if (member instanceof Constructor) {
            Constructor<?> ctor = (Constructor<?>) member;
            ReflectionUtils.makeAccessible(ctor);
            return ctor.newInstance(source);
        }
    } catch (InvocationTargetException ex) {
        throw new ConversionFailedException(sourceType, targetType, source, ex.getTargetException());
    } catch (Throwable ex) {
        throw new ConversionFailedException(sourceType, targetType, source, ex);
    }
    // method or Integer(java.lang.Number) constructor exists on java.lang.Integer.
    throw new IllegalStateException(String.format("No to%3$s() method exists on %1$s, " + "and no static valueOf/of/from(%1$s) method or %3$s(%1$s) constructor exists on %2$s.", sourceClass.getName(), targetClass.getName(), targetClass.getSimpleName()));
}
Also used : ConversionFailedException(org.springframework.core.convert.ConversionFailedException) Constructor(java.lang.reflect.Constructor) Method(java.lang.reflect.Method) Member(java.lang.reflect.Member) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 5 with Member

use of java.lang.reflect.Member in project spring-framework by spring-projects.

the class LocalVariableTableParameterNameDiscoverer method getParameterNames.

@Override
public String[] getParameterNames(Method method) {
    Method originalMethod = BridgeMethodResolver.findBridgedMethod(method);
    Class<?> declaringClass = originalMethod.getDeclaringClass();
    Map<Member, String[]> map = this.parameterNamesCache.get(declaringClass);
    if (map == null) {
        map = inspectClass(declaringClass);
        this.parameterNamesCache.put(declaringClass, map);
    }
    if (map != NO_DEBUG_INFO_MAP) {
        return map.get(originalMethod);
    }
    return null;
}
Also used : Method(java.lang.reflect.Method) Member(java.lang.reflect.Member)

Aggregations

Member (java.lang.reflect.Member)141 Method (java.lang.reflect.Method)42 Field (java.lang.reflect.Field)32 Type (java.lang.reflect.Type)13 ArrayList (java.util.ArrayList)13 Annotation (java.lang.annotation.Annotation)12 AccessibleObject (java.lang.reflect.AccessibleObject)12 Constructor (java.lang.reflect.Constructor)10 TypeVariable (java.lang.reflect.TypeVariable)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 ParameterizedType (java.lang.reflect.ParameterizedType)7 Map (java.util.Map)7 MetaInfo (org.qi4j.api.common.MetaInfo)7 InjectionPoint (com.google.inject.spi.InjectionPoint)6 AnnotatedElement (java.lang.reflect.AnnotatedElement)6 LinkedHashSet (java.util.LinkedHashSet)6 List (java.util.List)6 Optional (org.qi4j.api.common.Optional)6 ValueConstraintsInstance (org.qi4j.runtime.composite.ValueConstraintsInstance)6 ValueConstraintsModel (org.qi4j.runtime.composite.ValueConstraintsModel)6