/** * Gets the most specific type we can assign to the given genericParameter * to the clazz in the known fieldHierarchy. * * @param clazz * THe clazz we're looking for. * @param genericParameter * The parameter number of the clazz we're looking for. * @param propertyHierarchy * The filed hierarchy. * @return The most specific type we can assign to the given parameter. */ public static Type getPropertyGenericType(final Class<?> clazz, final int genericParameter, final Member... propertyHierarchy) { final Deque<Member> properties = new LinkedList<Member>(); for (final Member property : propertyHierarchy) { properties.add(property); } return GenericsUtil.getPropertyGenericType(clazz, genericParameter, properties); }
/** {@inheritDoc} */ @Override public void handleEvent(final UpdatedParameterEvent<P, B> event) throws EventException { SetCollectionTypeListener.LOGGER.debug("Handling event {}", event); Validate.notNull(event, "Event may not be null."); @SuppressWarnings("unchecked") final AnnotationCollectionParameterDescriptorBuilder<C, P, B> builder = (AnnotationCollectionParameterDescriptorBuilder<C, P, B>) event .getBuilder(); final Class<?> collectionType; final Deque<DescribedParameter<?>> params = new LinkedList<DescribedParameter<?>>(); params.add(event.getParameter()); final java.lang.reflect.Type tmpType = GenericsUtil.getPropertyGenericType(Collection.class, 0, params); if (tmpType instanceof Class) { collectionType = (Class<?>) tmpType; } else if (tmpType instanceof ParameterizedType) { final ParameterizedType ptype = (ParameterizedType) tmpType; final Type rawType = ptype.getRawType(); if (rawType instanceof Class) { collectionType = (Class<?>) rawType; } else { collectionType = Object.class; } } else { collectionType = Object.class; } builder.collectionType(collectionType); } }
result = GenericsUtil.getPropertyGenericType(declaringClass, index, propertyHierarchy);
/** {@inheritDoc} */ @Override public void handleEvent(final UpdatedCollectionResultTypeEvent<C, R, E, B> event) throws EventException { SetCollectionTypeListener.LOGGER.debug("Handling event {}", event); Validate.notNull(event, "Event may not be null."); final AnnotationCollectionResultDescriptorBuilder<C, R, E, B> builder = event.getBuilder(); final Deque<Member> members = new LinkedList<Member>(); members.addAll(builder.getBuilderContext().getAccessorContext().getDescribedProperties()); members.add(builder.getOperation()); final Class<?> collectionType; final java.lang.reflect.Type tmpType = GenericsUtil.getPropertyGenericType(Collection.class, 0, members); if (tmpType instanceof Class) { collectionType = (Class<?>) tmpType; } else if (tmpType instanceof ParameterizedType) { final ParameterizedType ptype = (ParameterizedType) tmpType; final Type rawType = ptype.getRawType(); if (rawType instanceof Class) { collectionType = (Class<?>) rawType; } else { collectionType = Object.class; } } else { collectionType = Object.class; } builder.collectionType(collectionType); } }
/** * Finds originating type for this field. * * @param properties * The fields to look through. * @return The true type. */ public static Type getRealType(final Deque<? extends Member> properties) { final Member property = properties.pop(); final Type fieldType = property.getGenericType(); Type result = null; if (fieldType instanceof TypeVariable) { final TypeVariable<?> typeVariable = (TypeVariable<?>) fieldType; final Class<?> declaringType = GenericsUtil.getDeclaringClass(property.getDeclaringType(), typeVariable); final TypeVariable<?> superTypeVariable = GenericsUtil.getSuperDeclaration(property, typeVariable); final int param = GenericsUtil.getRealParameter(superTypeVariable); result = GenericsUtil.getPropertyGenericType(declaringType, param, properties); } else if (fieldType instanceof Class) { result = fieldType; } else if (fieldType instanceof ParameterizedType) { final ParameterizedType pType = (ParameterizedType) fieldType; result = pType.getRawType(); } return result; }
/** {@inheritDoc} */ @Override public void handleEvent(final UpdatedPropertyTypeEvent<P, E, B> event) throws EventException { SetCollectionTypeListener.LOGGER.debug("Handle event {}", event); Validate.notNull(event, "Event may not be null."); @SuppressWarnings("unchecked") final AnnotationCollectionPropertyDescriptorBuilder<C, P, E, B> builder = (AnnotationCollectionPropertyDescriptorBuilder<C, P, E, B>) event .getBuilder(); event.getProperty(); final Class<?> collectionType; final java.lang.reflect.Type tmpType = GenericsUtil.getPropertyGenericType(Collection.class, 0, builder.getBuilderContext() .getAccessorContext().getDescribedProperties()); if (tmpType instanceof Class) { collectionType = (Class<?>) tmpType; } else if (tmpType instanceof ParameterizedType) { final ParameterizedType ptype = (ParameterizedType) tmpType; final Type rawType = ptype.getRawType(); if (rawType instanceof Class) { collectionType = (Class<?>) rawType; } else { collectionType = Object.class; } } else { collectionType = Object.class; } builder.collectionType(collectionType); } }