List of usage examples for java.lang Class isPrimitive
@HotSpotIntrinsicCandidate public native boolean isPrimitive();
From source file:com.wavemaker.commons.util.TypeConversionUtils.java
public static boolean primitivesMatch(Class<?> p1, Class<?> p2) { if (!p1.isPrimitive() && !p2.isPrimitive()) { return false; }/* w w w . j a v a 2s. co m*/ if (compare(p1, p2, Boolean.class, boolean.class)) { return true; } if (compare(p1, p2, Byte.class, byte.class)) { return true; } if (compare(p1, p2, Double.class, double.class)) { return true; } if (compare(p1, p2, Float.class, float.class)) { return true; } if (compare(p1, p2, Integer.class, int.class)) { return true; } if (compare(p1, p2, Long.class, long.class)) { return true; } if (compare(p1, p2, Short.class, short.class)) { return true; } if (compare(p1, p2, Character.class, char.class)) { return true; } return false; }
From source file:jp.go.nict.langrid.client.soap.io.SoapResponseParser.java
private static <T> T nodeToType(XPathWorkspace w, Node node, Class<T> clazz, Converter converter) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ConversionException, DOMException, ParseException { if (clazz.isPrimitive()) { return converter.convert(resolveHref(w, node).getTextContent(), clazz); } else if (clazz.equals(String.class)) { return clazz.cast(resolveHref(w, node).getTextContent()); } else if (clazz.equals(byte[].class)) { try {//from w w w . j av a 2 s .c o m return clazz.cast(Base64.decodeBase64(resolveHref(w, node).getTextContent().getBytes("ISO8859-1"))); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } else if (clazz.equals(Calendar.class)) { Date date = fmt.get().parse(resolveHref(w, node).getTextContent()); Calendar c = Calendar.getInstance(); c.setTime(date); return clazz.cast(c); } else if (clazz.isArray()) { Class<?> ct = clazz.getComponentType(); List<Object> elements = new ArrayList<Object>(); node = resolveHref(w, node); for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { if (!(child instanceof Element)) continue; elements.add(nodeToType(w, child, ct, converter)); } return clazz.cast(elements.toArray((Object[]) Array.newInstance(ct, elements.size()))); } else { T instance = clazz.newInstance(); node = resolveHref(w, node); for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { if (!(child instanceof Element)) continue; String nn = child.getLocalName(); Method setter = ClassUtil.findSetter(clazz, nn); setter.invoke(instance, nodeToType(w, resolveHref(w, child), setter.getParameterTypes()[0], converter)); } return instance; } }
From source file:io.github.benas.randombeans.util.ReflectionUtils.java
/** * Check if a field has a primitive type and matching default value which is set by the compiler. * * @param object instance to get the field value of * @param field field to check//from ww w . j a va 2 s.co m * @throws IllegalAccessException if field cannot be accessed */ public static boolean isPrimitiveFieldWithDefaultValue(final Object object, final Field field) throws IllegalAccessException { Class<?> fieldType = field.getType(); if (!fieldType.isPrimitive()) { return false; } Object fieldValue = getFieldValue(object, field); if (fieldValue == null) { return false; } if (fieldType.equals(boolean.class) && (boolean) fieldValue == false) { return true; } if (fieldType.equals(byte.class) && (byte) fieldValue == (byte) 0) { return true; } if (fieldType.equals(short.class) && (short) fieldValue == (short) 0) { return true; } if (fieldType.equals(int.class) && (int) fieldValue == 0) { return true; } if (fieldType.equals(long.class) && (long) fieldValue == 0L) { return true; } if (fieldType.equals(float.class) && (float) fieldValue == 0.0F) { return true; } if (fieldType.equals(double.class) && (double) fieldValue == 0.0D) { return true; } if (fieldType.equals(char.class) && (char) fieldValue == '\u0000') { return true; } return false; }
From source file:Main.java
public static boolean isBaseDateType(Field field) { Class clazz = field.getType(); return clazz.equals(String.class) || clazz.equals(Integer.class) || clazz.equals(Byte.class) || clazz.equals(Long.class) || clazz.equals(Double.class) || clazz.equals(Float.class) || clazz.equals(Character.class) || clazz.equals(Short.class) || clazz.equals(Boolean.class) || clazz.equals(Date.class) || clazz.equals(Date.class) || clazz.equals(java.sql.Date.class) || clazz.isPrimitive(); }
From source file:fit.TypeAdapter.java
public static TypeAdapter adapterFor(Class<?> type) throws UnsupportedOperationException { if (type.isPrimitive()) { if (type.equals(byte.class)) return new ByteAdapter(); if (type.equals(short.class)) return new ShortAdapter(); if (type.equals(int.class)) return new IntAdapter(); if (type.equals(long.class)) return new LongAdapter(); if (type.equals(float.class)) return new FloatAdapter(); if (type.equals(double.class)) return new DoubleAdapter(); if (type.equals(char.class)) return new CharAdapter(); if (type.equals(boolean.class)) return new BooleanAdapter(); throw new UnsupportedOperationException("can't yet adapt " + type); } else {//from w w w . j av a 2 s .c o m Object delegate = PARSE_DELEGATES.get(type); if (delegate instanceof DelegateClassAdapter) return (TypeAdapter) ((DelegateClassAdapter) delegate).clone(); if (delegate instanceof DelegateObjectAdapter) return (TypeAdapter) ((DelegateObjectAdapter) delegate).clone(); if (type.equals(Byte.class)) return new ClassByteAdapter(); if (type.equals(Short.class)) return new ClassShortAdapter(); if (type.equals(Integer.class)) return new ClassIntegerAdapter(); if (type.equals(Long.class)) return new ClassLongAdapter(); if (type.equals(Float.class)) return new ClassFloatAdapter(); if (type.equals(Double.class)) return new ClassDoubleAdapter(); if (type.equals(Character.class)) return new ClassCharacterAdapter(); if (type.equals(Boolean.class)) return new ClassBooleanAdapter(); if (type.isArray()) return new ArrayAdapter(); return new TypeAdapter(); } }
From source file:com.expressui.core.util.ReflectionUtil.java
/** * Asks if the bean's properties are empty. boolean properties that are false and numbers * that are zero are considered empty. String values that are zero-length are considered empty. * All other property types must be null to be considered empty. * * @param bean bean to check/* ww w . j a va 2s . com*/ * @return true if bean has no values */ public static boolean isBeanEmpty(Object bean) { if (bean == null) { return true; } WrapDynaBean wrapDynaBean = new WrapDynaBean(bean); DynaProperty[] properties = wrapDynaBean.getDynaClass().getDynaProperties(); for (DynaProperty property : properties) { String propertyName = property.getName(); Class propertyType = property.getType(); Object value = wrapDynaBean.get(propertyName); if (propertyType.isPrimitive()) { if (value instanceof Number && !value.toString().equals("0")) { return false; } else if (value instanceof Boolean && !value.toString().equals("false")) { return false; } else if (!value.toString().isEmpty()) { return false; } } else if (value != null) { if (!(value instanceof Collection)) { String convertedStringValue = ConvertUtils.convert(value); if (!StringUtil.isEmpty(convertedStringValue)) { return false; } } } } return true; }
From source file:nz.co.senanque.validationengine.ConvertUtils.java
public static Comparable<?> convertTo(Class<?> clazz, Object obj) { if (obj == null) { return null; }//from ww w .j av a 2s. c om if (clazz.isAssignableFrom(obj.getClass())) { return (Comparable<?>) obj; } if (clazz.isPrimitive()) { if (clazz.equals(Long.TYPE)) { clazz = Long.class; } else if (clazz.equals(Integer.TYPE)) { clazz = Integer.class; } else if (clazz.equals(Float.TYPE)) { clazz = Float.class; } else if (clazz.equals(Double.TYPE)) { clazz = Double.class; } else if (clazz.equals(Boolean.TYPE)) { clazz = Boolean.class; } } if (Number.class.isAssignableFrom(clazz)) { if (obj.getClass().equals(String.class)) { obj = new Double((String) obj); } if (!Number.class.isAssignableFrom(obj.getClass())) { throw new RuntimeException( "Cannot convert from " + obj.getClass().getName() + " to " + clazz.getName()); } Number number = (Number) obj; if (clazz.equals(Long.class)) { return new Long(number.longValue()); } if (clazz.equals(Integer.class)) { return new Integer(number.intValue()); } if (clazz.equals(Float.class)) { return new Float(number.floatValue()); } if (clazz.equals(Double.class)) { return new Double(number.doubleValue()); } if (clazz.equals(BigDecimal.class)) { return new BigDecimal(number.doubleValue()); } } final String oStr = String.valueOf(obj); if (clazz.equals(String.class)) { return oStr; } if (clazz.equals(java.util.Date.class)) { return java.sql.Date.valueOf(oStr); } if (clazz.equals(java.sql.Date.class)) { return java.sql.Date.valueOf(oStr); } if (clazz.equals(Boolean.class)) { return new Boolean(oStr); } throw new RuntimeException("Cannot convert from " + obj.getClass().getName() + " to " + clazz.getName()); }
From source file:net.servicefixture.converter.ObjectConverter.java
/** * Use the jexl expression to get the object from the * <code>GlobalContext</code>. * // w w w. j a v a 2 s . c o m * Note: the expression must follow <testTableKey>. <request[i]>| * <response>.* pattern. * * Throws FixtureException if the destType is not assignable from the object * type. */ private static Object evaluateJexlToken(String expression, Class<?> destType, boolean checkType) { Object result = JexlEvaluator.evaluate(expression, GlobalContext.getGlobalJexlContext()); if (result != null && checkType) { Class<?> resultType = result.getClass(); if (!destType.isAssignableFrom(resultType) && !(destType.isPrimitive() && resultType.getName().equalsIgnoreCase("java.lang." + destType.getName()))) { throw new ServiceFixtureException("Expression " + expression + " returns a object(" + resultType.getName() + ") that is not " + destType.getName() + " type."); } } return result; }
From source file:Main.java
public static boolean isBaseDateType(Field field) { Class<?> clazz = field.getType(); return clazz.equals(String.class) || clazz.equals(Integer.class) || clazz.equals(Byte.class) || clazz.equals(Long.class) || clazz.equals(Double.class) || clazz.equals(Float.class) || clazz.equals(Character.class) || clazz.equals(Short.class) || clazz.equals(Boolean.class) || clazz.equals(Date.class) || clazz.equals(Date.class) || clazz.equals(java.sql.Date.class) || clazz.isPrimitive(); }
From source file:Main.java
public static boolean isBaseDateType(Field field) { Class<?> clazz = field.getType(); return clazz.equals(String.class) || clazz.equals(Integer.class) || clazz.equals(Byte.class) || clazz.equals(Long.class) || clazz.equals(Double.class) || clazz.equals(Float.class) || clazz.equals(Character.class) || clazz.equals(Short.class) || clazz.equals(Boolean.class) || clazz.equals(Date.class) || clazz.equals(java.util.Date.class) || clazz.equals(java.sql.Date.class) || clazz.isPrimitive(); }