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:net.radai.beanz.util.ReflectionUtil.java

public static String prettyPrint(Type type) {
    if (type instanceof GenericArrayType) {
        GenericArrayType genericArrayType = (GenericArrayType) type;
        return prettyPrint(genericArrayType.getGenericComponentType()) + "[]";
    }//from ww w.  j a va  2  s  .c  o  m
    if (type instanceof WildcardType) {
        WildcardType wildcardType = (WildcardType) type;
        return wildcardType.toString();
    }
    if (type instanceof TypeVariable) {
        TypeVariable<?> typeVariable = (TypeVariable) type;
        return typeVariable.getName();
    }
    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        StringBuilder sb = new StringBuilder();
        sb.append(prettyPrint(parameterizedType.getRawType()));
        Type[] typeArguments = parameterizedType.getActualTypeArguments();
        if (typeArguments != null && typeArguments.length > 0) {
            sb.append("<");
            for (Type typeArgument : typeArguments) {
                sb.append(prettyPrint(typeArgument)).append(", ");
            }
            sb.delete(sb.length() - 2, sb.length()); // last ", "
            sb.append(">");
        }
        return sb.toString();
    }
    Class<?> clazz = (Class<?>) type;
    if (clazz.isArray()) {
        return prettyPrint(clazz.getComponentType()) + "[]";
    }
    return clazz.getSimpleName();
}

From source file:com.turbospaces.model.BO.java

/**
 * register the set of persistent classes and enrich kryo with some extract serialized related to persistent class.
 * //from w  w w.  jav  a 2 s . c  o m
 * @param kryo
 *            serialization provider
 * @param persistentEntities
 *            classes to register
 * @throws ClassNotFoundException
 *             re-throw conversion service
 * @throws NoSuchMethodException
 *             re-throw cglib's exception
 * @throws SecurityException
 *             re-throw cglib's exception
 * @throws IntrospectionException
 *             re-throw introspection exception
 */
public static void registerPersistentClasses(final DecoratedKryo kryo,
        final BasicPersistentEntity... persistentEntities)
        throws ClassNotFoundException, SecurityException, NoSuchMethodException, IntrospectionException {
    for (BasicPersistentEntity<?, ?> e : persistentEntities) {
        BO bo = new BO(e);
        bo.getOriginalPersistentEntity().doWithProperties(new PropertyHandler() {
            @Override
            public void doWithPersistentProperty(final PersistentProperty p) {
                Class type = p.getType();
                if (type.isArray() && !kryo.isTypeRegistered(type)) {
                    SingleDimensionArraySerializer serializer = new SingleDimensionArraySerializer(type, kryo);
                    kryo.register(type, serializer);
                } else if (type.isEnum() && !kryo.isTypeRegistered(type)) {
                    EnumSerializer enumSerializer = new EnumSerializer(type);
                    kryo.register(type, enumSerializer);
                }
            }
        });
        Class<?> arrayWrapperType = Class.forName("[L" + e.getType().getName() + ";");
        PropertiesSerializer serializer = new PropertiesSerializer(kryo, bo);
        SingleDimensionArraySerializer arraysSerializer = new SingleDimensionArraySerializer(arrayWrapperType,
                kryo);
        kryo.register(e.getType(), serializer);
        kryo.register(arrayWrapperType, arraysSerializer);
    }
}

From source file:com.jilk.ros.rosbridge.implementation.JSON.java

private static Object convertElementToJSON(Object elementObject) {
    Class elementClass = elementObject.getClass();
    Object resultObject;//from   w  w w. j a va  2s .com
    if (Message.isPrimitive(elementClass))
        resultObject = elementObject;
    else if (elementClass.isArray())
        resultObject = convertArrayToJSONArray(elementObject);
    else
        resultObject = convertObjectToJSONObject(elementObject);
    return resultObject;
}

From source file:com.britesnow.snow.util.ObjectUtil.java

public static final <T> T getValue(String valueStr, Class<T> cls, T defaultValue) {
    if (valueStr == null) {
        return defaultValue;
    } else {//  w  w w. j  a v  a2  s  . c  o  m
        try {
            if (cls == String.class) {
                return (T) valueStr;
            } else if (valueStr.length() > 0) {

                if (cls.isArray()) {
                    return getValue(new String[] { valueStr }, cls, defaultValue);
                } else if (cls == Integer.class) {
                    Integer value = numberFormat.parse(valueStr).intValue();
                    return (T) value;
                } else if (cls == Long.class) {
                    Long value = numberFormat.parse(valueStr).longValue();
                    return (T) value;
                } else if (cls == Float.class) {
                    Float value = numberFormat.parse(valueStr).floatValue();
                    return (T) value;
                } else if (cls == Double.class) {
                    Double value = numberFormat.parse(valueStr).doubleValue();
                    return (T) value;
                } else if (cls == Boolean.class) {
                    if ("true".equals(valueStr)) {
                        return (T) new Boolean(true);
                    } else {
                        return (T) new Boolean(false);
                    }
                } else if (cls.isEnum()) {
                    try {
                        return (T) Enum.valueOf((Class<Enum>) cls, valueStr);
                    } catch (IllegalArgumentException e) {
                        return defaultValue;
                    }
                } else if (cls == Date.class) {
                    SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATE_PATTERN);
                    return (T) new java.util.Date(sdf.parse(valueStr).getTime());
                }
            } else {
                return defaultValue;
            }
        } catch (Exception e) {
            return defaultValue;
        }
    }
    return defaultValue;
}

From source file:org.jfree.chart.demo.SymbolicXYPlotDemo.java

/**
 * Transform an primitive array to an object array.
 * //from   w ww  .ja va2s . c  o  m
 * @param arr
 *           the array.
 * @return an array.
 */
private static Object toArray(final Object arr) {

    if (arr == null) {
        return arr;
    }

    final Class cls = arr.getClass();
    if (!cls.isArray()) {
        return arr;
    }

    Class compType = cls.getComponentType();
    int dim = 1;
    while (!compType.isPrimitive()) {
        if (!compType.isArray()) {
            return arr;
        } else {
            dim++;
            compType = compType.getComponentType();
        }
    }

    final int[] length = new int[dim];
    length[0] = Array.getLength(arr);
    Object[] newarr = null;

    try {
        if (compType.equals(Integer.TYPE)) {
            newarr = (Object[]) Array.newInstance(Class.forName("java.lang.Integer"), length);
        } else if (compType.equals(Double.TYPE)) {
            newarr = (Object[]) Array.newInstance(Class.forName("java.lang.Double"), length);
        } else if (compType.equals(Long.TYPE)) {
            newarr = (Object[]) Array.newInstance(Class.forName("java.lang.Long"), length);
        } else if (compType.equals(Float.TYPE)) {
            newarr = (Object[]) Array.newInstance(Class.forName("java.lang.Float"), length);
        } else if (compType.equals(Short.TYPE)) {
            newarr = (Object[]) Array.newInstance(Class.forName("java.lang.Short"), length);
        } else if (compType.equals(Byte.TYPE)) {
            newarr = (Object[]) Array.newInstance(Class.forName("java.lang.Byte"), length);
        } else if (compType.equals(Character.TYPE)) {
            newarr = (Object[]) Array.newInstance(Class.forName("java.lang.Character"), length);
        } else if (compType.equals(Boolean.TYPE)) {
            newarr = (Object[]) Array.newInstance(Class.forName("java.lang.Boolean"), length);
        }
    } catch (ClassNotFoundException ex) {
        System.out.println(ex);
    }

    for (int i = 0; i < length[0]; i++) {
        if (dim != 1) {
            newarr[i] = toArray(Array.get(arr, i));
        } else {
            newarr[i] = Array.get(arr, i);
        }
    }
    return newarr;
}

From source file:com.laxser.blitz.lama.core.RowMapperFactoryImpl.java

private static Class<?> getRowType(Modifier modifier) {
    Class<?> returnClassType = modifier.getReturnType();
    if (Collection.class.isAssignableFrom(returnClassType)) {
        return getRowTypeFromCollectionType(modifier, returnClassType);
    } else if (Map.class == returnClassType) {
        return getRowTypeFromMapType(modifier, returnClassType);
    } else if (returnClassType.isArray() && returnClassType != byte[].class) {
        // , ??// ww w . ja  v a  2  s . co  m
        return returnClassType.getComponentType();
    }

    // DAO?
    return returnClassType;
}

From source file:com.springframework.beans.BeanUtils.java

/**
 * Check if the given type represents a "simple" property:
 * a primitive, a String or other CharSequence, a Number, a Date,
 * a URI, a URL, a Locale, a Class, or a corresponding array.
 * <p>Used to determine properties to check for a "simple" dependency-check.
 * @param clazz the type to check//from ww w. ja  va2 s . co  m
 * @return whether the given type represents a "simple" property
 */
public static boolean isSimpleProperty(Class<?> clazz) {
    Assert.notNull(clazz, "Class must not be null");
    return isSimpleValueType(clazz) || (clazz.isArray() && isSimpleValueType(clazz.getComponentType()));
}

From source file:cloudnet.util.Dumper.java

/**
 *
 * @param o// w ww  .jav  a 2s.  com
 * @param ctx
 * @return
 */
protected static String dump(Object o, DumpContext ctx) {
    if (o == null) {
        return "<null>";
    }

    ctx.callCount++;
    StringBuilder tabs = new StringBuilder();
    for (int k = 0; k < ctx.callCount; k++) {
        tabs.append("\t");
    }
    StringBuilder buffer = new StringBuilder();
    Class oClass = o.getClass();

    String oSimpleName = getSimpleNameWithoutArrayQualifier(oClass);

    if (ctx.ignoreList.get(oSimpleName + ":") != null) {
        return "<Ignored>";
    }

    if (oClass.isArray()) {
        buffer.append("\n");
        buffer.append(tabs.toString().substring(1));
        buffer.append("[\n");
        int rowCount = ctx.maxArrayElements == 0 ? Array.getLength(o)
                : FastMath.min(ctx.maxArrayElements, Array.getLength(o));
        for (int i = 0; i < rowCount; i++) {
            buffer.append(tabs.toString());
            try {
                Object value = Array.get(o, i);
                buffer.append(dumpValue(value, ctx));
            } catch (Exception e) {
                buffer.append(e.getMessage());
            }
            if (i < Array.getLength(o) - 1) {
                buffer.append(",");
            }
            buffer.append("\n");
        }
        if (rowCount < Array.getLength(o)) {
            buffer.append(tabs.toString());
            buffer.append(Array.getLength(o) - rowCount + " more array elements...");
            buffer.append("\n");
        }
        buffer.append(tabs.toString().substring(1));
        buffer.append("]");
    } else {
        buffer.append("\n");
        buffer.append(tabs.toString().substring(1));
        buffer.append("{\n");
        buffer.append(tabs.toString());
        buffer.append("hashCode: " + o.hashCode());
        buffer.append("\n");
        while (oClass != null && oClass != Object.class) {
            Field[] fields = oClass.getDeclaredFields();

            if (ctx.ignoreList.get(oClass.getSimpleName()) == null) {
                if (oClass != o.getClass()) {
                    buffer.append(tabs.toString().substring(1));
                    buffer.append("  Inherited from superclass " + oSimpleName + ":\n");
                }

                for (int i = 0; i < fields.length; i++) {

                    String fSimpleName = getSimpleNameWithoutArrayQualifier(fields[i].getType());
                    String fName = fields[i].getName();

                    fields[i].setAccessible(true);
                    buffer.append(tabs.toString());
                    buffer.append(fName + "(" + fSimpleName + ")");
                    buffer.append("=");

                    if (ctx.ignoreList.get(":" + fName) == null
                            && ctx.ignoreList.get(fSimpleName + ":" + fName) == null
                            && ctx.ignoreList.get(fSimpleName + ":") == null) {

                        try {
                            Object value = fields[i].get(o);
                            buffer.append(dumpValue(value, ctx));
                        } catch (Exception e) {
                            buffer.append(e.getMessage());
                        }
                        buffer.append("\n");
                    } else {
                        buffer.append("<Ignored>");
                        buffer.append("\n");
                    }
                }
                oClass = oClass.getSuperclass();
                oSimpleName = oClass.getSimpleName();
            } else {
                oClass = null;
                oSimpleName = "";
            }
        }
        buffer.append(tabs.toString().substring(1));
        buffer.append("}");
    }
    ctx.callCount--;
    return buffer.toString();
}

From source file:org.jfree.chart.demo.SampleXYSymbolicDataset.java

/**
 * Returns a clone of the array./* w ww  .j a  v  a2 s .  c  o m*/
 * 
 * @param arr
 *           the array.
 * @return a clone.
 */
private static Object cloneArray(final Object arr) {

    if (arr == null) {
        return arr;
    }

    final Class cls = arr.getClass();
    if (!cls.isArray()) {
        return arr;
    }

    final int length = Array.getLength(arr);
    final Object[] newarr = (Object[]) Array.newInstance(cls.getComponentType(), length);

    Object obj;

    for (int i = 0; i < length; i++) {
        obj = Array.get(arr, i);
        if (obj.getClass().isArray()) {
            newarr[i] = cloneArray(obj);
        } else {
            newarr[i] = obj;
        }
    }

    return newarr;
}

From source file:cn.afterturn.easypoi.util.PoiPublicUtil.java

/**
 * ?java/*from w w w. j a  v a  2s .co  m*/
 *
 * @param field
 * @return
 */
public static boolean isJavaClass(Field field) {
    Class<?> fieldType = field.getType();
    boolean isBaseClass = false;
    if (fieldType.isArray()) {
        isBaseClass = false;
    } else if (fieldType.isPrimitive() || fieldType.getPackage() == null
            || "java.lang".equals(fieldType.getPackage().getName())
            || "java.math".equals(fieldType.getPackage().getName())
            || "java.sql".equals(fieldType.getPackage().getName())
            || "java.time".equals(fieldType.getPackage().getName())
            || "java.util".equals(fieldType.getPackage().getName())) {
        isBaseClass = true;
    }
    return isBaseClass;
}