Java Code Examples for java.lang.reflect.ParameterizedType.toString()

The following are Jave code examples for showing how to use toString() of the java.lang.reflect.ParameterizedType class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: Android-Router   File: ReflectTool.java   View Source Code Vote up 6 votes
@SuppressWarnings("all")
public static String getFieldTypeWithGeneric(Field f) {
    Class fieldType = f.getType();
    //TODO Anonymous Inner Class Cant be find By Class.fromName,but getName could.
    String type = fieldType.getCanonicalName();
    if (fieldType.isAssignableFrom(List.class)) {
        try {
            ParameterizedType pt = (ParameterizedType) f.getGenericType();
            return pt.toString();
        } catch (ClassCastException ignored) {
        }
    }
    return type;
}
 
Example 2
Project: jsf-sdk   File: TelnetUtils.java   View Source Code Vote up 5 votes
public static void scanParameter(Class<?> parameter, Set<Class<?>> checkSet, StringBuilder builder){
    if(checkSet.contains(parameter) || parameter.isPrimitive() || parameter.isEnum()
            || parameter.getCanonicalName().startsWith("java.")){
        return;
    }
    //Bean
    checkSet.add(parameter);
    Field fieldList[] = CodecUtils.getFields(parameter);
    if(builder.lastIndexOf(",") != builder.length()-1){
        builder.append(",");
    }
    builder.append("\"").append(parameter.getSimpleName()).append("\":{");
    boolean isAdd = false;
    for(Field field : fieldList){
        Class<?> fieldClass = field.getType();
        Type fType = field.getGenericType();
        if(isAdd && builder.lastIndexOf(",") != builder.length()-1){
            builder.append(",");
        }
        if(!isAdd){
            isAdd = true;
        }
        if(fType instanceof ParameterizedType){
            ParameterizedType pfType = (ParameterizedType)fType;
            String typeStr = pfType.toString();
            builder.append("\""+field.getName() +"\":").append("\""+typeStr +"\",");
            getGenericField(typeStr, builder, checkSet);
        } else {
            if(fieldClass.isPrimitive() || fieldClass.isEnum() || fieldClass.getCanonicalName().startsWith("java.")){
                builder.append("\""+field.getName() +"\":").append("\""+fieldClass.getCanonicalName() +"\",");
                continue;
            }
            //自包类忽略
            if(fieldClass.equals(parameter)){
                builder.append("\""+field.getName() +"\":").append("\""+fieldClass.getCanonicalName() +"\",");
                continue;
            }
            if(fieldClass.isArray()){
                builder.append("\""+field.getName() +"\":").append("\""+fieldClass.getCanonicalName() +"\",");
                Class comClass = fieldClass.getComponentType();
                while(comClass.isArray()){
                    comClass = comClass.getComponentType();
                }
                scanParameter(comClass, checkSet, builder);
                continue;
            }
            scanParameter(fieldClass, checkSet, builder);
        }
    }
    if(builder.lastIndexOf(",") == builder.length() - 1){
        builder.deleteCharAt(builder.length()-1);
    }
    builder.append("}");
}