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.glaf.core.util.ReflectUtils.java

/**
 * get class desc. boolean[].class => "[Z" Object.class =>
 * "Ljava/lang/Object;"/*  ww  w.  j a va2s.com*/
 * 
 * @param c
 *            class.
 * @return desc.
 * @throws NotFoundException
 */
public static String getDesc(Class<?> c) {
    StringBuilder ret = new StringBuilder();

    while (c.isArray()) {
        ret.append('[');
        c = c.getComponentType();
    }

    if (c.isPrimitive()) {
        String t = c.getName();
        if ("void".equals(t))
            ret.append(JVM_VOID);
        else if ("boolean".equals(t))
            ret.append(JVM_BOOLEAN);
        else if ("byte".equals(t))
            ret.append(JVM_BYTE);
        else if ("char".equals(t))
            ret.append(JVM_CHAR);
        else if ("double".equals(t))
            ret.append(JVM_DOUBLE);
        else if ("float".equals(t))
            ret.append(JVM_FLOAT);
        else if ("int".equals(t))
            ret.append(JVM_INT);
        else if ("long".equals(t))
            ret.append(JVM_LONG);
        else if ("short".equals(t))
            ret.append(JVM_SHORT);
    } else {
        ret.append('L');
        ret.append(c.getName().replace('.', '/'));
        ret.append(';');
    }
    return ret.toString();
}

From source file:com.alibaba.dubboadmin.web.mvc.RouterController.java

private boolean isPrimitive(Class<?> cls) {
    return cls.isPrimitive() || cls == Boolean.class || cls == Byte.class || cls == Character.class
            || cls == Short.class || cls == Integer.class || cls == Long.class || cls == Float.class
            || cls == Double.class || cls == String.class;
}

From source file:com.evolveum.midpoint.repo.sql.query2.resolution.ItemPathResolver.java

/**
 * This method provides transformation from {@link String} value defined in
 * {@link com.evolveum.midpoint.repo.sql.query.definition.VirtualQueryParam#value()} to real object. Currently only
 * to simple types and enum values./*from   w ww .j a va  2s .  c o m*/
 */
private Object createQueryParamValue(VirtualQueryParam param) throws QueryException {
    Class type = param.type();
    String value = param.value();

    try {
        if (type.isPrimitive()) {
            return type.getMethod("valueOf", new Class[] { String.class }).invoke(null, new Object[] { value });
        }

        if (type.isEnum()) {
            return Enum.valueOf(type, value);
        }
    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | RuntimeException ex) {
        throw new QueryException("Couldn't transform virtual query parameter '" + param.name()
                + "' from String to '" + type + "', reason: " + ex.getMessage(), ex);
    }

    throw new QueryException("Couldn't transform virtual query parameter '" + param.name()
            + "' from String to '" + type + "', it's not yet implemented.");
}

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

public static boolean isAssignableValue(Class<?> type, Object value) {
    Assert.notNull(type, "Type must not be null");
    return (value != null ? isAssignable(type, value.getClass()) : !type.isPrimitive());
}

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

@Override
public boolean canSerialize(Class clazz, Class jsonClazz) {
    Class cc = clazz.getComponentType();
    return (super.canSerialize(clazz, jsonClazz)
            || ((jsonClazz == null || jsonClazz == JSONArray.class) && (clazz.isArray() && !cc.isPrimitive())));
}

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);
                }/*w ww.  ja  v  a 2s  .  co  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: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   www. j  a v  a  2  s  . c o 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:demo.config.PropertyConverter.java

/**
 * Converts the specified value to the target class. If the class is a
 * primitive type (Integer.TYPE, Boolean.TYPE, etc) the value returned will
 * use the wrapper type (Integer.class, Boolean.class, etc).
 * /*  ww  w.  j  a v a  2  s  .  c om*/
 * @param cls
 *            the target class of the converted value
 * @param value
 *            the value to convert
 * @param params
 *            optional parameters used for the conversion
 * @return the converted value
 * @throws ConversionException
 *             if the value is not compatible with the requested type
 * 
 * @since 1.5
 */
static Object to(Class<?> cls, Object value, Object[] params) throws ConversionException {
    if (cls.isInstance(value)) {
        return value; // no conversion needed
    }

    if (Boolean.class.equals(cls) || Boolean.TYPE.equals(cls)) {
        return toBoolean(value);
    } else if (Character.class.equals(cls) || Character.TYPE.equals(cls)) {
        return toCharacter(value);
    } else if (Number.class.isAssignableFrom(cls) || cls.isPrimitive()) {
        if (Integer.class.equals(cls) || Integer.TYPE.equals(cls)) {
            return toInteger(value);
        } else if (Long.class.equals(cls) || Long.TYPE.equals(cls)) {
            return toLong(value);
        } else if (Byte.class.equals(cls) || Byte.TYPE.equals(cls)) {
            return toByte(value);
        } else if (Short.class.equals(cls) || Short.TYPE.equals(cls)) {
            return toShort(value);
        } else if (Float.class.equals(cls) || Float.TYPE.equals(cls)) {
            return toFloat(value);
        } else if (Double.class.equals(cls) || Double.TYPE.equals(cls)) {
            return toDouble(value);
        } else if (BigInteger.class.equals(cls)) {
            return toBigInteger(value);
        } else if (BigDecimal.class.equals(cls)) {
            return toBigDecimal(value);
        }
    } else if (Date.class.equals(cls)) {
        return toDate(value, (String) params[0]);
    } else if (Calendar.class.equals(cls)) {
        return toCalendar(value, (String) params[0]);
    } else if (URL.class.equals(cls)) {
        return toURL(value);
    } else if (Locale.class.equals(cls)) {
        return toLocale(value);
    } else if (isEnum(cls)) {
        return convertToEnum(cls, value);
    } else if (Color.class.equals(cls)) {
        return toColor(value);
    } else if (cls.getName().equals(INTERNET_ADDRESS_CLASSNAME)) {
        return toInternetAddress(value);
    } else if (InetAddress.class.isAssignableFrom(cls)) {
        return toInetAddress(value);
    }

    throw new ConversionException("The value '" + value + "' (" + value.getClass() + ")"
            + " can't be converted to a " + cls.getName() + " object");
}

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);
                }/*  ww w  .  j  a  v  a  2s  . 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:com.phoenixnap.oss.ramlapisync.javadoc.JavaDocExtractor.java

/**
 * Extracts the Java Doc for a specific class from its Source code as well as any superclasses or implemented
 * interfaces./*from  w w w. j  a v a2s  .  co  m*/
 * 
 * @param clazz The Class for which to get javadoc
 * @return A parsed documentation store with the class's Javadoc or empty if none is found.
 */
public JavaDocStore getJavaDoc(Class<?> clazz) {

    if (clazz.isArray()) {
        clazz = clazz.getComponentType();
    }
    // we need to start off from some base directory
    if (baseDir == null || clazz.isPrimitive()) {
        return new JavaDocStore();
    }

    String classPackage = clazz.getPackage() == null ? "" : clazz.getPackage().getName();
    // lets eliminate some stardard stuff
    if (classPackage.startsWith("java") || (classPackage.startsWith("org")
            && (classPackage.startsWith("org.hibernate") || classPackage.startsWith("org.raml")
                    || classPackage.startsWith("org.springframework")))) {
        return new JavaDocStore();
    }

    if (javaDocCache.containsKey(clazz)) {
        return javaDocCache.get(clazz);
    }
    logger.debug("Getting Javadoc for: " + clazz.getSimpleName());
    JavaDocStore javaDoc = new JavaDocStore();

    try {

        File file = FileSearcher.fileSearch(baseDir, clazz);

        if (file != null) {
            logger.debug("Found: " + file.getAbsolutePath());
            FileInputStream in = new FileInputStream(file);

            CompilationUnit cu;
            try {
                // parse the file
                cu = JavaParser.parse(in);
            } finally {
                in.close();
            }
            // visit and print the class docs names
            new ClassVisitor(javaDoc).visit(cu, null);
            // visit and print the methods names
            new MethodVisitor(javaDoc).visit(cu, null);
            // visit and print the field names
            new FieldVisitor(javaDoc).visit(cu, null);
        } else {
            logger.warn("*** WARNING: Missing Source for: " + clazz.getSimpleName() + ". JavaDoc Unavailable.");
        }
    } catch (Exception ex) {
        logger.error(ex.getMessage(), ex);
    }
    // After we complete this class, we need to check its parents and interfaces to extract as much documentation as
    // possible
    if (clazz.getInterfaces() != null && clazz.getInterfaces().length > 0) {
        for (Class<?> interfaceClass : clazz.getInterfaces()) {
            javaDoc.merge(getJavaDoc(interfaceClass));
        }
    }
    if (clazz.getSuperclass() != null && !clazz.getSuperclass().equals(Object.class)) {
        javaDoc.merge(getJavaDoc(clazz.getSuperclass()));
    }

    javaDocCache.put(clazz, javaDoc);
    return javaDoc;
}