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:com.trafficspaces.api.model.Resource.java

public JSONObject getJSONObject() {
    JSONObject jsonObject = new JSONObject();
    Field[] fields = this.getClass().getFields();
    try {/*ww  w .j ava 2 s . c  o  m*/
        for (int i = 0; i < fields.length; i++) {

            Object fieldValue = fields[i].get(this);
            int fieldModifiers = fields[i].getModifiers();
            //System.out.println("name=" + fields[i].getName() + ", value=" + fieldValue + ", type=" +fields[i].getType() + 
            //      ", ispublic="+Modifier.isPublic(fieldModifiers) + ", isstatic="+Modifier.isStatic(fieldModifiers) + ", isnative="+Modifier.isNative(fieldModifiers));
            if (fieldValue != null && Modifier.isPublic(fieldModifiers) && !Modifier.isStatic(fieldModifiers)
                    && !Modifier.isNative(fieldModifiers)) {

                String name = fields[i].getName();
                Class type = fields[i].getType();

                if (type.isPrimitive()) {
                    if (type.equals(int.class)) {
                        jsonObject.put(name, fields[i].getInt(this));
                    } else if (type.equals(double.class)) {
                        jsonObject.put(name, fields[i].getDouble(this));
                    }
                } else if (type.isArray()) {
                    JSONObject jsonSubObject = new JSONObject();
                    JSONArray jsonArray = new JSONArray();

                    jsonObject.put(name, jsonSubObject);
                    jsonSubObject.put(name.substring(0, name.length() - 1), jsonArray);

                    Object[] values = (Object[]) fieldValue;
                    for (int j = 0; j < values.length; j++) {
                        if (values[j] != null && values[j] instanceof Resource) {
                            jsonArray.put(((Resource) values[j]).getJSONObject());
                        }
                    }
                } else if (Resource.class.isAssignableFrom(type)) {
                    jsonObject.put(name, ((Resource) fieldValue).getJSONObject());
                } else if (type.equals(String.class) && fieldValue != null) {
                    jsonObject.put(name, String.valueOf(fieldValue));
                }
            }
        }
    } catch (Exception nsfe) {
        //
    }
    return jsonObject;
}

From source file:net.firejack.platform.core.validation.LessThanProcessor.java

@Override
public List<Constraint> generate(Method readMethod, String property, Map<String, String> params) {
    List<Constraint> constraints = null;
    Annotation annotation = readMethod.getAnnotation(LessThan.class);
    if (annotation != null) {
        LessThan lessThan = (LessThan) annotation;
        Constraint constraint = new Constraint(lessThan.annotationType().getSimpleName());
        Class<?> returnType = readMethod.getReturnType();
        if (returnType.getSuperclass() == Number.class || returnType.isPrimitive()) {
            List<RuleParameter> ruleParameters = new ArrayList<RuleParameter>();

            boolean checkEquality = lessThan.checkEquality();
            RuleParameter checkEqualityParam = new RuleParameter("checkEquality", checkEquality);
            ruleParameters.add(checkEqualityParam);

            String errorMessage;/*from w ww.  jav a 2s  . c o  m*/
            RuleParameter valueParam;
            String messageKey = checkEquality ? lessThan.equalityMsgKey() : lessThan.msgKey();
            if (returnType == Float.class || returnType == float.class) {
                errorMessage = MessageResolver.messageFormatting(messageKey, Locale.ENGLISH, property,
                        lessThan.floatVal());
                valueParam = new RuleParameter("lessThanValue", lessThan.floatVal());
            } else if (returnType == Double.class || returnType == double.class) {
                errorMessage = MessageResolver.messageFormatting(messageKey, Locale.ENGLISH, property,
                        lessThan.doubleVal());
                valueParam = new RuleParameter("lessThanValue", lessThan.doubleVal());
            } else {
                errorMessage = MessageResolver.messageFormatting(messageKey, Locale.ENGLISH, property,
                        lessThan.intVal());
                valueParam = new RuleParameter("lessThanValue", lessThan.intVal());
            }
            constraint.setErrorMessage(errorMessage);
            ruleParameters.add(valueParam);

            constraint.setParams(ruleParameters);
            constraints = new ArrayList<Constraint>();
            constraints.add(constraint);
        }
    }
    return constraints;
}

From source file:com.ewcms.common.query.mongo.PropertyConvert.java

/**
 * ?{@value null}?/*www. j  a va  2  s. c  om*/
 * 
 * @param type 
 * @param value 
 * @return
 */
private boolean sameType(Class<?> type, Object value) {
    if (type.isPrimitive()) {
        Class<?> wapper = primitiveWrapper.get(type);
        if (wapper == null) {
            throw new IllegalArgumentException(type.getName() + " wapper is not exist!");
        }
        return wapper == value.getClass();
    }
    return type == value.getClass();
}

From source file:com.strider.datadefender.requirement.Parameter.java

private Object getArrayForValues(final List<Object> list, final String type) throws ClassNotFoundException {
    String dataType = type;//from   ww  w. j a v  a2s  . co m

    if ("String".equals(dataType)) {
        dataType = "java.lang.String";
    }

    final Class<?> c = ClassUtils.getClass(dataType);

    if (c.isPrimitive()) {
        final Class<?> w = ClassUtils.primitiveToWrapper(c);
        Object array = Array.newInstance(w, list.size());

        array = list.toArray((Object[]) array);

        if (c == boolean.class) {
            return ArrayUtils.toPrimitive((Boolean[]) array);
        } else if (c == byte.class) {
            return ArrayUtils.toPrimitive((Byte[]) array);
        } else if (c == short.class) {
            return ArrayUtils.toPrimitive((Short[]) array);
        } else if (c == char.class) {
            return ArrayUtils.toPrimitive((Character[]) array);
        } else if (c == int.class) {
            return ArrayUtils.toPrimitive((Integer[]) array);
        } else if (c == long.class) {
            return ArrayUtils.toPrimitive((Long[]) array);
        } else if (c == float.class) {
            return ArrayUtils.toPrimitive((Float[]) array);
        } else if (c == double.class) {
            return ArrayUtils.toPrimitive((Double[]) array);
        }

        throw new IllegalArgumentException("Unhandled primitive type: " + c.getName());
    }

    final Object array = Array.newInstance(c, list.size());

    return list.toArray((Object[]) array);
}

From source file:com.dianping.resource.io.util.ClassUtils.java

/**
 * Check if the given class represents a primitive (i.e. boolean, byte,
 * char, short, int, long, float, or double) or a primitive wrapper
 * (i.e. Boolean, Byte, Character, Short, Integer, Long, Float, or Double).
 * @param clazz the class to check//from   w w  w  .j a v a  2s  . c o  m
 * @return whether the given class is a primitive or primitive wrapper class
 */
public static boolean isPrimitiveOrWrapper(Class<?> clazz) {
    Assert.notNull(clazz, "Class must not be null");
    return (clazz.isPrimitive() || isPrimitiveWrapper(clazz));
}

From source file:com.dianping.resource.io.util.ClassUtils.java

/**
 * Resolve the given class if it is a primitive class,
 * returning the corresponding primitive wrapper type instead.
 * @param clazz the class to check/*  w  w w.  ja  v a 2  s .c  o m*/
 * @return the original class, or a primitive wrapper for the original primitive type
 */
public static Class<?> resolvePrimitiveIfNecessary(Class<?> clazz) {
    Assert.notNull(clazz, "Class must not be null");
    return (clazz.isPrimitive() && clazz != void.class ? primitiveTypeToWrapperMap.get(clazz) : clazz);
}

From source file:com.netflix.dyno.contrib.ElasticConnectionPoolConfigurationPublisher.java

JSONObject createJsonFromConfig(ConnectionPoolConfiguration config) throws JSONException {
    if (config == null) {
        throw new IllegalArgumentException("Valid ConnectionPoolConfiguration instance is required");
    }//  www  .j a  va 2  s.c o  m

    JSONObject json = new JSONObject();

    json.put("ApplicationName", applicationName);
    json.put("DynomiteClusterName", clusterName);

    Method[] methods = config.getClass().getMethods();
    for (Method method : methods) {
        if (method.getName().startsWith("get")) {
            Class<?> ret = method.getReturnType();
            if (ret.isPrimitive() || ret == java.lang.String.class) {
                try {
                    json.put(method.getName().substring(3), method.invoke(config));
                } catch (ReflectiveOperationException ex) {
                    /* forget it */
                }
            }
        }
    }

    // jar version
    Set<String> jars = new HashSet<String>() {
        {
            add("dyno-core");
            add("dyno-contrib");
            add("dyno-jedis");
            add("dyno-reddison");
        }
    };

    json.put("Versions", new JSONObject(getLibraryVersion(this.getClass(), jars)));

    return json;
}

From source file:com.freetmp.common.util.ClassUtils.java

public static boolean isPrimitiveOrWrapper(Class<?> clazz) {
    Assert.notNull(clazz, "Class must not be null");
    return (clazz.isPrimitive() || isPrimitiveWrapper(clazz));
}

From source file:com.freetmp.common.util.ClassUtils.java

public static Class<?> resolvePrimitiveIfNecessary(Class<?> clazz) {
    Assert.notNull(clazz, "Class must not be null");
    return (clazz.isPrimitive() && clazz != void.class ? primitiveTypeToWrapperMap.get(clazz) : clazz);
}