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.evolveum.midpoint.prism.parser.PrismBeanConverter.java

public void visit(Object bean, Handler<Object> handler) {
    if (bean == null) {
        return;//from  www . jav a2 s . co m
    }

    Class<? extends Object> beanClass = bean.getClass();

    handler.handle(bean);

    if (beanClass.isEnum() || beanClass.isPrimitive()) {
        //nothing more to do
        return;
    }

    // TODO: implement special handling for RawType, if necessary (it has no XmlType annotation any more)

    XmlType xmlType = beanClass.getAnnotation(XmlType.class);
    if (xmlType == null) {
        // no @XmlType annotation, we are not interested to go any deeper
        return;
    }

    List<String> propOrder = inspector.getPropOrder(beanClass);
    for (String fieldName : propOrder) {
        Method getter = inspector.findPropertyGetter(beanClass, fieldName);
        if (getter == null) {
            throw new IllegalStateException("No getter for field " + fieldName + " in " + beanClass);
        }
        Object getterResult;
        try {
            getterResult = getter.invoke(bean);
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            throw new SystemException(
                    "Cannot invoke method for field " + fieldName + " in " + beanClass + ": " + e.getMessage(),
                    e);
        }

        if (getterResult == null) {
            continue;
        }

        if (getterResult instanceof Collection<?>) {
            Collection col = (Collection) getterResult;
            if (col.isEmpty()) {
                continue;
            }

            for (Object element : col) {
                visitValue(element, handler);

            }
        } else {
            visitValue(getterResult, handler);
        }
    }
}

From source file:com.github.javalbert.reflection.ClassAccessFactory.java

private void visitMethodIndexReturnStatements(MethodNameReturnIndex methodIndex, Label defaultCaseLabel) {
    Label jumpLabel = methodIndex.returnIndexLabel;

    List<MethodInfo> methods = methodIndex.methods;
    for (int i = 0; i < methods.size(); i++) {
        MethodInfo methodInfo = methods.get(i);
        Class<?>[] parameterTypes = methodInfo.method.getParameterTypes();

        mv.visitLabel(jumpLabel);/*from   w  ww  . j  a  va2 s.  com*/
        mv.visitFrame(F_SAME, 0, null, 0, null);
        mv.visitVarInsn(ALOAD, 2);
        AsmUtils.visitZeroOperandInt(mv, parameterTypes.length);
        mv.visitTypeInsn(ANEWARRAY, "java/lang/Class");

        for (int j = 0; j < parameterTypes.length; j++) {
            mv.visitInsn(DUP);
            AsmUtils.visitZeroOperandInt(mv, j);

            Class<?> parameterType = parameterTypes[j];
            if (parameterType.isPrimitive()) {
                mv.visitFieldInsn(GETSTATIC, Type.getInternalName(ClassUtils.primitiveToWrapper(parameterType)),
                        "TYPE", "Ljava/lang/Class;");
            } else {
                mv.visitLdcInsn(Type.getType(parameterType));
            }

            mv.visitInsn(AASTORE);
        }

        mv.visitMethodInsn(INVOKESTATIC, "java/util/Arrays", "equals",
                "([Ljava/lang/Object;[Ljava/lang/Object;)Z", false);

        jumpLabel = i + 1 >= methods.size() ? defaultCaseLabel : new Label();
        mv.visitJumpInsn(IFEQ, jumpLabel);

        mv.visitLabel(new Label());
        AsmUtils.visitZeroOperandInt(mv, methodInfo.memberIndex);
        mv.visitInsn(IRETURN);
    }
}

From source file:net.ceos.project.poi.annotated.core.CGen.java

/**
 * Convert the file into object.//from   w w w . j  a v a 2s.  c om
 * 
 * @param object
 *            the object
 * @param oC
 *            the object class
 * @param values
 *            the array with the content at one line
 * @param idx
 *            the index of the field
 * @return the number of fields read
 * @throws WorkbookException
 */
private int unmarshal(final CConfigCriteria configCriteria, final Object object, final Class<?> oC,
        final String[] values, final int idx) throws WorkbookException {
    /* counter related to the number of fields (if new object) */
    int counter = -1;
    int index = idx;

    /* get declared fields */
    List<Field> fL = Arrays.asList(oC.getDeclaredFields());

    for (Field field : fL) {
        /* process each field from the object */
        Class<?> fT = field.getType();

        /* Process @XlsElement */
        if (PredicateFactory.isFieldAnnotationXlsElementPresent.test(field)) {
            XlsElement xlsAnnotation = (XlsElement) field.getAnnotation(XlsElement.class);

            /*
             * increment of the counter related to the number of fields (if
             * new object)
             */
            counter++;

            boolean isAppliedToBaseObject = toObject(object, fT, field, xlsAnnotation, values,
                    index + xlsAnnotation.position());

            if (!isAppliedToBaseObject && !fT.isPrimitive()) {
                try {
                    Object subObjbect = fT.newInstance();

                    Class<?> subObjbectClass = subObjbect.getClass();

                    int internalCellCounter = unmarshal(configCriteria, subObjbect, subObjbectClass, values,
                            index + xlsAnnotation.position() - 1);

                    /* set enabled the accessible object */
                    field.setAccessible(true);

                    /* add the sub object to the parent object */
                    field.set(object, subObjbect);

                    /* set disabled the accessible object */
                    field.setAccessible(false);

                    /* update the index */
                    index += internalCellCounter;
                } catch (InstantiationException | IllegalAccessException e) {
                    throw new CustomizedRulesException(ExceptionMessage.ELEMENT_NO_SUCH_METHOD.getMessage(), e);
                }
            }
        }
    }
    return counter;
}

From source file:com.sqewd.open.dal.core.persistence.handlers.StringArrayConvertor.java

private String convertToString(Object data, Field field) throws Exception {
    Class<?> type = field.getType().getComponentType();
    if (type.equals(String.class)) {
        String[] sarr = (String[]) data;
        StringBuffer buff = null;
        for (String str : sarr) {
            if (buff == null)
                buff = new StringBuffer();
            else {
                buff.append(_SPLIT_REGEX_);
            }/*from w  w  w.ja  v  a2 s. c o m*/
            buff.append(str);
        }
        return buff.toString();
    } else if (type.equals(Date.class)) {
        Date[] darr = (Date[]) data;
        StringBuffer buff = null;
        for (Date dt : darr) {
            if (buff == null)
                buff = new StringBuffer();
            else {
                buff.append(_SPLIT_REGEX_);
            }
            buff.append(dt.getTime());
        }
        return buff.toString();
    } else if (EnumPrimitives.isPrimitiveType(type)) {
        EnumPrimitives et = EnumPrimitives.type(type);
        StringBuffer buff = null;
        if (type.isPrimitive()) {
            switch (et) {
            case ECharacter:
                char[] carr = (char[]) data;
                for (int ii = 0; ii < carr.length; ii++) {
                    if (buff == null)
                        buff = new StringBuffer();
                    else
                        buff.append(_SPLIT_REGEX_);
                    buff.append(carr[ii]);
                }
                return buff.toString();
            case EShort:
                short[] sarr = (short[]) data;
                for (int ii = 0; ii < sarr.length; ii++) {
                    if (buff == null)
                        buff = new StringBuffer();
                    else
                        buff.append(_SPLIT_REGEX_);
                    buff.append(sarr[ii]);
                }
                return buff.toString();
            case EInteger:
                int[] iarr = (int[]) data;
                for (int ii = 0; ii < iarr.length; ii++) {
                    if (buff == null)
                        buff = new StringBuffer();
                    else
                        buff.append(_SPLIT_REGEX_);
                    buff.append(iarr[ii]);
                }
                return buff.toString();
            case ELong:
                long[] larr = (long[]) data;
                for (int ii = 0; ii < larr.length; ii++) {
                    if (buff == null)
                        buff = new StringBuffer();
                    else
                        buff.append(_SPLIT_REGEX_);
                    buff.append(larr[ii]);
                }
                return buff.toString();
            case EFloat:
                float[] farr = (float[]) data;
                for (int ii = 0; ii < farr.length; ii++) {
                    if (buff == null)
                        buff = new StringBuffer();
                    else
                        buff.append(_SPLIT_REGEX_);
                    buff.append(farr[ii]);
                }
                return buff.toString();
            case EDouble:
                double[] darr = (double[]) data;
                for (int ii = 0; ii < darr.length; ii++) {
                    if (buff == null)
                        buff = new StringBuffer();
                    else
                        buff.append(_SPLIT_REGEX_);
                    buff.append(darr[ii]);
                }
                return buff.toString();
            }
        } else {
            switch (et) {
            case ECharacter:
                Character[] carr = (Character[]) data;
                for (int ii = 0; ii < carr.length; ii++) {
                    if (buff == null)
                        buff = new StringBuffer();
                    else
                        buff.append(_SPLIT_REGEX_);
                    buff.append(carr[ii]);
                }
                return buff.toString();
            case EShort:
                Short[] sarr = (Short[]) data;
                for (int ii = 0; ii < sarr.length; ii++) {
                    if (buff == null)
                        buff = new StringBuffer();
                    else
                        buff.append(_SPLIT_REGEX_);
                    buff.append(sarr[ii]);
                }
                return buff.toString();
            case EInteger:
                Integer[] iarr = (Integer[]) data;
                for (int ii = 0; ii < iarr.length; ii++) {
                    if (buff == null)
                        buff = new StringBuffer();
                    else
                        buff.append(_SPLIT_REGEX_);
                    buff.append(iarr[ii]);
                }
                return buff.toString();
            case ELong:
                Long[] larr = (Long[]) data;
                for (int ii = 0; ii < larr.length; ii++) {
                    if (buff == null)
                        buff = new StringBuffer();
                    else
                        buff.append(_SPLIT_REGEX_);
                    buff.append(larr[ii]);
                }
                return buff.toString();
            case EFloat:
                Float[] farr = (Float[]) data;
                for (int ii = 0; ii < farr.length; ii++) {
                    if (buff == null)
                        buff = new StringBuffer();
                    else
                        buff.append(_SPLIT_REGEX_);
                    buff.append(farr[ii]);
                }
                return buff.toString();
            case EDouble:
                Double[] darr = (Double[]) data;
                for (int ii = 0; ii < darr.length; ii++) {
                    if (buff == null)
                        buff = new StringBuffer();
                    else
                        buff.append(_SPLIT_REGEX_);
                    buff.append(darr[ii]);
                }
                return buff.toString();
            }
        }
    }
    throw new Exception("Unsupported Array element type [" + type.getCanonicalName() + "]");
}

From source file:com.github.wshackle.java4cpp.J4CppMain.java

private static String addCppJThis(Class c) {
    if (c.isPrimitive() || c.isArray() || isString(c)) {
        return "";
    } else {//ww w.  jav  a2 s  .  com
        return ".jthis";
    }
}

From source file:com.github.wshackle.java4cpp.J4CppMain.java

private static String addConstRefIndicator(Class c, String orig) {
    if (!c.isPrimitive() && !isString(c) && !c.isArray()) {
        return "const " + orig + " &";
    }//from   ww w  .  j a  v a  2s .c  om
    return orig;
}

From source file:com.github.wshackle.java4cpp.J4CppMain.java

private static String getMethodReturnGet(String tabs, Class returnClass, Class relClass) {

    if (!returnClass.isArray() && !returnClass.isPrimitive() && !isString(returnClass)) {
        tabs += TAB_STRING;/*from   w  ww  .  j av a 2s.co m*/
        return tabs + "\n" + tabs + "jobjectRefType ref = env->GetObjectRefType(retVal);\n" + tabs
                + "if(GetDebugJ4Cpp()) DebugPrintJObject(__FILE__,__LINE__,\"retVal=\",retVal);"
                //+ tabs + "std::cout << \"ref=\" << ref << std::endl;\n"
                //                    + tabs + "std::cout << \"retVal=\" << retVal << std::endl;\n"
                //                    + tabs + "if(ref != JNIGlobalRefType) {\n" 
                //                    + tabs + "\tretVal = env->NewGlobalRef(retVal);\n"
                //                    + tabs + "}\n"
                //                    + tabs + "\n"
                //                    + tabs + "ref = env->GetObjectRefType(retVal);\n"
                //                    + tabs + "std::cout << \"ref=\" << ref << std::endl;\n"
                //                    + tabs + "std::cout << \"retVal=\" << retVal << std::endl;\n"
                + tabs + getCppRelativeName(returnClass, relClass) + " retObject(retVal,false);\n" + tabs
                + "return retObject;";
    }
    return isVoid(returnClass) ? "" : "return retVal;";
}

From source file:com.opensymphony.xwork2.util.LocalizedTextUtil.java

/**
 * Traverse up class hierarchy looking for message.  Looks at class, then implemented interface,
 * before going up hierarchy./*from   w  ww . j a v a  2  s.  c o m*/
 */
private static String findMessage(Class clazz, String key, String indexedKey, Locale locale, Object[] args,
        Set<String> checked, ValueStack valueStack) {
    if (checked == null) {
        checked = new TreeSet<String>();
    } else if (checked.contains(clazz.getName())) {
        return null;
    }

    // look in properties of this class
    String msg = getMessage(clazz.getName(), locale, key, valueStack, args);

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

    if (indexedKey != null) {
        msg = getMessage(clazz.getName(), locale, indexedKey, valueStack, args);

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

    // look in properties of implemented interfaces
    Class[] interfaces = clazz.getInterfaces();

    for (Class anInterface : interfaces) {
        msg = getMessage(anInterface.getName(), locale, key, valueStack, args);

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

        if (indexedKey != null) {
            msg = getMessage(anInterface.getName(), locale, indexedKey, valueStack, args);

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

    // traverse up hierarchy
    if (clazz.isInterface()) {
        interfaces = clazz.getInterfaces();

        for (Class anInterface : interfaces) {
            msg = findMessage(anInterface, key, indexedKey, locale, args, checked, valueStack);

            if (msg != null) {
                return msg;
            }
        }
    } else {
        if (!clazz.equals(Object.class) && !clazz.isPrimitive()) {
            return findMessage(clazz.getSuperclass(), key, indexedKey, locale, args, checked, valueStack);
        }
    }

    return null;
}

From source file:com.base.dao.sql.ReflectionUtils.java

public static Object convertValue(Object value, Class toType) {
    Object result = null;/*from  www .j  a v a  2s. c  o m*/
    if (value != null) {
        if (value.getClass().isArray() && toType.isArray()) {
            Class componentType = toType.getComponentType();
            result = Array.newInstance(componentType, Array.getLength(value));
            for (int i = 0, icount = Array.getLength(value); i < icount; i++) {
                Array.set(result, i, convertValue(Array.get(value, i), componentType));
            }
        } else {
            if ((toType == Integer.class) || (toType == Integer.TYPE))
                result = Integer.valueOf((int) longValue(value));
            if ((toType == Double.class) || (toType == Double.TYPE))
                result = new Double(doubleValue(value));
            if ((toType == Boolean.class) || (toType == Boolean.TYPE))
                result = booleanValue(value) ? Boolean.TRUE : Boolean.FALSE;
            if ((toType == Byte.class) || (toType == Byte.TYPE))
                result = Byte.valueOf((byte) longValue(value));
            if ((toType == Character.class) || (toType == Character.TYPE))
                result = new Character((char) longValue(value));
            if ((toType == Short.class) || (toType == Short.TYPE))
                result = Short.valueOf((short) longValue(value));
            if ((toType == Long.class) || (toType == Long.TYPE))
                result = Long.valueOf(longValue(value));
            if ((toType == Float.class) || (toType == Float.TYPE))
                result = new Float(doubleValue(value));
            if (toType == BigInteger.class)
                result = bigIntValue(value);
            if (toType == BigDecimal.class)
                result = bigDecValue(value);
            if (toType == String.class)
                result = stringValue(value);
            if (toType == Date.class) {
                result = DateUtils.toDate(stringValue(value));
            }
            if (Enum.class.isAssignableFrom(toType))
                result = enumValue((Class<Enum>) toType, value);
        }
    } else {
        if (toType.isPrimitive()) {
            result = primitiveDefaults.get(toType);
        }
    }
    return result;
}

From source file:com.xwtec.xwserver.util.json.JSONArray.java

/**
 * Creates a JSONArray.<br>//w ww.j av  a  2  s. c  o m
 * Inspects the object type to call the correct JSONArray factory method.
 * Accepts JSON formatted strings, arrays, Collections and Enums.
 *
 * @param object
 * @throws JSONException if the object can not be converted to a proper
 *         JSONArray.
 */
public static JSONArray fromObject(Object object, JsonConfig jsonConfig) {
    if (object instanceof JSONString) {
        return _fromJSONString((JSONString) object, jsonConfig);
    } else if (object instanceof JSONArray) {
        return _fromJSONArray((JSONArray) object, jsonConfig);
    } else if (object instanceof Collection) {
        return _fromCollection((Collection) object, jsonConfig);
    } else if (object instanceof JSONTokener) {
        return _fromJSONTokener((JSONTokener) object, jsonConfig);
    } else if (object instanceof String) {
        return _fromString((String) object, jsonConfig);
    } else if (object != null && object.getClass().isArray()) {
        Class type = object.getClass().getComponentType();
        if (!type.isPrimitive()) {
            return _fromArray((Object[]) object, jsonConfig);
        } else {
            if (type == Boolean.TYPE) {
                return _fromArray((boolean[]) object, jsonConfig);
            } else if (type == Byte.TYPE) {
                return _fromArray((byte[]) object, jsonConfig);
            } else if (type == Short.TYPE) {
                return _fromArray((short[]) object, jsonConfig);
            } else if (type == Integer.TYPE) {
                return _fromArray((int[]) object, jsonConfig);
            } else if (type == Long.TYPE) {
                return _fromArray((long[]) object, jsonConfig);
            } else if (type == Float.TYPE) {
                return _fromArray((float[]) object, jsonConfig);
            } else if (type == Double.TYPE) {
                return _fromArray((double[]) object, jsonConfig);
            } else if (type == Character.TYPE) {
                return _fromArray((char[]) object, jsonConfig);
            } else {
                throw new JSONException("Unsupported type");
            }
        }
    } else if (JSONUtils.isBoolean(object) || JSONUtils.isFunction(object) || JSONUtils.isNumber(object)
            || JSONUtils.isNull(object) || JSONUtils.isString(object) || object instanceof JSON) {
        fireArrayStartEvent(jsonConfig);
        JSONArray jsonArray = new JSONArray().element(object, jsonConfig);
        fireElementAddedEvent(0, jsonArray.get(0), jsonConfig);
        fireArrayStartEvent(jsonConfig);
        return jsonArray;
    } else if (object instanceof Enum) {
        return _fromArray((Enum) object, jsonConfig);
    } else if (object instanceof Annotation || (object != null && object.getClass().isAnnotation())) {
        throw new JSONException("Unsupported type");
    } else if (JSONUtils.isObject(object)) {
        fireArrayStartEvent(jsonConfig);
        JSONArray jsonArray = new JSONArray().element(JSONObject.fromObject(object, jsonConfig));
        fireElementAddedEvent(0, jsonArray.get(0), jsonConfig);
        fireArrayStartEvent(jsonConfig);
        return jsonArray;
    } else {
        throw new JSONException("Unsupported type");
    }
}