Java Code Examples for com.google.common.reflect.TypeToken#resolveType()
The following examples show how to use
com.google.common.reflect.TypeToken#resolveType() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ModelInfoLookup.java From archie with Apache License 2.0 | 6 votes |
private Method getAddMethod(Class clazz, TypeToken typeToken, Field field, String javaFieldNameUpperCased, Method getMethod) { Method addMethod = null; if (Collection.class.isAssignableFrom(getMethod.getReturnType())) { Type[] typeArguments = ((ParameterizedType) getMethod.getGenericReturnType()).getActualTypeArguments(); if (typeArguments.length == 1) { TypeToken singularParameter = typeToken.resolveType(typeArguments[0]); //TODO: does this work or should we use the typeArguments[0].getSomething? String addMethodName = "add" + toSingular(javaFieldNameUpperCased); addMethod = getMethod(clazz, addMethodName, singularParameter.getRawType()); if (addMethod == null) { //Due to generics, this does not always work Set<Method> allAddMethods = ReflectionUtils.getAllMethods(clazz, ReflectionUtils.withName(addMethodName)); if (allAddMethods.size() == 1) { addMethod = allAddMethods.iterator().next(); } else { logger.warn("strange number of add methods for field {} on class {}", field.getName(), clazz.getSimpleName()); } } } } return addMethod; }
Example 2
Source File: EndpointMethod.java From endpoints-java with Apache License 2.0 | 6 votes |
private void validateNoWildcards(TypeToken<?>[] types) { for (TypeToken<?> type : types) { Type resolved = type.getType(); if (resolved instanceof ParameterizedType) { Class<?> clazz = type.getRawType(); TypeToken<?>[] typeArgs = new TypeToken<?>[clazz.getTypeParameters().length]; for (int i = 0; i < typeArgs.length; i++) { typeArgs[i] = type.resolveType(clazz.getTypeParameters()[i]); } validateNoWildcards(typeArgs); } else if (Types.isWildcardType(type)) { throw new IllegalArgumentException( // TODO: Figure out a more useful error message. Maybe try to provide the // location of the wildcard instead of just its name ('T' is not the most useful info). String.format("Wildcard type %s not supported", resolved)); } } }
Example 3
Source File: Serializers.java From endpoints-java with Apache License 2.0 | 5 votes |
/** * Gets the {@code Serializer} source type for a class. This resolves placeholders in generics. * * @param clazz a class, possibly implementing {@code Transformer} * @return the resolved source type, null if clazz is not a serializer */ @Nullable public static TypeToken<?> getSourceType(Class<? extends Transformer<?, ?>> clazz) { try { TypeToken<?> token = TypeToken.of(clazz); return token.resolveType( Transformer.class.getMethod("transformFrom", Object.class).getGenericReturnType()); } catch (NoSuchMethodException e) { return null; } }
Example 4
Source File: CollectionUtils.java From OpenModsLib with MIT License | 5 votes |
private static Class<?> findTypeFromGenericInterface(Class<?> cls) { final TypeToken<?> token = TypeToken.of(cls); final TypeToken<?> typeB = token.resolveType(TypeUtils.FUNCTION_B_PARAM); if (typeB.getType() instanceof Class<?>) { return typeB.getRawType(); } return null; }
Example 5
Source File: TypeUtils.java From OpenModsLib with MIT License | 5 votes |
public static TypeToken<?> getTypeParameter(Class<?> intfClass, Class<?> instanceClass, int index) { final TypeVariable<?>[] typeParameters = intfClass.getTypeParameters(); Preconditions.checkElementIndex(index, typeParameters.length, intfClass + " type parameter index"); TypeVariable<?> arg = typeParameters[index]; TypeToken<?> type = TypeToken.of(instanceClass); Preconditions.checkArgument(type.getRawType() != Object.class, "Type %s is no fully parametrized", instanceClass); return type.resolveType(arg); }
Example 6
Source File: GuavaReflectionUtilsUnitTest.java From tutorials with MIT License | 5 votes |
@Test public void givenMapType_whenGetTypeArgumentOfEntry_thenShouldReturnTypeAtRuntime() throws NoSuchMethodException { //given TypeToken<Map<String, Integer>> mapToken = new TypeToken<Map<String, Integer>>() { }; //when TypeToken<?> entrySetToken = mapToken.resolveType(Map.class.getMethod("entrySet").getGenericReturnType()); //then assertEquals(entrySetToken, new TypeToken<Set<Map.Entry<String, Integer>>>() { }); }
Example 7
Source File: GuavaReflectionUtilsUnitTest.java From tutorials with MIT License | 5 votes |
@Test public void givenComplexType_whenGetTypeArgument_thenShouldReturnTypeAtRuntime() { //given TypeToken<Function<Integer, String>> funToken = new TypeToken<Function<Integer, String>>() { }; //when TypeToken<?> funResultToken = funToken.resolveType(Function.class.getTypeParameters()[1]); //then assertEquals(funResultToken, TypeToken.of(String.class)); }
Example 8
Source File: Reflections.java From brooklyn-server with Apache License 2.0 | 5 votes |
public static TypeToken<?>[] getGenericParameterTypeTokens(TypeToken<?> t) { Class<?> rawType = t.getRawType(); TypeVariable<?>[] pT = rawType.getTypeParameters(); TypeToken<?> pTT[] = new TypeToken<?>[pT.length]; for (int i=0; i<pT.length; i++) { pTT[i] = t.resolveType(pT[i]); } return pTT; }
Example 9
Source File: Serializers.java From endpoints-java with Apache License 2.0 | 5 votes |
/** * Gets the {@code Serializer} target type for a class. This resolves placeholders in generics. * * @param clazz a class, possibly implementing {@code Transformer} * @return the resolved target type, null if clazz is not a serializer */ @Nullable public static TypeToken<?> getTargetType(Class<? extends Transformer<?, ?>> clazz) { try { TypeToken<?> token = TypeToken.of(clazz); return token.resolveType( Transformer.class.getMethod("transformTo", Object.class).getGenericReturnType()); } catch (NoSuchMethodException e) { return null; } }
Example 10
Source File: ServiceOrchestrator.java From yanwte2 with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") default Class<? extends Function<?, ?>> getServiceType() { TypeToken<?> typeToken = new TypeToken<ServiceOrchestrator<T>>() { private static final long serialVersionUID = -4811029877591669034L; }; typeToken = typeToken.resolveType(getClass()); typeToken = typeToken.resolveType(ServiceOrchestrator.class.getTypeParameters()[0]); return (Class) typeToken.getRawType(); }
Example 11
Source File: Types.java From endpoints-java with Apache License 2.0 | 5 votes |
/** * Returns the type parameter at a specified index. * * @throws IllegalArgumentException if the type is not parameterized * @throws IndexOutOfBoundsException if the type doesn't have enough type parameters */ public static TypeToken<?> getTypeParameter(TypeToken<?> type, int index) { Preconditions.checkArgument( type.getType() instanceof ParameterizedType, "type is not parameterized"); Type[] typeArgs = ((ParameterizedType) type.getType()).getActualTypeArguments(); if (typeArgs.length <= index) { throw new IndexOutOfBoundsException( String.format("type '%s' has %d <= %d type arguments", type, typeArgs.length, index)); } return type.resolveType(typeArgs[index]); }
Example 12
Source File: Types.java From endpoints-java with Apache License 2.0 | 5 votes |
/** * Gets the element type of a type we want to treat as an array. Actual arrays or subtypes of * {@link java.util.Collection} can be treated as arrays. Returns null if the type cannot be * treated as an array. */ public static TypeToken<?> getArrayItemType(TypeToken<?> type) { if (type.isSubtypeOf(Collection.class)) { return type.resolveType(Collection.class.getTypeParameters()[0]); } else if (type.isArray()) { return type.getComponentType(); } return null; }
Example 13
Source File: CaptorParameterFactory.java From junit5-extensions with MIT License | 5 votes |
@Override public Object getParameterValue(Parameter parameter) { TypeToken<?> parameterType = TypeToken.of(parameter.getParameterizedType()); TypeToken<?> captorParameter = parameterType.resolveType(ArgumentCaptor.class.getTypeParameters()[0]); return ArgumentCaptor.forClass(captorParameter.getRawType()); }
Example 14
Source File: GenericMethodType.java From ProjectAres with GNU Affero General Public License v3.0 | 5 votes |
public GenericMethodType<? extends T> resolveIn(TypeToken<?> context) { final TypeToken<? extends T> returnType = (TypeToken<? extends T>) context.resolveType(this.returnType.getType()); final ImmutableList<TypeToken<?>> parameterTypes = this.parameterTypes.stream() .map(t -> context.resolveType(t.getType())) .collect(Collectors.toImmutableList()); return returnType.equals(this.returnType) && parameterTypes.equals(this.parameterTypes) ? this : new GenericMethodType<>(returnType, parameterTypes); }
Example 15
Source File: DataExtensionInitializer.java From yanwte2 with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") default Class<ED> getExtensibleDataType() { TypeToken<?> typeToken = new TypeToken<DataExtensionInitializer<ED, DataExtension>>() { private static final long serialVersionUID = 4639419131976092626L; }; typeToken = typeToken.resolveType(getClass()); typeToken = typeToken.resolveType(DataExtensionInitializer.class.getTypeParameters()[0]); return (Class) typeToken.getRawType(); }
Example 16
Source File: ModelInfoLookup.java From archie with Apache License 2.0 | 4 votes |
private void addRMAttributeInfo(Class clazz, RMTypeInfo typeInfo, TypeToken typeToken, Field field) { String attributeName = namingStrategy.getAttributeName(field); String javaFieldName = field.getName(); String javaFieldNameUpperCased = upperCaseFirstChar(javaFieldName); Method getMethod = getMethod(clazz, "get" + javaFieldNameUpperCased); Method setMethod = null, addMethod = null; if (getMethod == null) { getMethod = getMethod(clazz, "is" + javaFieldNameUpperCased); } if (getMethod != null) { setMethod = getMethod(clazz, "set" + javaFieldNameUpperCased, getMethod.getReturnType()); addMethod = getAddMethod(clazz, typeToken, field, javaFieldNameUpperCased, getMethod); } else { logger.warn("No get method found for field {} on class {}", field.getName(), clazz.getSimpleName()); } TypeToken fieldType = null; if (getMethod != null) { fieldType = typeToken.resolveType(getMethod.getGenericReturnType()); } else { fieldType = typeToken.resolveType(field.getGenericType()); } Class rawFieldType = fieldType.getRawType(); Class typeInCollection = getTypeInCollection(fieldType); if (setMethod != null) { RMAttributeInfo attributeInfo = new RMAttributeInfo( attributeName, field, rawFieldType, typeInCollection, field.getAnnotation(Nullable.class) != null, getMethod, setMethod, addMethod ); typeInfo.addAttribute(attributeInfo); } else { logger.info("property without a set method ignored for field {} on class {}", field.getName(), clazz.getSimpleName()); } }
Example 17
Source File: DefaultTypeClassifier.java From OpenPeripheral with MIT License | 4 votes |
private static IScriptType classifyCollectionType(ITypeClassifier classifier, TypeToken<?> typeToken) { final TypeToken<?> componentType = typeToken.resolveType(TypeUtils.COLLECTION_VALUE_PARAM); return createListType(classifier, componentType); }
Example 18
Source File: DefaultTypeClassifier.java From OpenPeripheral with MIT License | 4 votes |
private static IScriptType classifySetType(ITypeClassifier classifier, TypeToken<?> typeToken) { final TypeToken<?> componentType = typeToken.resolveType(TypeUtils.SET_VALUE_PARAM); return createSetType(classifier, componentType); }
Example 19
Source File: ResolvableTypeParameter.java From ProjectAres with GNU Affero General Public License v3.0 | 4 votes |
public TypeToken<? extends T> resolveIn(TypeToken<? extends D> context) { return (TypeToken<? extends T>) context.resolveType(typeVariable); }
Example 20
Source File: TypeUtils.java From OpenModsLib with MIT License | 4 votes |
public static TypeToken<?> resolveFieldType(Class<?> cls, Field field) { Type fieldType = field.getGenericType(); TypeToken<?> parentType = TypeToken.of(cls); return parentType.resolveType(fieldType); }