@SuppressWarnings("StatementWithEmptyBody") // intellij idea private static String getSimpleTypeName(TypeVariable type) { StringBuilder sb = new StringBuilder(); sb.append(type.getTypeName()); if (type.getBounds() == null || type.getBounds().length <= 0) { return sb.toString(); } String boundsString = Arrays.stream(type.getBounds()) .filter(t -> !t.getTypeName().equals("java.lang.Object")) .map(t -> getSimpleTypeNameWithoutTypeVariable(t, type)) .collect(Collectors.joining(" & ")); if (boundsString != null && boundsString.trim().length() > 0) { sb.append(" extends "); sb.append(boundsString); } return sb.toString(); }
@SuppressWarnings("rawtypes") private static Stream<String> typeToGenericString(final Set<String> classAlias, final Type type) { if (type instanceof TypeVariable) { final TypeVariable typeVariable = (TypeVariable) type; final String boundaryType = typeVariable.getBounds()[0].getTypeName(); final String genericAlias = typeVariable.getTypeName(); if (classAlias.contains(normalizeTypeAlias(genericAlias))) { return Stream.empty(); } if (boundaryType.equals("java.lang.Object")) { return Stream.of(genericAlias); } return Stream.of(String.format("%s extends %s", genericAlias, normalizeTypeName(boundaryType))); } else if (type instanceof ParameterizedType) { final ParameterizedType paramType = (ParameterizedType) type; return Arrays.stream(paramType.getActualTypeArguments()) .flatMap(arg -> typeToGenericString(classAlias, arg)); } return Stream.empty(); }
@SuppressWarnings("rawtypes") private static Stream<String> typeToGenericString(final Set<String> classAlias, final Type type) { if (type instanceof TypeVariable) { final TypeVariable typeVariable = (TypeVariable) type; final String boundaryType = typeVariable.getBounds()[0].getTypeName(); final String genericAlias = typeVariable.getTypeName(); if (classAlias.contains(normalizeTypeAlias(genericAlias))) { return Stream.empty(); } if (boundaryType.equals("java.lang.Object")) { return Stream.of(genericAlias); } return Stream.of(String.format("%s extends %s", genericAlias, normalizeTypeName(boundaryType))); } else if (type instanceof ParameterizedType) { final ParameterizedType paramType = (ParameterizedType) type; return Arrays.stream(paramType.getActualTypeArguments()) .flatMap(arg -> typeToGenericString(classAlias, arg)); } return Stream.empty(); }
/** * Determine the type of a generic property. * * @param property name of property * @return class of property type * @throws IllegalArgumentException if the generic property is not a {@link TypeVariable} */ public Class reifyPropertyType(PropertyDescriptor property) { Type type = property.getReadMethod().getGenericReturnType(); if (type instanceof TypeVariable<?>) { TypeVariable<?> pt = (TypeVariable<?>) type; return javaType.getBindings().findBoundType(pt.getTypeName()).getRawClass(); } else { return property.getPropertyType(); // throw new IllegalArgumentException("Can not reify property '" + property.getName() + "' type '" + type + "'"); } } }
IntStream.range(0, typeVariables.length).forEach(typeVariablesIndex -> { Type type = genericParameterTypes[typeVariablesIndex]; genericParameterTypesMap.put(typeVariables[typeVariablesIndex].getTypeName(), type); }); });