List of usage examples for java.lang Class isPrimitive
@HotSpotIntrinsicCandidate public native boolean isPrimitive();
From source file:Main.java
public static <T> Object convertListToArray(List<T> list, Class<T> destEntryType) { Object outArray = Array.newInstance(destEntryType, list.size()); int count = 0; int size = list.size(); for (int i = 0; i < size; i++) { Object element = list.get(i); Array.set(outArray, count, element); count++;/* w w w . j a v a 2s .c o m*/ } if (destEntryType.isPrimitive()) return outArray; else return (T[]) outArray; }
From source file:RealFunctionValidation.java
public static Object readAndWritePrimitiveValue(final DataInputStream in, final DataOutputStream out, final Class<?> type) throws IOException { if (!type.isPrimitive()) { throw new IllegalArgumentException("type must be primitive"); }/*from w w w. j a v a2s . co m*/ if (type.equals(Boolean.TYPE)) { final boolean x = in.readBoolean(); out.writeBoolean(x); return Boolean.valueOf(x); } else if (type.equals(Byte.TYPE)) { final byte x = in.readByte(); out.writeByte(x); return Byte.valueOf(x); } else if (type.equals(Character.TYPE)) { final char x = in.readChar(); out.writeChar(x); return Character.valueOf(x); } else if (type.equals(Double.TYPE)) { final double x = in.readDouble(); out.writeDouble(x); return Double.valueOf(x); } else if (type.equals(Float.TYPE)) { final float x = in.readFloat(); out.writeFloat(x); return Float.valueOf(x); } else if (type.equals(Integer.TYPE)) { final int x = in.readInt(); out.writeInt(x); return Integer.valueOf(x); } else if (type.equals(Long.TYPE)) { final long x = in.readLong(); out.writeLong(x); return Long.valueOf(x); } else if (type.equals(Short.TYPE)) { final short x = in.readShort(); out.writeShort(x); return Short.valueOf(x); } else { // This should never occur. throw new IllegalStateException(); } }
From source file:org.shept.util.BeanUtilsExtended.java
/** * Find all occurrences of targetClass in targetObject. * Be careful. This stuff may be recursive ! * Should be improved to prevent endless recursive loops. * /*from w ww .ja v a 2s. co m*/ * @param * @return * * @param targetObject * @param targetClass * @param nestedPath * @return * @throws Exception */ public static Map<String, ?> findNestedPaths(Object targetObject, Class<?> targetClass, String nestedPath, List<String> ignoreList, Set<Object> visited, int maxDepth) throws Exception { Assert.notNull(targetObject); Assert.notNull(targetClass); Assert.notNull(ignoreList); HashMap<String, Object> nestedPaths = new HashMap<String, Object>(); if (maxDepth <= 0) { return nestedPaths; } nestedPath = (nestedPath == null ? "" : nestedPath); BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(targetObject); PropertyDescriptor[] props = bw.getPropertyDescriptors(); for (PropertyDescriptor pd : props) { Class<?> clazz = pd.getPropertyType(); if (clazz != null && !clazz.equals(Class.class) && !clazz.isAnnotation() && !clazz.isPrimitive()) { Object value = null; String pathName = pd.getName(); if (!ignoreList.contains(pathName)) { try { value = bw.getPropertyValue(pathName); } catch (Exception e) { } // ignore any exceptions here if (StringUtils.hasText(nestedPath)) { pathName = nestedPath + PropertyAccessor.NESTED_PROPERTY_SEPARATOR + pathName; } // TODO break up this stuff into checking and excecution a la ReflectionUtils if (targetClass.isAssignableFrom(clazz)) { nestedPaths.put(pathName, value); } // exclude objects already visited from further inspection to prevent circular references // unfortunately this stuff isn't fool proof as there are ConcurrentModificationExceptions // when adding objects to the visited list if (value != null && !isInstanceVisited(visited, value)) { nestedPaths.putAll( findNestedPaths(value, targetClass, pathName, ignoreList, visited, maxDepth - 1)); } } } } return nestedPaths; }
From source file:com.jilk.ros.rosbridge.implementation.JSON.java
public static Object convertJSONPrimitiveToPrimitive(Object o, Class c) { Object result = o;//ww w. j a va 2 s. com if (c.isPrimitive() || Number.class.isAssignableFrom(c)) { if (c.equals(double.class) || c.equals(Double.class)) result = new Double(((Number) o).doubleValue()); else if (c.equals(float.class) || c.equals(Float.class)) result = new Float(((Number) o).floatValue()); else if (c.equals(long.class) || c.equals(Long.class)) result = new Long(((Number) o).longValue()); else if (int.class.equals(c) || c.equals(Integer.class)) result = new Integer(((Number) o).intValue()); else if (c.equals(short.class) || c.equals(Short.class)) result = new Short(((Number) o).shortValue()); else if (c.equals(byte.class) || c.equals(Byte.class)) result = new Byte(((Number) o).byteValue()); } return result; }
From source file:org.agiso.core.lang.util.ObjectUtils.java
/** * Generuje reprezentacj acuchow pola. Sprawdza, czy nie bya ona ju wczeniej * wyznaczana. Jeli tak, tworzy wersj skrcon reprezentacji takiego pola. * /* ww w. j a v a 2 s . c o m*/ * @param object */ private static void toStringField(Object object) { StringBuffer buffer = (StringBuffer) ThreadUtils.getAttribute(TOSTRING_TCBUFF); if (object == null) { buffer.append(TOSTRING_EMPTY); } else { Class<?> clazz = object.getClass(); if (clazz.isPrimitive()) { buffer.append(object.toString()); } else { @SuppressWarnings("unchecked") Set<String> converted = (Set<String>) ThreadUtils.getAttribute(TOSTRING_TCATTR); String hexHash = Integer.toHexString(System.identityHashCode(object)); if (converted.contains(clazz.getCanonicalName() + "@" + hexHash)) { buffer.append(clazz.getSimpleName()).append('@').append(hexHash).append(TOSTRING_REPEATE); } else { buffer.append(object.toString()); } } } }
From source file:objenome.util.TypeUtil.java
/** * Returns the object wrapper class type for a primitive class type * * @param type a numeric type, which is either a primitive or an object * @return the equivalent object class type *//*from w w w. j a v a 2 s . co m*/ public static Class<?> getObjectType(Class<?> type) { if (!type.isPrimitive()) { return type; } else if (int.class == type) { return Integer.class; } else if (long.class == type) { return Long.class; } else if (float.class == type) { return Float.class; } else if (double.class == type) { return Double.class; } else if (boolean.class == type) { return Boolean.class; } else if (short.class == type) { return Short.class; } else if (char.class == type) { return Character.class; } else if (byte.class == type) { return Byte.class; } else { throw new IllegalArgumentException(); } }
From source file:Main.java
/** * renderArray returns an array similar to a List. If the array type is an * object they are rendered with "render(object)" for each. If the array * type is a primitive each element is added directly to the string buffer * collecting the result./*from www. j a v a 2 s . co m*/ * * @param o * @param objectClass * @return */ private static StringBuffer renderArray(Object o, Class<?> objectClass) { Class<?> componentType = objectClass.getComponentType(); StringBuffer sb = new StringBuffer(ARRAY_PREFIX); if (componentType.isPrimitive() == false) { Object[] oa = (Object[]) o; for (int i = 0; i < oa.length; i++) { if (i > 0) { sb.append(ELEMENT_SEPARATOR); } sb.append(render(oa[i])); } } else { if (Boolean.TYPE.equals(componentType)) { boolean[] ba = (boolean[]) o; for (int i = 0; i < ba.length; i++) { if (i > 0) { sb.append(ELEMENT_SEPARATOR); } sb.append(ba[i]); } } else if (Integer.TYPE.equals(componentType)) { int[] ia = (int[]) o; for (int i = 0; i < ia.length; i++) { if (i > 0) { sb.append(ELEMENT_SEPARATOR); } sb.append(ia[i]); } } else if (Long.TYPE.equals(componentType)) { long[] ia = (long[]) o; for (int i = 0; i < ia.length; i++) { if (i > 0) { sb.append(ELEMENT_SEPARATOR); } sb.append(ia[i]); } } else if (Double.TYPE.equals(componentType)) { double[] ia = (double[]) o; for (int i = 0; i < ia.length; i++) { if (i > 0) { sb.append(ELEMENT_SEPARATOR); } sb.append(ia[i]); } } else if (Float.TYPE.equals(componentType)) { float[] ia = (float[]) o; for (int i = 0; i < ia.length; i++) { if (i > 0) { sb.append(ELEMENT_SEPARATOR); } sb.append(ia[i]); } } else if (Character.TYPE.equals(componentType)) { char[] ia = (char[]) o; for (int i = 0; i < ia.length; i++) { if (i > 0) { sb.append(ELEMENT_SEPARATOR); } sb.append(ia[i]); } } else if (Short.TYPE.equals(componentType)) { short[] ia = (short[]) o; for (int i = 0; i < ia.length; i++) { if (i > 0) { sb.append(ELEMENT_SEPARATOR); } sb.append(ia[i]); } } else if (Byte.TYPE.equals(componentType)) { byte[] ia = (byte[]) o; for (int i = 0; i < ia.length; i++) { if (i > 0) { sb.append(ELEMENT_SEPARATOR); } sb.append(ia[i]); } } } sb.append(ARRAY_SUFFIX); return sb; }
From source file:com.codelanx.codelanxlib.util.Reflections.java
/** * Returns a "default value" of -1 or {@code false} for a default type's * class or autoboxing class. Will return {@code null} if not relevant to * primitives/*from w w w. j a va2 s . c om*/ * * @since 0.1.0 * @version 0.1.0 * * @param <T> The type of the primitive * @param c The primitive class * @return The default value, or {@code null} if not a primitive */ public static <T> T defaultPrimitiveValue(Class<T> c) { if (c.isPrimitive() || Primitives.isWrapperType(c)) { c = Primitives.unwrap(c); T back; if (c == boolean.class) { back = c.cast(false); } else if (c == char.class) { //god help me back = c.cast((char) -1); } else if (c == float.class) { back = c.cast(-1F); } else if (c == long.class) { back = c.cast(-1L); } else if (c == double.class) { back = c.cast(-1D); } else if (c == int.class) { back = c.cast(-1); //ha } else if (c == short.class) { back = c.cast((short) -1); } else if (c == byte.class) { back = c.cast((byte) -1); } } return null; }
From source file:com.expressui.core.util.ReflectionUtil.java
/** * Asks if given type is a number type./*from w w w .j a va2 s . c o m*/ * * @param type type to check * @return true if a number */ public static boolean isNumberType(Class type) { Class clazz; if (type.isPrimitive()) { clazz = ClassUtils.primitiveToWrapper(type); } else { clazz = type; } return Number.class.isAssignableFrom(clazz); }
From source file:com.expressui.core.util.ReflectionUtil.java
/** * Asks if given type is a boolean type. * * @param type type to check// ww w . jav a2 s . c om * @return true if a boolean */ public static boolean isBooleanType(Class type) { Class clazz; if (type.isPrimitive()) { clazz = ClassUtils.primitiveToWrapper(type); } else { clazz = type; } return Boolean.class.isAssignableFrom(clazz); }