Example usage for java.lang Class isArray

List of usage examples for java.lang Class isArray

Introduction

In this page you can find the example usage for java.lang Class isArray.

Prototype

@HotSpotIntrinsicCandidate
public native boolean isArray();

Source Link

Document

Determines if this Class object represents an array class.

Usage

From source file:com.gzj.tulip.jade.rowmapper.DefaultRowMapperFactory.java

private static Class<?> getRowType(StatementMetaData statementMetaData) {
    Class<?> returnClassType = statementMetaData.getReturnType();
    if (Collection.class.isAssignableFrom(returnClassType)//
            || Iterable.class == returnClassType // 
            || Iterator.class == returnClassType) {
        return getRowTypeFromCollectionType(statementMetaData, returnClassType);
    } ///*  w ww .j a  v a 2s  .  com*/
    else if (Map.class.isAssignableFrom(returnClassType)) {
        return getRowTypeFromMapType(statementMetaData, returnClassType);
    }
    //
    else if (returnClassType.isArray() && returnClassType != byte[].class) {
        // , ??
        return returnClassType.getComponentType();
    }

    // DAO?
    return returnClassType;
}

From source file:ReflectUtils.java

/**
 * /*from   w  ww.  j a va2s.c  o m*/
 * @param theClass
 * 
 * @return The full name of the Java package that contains the given class, or
 *         null if the class is not in a package.
 * 
 */
public static String getPackageName(Class theClass) {
    //
    // NOTE: Using the Package would be the easiest way to get this
    // data, but the ClassLoader is not required to provide it. Thus,
    // we use the more reliable method of parsing the class name.
    //

    //
    // arrays will have the [ as part of their name - no good
    //
    if (theClass.isArray())
        theClass = getClassFromArrayClass(theClass);

    return getPackageName(theClass.getName());
}

From source file:cross.applicationContext.ReflectionApplicationContextGenerator.java

/**
 * Checks and adds type information for the given method, class and object
 * to the properties list./*from  ww  w .  j  ava 2s .  co  m*/
 *
 * @param method the method to check for a property
 * @param javaClass the class to check for settable property
 * @param obj the stub / default object of type javaClass
 * @param properties the properties list
 */
public static void checkMutableProperties(Method method, Class<?> javaClass, Object obj,
        List<ObjectProperty> properties) {
    if (method.getName().startsWith("get") || method.getName().startsWith("is")) {
        String methodBaseName = method.getName().startsWith("get") ? method.getName().substring(3)
                : method.getName().substring(2);
        try {
            //check for corresponding setter
            if (javaClass.getMethod("set" + methodBaseName, method.getReturnType()) != null) {
                String propertyName = toLowerCaseName(methodBaseName);
                //                            System.out.print("Handling property " + propertyName);
                ObjectProperty p = new ObjectProperty();
                Class<?> returnType = method.getReturnType();
                if (returnType.isArray()) {
                    p.type = "Array";
                } else {
                    p.type = returnType.getCanonicalName();
                }
                Class<?> genericReturnType = getGenericMethodReturnType(method);
                p.genericType = genericReturnType.getCanonicalName();
                p.name = propertyName;

                Object methodObject = null;
                try {
                    methodObject = method.invoke(obj);
                } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
                    log.warn(ex.getLocalizedMessage());
                }
                String value = null;
                if (methodObject == null) {
                    value = "";
                } else {
                    if (methodObject.getClass().isArray()) {
                        value = methodObject.getClass().getComponentType().getCanonicalName();
                    } else {
                        value = methodObject.toString();
                    }
                }
                if (value == null) {
                    value = "";
                } else {
                    value = value.trim();
                }
                if (value.contains("\"")) {
                    //remove the quotes that surround Strings
                    value = value.replaceAll("\"", "");
                } else if (value.contains("'")) {
                    //remove the quotes that surround characters
                    value = value.replaceAll("'", "");
                } else if (value.endsWith("d") || value.endsWith("D") || value.endsWith("f")
                        || value.endsWith("F") || value.endsWith("l") || value.endsWith("L")) {
                    //remove the "double", "float", or "long" letters if they are there
                    value = value.substring(0, value.length() - 1);
                }
                p.value = value;

                System.out.println(" value: " + p.value + " type: " + p.type);
                properties.add(p);
            }
        } catch (NoSuchMethodException ex) {
            log.info("Ignoring read-only property {}", methodBaseName);
            //Logger.getLogger(ReflectionApplicationContextGenerator.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            log.warn(ex.getLocalizedMessage());
        }
    }
}

From source file:MethodHashing.java

static String getTypeString(Class cl) {
    if (cl == Byte.TYPE) {
        return "B";
    } else if (cl == Character.TYPE) {
        return "C";
    } else if (cl == Double.TYPE) {
        return "D";
    } else if (cl == Float.TYPE) {
        return "F";
    } else if (cl == Integer.TYPE) {
        return "I";
    } else if (cl == Long.TYPE) {
        return "J";
    } else if (cl == Short.TYPE) {
        return "S";
    } else if (cl == Boolean.TYPE) {
        return "Z";
    } else if (cl == Void.TYPE) {
        return "V";
    } else if (cl.isArray()) {
        return "[" + getTypeString(cl.getComponentType());
    } else {//from w  w w. j a v a  2 s. c  om
        return "L" + cl.getName().replace('.', '/') + ";";
    }
}

From source file:jp.co.ctc_g.jfw.core.util.Beans.java

@SuppressWarnings("unchecked")
private static Object readElementAt(int index, Object arrayOrList) {
    Args.checkPositiveOrZero(index);/*  w  ww .jav  a 2  s.  co m*/
    Object contained = null;
    Class<?> type = arrayOrList.getClass();
    if (type.isArray()) {
        Object[] array = (Object[]) arrayOrList;
        contained = readElementFromArrayAt(index, array);
    } else if (List.class.isAssignableFrom(type)) {
        List<Object> container = (List<Object>) arrayOrList;
        contained = readElementFromListAt(index, container);
    } else {
        if (L.isDebugEnabled()) {
            Map<String, Object> replace = new HashMap<String, Object>(2);
            replace.put("index", index);
            L.debug(Strings.substitute(R.getString("D-UTIL#0003"), replace));
        }
    }
    return contained;
}

From source file:jp.co.ctc_g.jfw.core.util.Beans.java

@SuppressWarnings("unchecked")
private static boolean writeElementAt(int index, Object arrayOrList, Object newValue) {
    Args.checkPositiveOrZero(index);/*from ww  w .j av a  2 s. c  om*/
    Class<?> type = arrayOrList.getClass();
    if (type.isArray()) {
        Object[] array = (Object[]) arrayOrList;
        return writeElementToArrayAt(index, array, newValue);
    } else if (List.class.isAssignableFrom(type)) {
        List<Object> container = (List<Object>) arrayOrList;
        return writeElementToListAt(index, container, newValue);
    } else {
        if (L.isDebugEnabled()) {
            Map<String, Object> replace = new HashMap<String, Object>(2);
            replace.put("index", index);
            replace.put("value", newValue);
            L.debug(Strings.substitute(R.getString("D-UTIL#0004"), replace));
        }
        return false;
    }
}

From source file:jp.go.nict.langrid.servicecontainer.handler.jsonrpc.servlet.JsonRpcServlet.java

private static String prettyName(Class<?> clazz) {
    StringBuilder postfix = new StringBuilder();
    if (Proxy.isProxyClass(clazz)) {
        return "<em>Proxy Implementation (detailed information unavailable)</em>";
    }/*  w  w w . j av  a 2s.c  o  m*/
    if (clazz.isArray()) {
        clazz = clazz.getComponentType();
        appendGenericsInfo(clazz, postfix);
        postfix.append("[]");
    } else {
        appendGenericsInfo(clazz, postfix);
    }
    String n = clazz.getName();
    String sn = clazz.getSimpleName();
    return "<span class=\"info\">" + sn + postfix + "<span>" + n + postfix + "</span></span>";
}

From source file:de.alpharogroup.lang.ClassExtensions.java

/**
 * Gets the {@link ClassType} from the given class.
 *
 * @param clazz//from w w  w.  j a  v a2  s  . c o  m
 *            The class.
 * @return the {@link ClassType} from the given class.
 */
public static ClassType getClassType(final Class<?> clazz) {
    if (clazz.isArray()) {
        return ClassType.ARRAY;
    }
    if (isCollection(clazz)) {
        return ClassType.COLLECTION;
    }
    if (isMap(clazz)) {
        return ClassType.MAP;
    }
    if (clazz.isLocalClass()) {
        return ClassType.LOCAL;
    }
    if (clazz.isMemberClass()) {
        return ClassType.MEMBER;
    }
    if (clazz.isPrimitive()) {
        return ClassType.PRIMITIVE;
    }
    if (clazz.isAnnotation()) {
        return ClassType.ANNOTATION;
    }
    if (clazz.isEnum()) {
        return ClassType.ENUM;
    }
    if (clazz.isInterface()) {
        return ClassType.INTERFACE;
    }
    if (clazz.isSynthetic()) {
        return ClassType.SYNTHETIC;
    }
    if (clazz.isAnonymousClass()) {
        return ClassType.ANONYMOUS;
    }
    return ClassType.DEFAULT;
}

From source file:com.initialxy.cordova.themeablebrowser.ThemeableBrowserUnmarshaller.java

/**
 * Given an object extracted from JSONObject field, convert it to an
 * appropriate object with type appropriate for given type so that it can be
 * assigned to the associated field of the ummarshalled object. eg.
 * JSONObject value from a JSONObject field probably needs to be
 * unmarshalled to a class instance. Double from JSONObject may need to be
 * converted to Float. etc./*from   ww w. j ava  2  s.c  o m*/
 *
 * @param val Value extracted from JSONObject field.
 * @param genericType Type to convert to. Must be generic type. ie. From
 *                    field.getGenericType().
 * @return Object of the given type so it can be assinged to field with
 * field.set().
 * @throws com.initialxy.cordova.themeablebrowser.ThemeableBrowserUnmarshaller.TypeMismatchException
 */
private static Object valToType(Object val, Type genericType) {
    Object result = null;
    boolean isArray = false;

    Class<?> rawType = null;
    if (genericType instanceof ParameterizedType) {
        rawType = (Class<?>) ((ParameterizedType) genericType).getRawType();
    } else if (genericType instanceof GenericArrayType) {
        rawType = List.class;
        isArray = true;
    } else {
        rawType = (Class<?>) genericType;
    }

    isArray = isArray || rawType.isArray();

    if (val != null && val != JSONObject.NULL) {
        if (rawType.isAssignableFrom(String.class)) {
            if (val instanceof String) {
                result = val;
            } else {
                throw new TypeMismatchException(rawType, val.getClass());
            }
        } else if (isPrimitive(rawType)) {
            result = convertToPrimitiveFieldObj(val, rawType);
        } else if (isArray || rawType.isAssignableFrom(List.class)) {
            if (val instanceof JSONArray) {
                Type itemType = getListItemType(genericType);
                result = JSONToList((JSONArray) val, itemType);

                if (isArray) {
                    List<?> list = (List<?>) result;

                    Class<?> itemClass = null;
                    if (itemType instanceof ParameterizedType) {
                        itemClass = (Class<?>) ((ParameterizedType) itemType).getRawType();
                    } else {
                        itemClass = (Class<?>) itemType;
                    }

                    result = Array.newInstance(itemClass, list.size());
                    int cnt = 0;
                    for (Object i : list) {
                        Array.set(result, cnt, i);
                        cnt += 1;
                    }
                }
            } else {
                throw new TypeMismatchException(JSONArray.class, val.getClass());
            }
        } else if (val instanceof JSONObject) {
            result = JSONToObj((JSONObject) val, rawType);
        }
    }

    return result;
}

From source file:com.xwtec.xwserver.util.json.util.JSONUtils.java

/**
 * Returns the inner-most component type of an Array.
 *//* w w w  .  j  a  va2 s.c  o  m*/
public static Class getInnerComponentType(Class type) {
    if (!type.isArray()) {
        return type;
    }
    return getInnerComponentType(type.getComponentType());
}