List of usage examples for java.lang Class getComponentType
public Class<?> getComponentType()
From source file:com.laxser.blitz.lama.core.RowMapperFactoryImpl.java
private static Class<?> getRowType(Modifier modifier) { Class<?> returnClassType = modifier.getReturnType(); if (Collection.class.isAssignableFrom(returnClassType)) { return getRowTypeFromCollectionType(modifier, returnClassType); } else if (Map.class == returnClassType) { return getRowTypeFromMapType(modifier, returnClassType); } else if (returnClassType.isArray() && returnClassType != byte[].class) { // , ??/*from www . j ava 2s . c o m*/ return returnClassType.getComponentType(); } // DAO? return returnClassType; }
From source file:disko.DU.java
@SuppressWarnings("unchecked") public static <T> T cloneObject(T p, Mapping<Pair<Object, String>, Boolean> propertyFilter) throws Exception { if (p == null) return null; if (p instanceof Cloneable) { Method cloneMethod = p.getClass().getMethod("clone", (Class[]) null); if (cloneMethod != null) return (T) cloneMethod.invoke(p, (Object[]) null); } else if (p.getClass().isArray()) { Object[] A = (Object[]) p; Class<?> type = p.getClass(); Object[] ac = (Object[]) Array.newInstance(type.getComponentType(), A.length); for (int i = 0; i < A.length; i++) ac[i] = cloneObject(A[i], propertyFilter); return (T) ac; } else if (identityCloneClasses.contains(p.getClass())) return p; ////from w w w .j ava2 s . c o m // Need to implement cloning ourselves. We do this by copying bean properties. // Constructor<?> cons = null; try { cons = p.getClass().getConstructor((Class[]) null); } catch (Throwable t) { return p; } Object copy = cons.newInstance((Object[]) null); if (p instanceof Collection) { Collection<Object> cc = (Collection<Object>) copy; for (Object el : (Collection<?>) p) cc.add(cloneObject(el, propertyFilter)); } else if (p instanceof Map) { Map<Object, Object> cm = (Map<Object, Object>) copy; for (Object key : ((Map<Object, Object>) p).keySet()) cm.put(key, cloneObject(((Map<Object, Object>) p).get(key), propertyFilter)); } else { BeanInfo bean_info = Introspector.getBeanInfo(p.getClass()); PropertyDescriptor beanprops[] = bean_info.getPropertyDescriptors(); if (beanprops == null || beanprops.length == 0) copy = p; else for (PropertyDescriptor desc : beanprops) { Method rm = desc.getReadMethod(); Method wm = desc.getWriteMethod(); if (rm == null || wm == null) continue; Object value = rm.invoke(p, (Object[]) null); if (propertyFilter == null || propertyFilter.eval(new Pair<Object, String>(p, desc.getName()))) value = cloneObject(value, propertyFilter); wm.invoke(copy, new Object[] { value }); } } return (T) copy; }
From source file:jp.furplag.util.commons.ObjectUtils.java
/** * substitute for {@code instanceof}.//from ww w.j av a 2 s . c om * * @param clazz the Object, return false if null. * @param classes array of {@link java.lang.Class}. * @return if true, {@code o.getClass()} (or Class<?> o) contains given Classes. */ public static boolean isAny(final Object o, final Class<?>... classes) { if (classes == null) return o == null; if (classes.length < 1) return false; Class<?> type = o == null ? null : (o instanceof Class) ? ((Class<?>) o) : o.getClass().isArray() ? o.getClass().getComponentType() : o.getClass(); for (Class<?> clazz : classes) { if (o == null && clazz == null) return true; if (o == null || clazz == null) continue; if (clazz.equals(type)) return true; if (!(o instanceof Class) && ClassUtils.primitiveToWrapper(type).equals(ClassUtils.primitiveToWrapper(clazz))) return true; if (clazz.isArray() && type.equals(clazz.getComponentType())) return true; } return false; }
From source file:net.paoding.rose.jade.rowmapper.DefaultRowMapperFactory.java
private static Class<?> getRowType(StatementMetaData statementMetaData) { Class<?> returnClassType = statementMetaData.getMethod().getReturnType(); if (Collection.class.isAssignableFrom(returnClassType)) { return getRowTypeFromCollectionType(statementMetaData, returnClassType); } else if (Map.class == returnClassType) { return getRowTypeFromMapType(statementMetaData, returnClassType); } else if (returnClassType.isArray() && returnClassType != byte[].class) { // , ??//w w w . j a v a 2s . c o m return returnClassType.getComponentType(); } // DAO? return returnClassType; }
From source file:jenkins.model.RunIdMigrator.java
/** * Tries to move/rename a file from one path to another. * Uses {@link java.nio.file.Files#move} when available. * Does not use {@link java.nio.file.StandardCopyOption#REPLACE_EXISTING} or any other options. * TODO candidate for moving to {@link Util} *///from w w w. j a v a 2s . c om static void move(File src, File dest) throws IOException { Class<?> pathC; try { pathC = Class.forName("java.nio.file.Path"); } catch (ClassNotFoundException x) { // Java 6, do our best if (dest.exists()) { throw new IOException(dest + " already exists"); } if (src.renameTo(dest)) { return; } throw new IOException("could not move " + src + " to " + dest); } try { Method toPath = File.class.getMethod("toPath"); Class<?> copyOptionAC = Class.forName("[Ljava.nio.file.CopyOption;"); Class.forName("java.nio.file.Files").getMethod("move", pathC, pathC, copyOptionAC).invoke(null, toPath.invoke(src), toPath.invoke(dest), Array.newInstance(copyOptionAC.getComponentType(), 0)); } catch (InvocationTargetException x) { Throwable cause = x.getCause(); if (cause instanceof IOException) { throw (IOException) cause; } else { throw new IOException(cause); } } catch (Exception x) { throw new IOException(x); } }
From source file:org.uimafit.factory.ConfigurationParameterFactory.java
private static String getConfigurationParameterType(Field field) { Class<?> parameterClass = field.getType(); String parameterClassName;//w ww . j av a 2 s .com if (parameterClass.isArray()) { parameterClassName = parameterClass.getComponentType().getName(); } else if (Collection.class.isAssignableFrom(parameterClass)) { ParameterizedType collectionType = (ParameterizedType) field.getGenericType(); parameterClassName = ((Class<?>) (collectionType.getActualTypeArguments()[0])).getName(); } else { parameterClassName = parameterClass.getName(); } String parameterType = javaUimaTypeMap.get(parameterClassName); if (parameterType == null) { return ConfigurationParameter.TYPE_STRING; } return parameterType; }
From source file:com.feilong.core.lang.ClassUtilTest.java
/** * class info map for LOGGER./*from w w w .j a v a2 s . c o m*/ * * @param klass * the klass * @return <code>klass</code> nullempty, {@link Collections#emptyMap()}<br> */ public static Map<String, Object> getClassInfoMapForLog(Class<?> klass) { if (isNullOrEmpty(klass)) { return Collections.emptyMap(); } Map<String, Object> map = new LinkedHashMap<String, Object>(); map.put("clz.getCanonicalName()", klass.getCanonicalName());//"com.feilong.core.date.DatePattern" map.put("clz.getName()", klass.getName());//"com.feilong.core.date.DatePattern" map.put("clz.getSimpleName()", klass.getSimpleName());//"DatePattern" map.put("clz.getComponentType()", klass.getComponentType()); // ?"". ,voidboolean?byte?char?short?int?long?float double?. map.put("clz.isPrimitive()", klass.isPrimitive()); // ?"".,. map.put("clz.isLocalClass()", klass.isLocalClass()); // ?"?".?,?,?"""??". map.put("clz.isMemberClass()", klass.isMemberClass()); //isSynthetic()?Class?"??".java??false,?true.,JVM???,java??"??"? map.put("clz.isSynthetic()", klass.isSynthetic()); map.put("clz.isArray()", klass.isArray()); map.put("clz.isAnnotation()", klass.isAnnotation()); //??true. map.put("clz.isAnonymousClass()", klass.isAnonymousClass()); map.put("clz.isEnum()", klass.isEnum()); return map; }
From source file:org.jfree.chart.demo.SampleXYSymbolicDataset.java
/** * Returns a clone of the array./*from w w w.ja v a 2 s.c om*/ * * @param arr * the array. * @return a clone. */ private static Object cloneArray(final Object arr) { if (arr == null) { return arr; } final Class cls = arr.getClass(); if (!cls.isArray()) { return arr; } final int length = Array.getLength(arr); final Object[] newarr = (Object[]) Array.newInstance(cls.getComponentType(), length); Object obj; for (int i = 0; i < length; i++) { obj = Array.get(arr, i); if (obj.getClass().isArray()) { newarr[i] = cloneArray(obj); } else { newarr[i] = obj; } } return newarr; }
From source file:com.mtgi.analytics.aop.BehaviorTrackingAdvice.java
/** * marshal the given value to a String. Only java builtin types, * and arrays of those types, are converted; everything else is * represented as "[qualified.type.name]" to avoid invoking * costly (or buggy) toString() methods. *//*w w w .java 2 s .com*/ private static final void logValue(EventDataElement parent, String elementName, Class<?> expectedType, Object arg) { EventDataElement element = parent.addElement(elementName); if (arg == null) return; Class<?> type = arg.getClass(); //log the concrete type of the argument if it differs from the expected type (i.e. is a subclass) //the primitive type checks avoid logging redundant type info for autoboxed values if (type != expectedType && !(expectedType.isPrimitive() || type.isPrimitive())) element.add("type", type.getName()); //TODO: use annotations or some other configuration for custom //parameter logging? String value = "{object}"; if (type.isArray()) { if (shouldLog(type.getComponentType())) value = toStringArray(arg); } else { if (shouldLog(type)) value = arg.toString(); } element.setText(value); }
From source file:org.uimafit.factory.ConfigurationParameterFactory.java
/** * A factory method for creating a ConfigurationParameter object. *//*w ww . j a v a 2 s . com*/ public static ConfigurationParameter createPrimitiveParameter(String name, Class<?> parameterClass, String parameterDescription, boolean isMandatory) { String parameterClassName; if (parameterClass.isArray()) { parameterClassName = parameterClass.getComponentType().getName(); } else { parameterClassName = parameterClass.getName(); } String parameterType = javaUimaTypeMap.get(parameterClassName); if (parameterType == null) { // If we cannot map the type, we'll try to convert it to a String parameterType = ConfigurationParameter.TYPE_STRING; } return createPrimitiveParameter(name, parameterType, parameterDescription, parameterClass.isArray(), isMandatory); }