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:org.crazydog.util.spring.ClassUtils.java

/**
 * Return the qualified name of the given class: usually simply
 * the class name, but component type class name + "[]" for arrays.
 * @param clazz the class/*  w  ww  .  j a  v  a 2 s.  co  m*/
 * @return the qualified name of the class
 */
public static String getQualifiedName(Class<?> clazz) {
    org.springframework.util.Assert.notNull(clazz, "Class must not be null");
    if (clazz.isArray()) {
        return getQualifiedNameForArray(clazz);
    } else {
        return clazz.getName();
    }
}

From source file:org.crazydog.util.spring.ClassUtils.java

/**
 * Check if the given class represents an array of primitives,
 * i.e. boolean, byte, char, short, int, long, float, or double.
 * @param clazz the class to check//  ww  w . j av a2 s .  c  om
 * @return whether the given class is a primitive array class
 */
public static boolean isPrimitiveArray(Class<?> clazz) {
    org.springframework.util.Assert.notNull(clazz, "Class must not be null");
    return (clazz.isArray() && clazz.getComponentType().isPrimitive());
}

From source file:org.crazydog.util.spring.ClassUtils.java

/**
 * Check if the given class represents an array of primitive wrappers,
 * i.e. Boolean, Byte, Character, Short, Integer, Long, Float, or Double.
 * @param clazz the class to check// w w  w  .  java  2s .c o  m
 * @return whether the given class is a primitive wrapper array class
 */
public static boolean isPrimitiveWrapperArray(Class<?> clazz) {
    org.springframework.util.Assert.notNull(clazz, "Class must not be null");
    return (clazz.isArray() && isPrimitiveWrapper(clazz.getComponentType()));
}

From source file:FormatTest.java

/**
 * Adds a row to the main panel./*ww w .java 2 s  .  c  o  m*/
 * @param labelText the label of the field
 * @param field the sample field
 */
public void addRow(String labelText, final JFormattedTextField field) {
    mainPanel.add(new JLabel(labelText));
    mainPanel.add(field);
    final JLabel valueLabel = new JLabel();
    mainPanel.add(valueLabel);
    okButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            Object value = field.getValue();
            Class<?> cl = value.getClass();
            String text = null;
            if (cl.isArray()) {
                if (cl.getComponentType().isPrimitive()) {
                    try {
                        text = Arrays.class.getMethod("toString", cl).invoke(null, value).toString();
                    } catch (Exception ex) {
                        // ignore reflection exceptions
                    }
                } else
                    text = Arrays.toString((Object[]) value);
            } else
                text = value.toString();
            valueLabel.setText(text);
        }
    });
}

From source file:com.opensymphony.able.introspect.PropertyInfo.java

/**
 * Returns the component type of the property - e.g. ignoring the
 * cardinality (array or List)./* w  w w  . j  a v a2 s . c o m*/
 */
public Class getPropertyComponentType() {
    Class<?> propertyType = descriptor.getPropertyType();

    if (propertyType.isArray()) {
        return propertyType.getComponentType();
    }
    if (Collection.class.isAssignableFrom(propertyType)) {
        if (descriptor.getReadMethod() != null) {
            ParameterizedType genericSuperclass = (ParameterizedType) descriptor.getReadMethod()
                    .getGenericReturnType();
            Type[] typeArguments = genericSuperclass.getActualTypeArguments();
            return (Class) typeArguments[0];
        } else {
            // no idea what the type is
            // TODO do we have an annotation?
            return Object.class;
        }
    }
    return propertyType;
}

From source file:com.twosigma.beaker.kdb.KdbShell.java

/**
 * Try and convert an object to a List.// w  w w  .  j  a  v  a 2s .  c o m
 */
private List<?> toList(Object o) {
    Class<?> c = o.getClass();
    if (!c.isArray()) {
        return null;
    }

    if (c.getComponentType().isPrimitive()) {
        if (o instanceof boolean[]) {
            return Arrays.asList(ArrayUtils.toObject((boolean[]) o));
        } else if (o instanceof byte[]) {
            return Arrays.asList(ArrayUtils.toObject((byte[]) o));
        } else if (o instanceof short[]) {
            return Arrays.asList(ArrayUtils.toObject((short[]) o));
        } else if (o instanceof int[]) {
            return Arrays.asList(ArrayUtils.toObject((int[]) o));
        } else if (o instanceof long[]) {
            return Arrays.asList(ArrayUtils.toObject((long[]) o));
        } else if (o instanceof float[]) {
            return Arrays.asList(ArrayUtils.toObject((float[]) o));
        } else if (o instanceof double[]) {
            return Arrays.asList(ArrayUtils.toObject((double[]) o));
        } else if (o instanceof char[]) {
            return Arrays.asList(ArrayUtils.toObject((char[]) o));
        } else {
            throw new RuntimeException("Unhandled primitive type");
        }
    } else {
        return Arrays.asList((Object[]) o);
    }
}

From source file:com.openmeap.json.JSONObjectBuilder.java

private Object handleSimpleType(Class returnType, Object value) {
    if (returnType.isArray()) {
        Object[] returnValues = (Object[]) value;
        JSONArray jsonArray = new JSONArray();
        for (int returnValuesIdx = 0; returnValuesIdx < returnValues.length; returnValuesIdx++) {
            Object thisValue = returnValues[returnValuesIdx];
            jsonArray.put(thisValue);/*w  ww . j  a v a  2s  .c  o  m*/
        }
        return jsonArray;
    } else {
        return value;
    }
}

From source file:com.bstek.dorado.idesupport.robot.EntityDataTypeReflectionRobot.java

public Node execute(Node node, Properties properties) throws Exception {
    ConfigureStore configureStore = Configure.getStore();
    configureStore.set("core.contextConfigLocation", CONFIG_LOCATIONS);
    CommonContext.init();//from   ww  w  .ja v a  2s. co  m
    try {
        Context context = Context.getCurrent();

        XmlParserHelper xmlParserHelper = (XmlParserHelper) context.getServiceBean("xmlParserHelper");
        XmlParser dataTypeParser = xmlParserHelper.getXmlParser(EntityDataType.class);

        DataConfigManager dataConfigManager = (DataConfigManager) context.getServiceBean("dataConfigManager");
        dataConfigManager.initialize();

        DataParseContext parseContext = new DataParseContext();
        parseContext.setDataTypeDefinitionManager(
                (DataTypeDefinitionManager) context.getServiceBean("dataTypeDefinitionManager"));
        parseContext.setDataProviderDefinitionManager(
                (DataProviderDefinitionManager) context.getServiceBean("dataProviderDefinitionManager"));
        parseContext.setDataResolverDefinitionManager(
                (DataResolverDefinitionManager) context.getServiceBean("dataResolverDefinitionManager"));

        DataTypeDefinition dataTypeDefinition = (DataTypeDefinition) dataTypeParser.parse(node, parseContext);

        Class<?> matchType = dataTypeDefinition.getMatchType();
        if (matchType != null) {
            if (matchType == null || matchType.isPrimitive() || matchType.isArray()) {
                throw new IllegalArgumentException("[matchType] undefined or not a valid class.");
            }
            node = node.cloneNode(true);
            reflectAndComplete((Element) node, matchType);
        }
    } finally {
        CommonContext.dispose();
    }
    return node;
}

From source file:com.github.hateoas.forms.spring.SpringActionInputParameter.java

/**
 * Allows convenient access to multiple call values in case that this input parameter is an array or collection. Make sure to check
 * {@link #isArrayOrCollection()} before calling this method.
 *
 * @return call values or empty array//from   ww w .j a v a  2s .  c o  m
 * @throws UnsupportedOperationException if this input parameter is not an array or collection
 */
@Override
public Object[] getValues() {
    Object[] callValues;
    if (!isArrayOrCollection()) {
        throw new UnsupportedOperationException("parameter is not an array or collection");
    }
    Object callValue = getValue();
    if (callValue == null) {
        callValues = new Object[0];
    } else {
        Class<?> parameterType = getParameterType();
        if (parameterType.isArray()) {
            callValues = (Object[]) callValue;
        } else {
            callValues = ((Collection<?>) callValue).toArray();
        }
    }
    return callValues;
}

From source file:org.nabucco.alfresco.enhScriptEnv.common.script.converter.general.ArrayConverter.java

/**
 * {@inheritDoc}/*from w  w  w  . j a v  a  2 s  . c  om*/
 */
@Override
public int getForScriptConversionConfidence(final Class<?> valueInstanceClass, final Class<?> expectedClass) {
    final int confidence;

    if ((valueInstanceClass.isArray() || Collection.class.isAssignableFrom(valueInstanceClass)
            || Iterable.class.isAssignableFrom(valueInstanceClass)) && expectedClass.isArray()) {
        confidence = LOW_CONFIDENCE;
    } else {
        confidence = LOWEST_CONFIDENCE;
    }

    return confidence;
}