private void growCollectionIfNecessary(Collection<Object> collection, int index, String name, PropertyDescriptor pd, int nestingLevel) { if (!this.autoGrowNestedPaths) { return; } int size = collection.size(); if (index >= size && index < this.autoGrowCollectionLimit) { Class<?> elementType = GenericCollectionTypeResolver.getCollectionReturnType(pd.getReadMethod(), nestingLevel); if (elementType != null) { for (int i = collection.size(); i < index + 1; i++) { collection.add(newValue(elementType, name)); } } } }
/** * @param objectToUnserialize * @param objectRootURI * @param repo */ @SuppressWarnings( "unchecked" ) private <T extends Collection<?>> void extractCollectionDirect( final T objectToUnserialize, final String objectRootURI, final Repository repo ) { final Class<?> collectionType = GenericCollectionTypeResolver.getCollectionType( objectToUnserialize.getClass() ); // TODO: This needs to be sorted out. We can't pass null. // Unserialize the collection items. final Object[] seq = this.extractCollectionObjects( objectRootURI, repo, collectionType, "", objectRootURI, RDFSerializer.RDF_OPENIMAJ_P_COLLECTIONITEM ); ((Collection<Object>)objectToUnserialize).clear(); for( int i = 0; i < seq.length; i++ ) ((Collection<Object>)objectToUnserialize).add( seq[i] ); }
/** * @param source * @param depth * @param dtoToEntity * @param bean * @param field * @param toSet * @param targetClass * @throws IllegalAccessException */ @SuppressWarnings({ RAWTYPES, UNCHECKED }) private void mapCollection(final Object source, final int depth, final boolean dtoToEntity, final Object bean, Field field, Field toSet, Class<?> targetClass) throws IllegalAccessException { if (depth > 0) { Collection colTarget = getNewCollection(targetClass); Collection colSource = (Collection) field.get(source); if (colSource == null) { return; } Class targetRowType = GenericCollectionTypeResolver .getCollectionFieldType(toSet); for (Object row : colSource) { if (row instanceof Shareable) { colTarget.add(convert(row.getClass(), targetRowType, row, depth - 1, dtoToEntity)); } else { colTarget.add(map(row, targetRowType, depth - 1, dtoToEntity)); } } toSet.set(bean, colTarget); } }
private Class<?> getCollectionParameterType(MethodParameter parameter) { Class<?> paramType = parameter.getParameterType(); if (Collection.class.equals(paramType) || List.class.isAssignableFrom(paramType)){ Class<?> valueType = GenericCollectionTypeResolver.getCollectionParameterType(parameter); if (valueType != null) { return valueType; } } return null; }
@Override @SuppressWarnings("unchecked") protected List<Object> createInstance() { if (this.sourceList == null) { throw new IllegalArgumentException("'sourceList' is required"); } List<Object> result = null; if (this.targetListClass != null) { result = BeanUtils.instantiateClass(this.targetListClass); } else { result = new ArrayList<Object>(this.sourceList.size()); } Class<?> valueType = null; if (this.targetListClass != null) { valueType = GenericCollectionTypeResolver.getCollectionType(this.targetListClass); } if (valueType != null) { TypeConverter converter = getBeanTypeConverter(); for (Object elem : this.sourceList) { result.add(converter.convertIfNecessary(elem, valueType)); } } else { result.addAll(this.sourceList); } return result; }
@Override @SuppressWarnings("unchecked") protected Set<Object> createInstance() { if (this.sourceSet == null) { throw new IllegalArgumentException("'sourceSet' is required"); } Set<Object> result = null; if (this.targetSetClass != null) { result = BeanUtils.instantiateClass(this.targetSetClass); } else { result = new LinkedHashSet<Object>(this.sourceSet.size()); } Class<?> valueType = null; if (this.targetSetClass != null) { valueType = GenericCollectionTypeResolver.getCollectionType(this.targetSetClass); } if (valueType != null) { TypeConverter converter = getBeanTypeConverter(); for (Object elem : this.sourceSet) { result.add(converter.convertIfNecessary(elem, valueType)); } } else { result.addAll(this.sourceSet); } return result; }
@Override @SuppressWarnings("unchecked") protected Map<Object, Object> createInstance() { if (this.sourceMap == null) { throw new IllegalArgumentException("'sourceMap' is required"); } Map<Object, Object> result = null; if (this.targetMapClass != null) { result = BeanUtils.instantiateClass(this.targetMapClass); } else { result = new LinkedHashMap<Object, Object>(this.sourceMap.size()); } Class<?> keyType = null; Class<?> valueType = null; if (this.targetMapClass != null) { keyType = GenericCollectionTypeResolver.getMapKeyType(this.targetMapClass); valueType = GenericCollectionTypeResolver.getMapValueType(this.targetMapClass); } if (keyType != null || valueType != null) { TypeConverter converter = getBeanTypeConverter(); for (Map.Entry<?, ?> entry : this.sourceMap.entrySet()) { Object convertedKey = converter.convertIfNecessary(entry.getKey(), keyType); Object convertedValue = converter.convertIfNecessary(entry.getValue(), valueType); result.put(convertedKey, convertedValue); } } else { result.putAll(this.sourceMap); } return result; }
/** * Determine the expected type as the returned type of the method. * If the return type is a List it will return the generic element type instead of a List. * @param resource - resource with methods * @param method Method * @return Class - type of class it needs. */ @SuppressWarnings("rawtypes") protected static Class determineType(Class resource, Method method) { Method resolvedMethod = BridgeMethodResolver.findBridgedMethod(method); /* * The api is consistent that the object passed in must match the object passed out * however, operations are different, if the param is supplied it doesn't have to match * the return type. * So we need special logic for operations */ Annotation annot = AnnotationUtils.findAnnotation(resolvedMethod, Operation.class); if (annot != null) { return determineOperationType(resource, method); } else { Class returnType = GenericTypeResolver.resolveReturnType(resolvedMethod, resource); if (List.class.isAssignableFrom(returnType)) { return GenericCollectionTypeResolver.getCollectionReturnType(method); } return returnType; } }
private Class<?> getCollectionParameterType(MethodParameter methodParam) { Class<?> paramType = methodParam.getNestedParameterType(); if (Collection.class == paramType || List.class.isAssignableFrom(paramType)){ Class<?> valueType = GenericCollectionTypeResolver.getCollectionParameterType(methodParam); if (valueType != null) { return valueType; } } return null; }
private Class<?> getCollectionParameterType(MethodParameter parameter) { Class<?> paramType = parameter.getParameterType(); if (Collection.class == paramType || List.class.isAssignableFrom(paramType)){ Class<?> valueType = GenericCollectionTypeResolver.getCollectionParameterType(parameter); if (valueType != null) { return valueType; } } return null; }
@SuppressWarnings({ RAWTYPES, UNCHECKED }) private Object handleCollection(Class currentClass, Field field, Object current, String currentPath) { try { field.setAccessible(true); Object newList = field.get(current); if (newList == null) { newList = getNew(currentClass); field.set(current, newList); } Object toRet; Collection list = (Collection) newList; if (!nestedObjectHandler.has(currentPath)) { Class cClass = GenericCollectionTypeResolver .getCollectionFieldType(field); toRet = cClass.newInstance(); list.add(toRet); nestedObjectHandler.set(currentPath, toRet); } else { toRet = nestedObjectHandler.get(currentPath); } field.setAccessible(false); return toRet; } catch (Exception e) { throw new KarakuRuntimeException(e); } }
private static Class<?> getType(Field field) { Class<?> toRet = field.getType(); // Si tenemos una lista, buscar el tipo de la lista. if (Collection.class.isAssignableFrom(toRet)) { toRet = GenericCollectionTypeResolver.getCollectionFieldType(field); } return toRet; }
private boolean isMultipartFileCollection(MethodParameter parameter) { Class<?> paramType = parameter.getParameterType(); if (Collection.class.equals(paramType) || List.class.isAssignableFrom(paramType)){ Class<?> valueType = GenericCollectionTypeResolver.getCollectionParameterType(parameter); if (valueType != null && valueType.equals(MultipartFile.class)) { return true; } } return false; }
@Override @SuppressWarnings("unchecked") protected List createInstance() { if (this.sourceList == null) { throw new IllegalArgumentException("'sourceList' is required"); } List result = null; if (this.targetListClass != null) { result = (List) BeanUtils.instantiateClass(this.targetListClass); } else { result = new ArrayList(this.sourceList.size()); } Class valueType = null; if (this.targetListClass != null) { valueType = GenericCollectionTypeResolver.getCollectionType(this.targetListClass); } if (valueType != null) { TypeConverter converter = getBeanTypeConverter(); for (Object elem : this.sourceList) { result.add(converter.convertIfNecessary(elem, valueType)); } } else { result.addAll(this.sourceList); } return result; }
@Override @SuppressWarnings("unchecked") protected Set createInstance() { if (this.sourceSet == null) { throw new IllegalArgumentException("'sourceSet' is required"); } Set result = null; if (this.targetSetClass != null) { result = (Set) BeanUtils.instantiateClass(this.targetSetClass); } else { result = new LinkedHashSet(this.sourceSet.size()); } Class valueType = null; if (this.targetSetClass != null) { valueType = GenericCollectionTypeResolver.getCollectionType(this.targetSetClass); } if (valueType != null) { TypeConverter converter = getBeanTypeConverter(); for (Object elem : this.sourceSet) { result.add(converter.convertIfNecessary(elem, valueType)); } } else { result.addAll(this.sourceSet); } return result; }
@Override @SuppressWarnings("unchecked") protected Map createInstance() { if (this.sourceMap == null) { throw new IllegalArgumentException("'sourceMap' is required"); } Map result = null; if (this.targetMapClass != null) { result = (Map) BeanUtils.instantiateClass(this.targetMapClass); } else { result = new LinkedHashMap(this.sourceMap.size()); } Class keyType = null; Class valueType = null; if (this.targetMapClass != null) { keyType = GenericCollectionTypeResolver.getMapKeyType(this.targetMapClass); valueType = GenericCollectionTypeResolver.getMapValueType(this.targetMapClass); } if (keyType != null || valueType != null) { TypeConverter converter = getBeanTypeConverter(); for (Map.Entry entry : this.sourceMap.entrySet()) { Object convertedKey = converter.convertIfNecessary(entry.getKey(), keyType); Object convertedValue = converter.convertIfNecessary(entry.getValue(), valueType); result.put(convertedKey, convertedValue); } } else { result.putAll(this.sourceMap); } return result; }
/** * Determine the generic element type of the wrapped Collection parameter/field, if any. * @return the generic type, or {@code null} if none */ public Class<?> getCollectionType() { return (this.field != null ? GenericCollectionTypeResolver.getCollectionFieldType(this.field, this.nestingLevel) : GenericCollectionTypeResolver.getCollectionParameterType(this.methodParameter)); }
/** * Determine the generic key type of the wrapped Map parameter/field, if any. * @return the generic type, or {@code null} if none */ public Class<?> getMapKeyType() { return (this.field != null ? GenericCollectionTypeResolver.getMapKeyFieldType(this.field, this.nestingLevel) : GenericCollectionTypeResolver.getMapKeyParameterType(this.methodParameter)); }
/** * Determine the generic value type of the wrapped Map parameter/field, if any. * @return the generic type, or {@code null} if none */ public Class<?> getMapValueType() { return (this.field != null ? GenericCollectionTypeResolver.getMapValueFieldType(this.field, this.nestingLevel) : GenericCollectionTypeResolver.getMapValueParameterType(this.methodParameter)); }
@Override protected Class<?> resolveCollectionElementType() { return GenericCollectionTypeResolver.getCollectionParameterType(this.methodParameter); }
@Override protected Class<?> resolveMapKeyType() { return GenericCollectionTypeResolver.getMapKeyParameterType(this.methodParameter); }
@Override protected Class<?> resolveMapValueType() { return GenericCollectionTypeResolver.getMapValueParameterType(this.methodParameter); }
@Override @SuppressWarnings({ "unchecked", "rawtypes" }) protected Class<?> resolveCollectionElementType() { return GenericCollectionTypeResolver.getCollectionType((Class) getType()); }
@Override @SuppressWarnings({ "unchecked", "rawtypes" }) protected Class<?> resolveMapKeyType() { return GenericCollectionTypeResolver.getMapKeyType((Class) getType()); }
@Override @SuppressWarnings({ "unchecked", "rawtypes" }) protected Class<?> resolveMapValueType() { return GenericCollectionTypeResolver.getMapValueType((Class) getType()); }