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.google.ratel.util.RatelUtils.java

public static boolean isPojo(Class type) {
    if (defaultValues.containsKey(type)) {
        return false;
    } else if (type.isArray()) {
        return false;
    }//from  w w  w  .  j  a v  a  2s.  co  m
    return true;
}

From source file:io.github.benas.jpopulator.impl.PopulatorImpl.java

/**
 * Method to populate a collection type which can be an array or a {@link Collection}.
 *
 * @param field The field in which the generated value will be set
 * @return an random collection matching type of field.
 * @throws IllegalAccessException    Thrown when the generated value cannot be set to the given field
 * @throws NoSuchMethodException     Thrown when there is no setter for the given field
 * @throws InvocationTargetException Thrown when the setter of the given field can not be invoked
 *///from  w ww. j av a  2 s . c o  m
private Object getRandomCollection(final Field field)
        throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
    Class<?> fieldType = field.getType();

    //Array type
    if (fieldType.isArray()) {
        return Array.newInstance(fieldType.getComponentType(), 0);
    }

    //Collection type
    Object collection = null;
    if (List.class.isAssignableFrom(fieldType)) {
        collection = Collections.emptyList();
    } else if (NavigableSet.class.isAssignableFrom(fieldType)) {
        collection = new TreeSet();
    } else if (SortedSet.class.isAssignableFrom(fieldType)) {
        collection = new TreeSet();
    } else if (Set.class.isAssignableFrom(fieldType)) {
        collection = Collections.emptySet();
    } else if (Deque.class.isAssignableFrom(fieldType)) {
        collection = new ArrayDeque();
    } else if (Queue.class.isAssignableFrom(fieldType)) {
        collection = new ArrayDeque();
    } else if (NavigableMap.class.isAssignableFrom(fieldType)) {
        collection = new TreeMap();
    } else if (SortedMap.class.isAssignableFrom(fieldType)) {
        collection = new TreeMap();
    } else if (Map.class.isAssignableFrom(fieldType)) {
        collection = Collections.emptyMap();
    } else if (Collection.class.isAssignableFrom(fieldType)) {
        collection = Collections.emptyList();
    }

    return collection;
}

From source file:com.jeeframework.util.classes.ClassUtils.java

/**
 * Return a descriptive name for the given object's type: usually simply
 * the class name, but component type class name + "[]" for arrays,
 * and an appended list of implemented interfaces for JDK proxies.
 * @param value the value to introspect//from   w w  w. jav  a  2  s  . co  m
 * @return the qualified name of the class
 */
public static String getDescriptiveType(Object value) {
    if (value == null) {
        return null;
    }
    Class clazz = value.getClass();
    if (Proxy.isProxyClass(clazz)) {
        StringBuffer buf = new StringBuffer(clazz.getName());
        buf.append(" implementing ");
        Class[] ifcs = clazz.getInterfaces();
        for (int i = 0; i < ifcs.length; i++) {
            buf.append(ifcs[i].getName());
            if (i < ifcs.length - 1) {
                buf.append(',');
            }
        }
        return buf.toString();
    } else if (clazz.isArray()) {
        return getQualifiedNameForArray(clazz);
    } else {
        return clazz.getName();
    }
}

From source file:org.guicerecipes.spring.support.AutowiredMemberProvider.java

@Override
protected Object provide(Autowired annotation, Member member, TypeLiteral<?> typeLiteral, Class<?> memberType,
        Annotation[] annotations) {
    Predicate<Binding> filter = createQualifierFilter(member, annotations);

    Class<?> type = typeLiteral.getRawType();
    if (type.isArray()) {
        return provideArrayValue(member, typeLiteral, memberType, filter);
    } else if (Collection.class.isAssignableFrom(type)) {
        Collection collection = createCollection(type);
        return provideCollectionValues(collection, member, typeLiteral, filter);
    } else if (Map.class.isAssignableFrom(type)) {
        Map map = createMap(type);
        return provideMapValues(map, member, typeLiteral, filter);
    } else {/*w w  w.java 2s  .c o m*/
        return provideSingleValue(member, type, annotation, filter);
    }
}

From source file:com.nginious.http.serialize.JsonDeserializerCreator.java

/**
 * Creates a JSON deserializer for the specified bean class unless a deserializer has already
 * been created. Created deserializers are cached and returned on subsequent calls to this method.
 * // w w w  . ja v  a  2s.  c o  m
 * @param <T> class type for bean
 * @param beanClazz bean class for which a deserializer should be created
 * @return the created deserializer
 * @throws SerializerFactoryException if unable to create deserializer or class is not a bean
 */
@SuppressWarnings("unchecked")
protected <T> JsonDeserializer<T> create(Class<T> beanClazz) throws SerializerFactoryException {
    JsonDeserializer<T> deserializer = (JsonDeserializer<T>) deserializers.get(beanClazz);

    if (deserializer != null) {
        return deserializer;
    }

    try {
        synchronized (this) {
            deserializer = (JsonDeserializer<T>) deserializers.get(beanClazz);

            if (deserializer != null) {
                return deserializer;
            }

            checkDeserializability(beanClazz, "json");
            String intBeanClazzName = Serialization.createInternalClassName(beanClazz);
            Method[] methods = beanClazz.getMethods();

            String intDeserializerClazzName = new StringBuffer(intBeanClazzName).append("JsonDeserializer")
                    .toString();

            // Create class
            ClassWriter writer = new ClassWriter(0);
            String signature = Serialization
                    .createClassSignature("com/nginious/http/serialize/JsonDeserializer", intBeanClazzName);
            writer.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, intDeserializerClazzName, signature,
                    "com/nginious/http/serialize/JsonDeserializer", null);

            // Create constructor
            Serialization.createConstructor(writer, "com/nginious/http/serialize/JsonDeserializer");

            // Create deserialize method
            MethodVisitor visitor = createDeserializeMethod(writer, intBeanClazzName);

            for (Method method : methods) {
                Serializable info = method.getAnnotation(Serializable.class);
                boolean canDeserialize = info == null
                        || (info != null && info.deserialize() && info.types().indexOf("json") > -1);

                if (canDeserialize && method.getName().startsWith("set")
                        && method.getReturnType().equals(void.class)
                        && method.getParameterTypes().length == 1) {
                    Class<?>[] parameterTypes = method.getParameterTypes();
                    Class<?> parameterType = parameterTypes[0];

                    if (parameterType.isArray()) {
                        Class<?> arrayType = parameterType.getComponentType();

                        if (arrayType.equals(boolean.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeBooleanArray", "[Z", "[Z", intBeanClazzName, method.getName());
                        } else if (arrayType.equals(double.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeDoubleArray", "[D", "[D", intBeanClazzName, method.getName());
                        } else if (arrayType.equals(float.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeFloatArray", "[F", "[F", intBeanClazzName, method.getName());
                        } else if (arrayType.equals(int.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeIntArray", "[I", "[I", intBeanClazzName, method.getName());
                        } else if (arrayType.equals(long.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeLongArray", "[J", "[J", intBeanClazzName, method.getName());
                        } else if (arrayType.equals(short.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeShortArray", "[S", "[S", intBeanClazzName, method.getName());
                        } else if (arrayType.equals(String.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeStringArray", "[Ljava/lang/String;", "[Ljava/lang/String;",
                                    intBeanClazzName, method.getName());
                        }
                    } else if (parameterType.isPrimitive()) {
                        if (parameterType.equals(boolean.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeBoolean", "Z", "Z", intBeanClazzName, method.getName());
                        } else if (parameterType.equals(double.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeDouble", "D", "D", intBeanClazzName, method.getName());
                        } else if (parameterType.equals(float.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeFloat", "F", "F", intBeanClazzName, method.getName());
                        } else if (parameterType.equals(int.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeInt", "I", "I", intBeanClazzName, method.getName());
                        } else if (parameterType.equals(long.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeLong", "J", "J", intBeanClazzName, method.getName());
                        } else if (parameterType.equals(short.class)) {
                            createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                    "deserializeShort", "S", "S", intBeanClazzName, method.getName());
                        }
                    } else if (parameterType.equals(Calendar.class)) {
                        createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                "deserializeCalendar", "Ljava/util/Calendar;", "Ljava/util/Calendar;",
                                intBeanClazzName, method.getName());
                    } else if (parameterType.equals(Date.class)) {
                        createPropertyDeserializationCode(visitor, intDeserializerClazzName, "deserializeDate",
                                "Ljava/util/Date;", "Ljava/util/Date;", intBeanClazzName, method.getName());
                    } else if (parameterType.equals(String.class)) {
                        createPropertyDeserializationCode(visitor, intDeserializerClazzName,
                                "deserializeString", "Ljava/lang/String;", "Ljava/lang/String;",
                                intBeanClazzName, method.getName());
                    }
                }
            }

            visitor.visitVarInsn(Opcodes.ALOAD, 3);
            visitor.visitInsn(Opcodes.ARETURN);
            visitor.visitMaxs(5, 4);
            visitor.visitEnd();

            writer.visitEnd();
            byte[] clazzBytes = writer.toByteArray();
            ClassLoader controllerLoader = null;

            if (classLoader.hasLoaded(beanClazz)) {
                controllerLoader = beanClazz.getClassLoader();
            } else {
                controllerLoader = this.classLoader;
            }

            Class<?> clazz = Serialization.loadClass(controllerLoader,
                    intDeserializerClazzName.replace('/', '.'), clazzBytes);
            deserializer = (JsonDeserializer<T>) clazz.newInstance();
            deserializers.put(beanClazz, deserializer);
            return deserializer;
        }
    } catch (IllegalAccessException e) {
        throw new SerializerFactoryException(e);
    } catch (InstantiationException e) {
        throw new SerializerFactoryException(e);
    }
}

From source file:net.sf.morph.util.TestUtils.java

/**
 * Return a "not-same" instance./*from   www  . java 2 s.  c  om*/
 * @param type
 * @param o
 * @return
 */
public static Object getDifferentInstance(Class type, Object o) {
    if (type == null) {
        throw new IllegalArgumentException("Non-null type must be specified");
    }
    if (type.isPrimitive()) {
        type = ClassUtils.getPrimitiveWrapper(type);
    }
    if (o != null && !type.isInstance(o)) {
        throw new IllegalArgumentException("Negative example object should be of type " + type);
    }
    if (type == Number.class) {
        type = Integer.class;
    }
    if (Number.class.isAssignableFrom(type)) {
        byte b = (byte) (o == null ? 0 : ((Number) o).byteValue() + 1);
        try {
            return type.getConstructor(ONE_STRING).newInstance(new Object[] { Byte.toString(b) });
        } catch (Exception e) {
            throw e instanceof RuntimeException ? (RuntimeException) e : new NestableRuntimeException(e);
        }
    }
    if (type == Character.class) {
        char c = (char) (o == null ? 0 : ((Character) o).charValue() + 1);
        return new Character(c);
    }
    if (type == Boolean.class) {
        return o != null && ((Boolean) o).booleanValue() ? Boolean.FALSE : Boolean.TRUE;
    }
    if (type.isArray()) {
        return Array.newInstance(type.getComponentType(), 0);
    }
    if (type == Class.class) {
        return o == Object.class ? Class.class : Object.class;
    }
    return ClassUtils.newInstance(convertCommonInterfaces(type));
}

From source file:com.jaspersoft.jasperserver.remote.utils.RepositoryHelper.java

protected static Object getMultiParameterValues(JRParameter parameter, Collection values) {
    Object parameterValue;/*  ww  w .j  a v a2  s  .c om*/
    Class parameterType = parameter.getValueClass();
    if (parameterType.equals(Object.class) || parameterType.equals(Collection.class)
            || parameterType.equals(Set.class) || parameterType.equals(List.class)) {
        Collection paramValues;
        if (parameterType.equals(List.class)) {
            //if the parameter type is list, use a list
            paramValues = new ArrayList(values.size());
        } else {
            //else use an ordered set
            paramValues = new ListOrderedSet();
        }

        Class componentType = parameter.getNestedType();
        for (Iterator it = values.iterator(); it.hasNext();) {
            Object val = (Object) it.next();
            Object paramValue;
            if (componentType == null || !(val instanceof String)) {
                //no conversion if no nested type set for the parameter
                paramValue = val;
            } else {
                paramValue = stringToValue((String) val, componentType);
            }
            paramValues.add(paramValue);
        }
        parameterValue = paramValues;
    } else if (parameterType.isArray()) {
        Class componentType = parameterType.getComponentType();
        parameterValue = Array.newInstance(componentType, values.size());
        int idx = 0;
        for (Iterator iter = values.iterator(); iter.hasNext(); ++idx) {
            Object val = iter.next();
            Object paramValue;
            if (val instanceof String) {
                paramValue = stringToValue((String) val, componentType);
            } else {
                paramValue = val;
            }
            Array.set(parameterValue, idx, paramValue);
        }
    } else {
        parameterValue = values;
    }
    return parameterValue;
}

From source file:com.github.mybatis.spring.MapperFactoryBean.java

/**
 * ??/*from ww  w  .  ja v a2s.  co m*/
 *
 * @param args ?
 * @return ?
 */
protected String getParameters(Object[] args) {
    if (args == null)
        return "";
    StringBuilder sbd = new StringBuilder();
    if (args.length > 0) {
        for (Object i : args) {
            if (i == null) {
                sbd.append("null");
            } else {
                Class clz = i.getClass();
                if (isPrimitive(clz)) {
                    sbd.append(evalPrimitive(i));
                } else if (clz.isArray()) {
                    evalArray(i, sbd);
                } else if (Collection.class.isAssignableFrom(clz)) {
                    Object[] arr = ((Collection<?>) i).toArray();
                    evalArray(arr, sbd);
                } else if (i instanceof Date) {
                    sbd.append('"').append(formatYmdHis(((Date) i))).append('"');
                } else if (i instanceof IdEntity) {
                    sbd.append(i.getClass().getSimpleName()).append("[id=").append(((IdEntity) i).getId())
                            .append(']');
                } else {
                    sbd.append(clz.getSimpleName()).append(":OBJ");
                }
            }
            sbd.append(',');
        }
        sbd.setLength(sbd.length() - 1);
    }
    return sbd.toString();
}

From source file:org.nabucco.alfresco.enhScriptEnv.common.script.converter.rhino.NativeArrayConverter.java

/**
 * {@inheritDoc}/* w w  w . jav a2s . com*/
 */
@Override
public boolean canConvertValueForJava(final Object value, final ValueConverter globalDelegate,
        final Class<?> expectedClass) {
    boolean canConvert = value instanceof NativeArray;

    if (canConvert) {
        if (expectedClass.isAssignableFrom(List.class) || expectedClass.isArray()) {
            final NativeArray arr = (NativeArray) value;
            final Object[] ids = arr.getIds();
            canConvert = this.isArray(ids);

            if (canConvert) {
                final Class<?> expectedComponentClass = expectedClass.isArray()
                        ? expectedClass.getComponentType()
                        : Object.class;
                for (int idx = 0; idx < ids.length && canConvert; idx++) {
                    if (ids[idx] instanceof Integer) {
                        final Object element = arr.get(((Integer) ids[idx]).intValue(), arr);
                        canConvert = canConvert
                                && globalDelegate.canConvertValueForJava(element, expectedComponentClass);
                    }
                }
            }
        } else {
            canConvert = expectedClass.isAssignableFrom(Map.class);

            if (canConvert) {
                final NativeArray arr = (NativeArray) value;
                final Object[] ids = arr.getIds();
                for (final Object propId : ids) {
                    final Object val = arr.get(propId.toString(), arr);

                    canConvert = canConvert && globalDelegate.canConvertValueForJava(propId)
                            && globalDelegate.canConvertValueForJava(val);

                    if (!canConvert) {
                        break;
                    }
                }
            }
        }
    }
    return canConvert;
}