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:fr.mby.utils.spring.beans.factory.BasicProxywiredManager.java

/**
 * Find Bean Type to inject (not the generic type like Collection or Set).
 * /*  w  w  w . j a v a2 s.co  m*/
 * @param descriptor
 * @param autowiredBeanNames
 * @return
 */
protected Class<?> getBeanType(final DependencyDescriptor descriptor, final Set<String> autowiredBeanNames) {
    final Class<?> result;

    Type fieldType = null;

    final Field field = descriptor.getField();
    if (field == null) {
        // Annotation on the method
        final MethodParameter methodParameter = descriptor.getMethodParameter();
        if (methodParameter != null) {
            fieldType = methodParameter.getGenericParameterType();
        }
    } else {
        fieldType = field.getGenericType();
    }

    if (fieldType != null) {
        final Class<?> type = descriptor.getDependencyType();
        if (Collection.class.isAssignableFrom(type)) {
            final ParameterizedType parameterizedType = (ParameterizedType) fieldType;
            result = (Class<?>) parameterizedType.getActualTypeArguments()[0];
        } else if (Map.class.isAssignableFrom(type)) {
            final ParameterizedType parameterizedType = (ParameterizedType) fieldType;
            result = (Class<?>) parameterizedType.getActualTypeArguments()[1];
        } else if (type.isArray()) {
            // We can't do anything
            throw new IllegalStateException("You cannot use Proxywired annotation on an Array !");
        } else {
            result = type;
        }
    } else {
        throw new IllegalStateException("Unable to find the Bean type !");
    }

    return result;
}

From source file:com.wantscart.jade.core.RowMapperFactoryImpl.java

@Override
public RowMapper getRowMapper(Modifier modifier) {
    RowHandler rowHandler = modifier.getAnnotation(RowHandler.class);
    if (rowHandler != null) {
        if (rowHandler.rowMapper() != RowHandler.ByDefault.class) {
            try {
                RowMapper rowMapper = rowHandler.rowMapper().newInstance();
                if (logger.isInfoEnabled()) {
                    logger.info("using rowMapper " + rowMapper + " for " + modifier);
                }/*from w w w  . ja  v a 2  s .c  o m*/

                return rowMapper;
            } catch (Exception ex) {
                throw new BeanInstantiationException(rowHandler.rowMapper(), ex.getMessage(), ex);
            }
        }
    }
    //

    Class<?> returnClassType = modifier.getReturnType();
    Class<?> rowType = getRowType(modifier);

    // BUGFIX: SingleColumnRowMapper ?  Primitive Type 
    if (rowType.isPrimitive()) {
        rowType = ClassUtils.primitiveToWrapper(rowType);
    }

    // ?  RowMapper
    RowMapper rowMapper;

    // ?(?2Map)
    if (TypeUtils.isColumnType(rowType)) {
        if (returnClassType == Map.class) {
            rowMapper = new MapEntryColumnRowMapper(modifier, rowType);
        } else {
            rowMapper = new SingleColumnRowMapper(rowType);
        }
    }
    // Bean??????
    else {
        if (rowType == Map.class) {
            rowMapper = new ColumnMapRowMapper();
        } else if (rowType.isArray()) {
            rowMapper = new ArrayRowMapper(rowType);
        } else if ((rowType == List.class) || (rowType == Collection.class)) {
            rowMapper = new ListRowMapper(modifier);
        } else if (rowType == Set.class) {
            rowMapper = new SetRowMapper(modifier);
        } else {
            boolean checkColumns = (rowHandler == null) ? false : rowHandler.checkColumns();
            boolean checkProperties = (rowHandler == null) ? false : rowHandler.checkProperties();
            String key = rowType.getName() + "[checkColumns=" + checkColumns + "&checkProperties="
                    + checkProperties + "]";
            rowMapper = rowMappers.get(key);
            if (rowMapper == null) {
                rowMapper = new BeanPropertyRowMapper(rowType, checkColumns, checkProperties); // jade's BeanPropertyRowMapper here
                rowMappers.put(key, rowMapper);
            }
        }
        // DAOMaprowMapper?Map.Entry
        if (returnClassType == Map.class) {
            rowMapper = new MapEntryRowMapper(modifier, rowMapper);
        }
    }

    if (logger.isInfoEnabled()) {
        logger.info("using rowMapper " + rowMapper + " for " + modifier);
    }

    return rowMapper;
}

From source file:com.hc.wx.server.common.bytecode.ReflectUtils.java

private static Object getEmptyObject(Class<?> returnType, Map<Class<?>, Object> emptyInstances, int level) {
    if (level > 2)
        return null;
    if (returnType == null) {
        return null;
    } else if (returnType == boolean.class || returnType == Boolean.class) {
        return false;
    } else if (returnType == char.class || returnType == Character.class) {
        return '\0';
    } else if (returnType == byte.class || returnType == Byte.class) {
        return (byte) 0;
    } else if (returnType == short.class || returnType == Short.class) {
        return (short) 0;
    } else if (returnType == int.class || returnType == Integer.class) {
        return 0;
    } else if (returnType == long.class || returnType == Long.class) {
        return 0L;
    } else if (returnType == float.class || returnType == Float.class) {
        return 0F;
    } else if (returnType == double.class || returnType == Double.class) {
        return 0D;
    } else if (returnType.isArray()) {
        return Array.newInstance(returnType.getComponentType(), 0);
    } else if (returnType.isAssignableFrom(ArrayList.class)) {
        return new ArrayList<Object>(0);
    } else if (returnType.isAssignableFrom(HashSet.class)) {
        return new HashSet<Object>(0);
    } else if (returnType.isAssignableFrom(HashMap.class)) {
        return new HashMap<Object, Object>(0);
    } else if (String.class.equals(returnType)) {
        return "";
    } else if (!returnType.isInterface()) {
        try {//from  www.  java2 s .  c  om
            Object value = emptyInstances.get(returnType);
            if (value == null) {
                value = returnType.newInstance();
                emptyInstances.put(returnType, value);
            }
            Class<?> cls = value.getClass();
            while (cls != null && cls != Object.class) {
                Field[] fields = cls.getDeclaredFields();
                for (Field field : fields) {
                    Object property = getEmptyObject(field.getType(), emptyInstances, level + 1);
                    if (property != null) {
                        try {
                            if (!field.isAccessible()) {
                                field.setAccessible(true);
                            }
                            field.set(value, property);
                        } catch (Throwable e) {
                        }
                    }
                }
                cls = cls.getSuperclass();
            }
            return value;
        } catch (Throwable e) {
            return null;
        }
    } else {
        return null;
    }
}

From source file:net.paoding.rose.jade.rowmapper.DefaultRowMapperFactory.java

@Override
public RowMapper getRowMapper(StatementMetaData modifier) {
    RowHandler rowHandler = modifier.getAnnotation(RowHandler.class);
    if (rowHandler != null) {
        if (rowHandler.rowMapper() != RowHandler.ByDefault.class) {
            try {
                RowMapper rowMapper = rowHandler.rowMapper().newInstance();
                if (logger.isInfoEnabled()) {
                    logger.info("using rowMapper " + rowMapper + " for " + modifier);
                }//from   ww w  . j  a v a 2s . co m

                return rowMapper;
            } catch (Exception ex) {
                throw new BeanInstantiationException(rowHandler.rowMapper(), ex.getMessage(), ex);
            }
        }
    }
    //

    Class<?> returnClassType = modifier.getMethod().getReturnType();
    Class<?> rowType = getRowType(modifier);

    // BUGFIX: SingleColumnRowMapper ?  Primitive Type 
    if (rowType.isPrimitive()) {
        rowType = ClassUtils.primitiveToWrapper(rowType);
    }

    // ?  RowMapper
    RowMapper rowMapper;

    // ?(?2Map)
    if (TypeUtils.isColumnType(rowType)) {
        if (returnClassType == Map.class) {
            rowMapper = new MapEntryColumnRowMapper(modifier, rowType);
        } else {
            rowMapper = new SingleColumnRowMapper(rowType);
        }
    }
    // Bean??????
    else {
        if (rowType == Map.class) {
            rowMapper = new ColumnMapRowMapper();
        } else if (rowType.isArray()) {
            rowMapper = new ArrayRowMapper(rowType);
        } else if ((rowType == List.class) || (rowType == Collection.class)) {
            rowMapper = new ListRowMapper(modifier);
        } else if (rowType == Set.class) {
            rowMapper = new SetRowMapper(modifier);
        } else {
            boolean checkColumns = (rowHandler == null) ? true : rowHandler.checkColumns();
            boolean checkProperties = (rowHandler == null) ? false : rowHandler.checkProperties();
            String key = rowType.getName() + "[checkColumns=" + checkColumns + "&checkProperties="
                    + checkProperties + "]";
            rowMapper = rowMappers.get(key);
            if (rowMapper == null) {
                rowMapper = new BeanPropertyRowMapper(rowType, checkColumns, checkProperties); // jade's BeanPropertyRowMapper here
                rowMappers.put(key, rowMapper);
            }
        }
        // DAOMaprowMapper?Map.Entry
        if (returnClassType == Map.class) {
            rowMapper = new MapEntryRowMapper(modifier, rowMapper);
        }
    }

    if (logger.isInfoEnabled()) {
        logger.info("using rowMapper " + rowMapper + " for " + modifier);
    }

    return rowMapper;
}

From source file:com.gdevelop.gwt.syncrpc.SyncClientSerializationStreamReader.java

private Object deserializeImpl(Class<?> customSerializer, Class<?> instanceClass, Object instance)
        throws NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
        InvocationTargetException, SerializationException, ClassNotFoundException {

    if (customSerializer != null) {
        deserializeWithCustomFieldDeserializer(customSerializer, instanceClass, instance);
    } else if (instanceClass.isArray()) {
        instance = deserializeArray(instanceClass, instance);
    } else if (instanceClass.isEnum()) {
        // Enums are deserialized when they are instantiated
    } else {/*w w  w. j a v  a  2  s  .co  m*/
        deserializeClass(instanceClass, instance);
    }

    return instance;
}

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

@Override
public RowMapper getRowMapper(Modifier modifier) {
    RowHandler rowHandler = modifier.getAnnotation(RowHandler.class);
    if (rowHandler != null) {
        if (rowHandler.rowMapper() != RowHandler.ByDefault.class) {
            try {
                RowMapper rowMapper = rowHandler.rowMapper().newInstance();
                if (logger.isInfoEnabled()) {
                    logger.info("using rowMapper " + rowMapper + " for " + modifier);
                }/* www  .  ja  v  a  2  s.c  o m*/

                return rowMapper;
            } catch (Exception ex) {
                throw new BeanInstantiationException(rowHandler.rowMapper(), ex.getMessage(), ex);
            }
        }
    }
    //

    Class<?> returnClassType = modifier.getReturnType();
    Class<?> rowType = getRowType(modifier);

    // BUGFIX: SingleColumnRowMapper ?  Primitive Type 
    if (rowType.isPrimitive()) {
        rowType = ClassUtils.primitiveToWrapper(rowType);
    }

    // ?  RowMapper
    RowMapper rowMapper;

    // ?(?2Map)
    if (TypeUtils.isColumnType(rowType)) {
        if (returnClassType == Map.class) {
            rowMapper = new MapEntryColumnRowMapper(modifier, rowType);
        } else {
            rowMapper = new SingleColumnRowMapper(rowType);
        }
    }
    // Bean??????
    else {
        if (rowType == Map.class) {
            rowMapper = new ColumnMapRowMapper();
        } else if (rowType.isArray()) {
            rowMapper = new ArrayRowMapper(rowType);
        } else if ((rowType == List.class) || (rowType == Collection.class)) {
            rowMapper = new ListRowMapper(modifier);
        } else if (rowType == Set.class) {
            rowMapper = new SetRowMapper(modifier);
        } else {
            boolean checkColumns = (rowHandler == null) ? true : rowHandler.checkColumns();
            boolean checkProperties = (rowHandler == null) ? false : rowHandler.checkProperties();
            String key = rowType.getName() + "[checkColumns=" + checkColumns + "&checkProperties="
                    + checkProperties + "]";
            rowMapper = rowMappers.get(key);
            if (rowMapper == null) {
                rowMapper = new BeanPropertyRowMapper(rowType, checkColumns, checkProperties); // jade's BeanPropertyRowMapper here
                rowMappers.put(key, rowMapper);
            }
        }
        // DAOMaprowMapper?Map.Entry
        if (returnClassType == Map.class) {
            rowMapper = new MapEntryRowMapper(modifier, rowMapper);
        }
    }

    if (logger.isInfoEnabled()) {
        logger.info("using rowMapper " + rowMapper + " for " + modifier);
    }

    return rowMapper;
}

From source file:javadz.beanutils.LazyDynaBean.java

/**
 * Create a new Instance of an 'Indexed' Property
 * @param name The name of the property/*from   w  w  w  .  java2s.  com*/
 * @param type The class of the property
 * @return The new value
 */
protected Object createIndexedProperty(String name, Class type) {

    // Create the indexed object
    Object indexedProperty = null;

    if (type == null) {

        indexedProperty = defaultIndexedProperty(name);

    } else if (type.isArray()) {

        indexedProperty = Array.newInstance(type.getComponentType(), 0);

    } else if (List.class.isAssignableFrom(type)) {
        if (type.isInterface()) {
            indexedProperty = defaultIndexedProperty(name);
        } else {
            try {
                indexedProperty = type.newInstance();
            } catch (Exception ex) {
                throw new IllegalArgumentException("Error instantiating indexed property of type '"
                        + type.getName() + "' for '" + name + "' " + ex);
            }
        }
    } else {

        throw new IllegalArgumentException(
                "Non-indexed property of type '" + type.getName() + "' for '" + name + "'");
    }

    return indexedProperty;

}

From source file:com.trafficspaces.api.model.Resource.java

public void setJSONObject(JSONObject jsonObject) {
    Iterator itr = jsonObject.keys();
    while (itr.hasNext()) {
        String key = (String) itr.next();
        Object value = jsonObject.opt(key);

        try {/*  w  w w. j  a va  2 s .c o m*/
            Field field = this.getClass().getField(key);
            Class type = field.getType();

            int fieldModifiers = field.getModifiers();
            //System.out.println("key=" + key + ", name=" + field.getName() + ", value=" + value + ", type=" +type + ", componentType=" +type.getComponentType() + 
            //      ", ispublic="+Modifier.isPublic(fieldModifiers) + ", isstatic="+Modifier.isStatic(fieldModifiers) + ", isnative="+Modifier.isNative(fieldModifiers) +
            //      ", isprimitive="+type.isPrimitive() + ", isarray="+type.isArray() + ", isResource="+Resource.class.isAssignableFrom(type));

            if (type.isPrimitive()) {
                if (type.equals(int.class)) {
                    field.setInt(this, jsonObject.getInt(key));
                } else if (type.equals(double.class)) {
                    field.setDouble(this, jsonObject.getDouble(key));
                }
            } else if (type.isArray()) {
                JSONArray jsonArray = null;
                if (value instanceof JSONArray) {
                    jsonArray = (JSONArray) value;
                } else if (value instanceof JSONObject) {
                    JSONObject jsonSubObject = (JSONObject) value;
                    jsonArray = jsonSubObject.optJSONArray(key.substring(0, key.length() - 1));
                }
                if (jsonArray != null && jsonArray.length() > 0) {
                    Class componentType = type.getComponentType();
                    Object[] values = (Object[]) Array.newInstance(componentType, jsonArray.length());
                    for (int j = 0; j < jsonArray.length(); j++) {
                        Resource resource = (Resource) componentType.newInstance();
                        resource.setJSONObject(jsonArray.getJSONObject(j));
                        values[j] = resource;
                    }
                    field.set(this, values);
                }
            } else if (Resource.class.isAssignableFrom(type) && value instanceof JSONObject) {
                Resource resource = (Resource) type.newInstance();
                resource.setJSONObject((JSONObject) value);
                field.set(this, resource);
            } else if (type.equals(String.class) && value instanceof String) {
                field.set(this, (String) value);
            }
        } catch (NoSuchFieldException nsfe) {
            System.err.println("warning: field does not exist. key=" + key + ",value=" + value);
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("error: key=" + key + ",value=" + value + ", error=" + e.getMessage());
        }
    }
}

From source file:de.hasait.clap.impl.CLAPOptionNode.java

private CLAPOptionNode(final CLAP pCLAP, final Class<T> pResultClass, final Character pShortKey,
        final String pLongKey, final boolean pRequired, final Integer pArgCount, final Character pMultiArgSplit,
        final String pDescriptionNLSKey, final String pArgUsageNLSKey) {
    super(pCLAP);

    if (pShortKey != null && pShortKey == getCLAP().getShortOptPrefix()) {
        throw new IllegalArgumentException();
    }/*from   ww w .j  a  v  a  2 s  .  com*/
    if (pLongKey != null && pLongKey.contains(getCLAP().getLongOptEquals())) {
        throw new IllegalArgumentException();
    }

    if (pArgCount == null) {
        // autodetect using resultClass
        if (pResultClass.isArray() || Collection.class.isAssignableFrom(pResultClass)) {
            _argCount = CLAP.UNLIMITED_ARG_COUNT;
        } else if (pResultClass.equals(Boolean.class) || pResultClass.equals(Boolean.TYPE)) {
            _argCount = 0;
        } else {
            _argCount = 1;
        }
    } else {
        if (pArgCount < 0 && pArgCount != CLAP.UNLIMITED_ARG_COUNT) {
            throw new IllegalArgumentException();
        }

        if (pResultClass.isArray() || Collection.class.isAssignableFrom(pResultClass)) {
            if (pArgCount == 0) {
                throw new IllegalArgumentException();
            }
        } else if (pResultClass.equals(Boolean.class) || pResultClass.equals(Boolean.TYPE)) {
            if (pArgCount != 0 && pArgCount != 1) {
                throw new IllegalArgumentException();
            }
        } else {
            if (pArgCount != 1) {
                throw new IllegalArgumentException();
            }
        }

        _argCount = pArgCount;
    }

    if (pShortKey == null && pLongKey == null && _argCount == 0) {
        throw new IllegalArgumentException();
    }

    if (pResultClass.isArray()) {
        final Class<?> componentType = pResultClass.getComponentType();
        final CLAPConverter<?> converter = pCLAP.getConverter(componentType);
        _mapper = new Mapper<T>() {

            @Override
            public T transform(final String[] pStringValues) {
                final T result = (T) Array.newInstance(componentType, pStringValues.length);
                for (int i = 0; i < pStringValues.length; i++) {
                    try {
                        Array.set(result, i, converter.convert(pStringValues[i]));
                    } catch (final Exception e) {
                        throw new RuntimeException(e);
                    }
                }
                return result;
            }

        };
    } else if (Collection.class.isAssignableFrom(pResultClass)) {
        _mapper = null;
    } else {
        final CLAPConverter<? extends T> converter = pCLAP.getConverter(pResultClass);
        _mapper = new Mapper<T>() {

            @Override
            public T transform(final String[] pStringValues) {
                if (pStringValues.length == 0
                        && (pResultClass.equals(Boolean.class) || pResultClass.equals(Boolean.TYPE))) {
                    return (T) Boolean.TRUE;
                }
                return converter.convert(pStringValues[0]);
            }

        };
    }

    _shortKey = pShortKey;
    _longKey = pLongKey;
    _required = pRequired;
    _multiArgSplit = pMultiArgSplit;
    _descriptionNLSKey = pDescriptionNLSKey;
    _argUsageNLSKey = pArgUsageNLSKey;
    _resultClass = pResultClass;
}

From source file:com.link_intersystems.lang.reflect.Class2.java

/**
 * The string representation of a {@link Class2}.
 *
 * <ul>//  ww w . ja va2s  .co m
 * <li>
 * Types are represented by their canonical name. If a type is a
 * &quot;well-known&quot; type (all types in java.lang) the type's simple
 * name is used. E.g. String - java.util.List.</li>
 * <ul>
 * <li>
 * Arrays are represented by their type and appended by []. E.g. int[]
 * String[] java.beans.PropertyDescriptor[].</li>
 *
 * @param wellKnownPackages
 *            packages that are &quot;well known&quot; will not be printed
 *            in the string representation. E.g. if java.lang is defined as
 *            well known the Class2 that represents a String class will be
 *            printed just as &quot;String&quot; and not java.lang.String.
 *
 * @return a string representation of this {@link Class2};
 * @since 1.0.0.0
 */
public String toString(String... wellKnownPackages) {
    Assert.notNull("wellKnownPackages", wellKnownPackages);
    StringBuilder toStringBuilder = new StringBuilder();
    Class<?> clazz = getType();
    boolean isArray = clazz.isArray();
    if (isArray) {
        clazz = clazz.getComponentType();
    }
    Package clazzPackage = clazz.getPackage();
    String packageName = StringUtils.EMPTY;
    if (clazzPackage != null) {
        packageName = clazzPackage.getName();
    }

    boolean isWellKnownPackage = Arrays.binarySearch(wellKnownPackages, packageName) > -1;

    if (isWellKnownPackage) {
        toStringBuilder.append(clazz.getSimpleName());
    } else {
        toStringBuilder.append(clazz.getCanonicalName());
    }

    TypeVariable<?>[] typeParameters = clazz.getTypeParameters();
    String typeParametersToString = typeParametersToString(typeParameters);
    toStringBuilder.append(typeParametersToString);

    if (isArray) {
        toStringBuilder.append("[]");
    }
    return toStringBuilder.toString();
}