List of usage examples for java.lang Class equals
public boolean equals(Object obj)
From source file:com.vk.sdk.api.model.ParseUtils.java
/** * Parses object with follow rules:/*from w w w .j ava2 s. c o m*/ * * 1. All fields should had a public access. * 2. The name of the filed should be fully equal to name of JSONObject key. * 3. Supports parse of all Java primitives, all {@link String}, * arrays of primitive types, {@link String}s and {@link com.vk.sdk.api.model.VKApiModel}s, * list implementation line {@link com.vk.sdk.api.model.VKList}, {@link com.vk.sdk.api.model.VKAttachments.VKAttachment} or {@link com.vk.sdk.api.model.VKPhotoSizes}, * {@link com.vk.sdk.api.model.VKApiModel}s. * * 4. Boolean fields defines by vk_int == 1 expression. * @param object object to initialize * @param source data to read values * @return initialized according with given data object * @throws JSONException if source object structure is invalid */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static <T> T parseViaReflection(T object, JSONObject source) throws JSONException { if (source.has("response")) { source = source.optJSONObject("response"); } if (source == null) { return object; } for (Field field : object.getClass().getFields()) { field.setAccessible(true); String fieldName = field.getName(); Class<?> fieldType = field.getType(); Object value = source.opt(fieldName); if (value == null) { continue; } try { if (fieldType.isPrimitive() && value instanceof Number) { Number number = (Number) value; if (fieldType.equals(int.class)) { field.setInt(object, number.intValue()); } else if (fieldType.equals(long.class)) { field.setLong(object, number.longValue()); } else if (fieldType.equals(float.class)) { field.setFloat(object, number.floatValue()); } else if (fieldType.equals(double.class)) { field.setDouble(object, number.doubleValue()); } else if (fieldType.equals(boolean.class)) { field.setBoolean(object, number.intValue() == 1); } else if (fieldType.equals(short.class)) { field.setShort(object, number.shortValue()); } else if (fieldType.equals(byte.class)) { field.setByte(object, number.byteValue()); } } else { Object result = field.get(object); if (value.getClass().equals(fieldType)) { result = value; } else if (fieldType.isArray() && value instanceof JSONArray) { result = parseArrayViaReflection((JSONArray) value, fieldType); } else if (VKPhotoSizes.class.isAssignableFrom(fieldType) && value instanceof JSONArray) { Constructor<?> constructor = fieldType.getConstructor(JSONArray.class); result = constructor.newInstance((JSONArray) value); } else if (VKAttachments.class.isAssignableFrom(fieldType) && value instanceof JSONArray) { Constructor<?> constructor = fieldType.getConstructor(JSONArray.class); result = constructor.newInstance((JSONArray) value); } else if (VKList.class.equals(fieldType)) { ParameterizedType genericTypes = (ParameterizedType) field.getGenericType(); Class<?> genericType = (Class<?>) genericTypes.getActualTypeArguments()[0]; if (VKApiModel.class.isAssignableFrom(genericType) && Parcelable.class.isAssignableFrom(genericType) && Identifiable.class.isAssignableFrom(genericType)) { if (value instanceof JSONArray) { result = new VKList((JSONArray) value, genericType); } else if (value instanceof JSONObject) { result = new VKList((JSONObject) value, genericType); } } } else if (VKApiModel.class.isAssignableFrom(fieldType) && value instanceof JSONObject) { result = ((VKApiModel) fieldType.newInstance()).parse((JSONObject) value); } field.set(object, result); } } catch (InstantiationException e) { throw new JSONException(e.getMessage()); } catch (IllegalAccessException e) { throw new JSONException(e.getMessage()); } catch (NoSuchMethodException e) { throw new JSONException(e.getMessage()); } catch (InvocationTargetException e) { throw new JSONException(e.getMessage()); } catch (NoSuchMethodError e) { // ?????????? ???????: // ?? ?? ????????, ?? ? ????????? ???????? getFields() ???????? ??? ???. // ?????? ? ??????? ???????????, ????????? ?? ? ????????, ?????? Android ? ???????? ????????? ??????????. throw new JSONException(e.getMessage()); } } return object; }
From source file:com.springframework.beans.BeanUtils.java
/** * Check if the given type represents a "simple" value type: * a primitive, a String or other CharSequence, a Number, a Date, * a URI, a URL, a Locale or a Class.//from w ww . jav a 2 s. com * @param clazz the type to check * @return whether the given type represents a "simple" value type */ public static boolean isSimpleValueType(Class<?> clazz) { return ClassUtils.isPrimitiveOrWrapper(clazz) || clazz.isEnum() || CharSequence.class.isAssignableFrom(clazz) || Number.class.isAssignableFrom(clazz) || Date.class.isAssignableFrom(clazz) || clazz.equals(URI.class) || clazz.equals(URL.class) || clazz.equals(Locale.class) || clazz.equals(Class.class); }
From source file:com.vk.sdkweb.api.model.ParseUtils.java
/** * Parses object with follow rules:/*from w w w. java 2 s . c o m*/ * * 1. All fields should had a public access. * 2. The name of the filed should be fully equal to name of JSONObject key. * 3. Supports parse of all Java primitives, all {@link java.lang.String}, * arrays of primitive types, {@link java.lang.String}s and {@link com.vk.sdkweb.api.model.VKApiModel}s, * list implementation line {@link com.vk.sdkweb.api.model.VKList}, {@link com.vk.sdkweb.api.model.VKAttachments.VKAttachment} or {@link com.vk.sdkweb.api.model.VKPhotoSizes}, * {@link com.vk.sdkweb.api.model.VKApiModel}s. * * 4. Boolean fields defines by vk_int == 1 expression. * * @param object object to initialize * @param source data to read values * @param <T> type of result * @return initialized according with given data object * @throws JSONException if source object structure is invalid */ @SuppressWarnings("rawtypes") public static <T> T parseViaReflection(T object, JSONObject source) throws JSONException { if (source.has("response")) { source = source.optJSONObject("response"); } if (source == null) { return object; } for (Field field : object.getClass().getFields()) { field.setAccessible(true); String fieldName = field.getName(); Class<?> fieldType = field.getType(); Object value = source.opt(fieldName); if (value == null) { continue; } try { if (fieldType.isPrimitive() && value instanceof Number) { Number number = (Number) value; if (fieldType.equals(int.class)) { field.setInt(object, number.intValue()); } else if (fieldType.equals(long.class)) { field.setLong(object, number.longValue()); } else if (fieldType.equals(float.class)) { field.setFloat(object, number.floatValue()); } else if (fieldType.equals(double.class)) { field.setDouble(object, number.doubleValue()); } else if (fieldType.equals(boolean.class)) { field.setBoolean(object, number.intValue() == 1); } else if (fieldType.equals(short.class)) { field.setShort(object, number.shortValue()); } else if (fieldType.equals(byte.class)) { field.setByte(object, number.byteValue()); } } else { Object result = field.get(object); if (value.getClass().equals(fieldType)) { result = value; } else if (fieldType.isArray() && value instanceof JSONArray) { result = parseArrayViaReflection((JSONArray) value, fieldType); } else if (VKPhotoSizes.class.isAssignableFrom(fieldType) && value instanceof JSONArray) { Constructor<?> constructor = fieldType.getConstructor(JSONArray.class); result = constructor.newInstance((JSONArray) value); } else if (VKAttachments.class.isAssignableFrom(fieldType) && value instanceof JSONArray) { Constructor<?> constructor = fieldType.getConstructor(JSONArray.class); result = constructor.newInstance((JSONArray) value); } else if (VKList.class.equals(fieldType)) { ParameterizedType genericTypes = (ParameterizedType) field.getGenericType(); Class<?> genericType = (Class<?>) genericTypes.getActualTypeArguments()[0]; if (VKApiModel.class.isAssignableFrom(genericType) && Parcelable.class.isAssignableFrom(genericType) && Identifiable.class.isAssignableFrom(genericType)) { if (value instanceof JSONArray) { result = new VKList((JSONArray) value, genericType); } else if (value instanceof JSONObject) { result = new VKList((JSONObject) value, genericType); } } } else if (VKApiModel.class.isAssignableFrom(fieldType) && value instanceof JSONObject) { result = ((VKApiModel) fieldType.newInstance()).parse((JSONObject) value); } field.set(object, result); } } catch (InstantiationException e) { throw new JSONException(e.getMessage()); } catch (IllegalAccessException e) { throw new JSONException(e.getMessage()); } catch (NoSuchMethodException e) { throw new JSONException(e.getMessage()); } catch (InvocationTargetException e) { throw new JSONException(e.getMessage()); } catch (NoSuchMethodError e) { // ?: // , getFields() . // ? ? ?, ? ?, Android ? . throw new JSONException(e.getMessage()); } } return object; }
From source file:fi.foyt.foursquare.api.JSONFieldParser.java
/** * Parses single JSON object field into a value. Value might be of type String, Integer, Long, Double, Boolean or FoursquareEntity depending classes field type * /*from w ww .ja v a 2s .c om*/ * @param clazz class * @param jsonObject JSON Object * @param objectFieldName field to be parsed * @param skipNonExistingFields whether parser should ignore non-existing fields * @return field's value * @throws JSONException when JSON parsing error occures * @throws FoursquareApiException when something unexpected happens */ private static Object parseValue(Class<?> clazz, JSONObject jsonObject, String objectFieldName, boolean skipNonExistingFields) throws JSONException, FoursquareApiException { if (clazz.isArray()) { Object value = jsonObject.get(objectFieldName); JSONArray jsonArray; if (value instanceof JSONArray) { jsonArray = (JSONArray) value; } else { if ((value instanceof JSONObject) && (((JSONObject) value).has("items"))) { jsonArray = ((JSONObject) value).getJSONArray("items"); } else { throw new FoursquareApiException("JSONObject[\"" + objectFieldName + "\"] is neither a JSONArray nor a {count, items} object."); } } Class<?> arrayClass = clazz.getComponentType(); Object[] arrayValue = (Object[]) Array.newInstance(arrayClass, jsonArray.length()); for (int i = 0, l = jsonArray.length(); i < l; i++) { if (arrayClass.equals(String.class)) { arrayValue[i] = jsonArray.getString(i); } else if (arrayClass.equals(Integer.class)) { arrayValue[i] = jsonArray.getInt(i); } else if (arrayClass.equals(Long.class)) { arrayValue[i] = jsonArray.getLong(i); } else if (arrayClass.equals(Double.class)) { arrayValue[i] = jsonArray.getDouble(i); } else if (arrayClass.equals(Boolean.class)) { arrayValue[i] = jsonArray.getBoolean(i); } else if (isFoursquareEntity(arrayClass)) { arrayValue[i] = parseEntity(arrayClass, jsonArray.getJSONObject(i), skipNonExistingFields); } else { throw new FoursquareApiException("Unknown array type: " + arrayClass); } } return arrayValue; } else if (clazz.equals(String.class)) { return jsonObject.getString(objectFieldName); } else if (clazz.equals(Integer.class)) { return jsonObject.getInt(objectFieldName); } else if (clazz.equals(Long.class)) { return jsonObject.getLong(objectFieldName); } else if (clazz.equals(Double.class)) { return jsonObject.getDouble(objectFieldName); } else if (clazz.equals(Boolean.class)) { return jsonObject.getBoolean(objectFieldName); } else if (isFoursquareEntity(clazz)) { return parseEntity(clazz, jsonObject.getJSONObject(objectFieldName), skipNonExistingFields); } else { throw new FoursquareApiException("Unknown type: " + clazz); } }
From source file:com.link_intersystems.lang.Conversions.java
/** * * @param from//from ww w. j av a 2 s.co m * @param to * @return true if the conversion of type from to type to is a boxing * conversion. E.g. int -> Integer. * @since 1.0.0.0 * @see #getBoxingConversion(Class) */ public static boolean isBoxingConversion(Class<?> from, Class<?> to) { boolean isBoxingConversion = false; if (to != null) { Class<?> boxingConversion = getBoxingConversion(from); isBoxingConversion = to.equals(boxingConversion); } return isBoxingConversion; }
From source file:Classes.java
/** * Get the wrapper class for the given primitive type. * /*from www . java2 s. c o m*/ * @param type * Primitive class. * @return Wrapper class for primitive. * * @exception IllegalArgumentException * Type is not a primitive class */ public static Class getPrimitiveWrapper(final Class type) { if (!type.isPrimitive()) { throw new IllegalArgumentException("type is not a primitive class"); } for (int i = 0; i < PRIMITIVE_WRAPPER_MAP.length; i += 2) { if (type.equals(PRIMITIVE_WRAPPER_MAP[i])) return PRIMITIVE_WRAPPER_MAP[i + 1]; } // should never get here, if we do then PRIMITIVE_WRAPPER_MAP // needs to be updated to include the missing mapping System.out.println("Unreachable Statement Exception"); return null; }
From source file:com.link_intersystems.lang.Conversions.java
/** * * @param from/*from w w w. j a va 2 s . c o m*/ * @param to * @return true if the conversion of type from to type to is an unboxing * conversion. E.g. Integer -> int. * @since 1.0.0.0 * @see #getUnboxingConversion(Class) */ public static boolean isUnboxingConversion(Class<?> from, Class<?> to) { boolean isUnboxingConversion = false; if (to != null) { Class<?> unboxedClass = getUnboxingConversion(from); isUnboxingConversion = to.equals(unboxedClass); } return isUnboxingConversion; }
From source file:me.rojo8399.placeholderapi.impl.utils.TypeUtils.java
public static List<?> unboxPrimitiveArray(Object primArr) { if (primArr.getClass().isArray()) { if (primArr.getClass().getComponentType().isPrimitive()) { Class<?> primClazz = primArr.getClass().getComponentType(); if (primClazz.equals(int.class)) { int[] a = (int[]) primArr; List<Integer> out = new ArrayList<>(); for (int i : a) { out.add(i);/*from w w w. j a v a 2s .c o m*/ } return out; } if (primClazz.equals(long.class)) { long[] a = (long[]) primArr; List<Long> out = new ArrayList<>(); for (long i : a) { out.add(i); } return out; } if (primClazz.equals(short.class)) { short[] a = (short[]) primArr; List<Short> out = new ArrayList<>(); for (short i : a) { out.add(i); } return out; } if (primClazz.equals(byte.class)) { byte[] a = (byte[]) primArr; List<Byte> out = new ArrayList<>(); for (byte i : a) { out.add(i); } return out; } if (primClazz.equals(char.class)) { char[] a = (char[]) primArr; List<Character> out = new ArrayList<>(); for (char i : a) { out.add(i); } return out; } if (primClazz.equals(boolean.class)) { boolean[] a = (boolean[]) primArr; List<Boolean> out = new ArrayList<>(); for (boolean i : a) { out.add(i); } return out; } if (primClazz.equals(float.class)) { float[] a = (float[]) primArr; List<Float> out = new ArrayList<>(); for (float i : a) { out.add(i); } return out; } if (primClazz.equals(double.class)) { double[] a = (double[]) primArr; List<Double> out = new ArrayList<>(); for (double i : a) { out.add(i); } return out; } } return Arrays.asList((Object[]) primArr); } else { return Collections.singletonList(primArr); } }
From source file:at.ac.tuwien.infosys.jcloudscale.utility.ReflectionUtil.java
public static Field findField(Class<?> type, String fieldName) throws NoSuchFieldException, SecurityException { if (type.equals(Object.class)) { return type.getDeclaredField(fieldName); }/*from w ww .ja v a2 s . com*/ // now that's an ugly construct :) Field field = null; try { field = type.getDeclaredField(fieldName); } catch (NoSuchFieldException e) { field = findField(type.getSuperclass(), fieldName); } return field; }
From source file:com.evolveum.midpoint.prism.xml.XmlTypeConverter.java
public static <T> T toJavaValue(Element xmlElement, Class<T> type) throws SchemaException { if (type.equals(Element.class)) { return (T) xmlElement; } else if (type.equals(QName.class)) { return (T) DOMUtil.getQNameValue(xmlElement); } else if (PolyString.class.isAssignableFrom(type)) { return (T) polyStringToJava(xmlElement); } else {/*w ww .j a v a2s . c o m*/ String stringContent = xmlElement.getTextContent(); if (stringContent == null) { return null; } T javaValue = toJavaValue(stringContent, type); if (javaValue == null) { throw new IllegalArgumentException( "Unknown type for conversion: " + type + "(element " + DOMUtil.getQName(xmlElement) + ")"); } return javaValue; } }