List of usage examples for java.lang Class isArray
@HotSpotIntrinsicCandidate public native boolean isArray();
From source file:com.github.michalbednarski.intentslab.Utils.java
public static Object[] deepCastArray(Object[] array, Class targetType) { assert targetType.isArray() && !targetType.getComponentType().isPrimitive(); if (targetType.isInstance(array) || array == null) { return array; }/*www .j ava 2s . c om*/ Class componentType = targetType.getComponentType(); Class nestedComponentType = componentType.getComponentType(); Object[] newArray = (Object[]) Array.newInstance(componentType, array.length); if (nestedComponentType != null && !nestedComponentType.isPrimitive()) { for (int i = 0; i < array.length; i++) { newArray[i] = deepCastArray((Object[]) array[i], nestedComponentType); } } else { System.arraycopy(array, 0, newArray, 0, array.length); } return newArray; }
From source file:cop.raml.mocks.MockUtils.java
public static TypeElementMock createElement(@NotNull Class<?> cls) throws ClassNotFoundException { if (cls.isPrimitive()) return createPrimitiveElement(cls); if (cls.isEnum()) return createEnumElement(cls); if (cls.isArray()) return setAnnotations(createArrayElement(cls.getComponentType()), cls); if (Collection.class.isAssignableFrom(cls)) return setAnnotations(createCollectionElement(), cls); return createClassElement(cls); }
From source file:com.liferay.wsrp.proxy.TypeConvertorUtil.java
public static Object convert(Object source, int sourceVersion) throws Exception { if (source == null) { return null; }// w w w. j ava 2s. c o m String sourcePackage = _V1_PACKAGE; String destinationPackage = _V2_PACKAGE; if (sourceVersion == 2) { sourcePackage = _V2_PACKAGE; destinationPackage = _V1_PACKAGE; } Class<?> sourceClass = source.getClass(); String sourceClassName = sourceClass.getSimpleName(); Object destination = null; if (sourceClass.isArray()) { destination = source; Class<?> componentType = sourceClass.getComponentType(); if (componentType.getName().contains(sourcePackage)) { Object[] sourceArray = (Object[]) source; Class<?> destinationComponentType = Class .forName(destinationPackage + componentType.getSimpleName()); Object[] destinationArray = (Object[]) Array.newInstance(destinationComponentType, sourceArray.length); for (int i = 0; i < sourceArray.length; i++) { Object sourceArrayValue = sourceArray[i]; destinationArray[i] = convert(sourceArrayValue, sourceVersion); } destination = destinationArray; } } else if (sourceClass == CookieProtocol.class) { CookieProtocol cookieProtocol = (CookieProtocol) source; destination = oasis.names.tc.wsrp.v2.types.CookieProtocol.fromValue(cookieProtocol.getValue()); } else if (sourceClass == oasis.names.tc.wsrp.v2.types.CookieProtocol.class) { oasis.names.tc.wsrp.v2.types.CookieProtocol cookieProtocol = (oasis.names.tc.wsrp.v2.types.CookieProtocol) source; destination = CookieProtocol.fromValue(cookieProtocol.getValue()); } else if (sourceClass == StateChange.class) { StateChange stateChange = (StateChange) source; destination = oasis.names.tc.wsrp.v2.types.StateChange.fromValue(stateChange.getValue()); } else if (sourceClass == oasis.names.tc.wsrp.v2.types.StateChange.class) { oasis.names.tc.wsrp.v2.types.StateChange stateChange = (oasis.names.tc.wsrp.v2.types.StateChange) source; destination = StateChange.fromValue(stateChange.getValue()); } else { Class<?> destinationClass = Class.forName(destinationPackage + sourceClassName); destination = destinationClass.newInstance(); Map<String, Object> sourceChildren = PropertyUtils.describe(source); for (Map.Entry<String, Object> sourceChildEntry : sourceChildren.entrySet()) { String sourceChildName = sourceChildEntry.getKey(); if (sourceChildName.equals("class")) { continue; } Object sourceChild = sourceChildEntry.getValue(); if (sourceChild == null) { continue; } _convert(sourceVersion, sourcePackage, sourceClass, sourceChild, sourceChildName, destination); } } return destination; }
From source file:com.mawujun.util.AnnotationUtils.java
/** * <p>Checks if the specified type is permitted as an annotation member.</p> * * <p>The Java language specification only permits certain types to be used * in annotations. These include {@link String}, {@link Class}, primitive * types, {@link Annotation}, {@link Enum}, and single-dimensional arrays of * these types.</p>// w ww. jav a 2 s . c om * * @param type the type to check, {@code null} * @return {@code true} if the type is a valid type to use in an annotation */ public static boolean isValidAnnotationMemberType(Class<?> type) { if (type == null) { return false; } if (type.isArray()) { type = type.getComponentType(); } return type.isPrimitive() || type.isEnum() || type.isAnnotation() || String.class.equals(type) || Class.class.equals(type); }
From source file:com.jsen.javascript.java.HostedJavaMethod.java
/** * Casts the given arguments into given expected types. * /*from ww w .ja v a 2s . c om*/ * @param expectedTypes Types into which should be casted the given arguments. * @param args Arguments to be casted. * @return Array of the casted arguments if casting was successful, otherwise null. */ public static Object[] castArgs(Class<?>[] expectedTypes, Object... args) { if (expectedTypes != null && args != null) { if (expectedTypes.length <= args.length + 1 && expectedTypes.length > 0) { Class<?> lastType = expectedTypes[expectedTypes.length - 1]; if (lastType.isArray()) { Class<?> arrayType = lastType.getComponentType(); boolean maybeVarargs = true; if (expectedTypes.length == args.length) { Object lastArg = args[args.length - 1]; Class<?> lastArgClass = (lastArg != null) ? lastArg.getClass() : null; maybeVarargs = lastArgClass != null && !ClassUtils.isAssignable(lastArgClass, lastType); } if (maybeVarargs) { for (int i = expectedTypes.length - 1; i < args.length; i++) { if (args[i] == null) { continue; } Class<?> argType = args[i].getClass(); if (!ClassUtils.isAssignable(argType, arrayType)) { maybeVarargs = false; break; } } if (maybeVarargs) { Object[] oldArgs = args; args = new Object[expectedTypes.length]; for (int i = 0; i < expectedTypes.length - 1; i++) { args[i] = oldArgs[i]; } Object[] varargs = new Object[oldArgs.length - expectedTypes.length + 1]; for (int i = expectedTypes.length - 1; i < oldArgs.length; i++) { varargs[i - expectedTypes.length + 1] = oldArgs[i]; } args[expectedTypes.length - 1] = varargs; } } } } if (expectedTypes.length == args.length) { Object[] castedArgs = new Object[args.length]; for (int i = 0; i < args.length; i++) { Object arg = args[i]; Class<?> expectedType = expectedTypes[i]; if (arg == null) { castedArgs[i] = null; } else if (arg == Undefined.instance) { castedArgs[i] = null; } else if (arg instanceof ConsString) { castedArgs[i] = ((ConsString) arg).toString(); } else if (arg instanceof Double && (expectedType.equals(Integer.class) || expectedType.equals(int.class) || expectedType.equals(Long.class) || expectedType.equals(long.class))) { castedArgs[i] = ((Double) arg).intValue(); } else { castedArgs[i] = JavaScriptEngine.jsToJava(arg); //castedArgs[i] = Context.jsToJava(castedArgs[i], expectedType); } castedArgs[i] = HostedJavaObject.wrap(expectedTypes[i], castedArgs[i]); } return castedArgs; } } return null; }
From source file:de.pribluda.android.jsonmarshaller.JSONUnmarshaller.java
/** * recursively populate array out of hierarchy of JSON Objects * * @param arrayClass original array class * @param json json object in question * @return//from w ww . jav a 2s . co m */ @SuppressWarnings({ "unchecked", "rawtypes" }) private static Object populateRecursive(Class arrayClass, Object json) throws JSONException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { if (arrayClass.isArray() && json instanceof JSONArray) { final int length = ((JSONArray) json).length(); final Class componentType = arrayClass.getComponentType(); Object retval = Array.newInstance(componentType, length); for (int i = 0; i < length; i++) { Array.set(retval, i, populateRecursive(componentType, ((JSONArray) json).get(i))); } return retval; } else { // this is leaf object, JSON needs to be unmarshalled, if (json instanceof JSONObject) { return unmarshall((JSONObject) json, arrayClass); } else { // while all others can be returned verbatim return json; } } }
From source file:com.googlecode.jmapper.util.GeneralUtility.java
/** * Determines if the specified Class parameter represents an array class. * //from www. j a va 2 s . c o m * @param aClass the Class to be checked * @return true if the specified Class parameter represents an array class; false otherwise. */ private static boolean isArray(Class<?> aClass) { return aClass.isArray(); }
From source file:net.radai.beanz.util.ReflectionUtil.java
public static boolean isArray(Type type) { if (type instanceof ParameterizedType) { return false; }//from ww w . jav a2 s . c o m if (type instanceof Class<?>) { Class<?> clazz = (Class<?>) type; return clazz.isArray(); } if (type instanceof GenericArrayType) { return true; } throw new UnsupportedOperationException(); }
From source file:com.springframework.beans.BeanUtils.java
/** * Find a JavaBeans PropertyEditor following the 'Editor' suffix convention * (e.g. "mypackage.MyDomainClass" -> "mypackage.MyDomainClassEditor"). * <p>Compatible to the standard JavaBeans convention as implemented by * {@link java.beans.PropertyEditorManager} but isolated from the latter's * registered default editors for primitive types. * @param targetType the type to find an editor for * @return the corresponding editor, or {@code null} if none found *//*from www . j a v a2 s . c om*/ public static PropertyEditor findEditorByConvention(Class<?> targetType) { if (targetType == null || targetType.isArray() || unknownEditorTypes.contains(targetType)) { return null; } ClassLoader cl = targetType.getClassLoader(); if (cl == null) { try { cl = ClassLoader.getSystemClassLoader(); if (cl == null) { return null; } } catch (Throwable ex) { // e.g. AccessControlException on Google App Engine if (logger.isDebugEnabled()) { logger.debug("Could not access system ClassLoader: " + ex); } return null; } } String editorName = targetType.getName() + "Editor"; try { Class<?> editorClass = cl.loadClass(editorName); if (!PropertyEditor.class.isAssignableFrom(editorClass)) { if (logger.isWarnEnabled()) { logger.warn("Editor class [" + editorName + "] does not implement [java.beans.PropertyEditor] interface"); } unknownEditorTypes.add(targetType); return null; } return (PropertyEditor) instantiateClass(editorClass); } catch (ClassNotFoundException ex) { if (logger.isDebugEnabled()) { logger.debug("No property editor [" + editorName + "] found for type " + targetType.getName() + " according to 'Editor' suffix convention"); } unknownEditorTypes.add(targetType); return null; } }
From source file:com.wavemaker.commons.util.TypeConversionUtils.java
/** * Return true iff the parameter is an Array or a Collection. * * @param clazz/*from ww w . ja v a 2 s . c om*/ * @return */ public static boolean isArray(Class<?> clazz) { return clazz != null && (Collection.class.isAssignableFrom(clazz) || clazz.isArray()); }