Example usage for java.lang Class isPrimitive

List of usage examples for java.lang Class isPrimitive

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public native boolean isPrimitive();

Source Link

Document

Determines if the specified Class object represents a primitive type.

Usage

From source file:mil.army.usace.data.dataquery.utility.DefaultConverter.java

@Override
public Object convertType(Object obj, Method method) throws ClassNotFoundException, ConversionException {
    Class methodParam = method.getParameterTypes()[0];
    if (obj == null)
        return null;
    else {//  ww  w .j  a  va2 s.c o m
        if (methodParam.isPrimitive()) {
            return obj;
        } else {
            return convertType(obj, methodParam);
        }
    }
}

From source file:com.khubla.cbean.serializer.impl.json.JSONArrayFieldSerializer.java

@Override
public String serialize(Object o, Field field) throws SerializerException {
    try {/*from w w  w. j  a va2 s  .c o  m*/
        final Class<?> componentType = field.getType().getComponentType();
        if (componentType.isPrimitive()) {
            final String[] values = BeanUtils.getArrayProperty(o, field.getName());
            final JSONArray jsonArray = new JSONArray(values);
            return jsonArray.toString();
        } else {
            final CBean<Object> cBean = CBeanServer.getInstance().getCBean(componentType);
            final Object[] values = (Object[]) PropertyUtils.getProperty(o, field.getName());
            final JSONArray jsonArray = new JSONArray();
            if (null != values) {
                for (int i = 0; i < values.length; i++) {
                    cBean.save(values[i]);
                    jsonArray.put(i, cBean.getId(values[i]));
                }
            }
            return jsonArray.toString();
        }
    } catch (final Exception e) {
        throw new SerializerException(e);
    }
}

From source file:ObjectAnalyzerTest.java

/**
 * Converts an object to a string representation that lists all fields.
 * /* w w  w . j a va2 s  .co  m*/
 * @param obj
 *          an object
 * @return a string with the object's class name and all field names and
 *         values
 */
public String toString(Object obj) {
    if (obj == null)
        return "null";
    if (visited.contains(obj))
        return "...";
    visited.add(obj);
    Class cl = obj.getClass();
    if (cl == String.class)
        return (String) obj;
    if (cl.isArray()) {
        String r = cl.getComponentType() + "[]{";
        for (int i = 0; i < Array.getLength(obj); i++) {
            if (i > 0)
                r += ",";
            Object val = Array.get(obj, i);
            if (cl.getComponentType().isPrimitive())
                r += val;
            else
                r += toString(val);
        }
        return r + "}";
    }

    String r = cl.getName();
    // inspect the fields of this class and all superclasses
    do {
        r += "[";
        Field[] fields = cl.getDeclaredFields();
        AccessibleObject.setAccessible(fields, true);
        // get the names and values of all fields
        for (Field f : fields) {
            if (!Modifier.isStatic(f.getModifiers())) {
                if (!r.endsWith("["))
                    r += ",";
                r += f.getName() + "=";
                try {
                    Class t = f.getType();
                    Object val = f.get(obj);
                    if (t.isPrimitive())
                        r += val;
                    else
                        r += toString(val);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        r += "]";
        cl = cl.getSuperclass();
    } while (cl != null);

    return r;
}

From source file:com.khubla.cbean.serializer.impl.json.JSONArrayFieldSerializer.java

@Override
public void deserialize(Object o, Field field, String value) throws SerializerException {
    try {/*from  w  ww .j a  va  2  s  .  co  m*/
        final JSONArray jsonArray = new JSONArray(value);
        final Object od = Array.newInstance(field.getType().getComponentType(), jsonArray.length());
        PropertyUtils.setProperty(o, field.getName(), od);
        final Class<?> componentType = field.getType().getComponentType();
        if (componentType.isPrimitive()) {
            for (int i = 0; i < jsonArray.length(); i++) {
                final String v = jsonArray.getString(i);
                PropertyUtils.setIndexedProperty(o, field.getName(), i,
                        ConvertUtils.convert(v, field.getType().getComponentType()));
            }
        } else {
            final CBean<Object> cBean = CBeanServer.getInstance().getCBean(componentType);
            for (int i = 0; i < jsonArray.length(); i++) {
                final Object co = cBean.load(new CBeanKey(jsonArray.getString(i)));
                PropertyUtils.setIndexedProperty(o, field.getName(), i,
                        ConvertUtils.convert(co, field.getType().getComponentType()));
            }
        }
    } catch (final Exception e) {
        throw new SerializerException(e);
    }
}

From source file:com.wantscart.jade.provider.jdbc.PreparedStatementCallbackReturnId.java

public PreparedStatementCallbackReturnId(PreparedStatementSetter setter, Class<?> returnType) {
    this.setter = setter;
    if (returnType.isPrimitive()) {
        returnType = ClassUtils.primitiveToWrapper(returnType);
    }//  w  w w  .j ava  2  s .  c  om
    this.returnType = returnType;
    Class<?> idType = returnType;
    if (returnType.isArray()) {
        idType = returnType.getComponentType();
    }
    this.idType = idType;
    if (idType.isPrimitive()) {
        idType = ClassUtils.primitiveToWrapper(idType);
    }
    this.wrappedIdType = idType;
    this.mapper = new SingleColumnRowMapper(idType);
    if (wrappedIdType != Integer.class && wrappedIdType != Long.class && wrappedIdType != Number.class) {
        throw new IllegalArgumentException(
                "wrong return type(int/long type or its array type only): " + returnType);
    }
}

From source file:com.bacoder.parser.core.DumpVisitor.java

@Override
public void visitBefore(Node node) {
    String tag = String.format("%s<%s sl=\"%d\" sc=\"%d\" el=\"%d\" ec=\"%d\">\n",
            Strings.repeat(indent, level), node.getClass().getSimpleName(), node.getStartLine(),
            node.getStartColumn(), node.getEndLine(), node.getEndColumn());
    try {/*from  w  ww.  j  av  a2s.  c o m*/
        outputStream.write(tag.getBytes());

        Class<? extends Node> clazz = node.getClass();
        if (clazz.getAnnotation(DumpTextWithToString.class) != null) {
            String property = String.format("%s<text>%s</text>\n", Strings.repeat(indent, level + 1),
                    node.toString());
            try {
                outputStream.write(property.getBytes());
            } catch (IOException e) {
                throw new RuntimeException("Unable to write \'" + property + "\'", e);
            }
        } else {
            Field[] fields = node.getClass().getDeclaredFields();
            for (Field field : fields) {
                Class<?> type = field.getType();
                if (type.isPrimitive() || type.isEnum() || String.class.isAssignableFrom(type)) {
                    String propertyName = field.getName();
                    Object value;
                    try {
                        value = PropertyUtils.getSimpleProperty(node, propertyName);
                        String property = String.format("%s<%s>%s</%s>\n", Strings.repeat(indent, level + 1),
                                propertyName,
                                value == null ? "" : StringEscapeUtils.escapeXml(value.toString()),
                                propertyName);
                        try {
                            outputStream.write(property.getBytes());
                        } catch (IOException e) {
                            throw new RuntimeException("Unable to write \'" + property + "\'", e);
                        }
                    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                        // Ignore the field.
                    }
                }
            }
        }
    } catch (IOException e) {
        throw new RuntimeException("Unable to write \'" + tag + "\'", e);
    }
    level++;
}

From source file:com.metaparadigm.jsonrpc.ReferenceSerializer.java

@Override
public boolean canSerialize(Class clazz, Class jsonClazz) {
    return (!clazz.isArray() && !clazz.isPrimitive() && !clazz.isInterface()
            && (bridge.isReference(clazz) || bridge.isCallableReference(clazz))
            && (jsonClazz == null || jsonClazz == JSONObject.class));
}

From source file:com.glaf.core.util.ReflectUtils.java

/**
 * is compatible.// ww w . j  av a2 s  . c  o  m
 * 
 * @param c
 *            class.
 * @param o
 *            instance.
 * @return compatible or not.
 */
public static boolean isCompatible(Class<?> c, Object o) {
    boolean pt = c.isPrimitive();
    if (o == null)
        return !pt;

    if (pt) {
        if (c == int.class)
            c = Integer.class;
        else if (c == boolean.class)
            c = Boolean.class;
        else if (c == long.class)
            c = Long.class;
        else if (c == float.class)
            c = Float.class;
        else if (c == double.class)
            c = Double.class;
        else if (c == char.class)
            c = Character.class;
        else if (c == byte.class)
            c = Byte.class;
        else if (c == short.class)
            c = Short.class;
    }
    if (c == o.getClass())
        return true;
    return c.isInstance(o);
}

From source file:com.cognifide.slice.mapper.impl.processor.SliceReferenceFieldProcessor.java

@Override
public boolean accepts(final Resource resource, final Field field) {
    Class<?> type = field.getType();
    // additional checks of type for performance sake
    return type != String.class && !type.isPrimitive() && field.isAnnotationPresent(SliceReference.class);
}

From source file:eu.squadd.testing.objectspopulator.RandomValuePopulator.java

private boolean isSimpleType(final Class<?> fieldType) {
    return fieldType.isPrimitive() || fieldType.isEnum() || String.class.isAssignableFrom(fieldType)
            || Date.class.isAssignableFrom(fieldType);
}