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