List of usage examples for java.lang Class isAssignableFrom
@HotSpotIntrinsicCandidate public native boolean isAssignableFrom(Class<?> cls);
From source file:de.pribluda.android.jsonmarshaller.JSONMarshaller.java
/** * recursively marshall to JSON/* ww w. j ava 2s. c o m*/ * * @param sink * @param object */ static void marshallRecursive(JSONObject sink, Object object) throws JSONException, InvocationTargetException, IllegalAccessException, NoSuchMethodException { // nothing to marshall if (object == null) return; // primitive object is a field and does not interes us here if (object.getClass().isPrimitive()) return; // object not null, and is not primitive - iterate through getters for (Method method : object.getClass().getMethods()) { // our getters are parameterless and start with "get" if ((method.getName().startsWith(GETTER_PREFIX) && method.getName().length() > BEGIN_INDEX || method.getName().startsWith(IS_PREFIX) && method.getName().length() > IS_LENGTH) && (method.getModifiers() & Modifier.PUBLIC) != 0 && method.getParameterTypes().length == 0 && method.getReturnType() != void.class) { // is return value primitive? Class<?> type = method.getReturnType(); if (type.isPrimitive() || String.class.equals(type)) { // it is, marshall it Object val = method.invoke(object); if (val != null) { sink.put(propertize(method.getName()), val); } continue; } else if (type.isArray()) { Object val = marshallArray(method.invoke(object)); if (val != null) { sink.put(propertize(method.getName()), val); } continue; } else if (type.isAssignableFrom(Date.class)) { Date date = (Date) method.invoke(object); if (date != null) { sink.put(propertize(method.getName()), date.getTime()); } continue; } else if (type.isAssignableFrom(Boolean.class)) { Boolean b = (Boolean) method.invoke(object); if (b != null) { sink.put(propertize(method.getName()), b.booleanValue()); } continue; } else if (type.isAssignableFrom(Integer.class)) { Integer i = (Integer) method.invoke(object); if (i != null) { sink.put(propertize(method.getName()), i.intValue()); } continue; } else if (type.isAssignableFrom(Long.class)) { Long l = (Long) method.invoke(object); if (l != null) { sink.put(propertize(method.getName()), l.longValue()); } continue; } else { // does it have default constructor? try { if (method.getReturnType().getConstructor() != null) { Object val = marshall(method.invoke(object)); if (val != null) { sink.put(propertize(method.getName()), val); } continue; } } catch (NoSuchMethodException ex) { // just ignore it here, it means no such constructor was found } } } } }
From source file:me.xiaopan.android.gohttp.requestobject.RequestParser.java
/** * ?type/*w ww. jav a 2s.c o m*/ */ private static boolean isArrayByType(Field field, Class<?> type) { Class<?> fieldType = field.getType(); return fieldType.isArray() && type.isAssignableFrom(fieldType.getComponentType()); }
From source file:ca.uhn.fhir.rest.method.MethodUtil.java
@SuppressWarnings("unchecked") public static <T extends IIdType> T convertIdToType(IIdType value, Class<T> theIdParamType) { if (value != null && !theIdParamType.isAssignableFrom(value.getClass())) { IIdType newValue = ReflectionUtil.newInstance(theIdParamType); newValue.setValue(value.getValue()); value = newValue;/*from www . ja v a 2s .c o m*/ } return (T) value; }
From source file:com.initialxy.cordova.themeablebrowser.ThemeableBrowserUnmarshaller.java
/** * Given an object extracted from JSONObject field, convert it to an * appropriate object with type appropriate for given type so that it can be * assigned to the associated field of the ummarshalled object. eg. * JSONObject value from a JSONObject field probably needs to be * unmarshalled to a class instance. Double from JSONObject may need to be * converted to Float. etc.// ww w .j av a2 s .c o m * * @param val Value extracted from JSONObject field. * @param genericType Type to convert to. Must be generic type. ie. From * field.getGenericType(). * @return Object of the given type so it can be assinged to field with * field.set(). * @throws com.initialxy.cordova.themeablebrowser.ThemeableBrowserUnmarshaller.TypeMismatchException */ private static Object valToType(Object val, Type genericType) { Object result = null; boolean isArray = false; Class<?> rawType = null; if (genericType instanceof ParameterizedType) { rawType = (Class<?>) ((ParameterizedType) genericType).getRawType(); } else if (genericType instanceof GenericArrayType) { rawType = List.class; isArray = true; } else { rawType = (Class<?>) genericType; } isArray = isArray || rawType.isArray(); if (val != null && val != JSONObject.NULL) { if (rawType.isAssignableFrom(String.class)) { if (val instanceof String) { result = val; } else { throw new TypeMismatchException(rawType, val.getClass()); } } else if (isPrimitive(rawType)) { result = convertToPrimitiveFieldObj(val, rawType); } else if (isArray || rawType.isAssignableFrom(List.class)) { if (val instanceof JSONArray) { Type itemType = getListItemType(genericType); result = JSONToList((JSONArray) val, itemType); if (isArray) { List<?> list = (List<?>) result; Class<?> itemClass = null; if (itemType instanceof ParameterizedType) { itemClass = (Class<?>) ((ParameterizedType) itemType).getRawType(); } else { itemClass = (Class<?>) itemType; } result = Array.newInstance(itemClass, list.size()); int cnt = 0; for (Object i : list) { Array.set(result, cnt, i); cnt += 1; } } } else { throw new TypeMismatchException(JSONArray.class, val.getClass()); } } else if (val instanceof JSONObject) { result = JSONToObj((JSONObject) val, rawType); } } return result; }
From source file:com.evolveum.midpoint.schema.util.WfContextUtil.java
@SuppressWarnings("unchecked") public static <T extends CaseEventType> List<T> getEvents(@NotNull WfContextType wfc, @NotNull Class<T> clazz) { return wfc.getEvent().stream().filter(e -> clazz.isAssignableFrom(e.getClass())).map(e -> (T) e) .collect(Collectors.toList()); }
From source file:jp.terasoluna.fw.collector.util.ControlBreakChecker.java
/** * ???????????.<br>// w ww.j a v a 2 s . c o m * <ul> * <li>??null?????????. * <ul> * <li>compareStrategy?null?????compareStrategy?</li> * <li>compareStrategy?null???????????? * <ul> * <li>?Comparable????Comparable#compareTo? * <li>?Class??????????Object#equals? * <li>2????org.apache.commons.lang.builder.EqualsBuilder#reflectionEquals? * </ul> * </li> * </ul> * </li> * <li>??null?????????.</li> * <li>??????null???????????.</li> * </ul> * @param value1 Object * @param value2 Object * @param compareStrategy CompareStrategy * @return ???:true / ???????:false */ @SuppressWarnings({ "rawtypes", "unchecked" }) protected static boolean equalsObjects(Object value1, Object value2, CompareStrategy compareStrategy) { if (value1 != null && value2 != null) { // value1,value2????????? if (compareStrategy != null) { return compareStrategy.equalsObjects(value1, value2); } else { Class<? extends Object> clazz = value1.getClass(); if (value1 instanceof Comparable) { // value1?Comparable?????compareTo?? return (((Comparable) value1).compareTo(value2) == 0); } else if (clazz.isAssignableFrom(Class.class)) { // value1?Class?????????Object#equals?? return value1.equals(value2); } else { // ?????reflectionEquals?? return EqualsBuilder.reflectionEquals(value1, value2); } } } else if (value1 == null && value2 == null) { // value1,value2??????????? return true; } // value1,value2?????????? return false; }
From source file:adalid.commons.util.TimeUtils.java
public static java.util.Date toJavaDate(Object object, Class<? extends java.util.Date> targetClass) { if (object == null) { return null; }/*from ww w. ja v a2 s .c om*/ Class<?> sourceClass = object.getClass(); if (targetClass == null || targetClass.isAssignableFrom(sourceClass)) { return (java.util.Date) object; } else if (targetClass.equals(Date.class)) { return toDate(object); } else if (targetClass.equals(Time.class)) { return toTime(object); } else if (targetClass.equals(Timestamp.class)) { return toTimestamp(object); } else { return (java.util.Date) object; } }
From source file:com.evolveum.midpoint.test.util.MidPointAsserts.java
public static <F extends FocusType> void assertAssignments(PrismObject<F> user, Class expectedType, int expectedNumber) { F userType = user.asObjectable();//from w w w .j a v a2s . c o m int actualAssignments = 0; List<AssignmentType> assignments = userType.getAssignment(); for (AssignmentType assignment : assignments) { ObjectReferenceType targetRef = assignment.getTargetRef(); if (targetRef != null) { QName type = targetRef.getType(); if (type != null) { Class<? extends ObjectType> assignmentTargetClass = ObjectTypes.getObjectTypeFromTypeQName(type) .getClassDefinition(); if (expectedType.isAssignableFrom(assignmentTargetClass)) { actualAssignments++; } } } } assertEquals("Unexepected number of assignments of type " + expectedType + " in " + user + ": " + userType.getAssignment(), expectedNumber, actualAssignments); }
From source file:com.evolveum.midpoint.schema.util.WfContextUtil.java
public static <T extends WorkItemEventType> List<T> getWorkItemEvents(@NotNull WfContextType wfc, @NotNull String workItemId, Class<T> clazz) { return wfc.getEvent().stream() .filter(e -> clazz.isAssignableFrom(e.getClass()) && workItemId.equals(((WorkItemEventType) e).getExternalWorkItemId())) .map(e -> (T) e).collect(Collectors.toList()); }
From source file:adalid.core.XS1.java
static boolean checkFieldAnnotation(boolean log, Field field, Class<? extends Annotation> annotation, Class<?>[] validTypes) { String name = field.getName(); Class<?> type = field.getDeclaringClass(); String string;//ww w . ja va 2 s . c o m int modifiers = field.getModifiers(); if (Modifier.isStatic(modifiers) || Modifier.isFinal(modifiers)) { string = "field " + name + " has static and/or final modifier"; if (log) { logFieldAnnotationErrorMessage(name, type, annotation, string); } return false; } int length = validTypes == null ? 0 : validTypes.length; if (length < 1) { return true; } Class<?> ft = getTrueType(field.getType()); String[] strings = new String[length]; int i = 0; for (Class<?> vt : validTypes) { if (vt.isAssignableFrom(ft)) { return true; } strings[i++] = vt.getSimpleName(); } string = "type of " + name + " is not " + StringUtils.join(strings, " or "); if (log) { logFieldAnnotationErrorMessage(name, type, annotation, string); } return false; }