Example usage for java.lang.reflect Type toString

List of usage examples for java.lang.reflect Type toString

Introduction

In this page you can find the example usage for java.lang.reflect Type toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:gov.nih.nci.cabig.caaers.tools.ObjectDump.java

public static void main(String[] args) {
    ObjectDump target = new ObjectDump();

    Class cls = null;/*  w  w  w .j  a v  a  2s .  c  o m*/
    Field[] fields;

    try {
        cls = Class.forName(target.getClass().getName());
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    write("Class: " + target.getClass().getName() + "\n");
    fields = cls.getFields();

    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];
        write("\tfield[" + (i + 1) + "]: " + field.getName() + "\r\n");

        Type type = field.getType();
        write(type.toString());
    }

}

From source file:Main.java

public static void main(String... args) {
    try {/*w w  w .  j  av a  2  s .  c om*/
        Class<?> c = Class.forName(args[0]);
        out.format("Class:%n  %s%n%n", c.getCanonicalName());
        out.format("Modifiers:%n  %s%n%n", Modifier.toString(c.getModifiers()));

        out.format("Type Parameters:%n");
        TypeVariable[] tv = c.getTypeParameters();
        if (tv.length != 0) {
            out.format("  ");
            for (TypeVariable t : tv)
                out.format("%s ", t.getName());
            out.format("%n%n");
        } else {
            out.format("  -- No Type Parameters --%n%n");
        }

        out.format("Implemented Interfaces:%n");
        Type[] intfs = c.getGenericInterfaces();
        if (intfs.length != 0) {
            for (Type intf : intfs)
                out.format("  %s%n", intf.toString());
            out.format("%n");
        } else {
            out.format("  -- No Implemented Interfaces --%n%n");
        }

        out.format("Inheritance Path:%n");
        List<Class> l = new ArrayList<Class>();
        printAncestor(c, l);
        if (l.size() != 0) {
            for (Class<?> cl : l)
                out.format("  %s%n", cl.getCanonicalName());
            out.format("%n");
        } else {
            out.format("  -- No Super Classes --%n%n");
        }

        out.format("Annotations:%n");
        Annotation[] ann = c.getAnnotations();
        if (ann.length != 0) {
            for (Annotation a : ann)
                out.format("  %s%n", a.toString());
            out.format("%n");
        } else {
            out.format("  -- No Annotations --%n%n");
        }

        // production code should handle this exception more gracefully
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    }
}

From source file:Main.java

static String typeToString(Type paramType) {
    if ((paramType instanceof Class))
        return ((Class) paramType).getName();
    return paramType.toString();
}

From source file:Main.java

public static String getClassName(Type type) {
    if (type == null) {
        return "";
    }//from  w w  w.  j  a v a2s.c  o m
    String className = type.toString();
    if (className.startsWith(TYPE_NAME_PREFIX)) {
        className = className.substring(TYPE_NAME_PREFIX.length());
    }
    return className;
}

From source file:Main.java

/**
 * {@link Type#toString()} value is the fully qualified class name prefixed
 * with {@link ReflectionUtils#TYPE_NAME_PREFIX}. This method will substring it, for it to be eligible
 * for {@link Class#forName(String)}./*  w ww  .  j  a  v  a2 s .c  o  m*/
 *
 * @param type the {@code Type} value whose class name is needed.
 * @return {@code String} class name of the invoked {@code type}.
 *
 * @see {@link ReflectionUtils#getClass()}
 */
public static String getClassName(Type type) {
    if (type == null) {
        return "";
    }
    String className = type.toString();
    if (className.startsWith(TYPE_CLASS_NAME_PREFIX)) {
        className = className.substring(TYPE_CLASS_NAME_PREFIX.length());
    } else if (className.startsWith(TYPE_INTERFACE_NAME_PREFIX)) {
        className = className.substring(TYPE_INTERFACE_NAME_PREFIX.length());
    }
    return className;
}

From source file:Main.java

private static String getClassName(Type type) {
    if (type == null) {
        return "";
    }//from   ww w . j a v  a2  s .co m
    String clazzName = type.toString();
    if (clazzName.startsWith(TYPE_NAME_PREFIX)) {
        clazzName = clazzName.replace(TYPE_NAME_PREFIX, "");
    }
    return clazzName;
}

From source file:Main.java

public static Class<?>[] getGenericArgs(ParameterizedType genericType) {
    Type[] typeArr = genericType.getActualTypeArguments();
    Class<?>[] argsArr = new Class<?>[typeArr.length];
    for (int i = 0; i < typeArr.length; i++) {
        Type t = typeArr[i];
        if (t instanceof ParameterizedType) {
            // TODO full hierarchy
            t = ((ParameterizedType) t).getRawType();
        }//from   w  w  w  .  j  av a 2s .c  o  m
        String clsName = t.toString().replaceFirst("^(class|interface) ", "");
        argsArr[i] = classForName(clsName);
    }
    return argsArr;
}

From source file:Main.java

public static void setField(Object object, String fieldName, Object fieldValue) {
    Class<?> objectClass = object.getClass();
    if (objectClass != null) {
        try {/*www.  j ava2 s  .  c  om*/
            Field field = objectClass.getDeclaredField(fieldName);
            field.setAccessible(true);
            Type type = field.getGenericType();

            //This is bad, but dear God I can't figure out how to convert a runtime Type to its actual type through reflection. I know how in C#....
            if (type.toString().toLowerCase().contains("short")) {
                fieldValue = Short.parseShort((String) fieldValue);
            }

            else if (type.toString().toLowerCase().contains("integer")) {
                fieldValue = Integer.parseInt((String) fieldValue);
            }

            else if (type.toString().toLowerCase().contains("long")) {
                fieldValue = Long.parseLong((String) fieldValue);
            }

            else if (type.toString().toLowerCase().contains("boolean")) {
                fieldValue = "1".equals(fieldValue); //Java, you're the worst language. And SQLite isn't helping.
            }

            field.set(object, fieldValue);
        }

        catch (NoSuchFieldException e) {
            return;
        }

        catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }
}

From source file:org.nuxeo.ecm.core.io.registry.MarshallerHelper.java

/**
 * Checks the marshallers isn't null. Throw an exception if it is.
 *///from w ww.j av a2  s . co m
private static void checkMarshaller(Type type, Marshaller<?> marshaller) {
    if (marshaller == null) {
        throw new MarshallingException("No marshaller found for type " + type.toString());
    }
}

From source file:com.github.juanmf.java2plant.Parser.java

protected static Set<String> getTypeParams(ParameterizedType f) {
    Set<String> typeVars = new HashSet<>();
    Type[] actualTypeArguments = f.getActualTypeArguments();
    for (Type t : actualTypeArguments) {
        typeVars.add(t.toString().replace("class ", ""));
    }//from  w w  w . ja  v  a 2 s . c  o m
    return typeVars;
}