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.github.michalbednarski.intentslab.Utils.java

public static Object[] deepCastArray(Object[] array, Class targetType) {
    assert targetType.isArray() && !targetType.getComponentType().isPrimitive();

    if (targetType.isInstance(array) || array == null) {
        return array;
    }/*from   w  w  w  .ja va  2  s .  co  m*/

    Class componentType = targetType.getComponentType();
    Class nestedComponentType = componentType.getComponentType();
    Object[] newArray = (Object[]) Array.newInstance(componentType, array.length);
    if (nestedComponentType != null && !nestedComponentType.isPrimitive()) {
        for (int i = 0; i < array.length; i++) {
            newArray[i] = deepCastArray((Object[]) array[i], nestedComponentType);
        }
    } else {
        System.arraycopy(array, 0, newArray, 0, array.length);
    }
    return newArray;
}

From source file:cn.afterturn.easypoi.util.PoiPublicUtil.java

/**
 * ?java// w ww .j a v  a2 s  . co m
 *
 * @param field
 * @return
 */
public static boolean isJavaClass(Field field) {
    Class<?> fieldType = field.getType();
    boolean isBaseClass = false;
    if (fieldType.isArray()) {
        isBaseClass = false;
    } else if (fieldType.isPrimitive() || fieldType.getPackage() == null
            || "java.lang".equals(fieldType.getPackage().getName())
            || "java.math".equals(fieldType.getPackage().getName())
            || "java.sql".equals(fieldType.getPackage().getName())
            || "java.time".equals(fieldType.getPackage().getName())
            || "java.util".equals(fieldType.getPackage().getName())) {
        isBaseClass = true;
    }
    return isBaseClass;
}

From source file:Main.java

private static ArrayList<ArrayList> sortObjectArrayListSimpleMaster(ArrayList listIn, String paramName) {
    ArrayList<ArrayList> answer = new ArrayList<ArrayList>();
    ArrayList newList = new ArrayList();
    ArrayList<Integer> indices = new ArrayList<Integer>();
    try {//w  ww . j a  v a  2 s.c o m
        if (listIn.size() > 0) {
            Class<?> c = listIn.get(0).getClass();
            Field f = c.getDeclaredField(paramName);
            f.setAccessible(true);
            Class<?> t = f.getType();
            Double dd = new Double(14);
            Float ff = new Float(14);
            Integer ii = new Integer(14);
            Map sortedPos = new LinkedHashMap();
            Map sortedNeg = new LinkedHashMap();
            Map unsorted = new LinkedHashMap();
            int indexCount = 0;
            long count = 0;
            if (t.isPrimitive()) {
                for (Object thisObj : listIn) {
                    Object o = f.get(thisObj);
                    double d = 0;
                    if (t.getName().equals("char")) {
                        d = (int) ((Character) o);
                    } else if (t.isInstance(dd))
                        d = (Double) o;
                    else if (t.isInstance(ff))
                        d = (Float) o;
                    else if (t.isInstance(ii))
                        d = (Integer) o;
                    else
                        d = new Double(o.toString());

                    boolean isNegative = false;

                    if (d < 0) {
                        isNegative = true;
                        d = Math.abs(d);
                    }

                    String format = "%1$30f";
                    String newKey = String.format(format, d);
                    String format2 = "%1$20d";
                    String countString = String.format(format2, count);
                    newKey += "-" + countString;
                    if (isNegative) {
                        sortedNeg.put(newKey, thisObj);
                    } else {
                        sortedPos.put(newKey, thisObj);
                    }
                    unsorted.put(thisObj, indexCount);
                    count++;
                    indexCount++;
                }
                TreeMap<String, Object> resultPos = new TreeMap();
                resultPos.putAll(sortedPos);
                sortedPos = resultPos;
                TreeMap<String, Object> resultNeg = new TreeMap();
                resultNeg.putAll(sortedNeg);
                sortedNeg = resultNeg;
            } else if (t.isInstance(paramName)) {
                // System.out.println("is a string with value " + o);
                for (Object thisObj : listIn) {
                    String key = (String) (f.get(thisObj));
                    sortedPos.put(key + "-" + count, thisObj);
                    unsorted.put(thisObj, indexCount);
                    count++;
                    indexCount++;
                }
                TreeMap<String, Object> result = new TreeMap(String.CASE_INSENSITIVE_ORDER);
                result.putAll(sortedPos);
                sortedPos = result;
            }

            Iterator itNeg = sortedNeg.entrySet().iterator();
            while (itNeg.hasNext()) {
                Map.Entry pairs = (Map.Entry) itNeg.next();
                newList.add(pairs.getValue());
                itNeg.remove();
            }

            Collections.reverse(newList);

            Iterator itPos = sortedPos.entrySet().iterator();
            while (itPos.hasNext()) {
                Map.Entry pairs = (Map.Entry) itPos.next();
                Object obj = pairs.getValue();
                newList.add(obj);
                indices.add((Integer) unsorted.get(obj));
                itPos.remove();
            }
        }
    } catch (Exception e) {
        System.out
                .println("problem sorting list.  listIn.size(): " + listIn.size() + " and param: " + paramName);
        answer.add(newList);
        answer.add(indices);
        return answer;
    }
    answer.add(newList);
    answer.add(indices);
    return answer;
}

From source file:io.github.moosbusch.lumpi.util.LumpiUtil.java

@SuppressWarnings("unchecked")
public static <T> T cast(Object obj, Class<?> type) {
    if ((obj != null) && (type != null)) {
        if (type.isPrimitive()) {
            Class<?> wrapperType = ClassUtils.primitiveToWrapper(type);

            if (ClassUtils.isAssignable(obj.getClass(), wrapperType)) {
                return (T) wrapperType.cast(obj);
            }// w  w w  . j a v  a2s . co  m
        } else {
            if (ClassUtils.isAssignable(obj.getClass(), type)) {
                return (T) type.cast(obj);
            }
        }
    }

    return null;
}

From source file:Main.java

public static boolean isBaseDateType(Field field) {
    Class<?> clazz = field.getType();
    return clazz.equals(Integer.class) || clazz.equals(int.class) || clazz.equals(Byte.class)
            || clazz.equals(byte.class) || clazz.equals(Character.class) || clazz.equals(char.class)
            || clazz.equals(Long.class) || clazz.equals(long.class) || clazz.equals(Double.class)
            || clazz.equals(double.class) || clazz.equals(Float.class) || clazz.equals(float.class)
            || clazz.equals(Short.class) || clazz.equals(short.class) || clazz.equals(Boolean.class)
            || clazz.equals(boolean.class) || clazz.equals(Date.class) || clazz.equals(java.sql.Date.class)
            || clazz.isPrimitive() || clazz.equals(String.class);
}

From source file:com.amazon.carbonado.layout.LayoutFactory.java

private static int annValueHashCode(Object value) {
    Class type = value.getClass();
    if (!type.isArray()) {
        if (value instanceof String || type.isPrimitive()) {
            return value.hashCode();
        } else if (value instanceof Class) {
            // Use name for stable hash code.
            return ((Class) value).getName().hashCode();
        } else if (value instanceof Enum) {
            // Use name for stable hash code.
            return ((Enum) value).name().hashCode();
        } else if (value instanceof Annotation) {
            return annHashCode((Annotation) value);
        } else {/* www  .  j a v a2  s  .c o  m*/
            return value.hashCode();
        }
    } else if (type == byte[].class) {
        return Arrays.hashCode((byte[]) value);
    } else if (type == char[].class) {
        return Arrays.hashCode((char[]) value);
    } else if (type == double[].class) {
        return Arrays.hashCode((double[]) value);
    } else if (type == float[].class) {
        return Arrays.hashCode((float[]) value);
    } else if (type == int[].class) {
        return Arrays.hashCode((int[]) value);
    } else if (type == long[].class) {
        return Arrays.hashCode((long[]) value);
    } else if (type == short[].class) {
        return Arrays.hashCode((short[]) value);
    } else if (type == boolean[].class) {
        return Arrays.hashCode((boolean[]) value);
    } else {
        return Arrays.hashCode((Object[]) value);
    }
}

From source file:com.espertech.esper.util.MethodResolver.java

private static boolean isIdentityConversion(Class declarationType, Class invocationType) {
    if (wrappingConversions.containsKey(declarationType)) {
        return wrappingConversions.get(declarationType).contains(invocationType)
                || declarationType.isAssignableFrom(invocationType);
    } else {/*from   www  . j a  va  2  s  .c  o  m*/
        if (invocationType == null) {
            return !declarationType.isPrimitive();
        }
        return declarationType.isAssignableFrom(invocationType);
    }

}

From source file:com.netflix.iep.config.Configuration.java

@SuppressWarnings("unchecked")
public static <T> T newProxy(final Class<T> ctype, final String prefix) {
    InvocationHandler handler = new InvocationHandler() {
        @Override//  ww  w .  j a  va 2 s  .  co m
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if (method.getName().equals("get")) {
                return iConfiguration.get((args[0] == null) ? null : args[0].toString());
            } else {
                Class rt = method.getReturnType();
                String key = (prefix == null) ? method.getName() : prefix + "." + method.getName();
                if (IConfiguration.class.isAssignableFrom(rt)) {
                    return newProxy(rt, key);
                } else {
                    String value = iConfiguration.get(key);
                    if (value == null) {
                        DefaultValue anno = method.getAnnotation(DefaultValue.class);
                        value = (anno == null) ? null : anno.value();
                    }
                    if (value == null) {
                        if (rt.isPrimitive())
                            throw new IllegalStateException("no value for property " + method.getName());
                        return null;
                    }
                    return Strings.cast(rt, value);
                }
            }
        }
    };
    return (T) Proxy.newProxyInstance(ctype.getClassLoader(), new Class[] { ctype }, handler);
}

From source file:es.caib.zkib.jxpath.util.ValueUtils.java

/**
 * Returns 1 if the type is a collection,
 * -1 if it is definitely not// w w  w  .jav  a  2  s . c o  m
 * and 0 if it may be a collection in some cases.
 * @param clazz to test
 * @return int
 */
public static int getCollectionHint(Class clazz) {
    if (clazz.isArray()) {
        return 1;
    }

    if (Collection.class.isAssignableFrom(clazz)) {
        return 1;
    }

    if (clazz.isPrimitive()) {
        return -1;
    }

    if (clazz.isInterface()) {
        return 0;
    }

    if (Modifier.isFinal(clazz.getModifiers())) {
        return -1;
    }

    return 0;
}

From source file:com.addthis.codec.config.Configs.java

private static boolean isCodableType(CodableFieldInfo fieldInfo) {
    Class<?> expectedType = elementType(fieldInfo);
    if (expectedType.isAssignableFrom(String.class)) {
        return false;
    } else if ((expectedType == boolean.class) || (expectedType == Boolean.class)) {
        return false;
    } else if (expectedType == AtomicBoolean.class) {
        return false;
    } else if (Number.class.isAssignableFrom(expectedType) || expectedType.isPrimitive()) {
        // primitive numeric types are not subclasses of Number, so just catch all non-booleans
        return false;
    } else if (expectedType.isEnum()) {
        return false;
    } else {/*from   w  w w. j  a  v a  2 s  .co  m*/
        return true;
    }
}