List of usage examples for java.lang Class isArray
@HotSpotIntrinsicCandidate public native boolean isArray();
From source file:com.xwtec.xwserver.util.json.util.JSONUtils.java
/** * Tests if a Class represents an array or Collection. *//*from www.java 2 s. c o m*/ public static boolean isArray(Class clazz) { return clazz != null && (clazz.isArray() || Collection.class.isAssignableFrom(clazz) || (JSONArray.class.isAssignableFrom(clazz))); }
From source file:org.forgerock.openig.util.Json.java
/** * Verify that the given parameter object is of a JSON compatible type (recursively). If no exception is thrown that * means the parameter can be used in the JWT session (that is a JSON value). * * @param trail//w w w . j a va 2 s . c om * pointer to the verified object * @param value * object to verify */ public static void checkJsonCompatibility(final String trail, final Object value) { // Null is OK if (value == null) { return; } Class<?> type = value.getClass(); Object object = value; // JSON supports Boolean if (object instanceof Boolean) { return; } // JSON supports Chars (as String) if (object instanceof Character) { return; } // JSON supports Numbers (Long, Float, ...) if (object instanceof Number) { return; } // JSON supports String if (object instanceof CharSequence) { return; } // Consider array like a List if (type.isArray()) { object = Arrays.asList((Object[]) value); } if (object instanceof List) { List<?> list = (List<?>) object; for (int i = 0; i < list.size(); i++) { checkJsonCompatibility(format("%s[%d]", trail, i), list.get(i)); } return; } if (object instanceof Map) { Map<?, ?> map = (Map<?, ?>) object; for (Map.Entry<?, ?> entry : map.entrySet()) { checkJsonCompatibility(format("%s/%s", trail, entry.getKey()), entry.getValue()); } return; } throw new IllegalArgumentException( format("The object referenced through '%s' cannot be safely serialized as JSON", trail)); }
From source file:me.xiaopan.android.gohttp.requestobject.RequestParser.java
/** * ?type//from w w w .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:io.github.moosbusch.lumpi.util.LumpiUtil.java
public static boolean isCollectionOrSequenceOrArrayOrEnum(Class<?> type) { if (!((ClassUtils.isAssignable(type, java.util.Collection.class))) || (ClassUtils.isAssignable(type, org.apache.pivot.collections.Collection.class)) || (ClassUtils.isAssignable(type, org.apache.pivot.collections.Sequence.class))) { return type.isArray() || type.isEnum(); }// w ww .j a v a 2s . co m return true; }
From source file:com.sunchenbin.store.feilong.core.bean.ConvertUtil.java
/** * ? Primitive./* www .j a va2s . co m*/ * * @param o * the o * @return true, if checks if is primitive array * * @since 1.4.0 */ private static boolean isPrimitiveArray(Object o) { // Allocate a new Array Class<? extends Object> klass = o.getClass(); if (!klass.isArray()) { return false; } Class<?> componentType = klass.getComponentType(); // return componentType.isPrimitive(); }
From source file:com.github.dozermapper.core.util.MappingUtils.java
public static Class<?> determineCustomConverter(FieldMap fieldMap, Cache converterByDestTypeCache, CustomConverterContainer customConverterContainer, Class<?> srcClass, Class<?> destClass) { if (customConverterContainer == null) { return null; }/*from ww w . ja v a2 s. c o m*/ // This method is messy. Just trying to isolate the junk into this one method instead of spread across the mapping // processor until a better solution can be put into place // For indexed mapping, need to use the actual class at index to determine the custom converter. if (fieldMap != null && fieldMap.isDestFieldIndexed()) { if (destClass.isArray()) { destClass = destClass.getComponentType(); } else if (destClass.isAssignableFrom(Collection.class) && fieldMap.getDestHintContainer() != null && !fieldMap.getDestHintContainer().hasMoreThanOneHint()) { // use hint when trying to find a custom converter destClass = fieldMap.getDestHintContainer().getHint(); } } return findCustomConverter(converterByDestTypeCache, customConverterContainer, srcClass, destClass); }
From source file:com.dsj.core.beans.ObjectUtils.java
/** * Convert a primitive array to an object array of primitive wrapper * objects.//from w w w.j av a 2 s . c om * * @param primitiveArray * the primitive array * @return the object array * @throws IllegalArgumentException * if the parameter is not a primitive array */ public static Object[] toObjectArray(Object primitiveArray) { if (primitiveArray == null) { return new Object[0]; } Class clazz = primitiveArray.getClass(); if (!clazz.isArray() || !clazz.getComponentType().isPrimitive()) { throw new IllegalArgumentException("The specified parameter is not a primitive array"); } int length = Array.getLength(primitiveArray); if (length == 0) { return new Object[0]; } Class wrapperType = Array.get(primitiveArray, 0).getClass(); Object[] newArray = (Object[]) Array.newInstance(wrapperType, length); for (int i = 0; i < length; i++) { newArray[i] = Array.get(primitiveArray, i); } return newArray; }
From source file:jef.tools.collection.CollectionUtil.java
/** * ??//from w w w . j a va 2s . co m * * @param type * @return */ public static boolean isArrayOrCollection(Type type) { if (type instanceof GenericArrayType) { return true; } else if (type instanceof Class) { Class<?> rawType = (Class<?>) type; return rawType.isArray() || Collection.class.isAssignableFrom(rawType); } Class<?> rawType = GenericUtils.getRawClass(type); return Collection.class.isAssignableFrom(rawType); }
From source file:jef.tools.collection.CollectionUtil.java
/** * ?//from www . j a v a2 s .co m * * @param type * @return ??null,??? */ public static Type getComponentType(Type type) { if (type instanceof GenericArrayType) { return ((GenericArrayType) type).getGenericComponentType(); } else if (type instanceof Class) { Class<?> rawType = (Class<?>) type; if (rawType.isArray()) { return rawType.getComponentType(); } else if (Collection.class.isAssignableFrom(rawType)) { // ??Object return Object.class; } } else if (type instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) type; Type rawType = pType.getRawType(); if (isCollection(rawType)) { return pType.getActualTypeArguments()[0]; } } return null; }
From source file:com.taobao.rpc.doclet.RPCAPIInfoHelper.java
/** * ?/* w ww . j a v a2 s .c om*/ * * @param method * @return */ public static Object buildTypeStructure(Class<?> type, Type genericType, Type oriGenericType) { if ("void".equalsIgnoreCase(type.getName()) || ClassUtils.isPrimitiveOrWrapper(type) || String.class.isAssignableFrom(type) || Date.class.isAssignableFrom(type) || URL.class.isAssignableFrom(type)) { // return type.getName().replaceAll("java.lang.", "").replaceAll("java.util.", "").replaceAll("java.sql.", ""); } // end if if (type.isArray()) { // return new Object[] { buildTypeStructure(type.getComponentType(), type.getComponentType(), genericType) }; } // end if if (ClassUtils.isAssignable(Map.class, type)) { // Map return Map.class.getName(); } // end if if (type.isEnum()) { // Enum return Enum.class.getName(); } // end if boolean isCollection = type != null ? Collection.class.isAssignableFrom(type) : false; if (isCollection) { Type rawType = type; if (genericType != null) { if (genericType instanceof ParameterizedType) { ParameterizedType _type = (ParameterizedType) genericType; Type[] actualTypeArguments = _type.getActualTypeArguments(); rawType = actualTypeArguments[0]; } else if (genericType instanceof GenericArrayType) { rawType = ((GenericArrayType) genericType).getGenericComponentType(); } if (genericType instanceof WildcardType) { rawType = ((WildcardType) genericType).getUpperBounds()[0]; } } if (rawType == type) { return new Object[] { rawType.getClass().getName() }; } else { if (rawType.getClass().isAssignableFrom(TypeVariableImpl.class)) { return new Object[] { buildTypeStructure( (Class<?>) ((ParameterizedType) oriGenericType).getActualTypeArguments()[0], rawType, genericType) }; } else { if (rawType instanceof ParameterizedType) { if (((ParameterizedType) rawType).getRawType() == Map.class) { return new Object[] { Map.class.getName() }; } } if (oriGenericType == rawType) { return new Object[] { rawType.getClass().getName() }; } return new Object[] { buildTypeStructure((Class<?>) rawType, rawType, genericType) }; } } } if (type.isInterface()) { return type.getName(); } ClassInfo paramClassInfo = RPCAPIDocletUtil.getClassInfo(type.getName()); //added if (null == paramClassInfo) { System.out.println("failed to get paramClassInfo for :" + type.getName()); return null; } List<FieldInfo> typeConstructure = new ArrayList<FieldInfo>(); BeanWrapper bean = new BeanWrapperImpl(type); PropertyDescriptor[] propertyDescriptors = bean.getPropertyDescriptors(); Method readMethod; String name; for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { readMethod = propertyDescriptor.getReadMethod(); if (readMethod == null || "getClass".equals(readMethod.getName())) { continue; } name = propertyDescriptor.getName(); FieldInfo fieldInfo = paramClassInfo.getFieldInfo(name); if (readMethod.getReturnType().isAssignableFrom(type)) { String comment = "structure is the same with parent."; typeConstructure .add(FieldInfo.create(name, fieldInfo != null ? fieldInfo.getComment() : "", comment)); } else { typeConstructure.add( FieldInfo.create(name, fieldInfo != null ? fieldInfo.getComment() : "", buildTypeStructure( readMethod.getReturnType(), readMethod.getGenericReturnType(), genericType))); } // end if } return typeConstructure; }