List of usage examples for java.lang Class isArray
@HotSpotIntrinsicCandidate public native boolean isArray();
From source file:com.github.haixing_hu.data.bean.Property.java
/** * Constructs a {@link Property}.//from www . ja v a 2s .c om * <p> * The constructor will determine the kind of the new property according to * the value type. If the value type is the class of an Java build-in array, * or a sub-class of the {@link java.util.List}, the new property will be an * indexed property; if the value type is the sub-class of * {@link java.util.Map}, the new property is a mapped property; otherwise, * the new property is a simple property. * * @param name * the name of the property, which cannot be {@code null} and must be * a valid property name. * @param type * the type of the values stored in the property, which cannot be * {@code null}. * @param contentType * the type of contents stored in the value of this property; or * {@code null} if this property is a simple property. But if the * property is not a simple property, this argument must be provided * and cannot be {@code null}. * @throws NullPointerException * if {@code name} or {@code type} is {@code null}; or if the new * property is not simple, but {@code contentType} is {@code null}. * @throws IllegalArgumentException * if {@code name} is not a valid property name. * @see #isValidName(String) */ public Property(final String name, final Class<?> type, @Nullable final Class<?> contentType) { this.name = requireNonEmpty("name", name); this.type = requireNonNull("type", type); this.contentType = contentType; if (!isValidName(name)) { throw new IllegalArgumentException("Invalid property name: " + name); } if (Map.class.isAssignableFrom(type)) { kind = Kind.MAPPED; } else if (List.class.isAssignableFrom(type)) { kind = Kind.INDEXED; } else if (type.isArray()) { kind = Kind.INDEXED; } else { kind = Kind.SIMPLE; } if ((kind != Kind.SIMPLE) && (contentType == null)) { throw new NullPointerException("The contentType cannot be null for non-simple property."); } }
From source file:com.adaptris.core.marshaller.xstream.AliasedElementReflectionConverter.java
private void writeValueToImplicitCollection(Object value, Map implicitCollections, Object result, String implicitFieldName) { Collection collection = (Collection) implicitCollections.get(implicitFieldName); if (collection == null) { Class physicalFieldType = reflectionProvider.getFieldType(result, implicitFieldName, null); if (physicalFieldType.isArray()) { collection = new ArraysList(physicalFieldType); } else {//from ww w.jav a 2 s . co m Class fieldType = mapper.defaultImplementationOf(physicalFieldType); if (!(Collection.class.isAssignableFrom(fieldType) || Map.class.isAssignableFrom(fieldType))) { throw new ObjectAccessException( "Field " + implicitFieldName + " of " + result.getClass().getName() + " is configured for an implicit Collection or Map, but field is of type " + fieldType.getName()); } if (pureJavaReflectionProvider == null) { pureJavaReflectionProvider = new PureJavaReflectionProvider(); } Object instance = pureJavaReflectionProvider.newInstance(fieldType); if (instance instanceof Collection) { collection = (Collection) instance; } else { Mapper.ImplicitCollectionMapping implicitCollectionMapping = mapper .getImplicitCollectionDefForFieldName(result.getClass(), implicitFieldName); collection = new MappingList((Map) instance, implicitCollectionMapping.getKeyFieldName()); } reflectionProvider.writeField(result, implicitFieldName, instance, null); } implicitCollections.put(implicitFieldName, collection); } collection.add(value); }
From source file:com.wavemaker.runtime.service.ElementType.java
/** * Create an ElementType with one level of children (populated by looking at the bean properties of javaType). This * method should not be used recursively. */// ww w .j a v a2s . c o m public ElementType(String name, Class<?> javaType, boolean isList) { this(name, javaType.getName(), isList); PropertyDescriptor[] pds; pds = PropertyUtils.getPropertyDescriptors(javaType); List<ElementType> elements = new ArrayList<ElementType>(pds.length); for (PropertyDescriptor pd : pds) { if (pd.getName().equals("class")) { continue; } if (pd.getReadMethod() == null && pd.getWriteMethod() == null) { continue; } Class<?> klass; Type type; if (pd.getReadMethod() != null) { klass = pd.getReadMethod().getReturnType(); type = pd.getReadMethod().getGenericReturnType(); } else { klass = pd.getWriteMethod().getParameterTypes()[0]; type = pd.getWriteMethod().getGenericParameterTypes()[0]; } ElementType element; if (klass.isArray()) { element = new ElementType(pd.getName(), klass.getComponentType().getName(), true); } else if (Collection.class.isAssignableFrom(klass) && type instanceof ParameterizedType) { ParameterizedType ptype = (ParameterizedType) type; Type aType = ptype.getActualTypeArguments()[0]; element = new ElementType(pd.getName(), ((Class<?>) aType).getName(), true); } else { element = new ElementType(pd.getName(), klass.getName()); } elements.add(element); } this.properties = elements; }
From source file:ch.systemsx.cisd.openbis.generic.server.authorization.DefaultReturnValueFilter.java
@Private final <T> Object proceed(final PersonPE person, final Method method, final Object returnValue, final IValidator<T> validator) { final String validatorClassName = validator.getClass().getName(); final Class<?> returnValueClass = returnValue.getClass(); if (returnValue instanceof List<?>) { final List<T> list = castToList(returnValue); try {//from w w w . ja v a 2 s . co m return proceedList(person, method, list, validator); } catch (final ClassCastException ex) { throw new IllegalArgumentException( String.format("Given validator class '%s' " + "and list type '%s' are not compatible.", validatorClassName, list.get(0).getClass().getName())); } } else if (returnValueClass.isArray()) { final T[] array = castToArray(returnValue); try { return proceedArray(person, method, validator, array); } catch (final ClassCastException e) { throw new IllegalArgumentException( String.format("Given validator class '%s' " + "and array type '%s' are not compatible.", validatorClassName, returnValueClass.getComponentType().getName())); } } else { final T value = cast(returnValue); try { return proceedValue(person, method, returnValue, validator, value); } catch (final ClassCastException ex) { throw new IllegalArgumentException(String.format( "Given validator class '%s' " + "and return value type '%s' are not compatible.", validatorClassName, returnValueClass.getName())); } } }
From source file:com.gdevelop.gwt.syncrpc.SyncClientSerializationStreamReader.java
/** * Deserialize an instance that is an array. Will default to deserializing as * an Object vector if the instance is not a primitive vector. * /*from ww w . ja v a2 s .c o m*/ * @param instanceClass * @param instance * @throws SerializationException */ @SuppressWarnings("unchecked") private Object deserializeArray(Class<?> instanceClass, Object instance) throws SerializationException { assert (instanceClass.isArray()); BoundedList<Object> buffer = (BoundedList<Object>) instance; VectorReader instanceReader = CLASS_TO_VECTOR_READER.get(instanceClass); if (instanceReader != null) { return instanceReader.read(this, buffer); } else { return SyncClientSerializationStreamReader.VectorReader.OBJECT_VECTOR.read(this, buffer); } }
From source file:org.kmnet.com.fw.web.message.MessagesPanelTag.java
/** * Writes the messages which have been set in the model * <p>//from w w w. java2 s .c o m * If messages stored in the model is in the form of a class that extends * {@code Iterable} or an Array, {@link #writeMessage(TagWriter, Object)} is * called multiple times, to write<br> * {@link #innerElement} and messages.<br> * * If there is only a single message, this method calls * {@link #writeMessage(TagWriter, Object)} only once * </p> * * @param tagWriter * @param messages * @throws JspException * If {@link JspException} occurs in caller writeMessage */ protected void writeMessages(TagWriter tagWriter, Object messages) throws JspException { Class<?> clazz = messages.getClass(); if (Iterable.class.isAssignableFrom(clazz)) { Iterable<?> col = (Iterable<?>) messages; for (Object message : col) { writeMessage(tagWriter, message); } } else if (clazz.isArray()) { Class<?> type = clazz.getComponentType(); if (Object.class.isAssignableFrom(type)) { Object[] arr = (Object[]) messages; for (Object message : arr) { writeMessage(tagWriter, message); } } else { int len = Array.getLength(messages); for (int i = 0; i < len; i++) { Object message = Array.get(messages, i); writeMessage(tagWriter, message); } } } else { writeMessage(tagWriter, messages); } }
From source file:edu.cmu.tetrad.util.TetradSerializableUtils.java
/** * Checks all of the classes in the serialization scope that implement * TetradSerializable to make sure all of their fields are either themselves * (a) primitive, (b) TetradSerializable, or (c) assignable from types * designated as safely serializable by virtue of being included in the * safelySerializableTypes array (see), or are arrays whose lowest order * component types satisfy either (a), (b), or (c). Safely serializable * classes in the Java API currently include collections classes, plus * String and Class. Collections classes are included, since their types * will be syntactically checkable in JDK 1.5. String and Class are members * of a broader type of Class whose safely can by checked by making sure * there is no way to pass into them via constructor or method argument any * object that is not TetradSerializable or safely serializable. But it's * easy enough now to just make a list.//from ww w . j a v a 2s .c o m * * @see #safelySerializableTypes */ public void checkNestingOfFields() { List classes = getAssignableClasses(new File(getSerializableScope()), TetradSerializable.class); boolean foundUnsafeField = false; for (Object aClass : classes) { Class clazz = (Class) aClass; if (TetradSerializableExcluded.class.isAssignableFrom(clazz)) { continue; } Field[] fields = clazz.getDeclaredFields(); FIELDS: for (Field field : fields) { // System.out.println(field); if (Modifier.isTransient(field.getModifiers())) { continue; } if (Modifier.isStatic(field.getModifiers())) { continue; } Class type = field.getType(); while (type.isArray()) { type = type.getComponentType(); } if (type.isPrimitive()) { continue; } if (type.isEnum()) { continue; } // // Printing out Collections fields temporarily. // if (Collection.class.isAssignableFrom(type)) { // System.out.println("COLLECTION FIELD: " + field); // } // // if (Map.class.isAssignableFrom(type)) { // System.out.println("MAP FIELD: " + field); // } if (TetradSerializable.class.isAssignableFrom(type) && !TetradSerializableExcluded.class.isAssignableFrom(clazz)) { continue; } for (Class safelySerializableClass : safelySerializableTypes) { if (safelySerializableClass.isAssignableFrom(type)) { continue FIELDS; } } // A reference in an inner class to the outer class. if (field.getName().equals("this$0")) { continue; } System.out.println("UNSAFE FIELD:" + field); foundUnsafeField = true; } } if (foundUnsafeField) { throw new RuntimeException("Unsafe serializable fields found. Please " + "fix immediately."); } }
From source file:org.ocelotds.core.services.ArgumentConvertor.java
private JavaType getJavaType(Type type) { Class clazz; logger.debug("Computing type of {} - {}", type.getClass(), type.toString()); if (type instanceof ParameterizedType) { clazz = (Class) ((ParameterizedType) type).getRawType(); } else {/*from w w w.ja va 2 s. c om*/ clazz = (Class) type; } JavaType javaType; Type actualType; if (Collection.class.isAssignableFrom(clazz)) { ParameterizedType pt = (ParameterizedType) type; actualType = pt.getActualTypeArguments()[0]; JavaType t1 = getJavaType(actualType); javaType = CollectionType.construct(Collection.class, t1); } else if (clazz.isArray()) { Class t = clazz.getComponentType(); JavaType t1 = getJavaType(t); javaType = ArrayType.construct(t1, null, null); } else if (Map.class.isAssignableFrom(clazz)) { ParameterizedType pt = (ParameterizedType) type; actualType = pt.getActualTypeArguments()[0]; JavaType t1 = getJavaType(actualType); actualType = pt.getActualTypeArguments()[1]; JavaType t2 = getJavaType(actualType); javaType = MapType.construct(Map.class, t1, t2); } else { javaType = SimpleType.construct(clazz); } return javaType; }
From source file:com.flexive.faces.renderer.FxSelectRenderer.java
protected Object convertSelectManyValuesForModel(FacesContext context, UISelectMany uiSelectMany, Class modelType, String[] newValues) { Object result = null;//from w w w . j a v a2 s . com if (modelType.isArray()) result = convertSelectManyValues(context, uiSelectMany, modelType, newValues); else if (List.class.isAssignableFrom(modelType)) { Object[] values = (Object[]) convertSelectManyValues(context, uiSelectMany, Object[].class, newValues); // perform a manual copy as the Array returned from // Arrays.asList() isn't mutable. It seems a waste // to also call Collections.addAll(Arrays.asList()) List<Object> l = new ArrayList<Object>(values.length); //noinspection ManualArrayToCollectionCopy for (Object v : values) l.add(v); result = l; } return result; }
From source file:com.gdevelop.gwt.syncrpc.SyncClientSerializationStreamReader.java
private void deserializeWithCustomFieldDeserializer(Class<?> customSerializer, Class<?> instanceClass, Object instance) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { assert (!instanceClass.isArray()); for (Method method : customSerializer.getMethods()) { if ("deserialize".equals(method.getName())) { method.invoke(null, this, instance); return; }/* w w w . j a v a2 s .c o m*/ } throw new NoSuchMethodException("deserialize"); }