List of usage examples for java.lang Class getComponentType
public Class<?> getComponentType()
From source file:one.nio.serial.Repository.java
private static synchronized Serializer generateFor(Class<?> cls) { Serializer serializer = classMap.get(cls); if (serializer == null) { if (cls.isArray()) { get(cls.getComponentType()); serializer = new ObjectArraySerializer(cls); } else if ((cls.getModifiers() & ENUM) != 0) { if (cls.getSuperclass() != Enum.class) { serializer = get(cls.getSuperclass()); classMap.put(cls, serializer); return serializer; }/* w w w . j a v a 2s . c o m*/ serializer = new EnumSerializer(cls); } else if (Externalizable.class.isAssignableFrom(cls)) { serializer = new ExternalizableSerializer(cls); } else if (Collection.class.isAssignableFrom(cls) && !hasOptions(cls, FIELD_SERIALIZATION)) { serializer = new CollectionSerializer(cls); } else if (Map.class.isAssignableFrom(cls) && !hasOptions(cls, FIELD_SERIALIZATION)) { serializer = new MapSerializer(cls); } else if (Serializable.class.isAssignableFrom(cls)) { serializer = new GeneratedSerializer(cls); } else { serializer = new InvalidSerializer(cls); } serializer.generateUid(); provideSerializer(serializer); if (cls.isAnonymousClass()) { log.warn("Trying to serialize anonymous class: " + cls.getName()); anonymousClasses.incrementAndGet(); } Renamed renamed = cls.getAnnotation(Renamed.class); if (renamed != null) { renamedClasses.put(renamed.from(), cls); } } return serializer; }
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:net.radai.beanz.util.ReflectionUtil.java
public static Type getElementType(Type type) { if (isArray(type)) { if (type instanceof Class<?>) { Class<?> clazz = (Class<?>) type; return clazz.getComponentType(); }//from www .j av a2 s .co m if (type instanceof GenericArrayType) { GenericArrayType arrayType = (GenericArrayType) type; return arrayType.getGenericComponentType(); } throw new UnsupportedOperationException(); } if (isCollection(type)) { if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; Type[] typeArguments = parameterizedType.getActualTypeArguments(); if (typeArguments.length != 1) { throw new UnsupportedOperationException(); } return typeArguments[0]; } if (type instanceof Class<?>) { //this is something like a plain List(), no generic information, so type unknown return null; } throw new UnsupportedOperationException(); } if (isMap(type)) { if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; Type[] typeArguments = parameterizedType.getActualTypeArguments(); if (typeArguments.length != 2) { throw new UnsupportedOperationException(); } return typeArguments[1]; } if (type instanceof Class<?>) { //this is something like a plain Map(), no generic information, so type unknown return null; } } throw new UnsupportedOperationException(); }
From source file:org.apache.catalina.mbeans.MBeanDumper.java
/** * The following code to dump MBeans has been copied from JMXProxyServlet. * *///www . ja v a 2 s. c om public static String dumpBeans(MBeanServer mbeanServer, Set<ObjectName> names) { StringBuilder buf = new StringBuilder(); Iterator<ObjectName> it = names.iterator(); while (it.hasNext()) { ObjectName oname = it.next(); buf.append("Name: "); buf.append(oname.toString()); buf.append(CRLF); try { MBeanInfo minfo = mbeanServer.getMBeanInfo(oname); // can't be null - I think String code = minfo.getClassName(); if ("org.apache.commons.modeler.BaseModelMBean".equals(code)) { code = (String) mbeanServer.getAttribute(oname, "modelerType"); } buf.append("modelerType: "); buf.append(code); buf.append(CRLF); MBeanAttributeInfo attrs[] = minfo.getAttributes(); Object value = null; for (int i = 0; i < attrs.length; i++) { if (!attrs[i].isReadable()) continue; String attName = attrs[i].getName(); if ("modelerType".equals(attName)) continue; if (attName.indexOf("=") >= 0 || attName.indexOf(":") >= 0 || attName.indexOf(" ") >= 0) { continue; } try { value = mbeanServer.getAttribute(oname, attName); } catch (JMRuntimeException rme) { Throwable cause = rme.getCause(); if (cause instanceof UnsupportedOperationException) { if (log.isDebugEnabled()) { log.debug("Error getting attribute " + oname + " " + attName, rme); } } else if (cause instanceof NullPointerException) { if (log.isDebugEnabled()) { log.debug("Error getting attribute " + oname + " " + attName, rme); } } else { log.error("Error getting attribute " + oname + " " + attName, rme); } continue; } catch (Throwable t) { ExceptionUtils.handleThrowable(t); log.error("Error getting attribute " + oname + " " + attName, t); continue; } if (value == null) continue; String valueString; try { Class<?> c = value.getClass(); if (c.isArray()) { int len = Array.getLength(value); StringBuilder sb = new StringBuilder( "Array[" + c.getComponentType().getName() + "] of length " + len); if (len > 0) { sb.append(CRLF); } for (int j = 0; j < len; j++) { sb.append("\t"); Object item = Array.get(value, j); if (item == null) { sb.append("NULL VALUE"); } else { try { sb.append(escape(item.toString())); } catch (Throwable t) { ExceptionUtils.handleThrowable(t); sb.append("NON-STRINGABLE VALUE"); } } if (j < len - 1) { sb.append(CRLF); } } valueString = sb.toString(); } else { valueString = escape(value.toString()); } buf.append(attName); buf.append(": "); buf.append(valueString); buf.append(CRLF); } catch (Throwable t) { ExceptionUtils.handleThrowable(t); } } } catch (Throwable t) { ExceptionUtils.handleThrowable(t); } buf.append(CRLF); } return buf.toString(); }
From source file:org.apache.axis.encoding.ser.ArraySerializer.java
private static Class getComponentType(Class clazz) { if (clazz.isArray()) { return clazz.getComponentType(); } else if (java.util.Collection.class.isAssignableFrom(clazz)) { return Object.class; } else {/* w w w. j a va2s . c om*/ return null; } }
From source file:Main.java
/** * This method was added to address [JACKSON-53]: need to weed out * CGLib-injected "getCallbacks". /* www . jav a 2s .c o m*/ * At this point caller has detected a potential getter method * with name "getCallbacks" and we need to determine if it is * indeed injectect by Cglib. We do this by verifying that the * result type is "net.sf.cglib.proxy.Callback[]" *<p> * Also, see [JACKSON-177]; Hibernate may repackage cglib * it uses, so we better catch that too */ protected static boolean isCglibGetCallbacks(AnnotatedMethod am) { Class<?> rt = am.getRawType(); // Ok, first: must return an array type if (rt == null || !rt.isArray()) { return false; } /* And that type needs to be "net.sf.cglib.proxy.Callback". * Theoretically could just be a type that implements it, but * for now let's keep things simple, fix if need be. */ Class<?> compType = rt.getComponentType(); // Actually, let's just verify it's a "net.sf.cglib.*" class/interface Package pkg = compType.getPackage(); if (pkg != null) { String pname = pkg.getName(); if (pname.startsWith("net.sf.cglib") // also, as per [JACKSON-177] || pname.startsWith("org.hibernate.repackage.cglib")) { return true; } } return false; }
From source file:com.browseengine.bobo.serialize.JSONSerializer.java
public static JSONSerializable deSerialize(Class clz, JSONObject jsonObj) throws JSONSerializationException, JSONException { Iterator iter = jsonObj.keys(); if (!JSONSerializable.class.isAssignableFrom(clz)) { throw new JSONSerializationException(clz + " is not an instance of " + JSONSerializable.class); }//www . j a v a 2 s . co m JSONSerializable retObj; try { retObj = (JSONSerializable) clz.newInstance(); } catch (Exception e1) { throw new JSONSerializationException( "trouble with no-arg instantiation of " + clz.toString() + ": " + e1.getMessage(), e1); } if (JSONExternalizable.class.isAssignableFrom(clz)) { ((JSONExternalizable) retObj).fromJSON(jsonObj); return retObj; } while (iter.hasNext()) { String key = (String) iter.next(); try { Field f = clz.getDeclaredField(key); if (f != null) { f.setAccessible(true); Class type = f.getType(); if (type.isArray()) { JSONArray array = jsonObj.getJSONArray(key); int len = array.length(); Class cls = type.getComponentType(); Object newArray = Array.newInstance(cls, len); for (int k = 0; k < len; ++k) { loadObject(newArray, cls, k, array); } f.set(retObj, newArray); } else { loadObject(retObj, f, jsonObj); } } } catch (Exception e) { throw new JSONSerializationException(e.getMessage(), e); } } return retObj; }
From source file:net.radai.beanz.util.ReflectionUtil.java
public static String prettyPrint(Type type) { if (type instanceof GenericArrayType) { GenericArrayType genericArrayType = (GenericArrayType) type; return prettyPrint(genericArrayType.getGenericComponentType()) + "[]"; }/*w w w .j a va 2 s. c om*/ if (type instanceof WildcardType) { WildcardType wildcardType = (WildcardType) type; return wildcardType.toString(); } if (type instanceof TypeVariable) { TypeVariable<?> typeVariable = (TypeVariable) type; return typeVariable.getName(); } if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; StringBuilder sb = new StringBuilder(); sb.append(prettyPrint(parameterizedType.getRawType())); Type[] typeArguments = parameterizedType.getActualTypeArguments(); if (typeArguments != null && typeArguments.length > 0) { sb.append("<"); for (Type typeArgument : typeArguments) { sb.append(prettyPrint(typeArgument)).append(", "); } sb.delete(sb.length() - 2, sb.length()); // last ", " sb.append(">"); } return sb.toString(); } Class<?> clazz = (Class<?>) type; if (clazz.isArray()) { return prettyPrint(clazz.getComponentType()) + "[]"; } return clazz.getSimpleName(); }
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 .com*/ 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:org.eiichiro.bootleg.Types.java
/** * Returns the element type of the specified collection type. * The specified type must be collection or array. To make it sure, use * {@code #isCollection(Type)} method or {@code #isArray(Type)} method. * /*from w ww . j a v a2 s . c o m*/ * @param type Collection or array type. * @return The element type of the specified collection or array. * @throws IllegalArgumentException If the specified 'type' is not a * collection or array. */ public static Class<?> getElementType(Type type) { if (isCollection(type)) { if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; return (Class<?>) parameterizedType.getActualTypeArguments()[0]; } else { return Object.class; } } else if (isArray(type)) { if (type instanceof GenericArrayType) { GenericArrayType genericArrayType = (GenericArrayType) type; return (Class<?>) genericArrayType.getGenericComponentType(); } else { Class<?> clazz = (Class<?>) type; return clazz.getComponentType(); } } else { throw new IllegalArgumentException("'type' must be a collection or array"); } }