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:com.github.juanmf.java2plant.Parser.java

protected static void addUse(Set<Relation> relations, Class<?> fromType, Type toType, Member m, String msg) {
    String toName = toType.toString();
    if (isMulti(toType)) {
        if (!((Class) toType).isArray()) {
            if (toType instanceof ParameterizedType) {
                ParameterizedType pt = (ParameterizedType) toType;
                Set<String> typeVars = getTypeParams(pt);
                for (String t : typeVars) {
                    msg += toName;// ww  w  .  jav a2  s .co m
                    Relation use = new Use(fromType, t, m, msg);
                    relations.add(use);
                }
                return;
            }
        }
        toName = CanonicalName.getClassName(((Class) toType).getName());
    }
    Relation use = new Use(fromType, toName, m, msg);
    relations.add(use);
}

From source file:com.sugaronrest.restapicalls.ModuleInfo.java

/**
 *  Gets class name (module name) from Java class type.
 *
 * @param type The Java type./*from  ww w  .  ja  va  2 s. c o m*/
 * @return Class name.
 */
public static String getClassName(Type type) {
    if (type == null) {
        return StringUtils.EMPTY;
    }
    String typeToString = type.toString();
    typeToString = typeToString.trim();
    String[] splitArray = typeToString.split("\\.");
    if (splitArray.length > 0) {
        return splitArray[splitArray.length - 1];
    }

    return StringUtils.EMPTY;
}

From source file:de.itomig.itoplib.GetItopJSON.java

public static <T> ArrayList<T> getArrayFromJson(String json, Type T) {
    ArrayList<T> list = new ArrayList<T>();
    String code = "100"; // json error code, "0" => everything is o.k.
    Log.d(TAG, "getArrayFromJson - Type=" + T.toString());

    JSONObject jsonObject = null;/*  w  ww.ja va  2s. com*/
    try {
        jsonObject = new JSONObject(json);
        code = jsonObject.getString("code");
        Log.d(TAG, "code=" + code);
        Log.d(TAG, "message=" + jsonObject.getString("message"));

    } catch (JSONException e) {
        Log.e(TAG, "error in getArrayFromJSON " + e.getMessage());
    }

    if ((jsonObject != null) && (code.trim().equals("0"))) {
        try {

            JSONObject objects = jsonObject.getJSONObject("objects");
            Iterator<?> keys = objects.keys();

            while (keys.hasNext()) {
                String key = (String) keys.next();
                Log.d(TAG, "key=" + key);
                if (objects.get(key) instanceof JSONObject) {
                    // Log.d(TAG,"obj="+objects.get(key).toString());
                    JSONObject o = (JSONObject) objects.get(key);
                    JSONObject fields = o.getJSONObject("fields");
                    Log.d(TAG, "fields=" + fields.toString());

                    Gson gson = new Gson();
                    String k[] = key.split(":");
                    int id = Integer.parseInt(k[2]);
                    T jf = gson.fromJson(fields.toString(), T);
                    if (jf instanceof CMDBObject) {
                        ((CMDBObject) jf).id = id;
                    }
                    list.add(jf);
                }
            }
            Log.d(TAG, "code=" + jsonObject.getString("code"));
            Log.d(TAG, "message=" + jsonObject.getString("message"));

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } // endif (jsonObject != null)

    return list;

}

From source file:org.evosuite.testcase.statements.ArrayStatement.java

/**
 * <p>//from   w  ww  . j a v a2s .co  m
 * determineDimensions
 * </p>
 * 
 * @param type
 *            a {@link java.lang.reflect.Type} object.
 * @return a int.
 */
public static int determineDimensions(java.lang.reflect.Type type) {
    String name = type.toString().replace("class", "").trim();
    int count = 0;
    for (int i = 0; i < name.length(); i++) {
        if (name.charAt(i) == '[') {
            count++;
        }
    }
    return count;
}

From source file:org.ormma.controller.OrmmaController.java

/**
 * Constructs an object from json via reflection
 *
 * @param json the json/*ww  w . jav  a  2 s .co  m*/
 * @param c the class to convert into
 * @return the instance constructed
 * @throws IllegalAccessException the illegal access exception
 * @throws InstantiationException the instantiation exception
 * @throws NumberFormatException the number format exception
 * @throws NullPointerException the null pointer exception
 */
protected static Object getFromJSON(JSONObject json, Class<?> c)
        throws IllegalAccessException, InstantiationException, NumberFormatException, NullPointerException {
    Field[] fields = null;
    fields = c.getDeclaredFields();
    Object obj = c.newInstance();

    for (int i = 0; i < fields.length; i++) {
        Field f = fields[i];
        String name = f.getName();
        String JSONName = name.replace('_', '-');
        Type type = f.getType();
        String typeStr = type.toString();
        try {
            if (typeStr.equals(INT_TYPE)) {
                String value = json.getString(JSONName).toLowerCase();
                int iVal = 0;
                if (value.startsWith("#")) {
                    iVal = Color.WHITE;
                    try {
                        if (value.startsWith("#0x")) {
                            iVal = Integer.decode(value.substring(1)).intValue();
                        } else {
                            iVal = Integer.parseInt(value.substring(1), 16);
                        }
                    } catch (NumberFormatException e) {
                        // TODO: handle exception
                    }
                } else {
                    iVal = Integer.parseInt(value);
                }
                f.set(obj, iVal);
            } else if (typeStr.equals(STRING_TYPE)) {
                String value = json.getString(JSONName);
                f.set(obj, value);
            } else if (typeStr.equals(BOOLEAN_TYPE)) {
                boolean value = json.getBoolean(JSONName);
                f.set(obj, value);
            } else if (typeStr.equals(FLOAT_TYPE)) {
                float value = Float.parseFloat(json.getString(JSONName));
                f.set(obj, value);
            } else if (typeStr.equals(NAVIGATION_TYPE)) {
                NavigationStringEnum value = NavigationStringEnum.fromString(json.getString(JSONName));
                f.set(obj, value);
            } else if (typeStr.equals(TRANSITION_TYPE)) {
                TransitionStringEnum value = TransitionStringEnum.fromString(json.getString(JSONName));
                f.set(obj, value);
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    return obj;
}

From source file:at.tuwien.ifs.somtoolbox.apps.SOMToolboxMain.java

/**
 * @param screenWidth the with of the screen
 * @param runnables {@link ArrayList} of available runnables.
 *//*  ww w . j  a  v  a 2  s . c  om*/
private static void printAvailableRunnables(int screenWidth,
        ArrayList<Class<? extends SOMToolboxApp>> runnables) {
    Collections.sort(runnables, SOMToolboxApp.TYPE_GROUPED_COMPARATOR);

    ArrayList<Class<? extends SOMToolboxApp>> runnableClassList = new ArrayList<Class<? extends SOMToolboxApp>>();
    ArrayList<String> runnableNamesList = new ArrayList<String>();
    ArrayList<String> runnableDeskrList = new ArrayList<String>();

    for (Class<? extends SOMToolboxApp> c : runnables) {
        try {
            // Ignore abstract classes and interfaces
            if (Modifier.isAbstract(c.getModifiers()) || Modifier.isInterface(c.getModifiers())) {
                continue;
            }
            runnableClassList.add(c);
            runnableNamesList.add(c.getSimpleName());

            String desk = null;
            try {
                Field f = c.getDeclaredField("DESCRIPTION");
                desk = (String) f.get(null);
            } catch (Exception e) {
            }

            if (desk != null) {
                runnableDeskrList.add(desk);
            } else {
                runnableDeskrList.add("");
            }
        } catch (SecurityException e) {
            // Should not happen - no Security
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
    }
    StringBuilder sb = new StringBuilder();
    String lineSep = System.getProperty("line.separator", "\n");

    int maxLen = StringUtils.getLongestStringLength(runnableNamesList);
    sb.append("Runnable classes:").append(lineSep);
    for (int i = 0; i < runnableNamesList.size(); i++) {
        final Type cType = Type.getType(runnableClassList.get(i));
        if (i == 0 || !cType.equals(Type.getType(runnableClassList.get(i - 1)))) {
            sb.append(String.format("-- %s %s%s", cType.toString(),
                    StringUtils.repeatString(screenWidth - (8 + cType.toString().length()), "-"), lineSep));
        }
        sb.append("    ");
        sb.append(runnableNamesList.get(i));
        sb.append(StringUtils.getSpaces(4 + maxLen - runnableNamesList.get(i).length())).append("- ");
        sb.append(runnableDeskrList.get(i));
        sb.append(lineSep);
    }
    System.out.println(StringUtils.wrap(sb.toString(), screenWidth, StringUtils.getSpaces(maxLen + 10), true));
}

From source file:org.springframework.web.method.support.InvocableHandlerMethod.java

private static String getClassName(Type type) {
    if (type == null) {
        return "";
    }//  www  .j  a  v  a  2  s. co m
    String className = type.toString();
    if (className.startsWith(TYPE_NAME_PREFIX)) {
        className = className.substring(TYPE_NAME_PREFIX.length());
    }
    return className;
}

From source file:com.wavemaker.json.type.reflect.ReflectTypeUtils.java

/**
 * Return the type name for the corresponding class and fields.
 * /*from  w w  w . ja  va 2 s. c  o m*/
 * @param klass Generally, the klass is sufficient to identify the class.
 * @param mapFields If klass is a Map type, this should be the generic parameters.
 * @return A String uniquely identifying this type.
 */
public static String getTypeName(Type type) {

    if (type instanceof Class) {
        return ((Class<?>) type).getName();
    } else if (type instanceof ParameterizedType) {
        return type.toString().replace(" ", "");
    } else {
        throw new WMRuntimeException(MessageResource.JSON_TYPE_UNKNOWNPARAMTYPE, type,
                type != null ? type.getClass() : null);
    }
}

From source file:com.almende.eve.protocol.jsonrpc.JSONRpc.java

/**
 * Get type description from a class. Returns for example "String" or
 * "List<String>".// w  w  w.ja v  a 2  s  .co m
 * 
 * @param c
 *            the c
 * @return the string
 */
private static String typeToString(final Type c) {
    String s = c.toString();

    // replace full namespaces to short names
    int point = s.lastIndexOf('.');
    while (point >= 0) {
        final int angle = s.lastIndexOf('<', point);
        final int space = s.lastIndexOf(' ', point);
        final int start = Math.max(angle, space);
        s = s.substring(0, start + 1) + s.substring(point + 1);
        point = s.lastIndexOf('.');
    }

    // remove modifiers like "class blabla" or "interface blabla"
    final int space = s.indexOf(' ');
    final int angle = s.indexOf('<', point);
    if (space >= 0 && (angle < 0 || angle > space)) {
        s = s.substring(space + 1);
    }

    return s;
}

From source file:org.apache.bval.jsr.ConstraintValidation.java

private static String stringForType(Type clazz) {
    if (clazz instanceof Class<?>) {
        return ((Class<?>) clazz).isArray() ? ((Class<?>) clazz).getComponentType().getName() + "[]"
                : ((Class<?>) clazz).getName();
    }//  w  w w . j a  v  a2  s. com
    return clazz.toString();
}