List of usage examples for java.lang Class isArray
@HotSpotIntrinsicCandidate public native boolean isArray();
From source file:com.polarion.alm.ws.client.internal.encoding.BeanDeserializer.java
/** * Get the Deserializer for the attribute or child element. * /*from w w w . j a v a2s.c om*/ * @param xmlType * QName of the attribute/child element or null if not known. * @param javaType * Class of the corresponding property * @param href * String is the value of the href attribute, which is used to * determine whether the child element is complete or an href to * another element. * @param context * DeserializationContext * @return Deserializer or null if not found. */ protected Deserializer getDeserializer(QName xmlType, Class javaType, String href, DeserializationContext context) { if (javaType.isArray()) { context.setDestinationClass(javaType); } // See if we have a cached deserializer if (cacheStringDSer != null) { if (String.class.equals(javaType) && href == null && (cacheXMLType == null && xmlType == null || cacheXMLType != null && cacheXMLType.equals(xmlType))) { cacheStringDSer.reset(); return cacheStringDSer; } } Deserializer dSer = null; if (xmlType != null && href == null) { // Use the xmlType to get the deserializer. dSer = context.getDeserializerForType(xmlType); } else { // If the xmlType is not set, get a default xmlType TypeMapping tm = context.getTypeMapping(); QName defaultXMLType = tm.getTypeQName(javaType); // If there is not href, then get the deserializer // using the javaType and default XMLType, // If there is an href, the create the generic // DeserializerImpl and set its default type (the // default type is used if the href'd element does // not have an xsi:type. if (href == null) { dSer = context.getDeserializer(javaType, defaultXMLType); } else { dSer = new DeserializerImpl(); context.setDestinationClass(javaType); dSer.setDefaultType(defaultXMLType); } } if (javaType.equals(String.class) && dSer instanceof SimpleDeserializer) { cacheStringDSer = (SimpleDeserializer) dSer; cacheXMLType = xmlType; } return dSer; }
From source file:org.terasoluna.gfw.web.el.ObjectToMapConverter.java
/** * Add the pair of the given name and value to the given map. The value is flattened if required to be available in query * params. <br>//from ww w . j a v a2s .c o m * Return whether the value is flattened. The value is flattened in the case of the following types: * <ul> * <li>Array (unless the name is empty)</li> * <li>Iterable (unless the name is empty)</li> * <li>Map</li> * <li>{@link BeanUtils#isSimpleProperty(java.lang.Class)}</li> * <li>if {@link #conversionService} can convert</li> * </ul> * <p> * The value is formatted using {@link FormattingConversionService} is possible. If the value of a property is {@code null}, * the value is converted to an empty string and the key is prefixed with {@code "_"}. Request parameter that start with * {@code "_"} is reset parameter provided by Spring Web MVC. If a reset parameter is specified, Spring Web MVC bind * {@code null} to a property value. * </p> * @param map map to add * @param prefix prefix of the key * @param name name of the value * @param value value to convert * @param sourceType {@link TypeDescriptor} to use * @return flatten map */ private boolean flatten(Map<String, String> map, String prefix, String name, Object value, TypeDescriptor sourceType) { String key = StringUtils.isEmpty(prefix) ? name : prefix + "." + name; if (value == null) { String resetKey = "_" + key; map.put(resetKey, ""); // the value has been flatten return true; } Class<?> clazz = value.getClass(); if (value instanceof Iterable) { if (StringUtils.isEmpty(name)) { // skip flatten return true; } Iterable iterable = (Iterable) value; map.putAll(this.convert(key, iterable)); } else if (clazz.isArray()) { if (StringUtils.isEmpty(name)) { // skip flatten return true; } map.putAll(this.convert(key, arrayObjectToList(value))); } else if (value instanceof Map) { Map m = (Map) value; map.putAll(this.convert(key, m)); } else { TypeDescriptor descriptor = (sourceType != null) ? sourceType : TypeDescriptor.forObject(value); if (BeanUtils.isSimpleProperty(clazz) || conversionService.canConvert(descriptor, STRING_DESC)) { map.put(key, conversionService.convert(value, descriptor, STRING_DESC).toString()); } else { // the value is Java Bean? return false; } } // the value has been flatten return true; }
From source file:java2typescript.jackson.module.StaticFieldExporter.java
private AbstractType typeScriptTypeFromJavaType(Module module, Class<?> type) { if (type == boolean.class) { return BooleanType.getInstance(); } else if (type == int.class) { return NumberType.getInstance(); } else if (type == double.class) { return NumberType.getInstance(); } else if (type == String.class) { return StringType.getInstance(); } else if (type.isEnum()) { return tsJsonFormatVisitorWrapper.parseEnumOrGetFromCache(module, SimpleType.construct(type)); } else if (type.isArray()) { return new ArrayType(AnyType.getInstance()); }//from w w w .j a v a2 s . c o m throw new UnsupportedOperationException(); }
From source file:com.alibaba.citrus.service.form.impl.GroupImpl.java
/** * fields//ww w. j ava 2 s . c om * <p> * <code>isValidated()</code><code>true</code>group * </p> */ public void mapTo(Object object) { if (isValidated() || object == null) { return; } if (log.isDebugEnabled()) { log.debug("Mapping properties to fields: group=\"{}\", object={}", getName(), ObjectUtil.identityToString(object)); } BeanWrapper bean = new BeanWrapperImpl(object); getForm().getFormConfig().getPropertyEditorRegistrar().registerCustomEditors(bean); for (Field field : getFields()) { String propertyName = field.getFieldConfig().getPropertyName(); if (bean.isReadableProperty(propertyName)) { Object propertyValue = bean.getPropertyValue(propertyName); Class<?> propertyType = bean.getPropertyType(propertyName); PropertyEditor editor = bean.findCustomEditor(propertyType, propertyName); if (editor == null) { editor = BeanUtils.findEditorByConvention(propertyType); } if (editor == null) { if (propertyType.isArray() || CollectionFactory.isApproximableCollectionType(propertyType)) { field.setValues((String[]) bean.convertIfNecessary(propertyValue, String[].class)); } else { field.setValue(bean.convertIfNecessary(propertyValue, String.class)); } } else { editor.setValue(propertyValue); field.setValue(editor.getAsText()); } } else { log.debug("No readable property \"{}\" found in type {}", propertyName, object.getClass().getName()); } } }
From source file:com.ewcms.common.query.mongo.PropertyConvert.java
/** * ?{@link RuntimeException}// w w w.ja v a2s . com * * @param name ???{@literal null} * @return {@value Class<?>} */ public Class<?> getPropertyType(String propertyName) { if (!StringUtils.hasText(propertyName)) { throw new IllegalArgumentException("Property's name must not null or empty!"); } String[] names = StringUtils.tokenizeToStringArray(propertyName, NESTED); Class<?> type = beanClass; PropertyDescriptor pd = null; for (String name : names) { pd = BeanUtils.getPropertyDescriptor(type, name); if (pd == null) { logger.error("\"{}\" property isn't exist.", propertyName); throw new RuntimeException(propertyName + " property isn't exist."); } type = pd.getPropertyType(); } if (type.isArray()) { return type.getComponentType(); } if (Collection.class.isAssignableFrom(type)) { Method method = pd.getReadMethod(); if (method == null) { logger.error("\"{}\" property is not read method.", propertyName); throw new RuntimeException(propertyName + " property is not read method."); } ParameterizedType returnType = (ParameterizedType) method.getGenericReturnType(); if (returnType.getActualTypeArguments().length > 0) { return (Class<?>) returnType.getActualTypeArguments()[0]; } logger.error("\"{}\" property is collection,but it's not generic.", propertyName); throw new RuntimeException(propertyName + " property is collection,but it's not generic."); } return type; }
From source file:org.gvnix.web.json.ConversionServiceBeanSerializerModifier.java
@Override public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) { // We need the BeanPropertyDefinition to get the related Field List<BeanPropertyDefinition> properties = beanDesc.findProperties(); Map<String, BeanPropertyDefinition> propertyDefMap = new HashMap<String, BeanPropertyDefinition>(); for (BeanPropertyDefinition property : properties) { propertyDefMap.put(property.getName(), property); }/* w w w . ja v a2 s .c om*/ // iterate over bean's properties to configure serializers for (int i = 0; i < beanProperties.size(); i++) { BeanPropertyWriter beanPropertyWriter = beanProperties.get(i); Class<?> propertyType = beanPropertyWriter.getPropertyType(); if (beanPropertyWriter.hasSerializer()) { continue; } // For conversion between collection, array, and map types, // ConversionService.canConvert() method will return 'true' // but better to delegate in default Jackson property writer for // right start and ends markers serialization and iteration if (propertyType.isArray() || Collection.class.isAssignableFrom(propertyType) || Map.class.isAssignableFrom(propertyType)) { // Don't set ConversionService serializer, let Jackson // use default Collection serializer continue; } else if (BindingResult.class.isAssignableFrom(propertyType)) { // Use BindingResultSerializer beanPropertyWriter.assignSerializer(bindingResultSerializer); } else { // ConversionService uses value Class plus related Field // annotations to be able to select the right converter, // so we must get/ the Field annotations for success // formatting BeanPropertyDefinition propertyDef = propertyDefMap.get(beanPropertyWriter.getName()); AnnotatedField annotatedField = propertyDef.getField(); if (annotatedField == null) { continue; } AnnotatedElement annotatedEl = annotatedField.getAnnotated(); // Field contains info about Annotations, info that // ConversionService uses for success formatting, use it if // available. Otherwise use the class of given value. TypeDescriptor sourceType = annotatedEl != null ? new TypeDescriptor((Field) annotatedEl) : TypeDescriptor.valueOf(propertyType); TypeDescriptor targetType = TypeDescriptor.valueOf(String.class); if (beanPropertyWriter.getSerializationType() != null) { targetType = TypeDescriptor.valueOf(beanPropertyWriter.getSerializationType().getRawClass()); } if (ObjectUtils.equals(sourceType, targetType)) { // No conversion needed continue; } else if (sourceType.getObjectType() == Object.class && targetType.getObjectType() == String.class && beanPropertyWriter.getSerializationType() == null) { // Can't determine source type and no target type has been // configure. Delegate on jackson. continue; } // All other converters must be set in ConversionService if (this.conversionService.canConvert(sourceType, targetType)) { // We must create BeanPropertyWriter own Serializer that // has knowledge about the Field related to that // BeanPropertyWriter in order to have access to // Field Annotations for success serialization JsonSerializer<Object> jsonSerializer = new ConversionServicePropertySerializer( this.conversionService, sourceType, targetType); beanPropertyWriter.assignSerializer(jsonSerializer); } // If no converter set, use default Jackson property writer else { continue; } } } return beanProperties; }
From source file:com.github.ibole.infrastructure.persistence.db.mybatis.pagination.PagingParametersFinder.java
/** * Find whether contains <code>PaginationCriteria</code> objects in the object. * * @param object parameter object.//w w w . jav a2 s.c o m * @return PaginationCriteria */ @SuppressWarnings("rawtypes") private PagingCriteria findCriteriaFromObject(Object object) { // ???NULL if (searchMap.containsKey(object)) { return null; } // object class Class<?> objClass = object.getClass(); PagingCriteria pc; // primitive if (isPrimitiveType(objClass)) { pc = null; } else if (object instanceof PagingCriteria) { pc = (PagingCriteria) object; } else if (object instanceof Map) { pc = findCriteriaFromMap((Map) object); } else if (object instanceof Collection) { pc = findCriteriaFromCollection((Collection) object); } else if (objClass.isArray()) { pc = findCriteriaFromArray(object); } else { BeanMap map = new BeanMap(object); return findCriteriaFromMap(map); } searchMap.put(object, SqlHelper.EMPTY); return pc; }
From source file:hu.bme.mit.sette.common.validator.reflection.ClassValidator.java
/** * Sets the required type for the Java class. * * @param type/* w w w.ja v a 2s.c o m*/ * the required type for the Java class. * @return this object */ public ClassValidator type(final ClassType type) { Validate.notNull(type, "The type must not be null"); if (getSubject() != null) { Class<?> javaClass = getSubject(); boolean isTypeValid = false; switch (type) { case CLASS: isTypeValid = !javaClass.isInterface() && !javaClass.isEnum() && !javaClass.isAnnotation() && !javaClass.isPrimitive() && !javaClass.isArray(); break; case REGULAR_CLASS: isTypeValid = !javaClass.isInterface() && !javaClass.isEnum() && !javaClass.isAnnotation() && !javaClass.isPrimitive() && !javaClass.isArray() && !javaClass.isMemberClass() && !javaClass.isAnonymousClass() && !javaClass.isLocalClass(); break; case MEMBER_CLASS: isTypeValid = !javaClass.isInterface() && !javaClass.isEnum() && !javaClass.isAnnotation() && !javaClass.isPrimitive() && !javaClass.isArray() && javaClass.isMemberClass(); break; case ANONYMOUS_CLASS: isTypeValid = !javaClass.isInterface() && !javaClass.isEnum() && !javaClass.isAnnotation() && !javaClass.isPrimitive() && !javaClass.isArray() && javaClass.isAnonymousClass(); break; case LOCAL_CLASS: isTypeValid = !javaClass.isInterface() && !javaClass.isEnum() && !javaClass.isAnnotation() && !javaClass.isPrimitive() && !javaClass.isArray() && javaClass.isLocalClass(); break; case INTERFACE: isTypeValid = javaClass.isInterface(); break; case REGULAR_INTERFACE: isTypeValid = javaClass.isInterface() && !javaClass.isMemberClass(); break; case MEMBER_INTERFACE: isTypeValid = javaClass.isInterface() && javaClass.isMemberClass(); break; case ENUM: isTypeValid = javaClass.isEnum(); break; case REGULAR_ENUM: isTypeValid = javaClass.isEnum() && !javaClass.isMemberClass(); break; case MEMBER_ENUM: isTypeValid = javaClass.isEnum() && javaClass.isMemberClass(); break; case ANNOTATION: isTypeValid = javaClass.isAnnotation(); break; case REGULAR_ANNOTATION: isTypeValid = javaClass.isAnnotation() && !javaClass.isMemberClass(); break; case MEMBER_ANNOTATION: isTypeValid = javaClass.isAnnotation() && javaClass.isMemberClass(); break; case PRIMITIVE: isTypeValid = javaClass.isPrimitive(); break; case ARRAY: isTypeValid = javaClass.isArray(); break; default: throw new UnsupportedOperationException("Unknown class type: " + type); } if (!isTypeValid) { this.addException( String.format("The Java class must have the specified type\n" + "(type: [%s])", type)); } } return this; }
From source file:com.cinnober.msgcodec.json.JsonCodec.java
@SuppressWarnings({ "rawtypes", "unchecked" }) private JsonValueHandler createValueHandler(Schema schema, TypeDef type, Class<?> javaClass, Class<?> componentJavaClass, SymbolMapping<?> symbolMapping, Accessor accessor, boolean jsSafe) { type = schema.resolveToType(type, true); GroupDef group = schema.resolveToGroup(type); switch (type.getType()) { case SEQUENCE: if (javaClass.isArray()) { return new JsonValueHandler.ArraySequenceHandler( createValueHandler(schema, ((Sequence) type).getComponentType(), componentJavaClass, null, symbolMapping, accessor, jsSafe), componentJavaClass); } else { // collection return new JsonValueHandler.ListSequenceHandler( createValueHandler(schema, ((Sequence) type).getComponentType(), componentJavaClass, null, symbolMapping, accessor, jsSafe)); }/* w w w .ja v a2 s . c om*/ case REFERENCE: return lookupGroupByName(group.getName()); case DYNAMIC_REFERENCE: return dynamicGroupHandler; // TODO: restrict to some base type (if group is not null) default: return JsonValueHandler.getValueHandler(type, javaClass, (SymbolMapping) symbolMapping, jsSafe, accessor); } }
From source file:info.magnolia.jcr.node2bean.impl.TypeMappingImpl.java
/** * Gets type descriptor from bean class. *//*from w w w . ja v a 2 s . co m*/ private TypeDescriptor getTypeDescriptor(Class<?> beanClass, Method method) { TypeDescriptor dscr = types.get(beanClass); // eh, we know about this type, don't bother resolving any further. if (dscr != null) { return dscr; } dscr = new TypeDescriptor(); dscr.setType(beanClass); dscr.setMap(Map.class.isAssignableFrom(beanClass)); dscr.setCollection(Collection.class.isAssignableFrom(beanClass)); dscr.setArray(beanClass.isArray()); try { dscr.setTransformer(resolveTransformer(beanClass, method)); } catch (Node2BeanException e) { log.error("Can't create transformer for bean [" + beanClass + "]", e); } types.put(beanClass, dscr); return dscr; }