Java Code Examples for java.lang.reflect.Type.getTypeName()

The following are Jave code examples for showing how to use getTypeName() of the java.lang.reflect.Type class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: uroborosql   File: JavaType.java   Source Code and License Vote up 6 votes
private static JavaType create(final ImplementClass implementClass, final Type type) {
	if (type instanceof Class<?>) {
		return new ClassJavaType(implementClass, (Class<?>) type);
	} else if (type instanceof TypeVariable<?>) {
		TypeVariable<?> variable = (TypeVariable<?>) type;
		return createByParamName(implementClass, variable);
	} else if (type instanceof GenericArrayType) {
		GenericArrayType arrayType = (GenericArrayType) type;
		return new ArrayJavaType(implementClass, arrayType);
	} else if (type instanceof ParameterizedType) {
		ParameterizedType parameterizedType = (ParameterizedType) type;
		return new ParameterizedJavaType(implementClass, parameterizedType);
	} else if (type instanceof WildcardType) {
		WildcardType wildcardType = (WildcardType) type;
		return new WildcardJavaType(implementClass, wildcardType);
	}

	throw new IllegalArgumentException(type.getTypeName());

}
 
Example 2
Project: jkes   File: ReflectionUtils.java   Source Code and License Vote up 5 votes
/**
 * @param method {@link Method} object
 * @return the actual type parameters used in the source code. Return <strong>empty</strong> list if no parametrized type
 */
public static List<String> getReturnTypeParameters(Method method) {
    List<String> typeParameters = new ArrayList<>();

    Type type = method.getGenericReturnType();
    String typeName = type.getTypeName(); // ex: java.util.List<com.timeyang.search.entity.Person>, java.lang.Long
    Pattern p = Pattern.compile("<((\\S+\\.?),?\\s*)>");
    Matcher m = p.matcher(typeName);
    while (m.find()) {
        typeParameters.add(m.group(2));
    }

    return typeParameters;
}
 
Example 3
Project: openjdk-jdk10   File: ResolvedJavaMethod.java   Source Code and License Vote up 5 votes
@Override
public String toString() {
    Type type = getParameterizedType();
    String typename = type.getTypeName();
    if (isVarArgs()) {
        typename = typename.replaceFirst("\\[\\]$", "...");
    }

    final StringBuilder sb = new StringBuilder(Modifier.toString(getModifiers()));
    if (sb.length() != 0) {
        sb.append(' ');
    }
    return sb.append(typename).append(' ').append(getName()).toString();
}
 
Example 4
Project: limberest   File: BodyParameterValidator.java   Source Code and License Vote up 4 votes
protected Result matchType(Object obj, Property prop, String path) throws ValidationException {
    String expectedType = prop.getType();
    if ("array".equals(expectedType)) {
        expectedType = JSONArray.class.getName();
    }
    else if ("ref".equals(expectedType) || "object".equals(expectedType)) {
        expectedType = JSONObject.class.getName();
    }
    else {
        PrimitiveType type = PrimitiveType.fromName(prop.getType());
        if (type != null) {
            expectedType = type.getKeyClass().getName();
            if (BigDecimal.class.getName().equals(expectedType))
                expectedType = Double.class.getName();
        }
        else {
            Type refType = ReflectionUtils.typeFromString(prop.getType());
            if (refType != null)
                expectedType = refType.getTypeName();
        }
    }
        
    String foundType = obj.getClass().getName();
    boolean match = foundType.equals(expectedType);
    if (!match) {
        // forgive rounded numeric types
        if (BigDecimal.class.getName().equals(expectedType)) {
            if (foundType.equals(Double.class.getName()) || foundType.equals(Integer.class.getName()))
                match = true;
        }
        else if (Double.class.getName().equals(expectedType)) {
            if (foundType.equals(Integer.class.getName()))
                match = true;
        }
        else if (Integer.class.getName().equals(expectedType)) {
            if (foundType.equals(Long.class.getName()) && "int64".equals(prop.getFormat()))
                match = true;
        }
    }
    
    if (match)
        return new Result();
    else
        return new Result(BAD_REQUEST, path + ": expected " + expectedType + " but found " + foundType);
}
 
Example 5
Project: ja-micro   File: MessageType.java   Source Code and License Vote up 4 votes
public static MessageType of(Type t) {
    return new MessageType(t.getTypeName());
}
 
Example 6
Project: jkes   File: ReflectionUtils.java   Source Code and License Vote up 3 votes
public static String getTypeName(Method method) {
    Type type = method.getGenericReturnType();

    String typeName = type.getTypeName(); // ex: java.util.List<com.timeyang.search.entity.Person>, java.lang.Long

    return typeName;
}
 
Example 7
Project: jkes   File: ReflectionUtils.java   Source Code and License Vote up 3 votes
/**
 * If return type is genetic type, then return last parameterized type, else return the formal return type name of the method represented by this {@code Method}  object.
 * @param method {@link Method} method
 * @return If return type is genetic type, then return last parameterized type, else return the formal return type name of the method represented by this {@code Method}  object.
 */
public static String getInnermostType(Method method) {
    Type type = method.getGenericReturnType();

    String typeName = type.getTypeName(); // ex: java.util.List<com.timeyang.search.entity.Person>, java.lang.Long

    String[] types = typeName.split(",\\s*|<|<|>+");

    return types[types.length - 1];
}
 
Example 8
Project: jkes   File: ReflectionUtils.java   Source Code and License Vote up 3 votes
/**
 * If the field object is a generic type, return innermost type; else return field type name directly
 * @param field Field object
 * @return If the field object is a generic type, return innermost type; else return field type name directly
 */
public static String getInnermostType(Field field) {
    Type type = field.getGenericType();

    String typeName = type.getTypeName(); // ex: java.util.List<com.timeyang.search.entity.Person>, java.lang.Long

    String[] types = typeName.split(",\\s*|<|<|>+");

    return types[types.length - 1];
}