Search in sources :

Example 21 with Member

use of java.lang.reflect.Member in project hibernate-orm by hibernate.

the class AttributeConverterDescriptorImpl method resolveMember.

private ResolvedMember resolveMember(XProperty xProperty, MetadataBuildingContext buildingContext) {
    final ClassmateContext classmateContext = buildingContext.getMetadataCollector().getClassmateContext();
    final ReflectionManager reflectionManager = buildingContext.getBuildingOptions().getReflectionManager();
    final ResolvedType declaringClassType = classmateContext.getTypeResolver().resolve(reflectionManager.toClass(xProperty.getDeclaringClass()));
    final ResolvedTypeWithMembers declaringClassWithMembers = classmateContext.getMemberResolver().resolve(declaringClassType, null, null);
    final Member member = toMember(xProperty);
    if (member instanceof Method) {
        for (ResolvedMethod resolvedMember : declaringClassWithMembers.getMemberMethods()) {
            if (resolvedMember.getName().equals(member.getName())) {
                return resolvedMember;
            }
        }
    } else if (member instanceof Field) {
        for (ResolvedField resolvedMember : declaringClassWithMembers.getMemberFields()) {
            if (resolvedMember.getName().equals(member.getName())) {
                return resolvedMember;
            }
        }
    } else {
        throw new HibernateException("Unexpected java.lang.reflect.Member type from org.hibernate.annotations.common.reflection.java.JavaXMember : " + member);
    }
    throw new HibernateException("Could not locate resolved type information for attribute [" + member.getName() + "] from Classmate");
}
Also used : Field(java.lang.reflect.Field) ResolvedField(com.fasterxml.classmate.members.ResolvedField) ResolvedMethod(com.fasterxml.classmate.members.ResolvedMethod) ResolvedField(com.fasterxml.classmate.members.ResolvedField) ReflectionManager(org.hibernate.annotations.common.reflection.ReflectionManager) HibernateException(org.hibernate.HibernateException) ResolvedMethod(com.fasterxml.classmate.members.ResolvedMethod) Method(java.lang.reflect.Method) Member(java.lang.reflect.Member) ResolvedMember(com.fasterxml.classmate.members.ResolvedMember) ResolvedType(com.fasterxml.classmate.ResolvedType) ResolvedTypeWithMembers(com.fasterxml.classmate.ResolvedTypeWithMembers)

Example 22 with Member

use of java.lang.reflect.Member in project hibernate-orm by hibernate.

the class AttributeFactory method determineAttributeMetadata.

/**
	 * Here is most of the nuts and bolts of this factory, where we interpret the known JPA metadata
	 * against the known Hibernate metadata and build a descriptor for the attribute.
	 *
	 * @param attributeContext The attribute to be described
	 * @param memberResolver Strategy for how to resolve the member defining the attribute.
	 * @param <X> The owner type
	 * @param <Y> The attribute type
	 *
	 * @return The attribute description
	 */
@SuppressWarnings({ "unchecked" })
private <X, Y> AttributeMetadata<X, Y> determineAttributeMetadata(AttributeContext<X> attributeContext, MemberResolver memberResolver) {
    LOG.trace("Starting attribute metadata determination [" + attributeContext.getPropertyMapping().getName() + "]");
    final Member member = memberResolver.resolveMember(attributeContext);
    LOG.trace("    Determined member [" + member + "]");
    final Value value = attributeContext.getPropertyMapping().getValue();
    final org.hibernate.type.Type type = value.getType();
    LOG.trace("    Determined type [name=" + type.getName() + ", class=" + type.getClass().getName() + "]");
    if (type.isAnyType()) {
        // ANY mappings are currently not supported in the JPA metamodel; see HHH-6589
        if (context.isIgnoreUnsupported()) {
            return null;
        } else {
            throw new UnsupportedOperationException("ANY not supported");
        }
    } else if (type.isAssociationType()) {
        // collection or entity
        if (type.isEntityType()) {
            // entity
            return new SingularAttributeMetadataImpl<X, Y>(attributeContext.getPropertyMapping(), attributeContext.getOwnerType(), member, determineSingularAssociationAttributeType(member));
        }
        // collection
        if (value instanceof Collection) {
            final Collection collValue = (Collection) value;
            final Value elementValue = collValue.getElement();
            final org.hibernate.type.Type elementType = elementValue.getType();
            // First, determine the type of the elements and use that to help determine the
            // collection type)
            final Attribute.PersistentAttributeType elementPersistentAttributeType;
            final Attribute.PersistentAttributeType persistentAttributeType;
            if (elementType.isAnyType()) {
                if (context.isIgnoreUnsupported()) {
                    return null;
                } else {
                    throw new UnsupportedOperationException("collection of any not supported yet");
                }
            }
            final boolean isManyToMany = isManyToMany(member);
            if (elementValue instanceof Component) {
                elementPersistentAttributeType = Attribute.PersistentAttributeType.EMBEDDED;
                persistentAttributeType = Attribute.PersistentAttributeType.ELEMENT_COLLECTION;
            } else if (elementType.isAssociationType()) {
                elementPersistentAttributeType = isManyToMany ? Attribute.PersistentAttributeType.MANY_TO_MANY : Attribute.PersistentAttributeType.ONE_TO_MANY;
                persistentAttributeType = elementPersistentAttributeType;
            } else {
                elementPersistentAttributeType = Attribute.PersistentAttributeType.BASIC;
                persistentAttributeType = Attribute.PersistentAttributeType.ELEMENT_COLLECTION;
            }
            final Attribute.PersistentAttributeType keyPersistentAttributeType;
            // Finally, we determine the type of the map key (if needed)
            if (value instanceof Map) {
                final Value keyValue = ((Map) value).getIndex();
                final org.hibernate.type.Type keyType = keyValue.getType();
                if (keyType.isAnyType()) {
                    if (context.isIgnoreUnsupported()) {
                        return null;
                    } else {
                        throw new UnsupportedOperationException("collection of any not supported yet");
                    }
                }
                if (keyValue instanceof Component) {
                    keyPersistentAttributeType = Attribute.PersistentAttributeType.EMBEDDED;
                } else if (keyType.isAssociationType()) {
                    keyPersistentAttributeType = Attribute.PersistentAttributeType.MANY_TO_ONE;
                } else {
                    keyPersistentAttributeType = Attribute.PersistentAttributeType.BASIC;
                }
            } else {
                keyPersistentAttributeType = null;
            }
            return new PluralAttributeMetadataImpl(attributeContext.getPropertyMapping(), attributeContext.getOwnerType(), member, persistentAttributeType, elementPersistentAttributeType, keyPersistentAttributeType);
        } else if (value instanceof OneToMany) {
            // element value within a o.h.mapping.Collection (see logic branch above)
            throw new IllegalArgumentException("HUH???");
        //					final boolean isManyToMany = isManyToMany( member );
        //					//one to many with FK => entity
        //					return new PluralAttributeMetadataImpl(
        //							attributeContext.getPropertyMapping(),
        //							attributeContext.getOwnerType(),
        //							member,
        //							isManyToMany
        //									? Attribute.PersistentAttributeType.MANY_TO_MANY
        //									: Attribute.PersistentAttributeType.ONE_TO_MANY
        //							value,
        //							AttributeContext.TypeStatus.ENTITY,
        //							Attribute.PersistentAttributeType.ONE_TO_MANY,
        //							null, null, null
        //					);
        }
    } else if (attributeContext.getPropertyMapping().isComposite()) {
        // component
        return new SingularAttributeMetadataImpl<X, Y>(attributeContext.getPropertyMapping(), attributeContext.getOwnerType(), member, Attribute.PersistentAttributeType.EMBEDDED);
    } else {
        // basic type
        return new SingularAttributeMetadataImpl<X, Y>(attributeContext.getPropertyMapping(), attributeContext.getOwnerType(), member, Attribute.PersistentAttributeType.BASIC);
    }
    throw new UnsupportedOperationException("oops, we are missing something: " + attributeContext.getPropertyMapping());
}
Also used : OneToMany(org.hibernate.mapping.OneToMany) EmbeddedComponentType(org.hibernate.type.EmbeddedComponentType) EntityType(org.hibernate.type.EntityType) ComponentType(org.hibernate.type.ComponentType) Type(javax.persistence.metamodel.Type) ParameterizedType(java.lang.reflect.ParameterizedType) Value(org.hibernate.mapping.Value) Collection(org.hibernate.mapping.Collection) Component(org.hibernate.mapping.Component) Member(java.lang.reflect.Member) Map(org.hibernate.mapping.Map)

Example 23 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 24 with Member

use of java.lang.reflect.Member in project jackson-module-afterburner by FasterXML.

the class DeserializerModifier method findOptimizableProperties.

/*
    /********************************************************************** 
    /* Internal methods
    /********************************************************************** 
     */
protected List<OptimizedSettableBeanProperty<?>> findOptimizableProperties(DeserializationConfig config, PropertyMutatorCollector collector, Iterator<SettableBeanProperty> propIterator) {
    ArrayList<OptimizedSettableBeanProperty<?>> newProps = new ArrayList<OptimizedSettableBeanProperty<?>>();
    // Ok, then, find any properties for which we could generate accessors
    while (propIterator.hasNext()) {
        SettableBeanProperty prop = propIterator.next();
        AnnotatedMember member = prop.getMember();
        Member jdkMember = member.getMember();
        // if we ever support virtual properties, this would be null, so check, skip
        if (jdkMember == null) {
            continue;
        }
        // First: we can't access private fields or methods....
        if (Modifier.isPrivate(jdkMember.getModifiers())) {
            continue;
        }
        // 30-Jul-2012, tatu: [module-afterburner#6]: Needs to skip custom deserializers, if any.
        if (prop.hasValueDeserializer()) {
            if (!isDefaultDeserializer(prop.getValueDeserializer())) {
                continue;
            }
        }
        if (prop instanceof MethodProperty) {
            // simple setter methods
            Class<?> type = ((AnnotatedMethod) member).getRawParameterType(0);
            if (type.isPrimitive()) {
                if (type == Integer.TYPE) {
                    newProps.add(collector.addIntSetter(prop));
                } else if (type == Long.TYPE) {
                    newProps.add(collector.addLongSetter(prop));
                } else if (type == Boolean.TYPE) {
                    newProps.add(collector.addBooleanSetter(prop));
                }
            } else {
                if (type == String.class) {
                    newProps.add(collector.addStringSetter(prop));
                } else {
                    // any other Object types; we can at least call accessor
                    newProps.add(collector.addObjectSetter(prop));
                }
            }
        } else if (prop instanceof FieldProperty) {
            // be overwritable via Reflection)
            if (Modifier.isFinal(prop.getMember().getMember().getModifiers())) {
                continue;
            }
            Class<?> type = member.getRawType();
            if (type.isPrimitive()) {
                if (type == Integer.TYPE) {
                    newProps.add(collector.addIntField(prop));
                } else if (type == Long.TYPE) {
                    newProps.add(collector.addLongField(prop));
                } else if (type == Boolean.TYPE) {
                    newProps.add(collector.addBooleanField(prop));
                }
            } else {
                if (type == String.class) {
                    newProps.add(collector.addStringField(prop));
                } else {
                    // any other Object types; we can at least call accessor
                    newProps.add(collector.addObjectField(prop));
                }
            }
        }
    }
    return newProps;
}
Also used : FieldProperty(com.fasterxml.jackson.databind.deser.impl.FieldProperty) MethodProperty(com.fasterxml.jackson.databind.deser.impl.MethodProperty) Member(java.lang.reflect.Member)

Example 25 with Member

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

the class WeldResourceInjectionServices method resolveResource.

@Override
public Object resolveResource(InjectionPoint injectionPoint) {
    final Member member = injectionPoint.getMember();
    AnnotatedMember<?> annotatedMember;
    if (injectionPoint.getAnnotated() instanceof AnnotatedField) {
        annotatedMember = (AnnotatedField<?>) injectionPoint.getAnnotated();
    } else {
        annotatedMember = ((AnnotatedParameter<?>) injectionPoint.getAnnotated()).getDeclaringCallable();
    }
    if (!annotatedMember.isAnnotationPresent(Resource.class)) {
        throw WeldLogger.ROOT_LOGGER.annotationNotFound(Resource.class, member);
    }
    if (member instanceof Method && ((Method) member).getParameterTypes().length != 1) {
        throw WeldLogger.ROOT_LOGGER.injectionPointNotAJavabean((Method) member);
    }
    String name = getResourceName(injectionPoint);
    for (ResourceInjectionResolver resolver : resourceResolvers) {
        Object result = resolver.resolve(name);
        if (result != null) {
            return result;
        }
    }
    try {
        return context.lookup(name);
    } catch (NamingException e) {
        throw WeldLogger.ROOT_LOGGER.couldNotFindResource(name, injectionPoint.getMember().toString(), e);
    }
}
Also used : Resource(javax.annotation.Resource) ResourceInjectionResolver(org.jboss.as.weld.spi.ResourceInjectionResolver) NamingException(javax.naming.NamingException) Method(java.lang.reflect.Method) AnnotatedField(javax.enterprise.inject.spi.AnnotatedField) AnnotatedMember(javax.enterprise.inject.spi.AnnotatedMember) Member(java.lang.reflect.Member)

Aggregations

Member (java.lang.reflect.Member)59 Method (java.lang.reflect.Method)13 Annotation (java.lang.annotation.Annotation)8 AccessibleObject (java.lang.reflect.AccessibleObject)7 MetaInfo (org.qi4j.api.common.MetaInfo)7 Field (java.lang.reflect.Field)6 Optional (org.qi4j.api.common.Optional)6 ValueConstraintsInstance (org.qi4j.runtime.composite.ValueConstraintsInstance)6 ValueConstraintsModel (org.qi4j.runtime.composite.ValueConstraintsModel)6 TypeVariable (java.lang.reflect.TypeVariable)5 NodeId (com.google.inject.grapher.NodeId)4 Constructor (java.lang.reflect.Constructor)4 Type (java.lang.reflect.Type)4 ArrayList (java.util.ArrayList)4 ManyAssociation (org.qi4j.api.association.ManyAssociation)4 NamedAssociation (org.qi4j.api.association.NamedAssociation)4 QualifiedName (org.qi4j.api.common.QualifiedName)4 ManyAssociationModel (org.qi4j.runtime.association.ManyAssociationModel)4 NamedAssociationModel (org.qi4j.runtime.association.NamedAssociationModel)4 HashSet (java.util.HashSet)3