/** * Creates a new {@link ResourceProcessorInvoker} to consider the given {@link ResourceProcessor} to post-process the * controller methods return value to before invoking the delegate. * * @param processors the {@link ResourceProcessor}s to be considered, must not be {@literal null}. */ public ResourceProcessorInvoker(Collection<ResourceProcessor<?>> processors) { Assert.notNull(processors, "ResourceProcessors must not be null!"); this.processors = new ArrayList<ProcessorWrapper>(); for (ResourceProcessor<?> processor : processors) { ResolvableType processorType = ResolvableType.forClass(ResourceProcessor.class, processor.getClass()); Class<?> rawType = processorType.getGeneric(0).resolve(); if (Resource.class.isAssignableFrom(rawType)) { this.processors.add(new ResourceProcessorWrapper(processor)); } else if (Resources.class.isAssignableFrom(rawType)) { this.processors.add(new ResourcesProcessorWrapper(processor)); } else { this.processors.add(new DefaultProcessorWrapper(processor)); } } Collections.sort(this.processors, AnnotationAwareOrderComparator.INSTANCE); }
/** * Returns whether the given {@link Resource} matches the given target {@link ResolvableType}. We inspect the * {@link Resource}'s value to determine the match. * * @param resource * @param target must not be {@literal null}. * @return whether the given {@link Resource} can be assigned to the given target {@link ResolvableType} */ private static boolean isValueTypeMatch(Resource<?> resource, ResolvableType target) { if (resource == null || !isRawTypeAssignable(target, resource.getClass())) { return false; } Object content = resource.getContent(); if (content == null) { return false; } ResolvableType type = findGenericType(target, Resource.class); return type != null && type.getGeneric(0).isAssignableFrom(ResolvableType.forClass(content.getClass())); }
/** * Creates a new {@link ResourceProcessorInvoker} to consider the given {@link ResourceProcessor} to post-process the * controller methods return value to before invoking the delegate. * * @param processors the {@link ResourceProcessor}s to be considered, must not be {@literal null}. */ public ResourceProcessorInvoker(Collection<ResourceProcessor<?>> processors) { Assert.notNull(processors, "ResourceProcessors must not be null!"); this.processors = new ArrayList<ProcessorWrapper>(); for (ResourceProcessor<?> processor : processors) { ResolvableType processorType = ResolvableType.forClass(ResourceProcessor.class, processor.getClass()); Class<?> rawType = processorType.getGeneric(0).resolve(); if (Resource.class.isAssignableFrom(rawType)) { this.processors.add(new ResourceProcessorWrapper(processor)); } else if (Resources.class.isAssignableFrom(rawType)) { this.processors.add(new ResourcesProcessorWrapper(processor)); } else { this.processors.add(new DefaultProcessorWrapper(processor)); } } Collections.sort(this.processors, AnnotationAwareOrderComparator.INSTANCE); }
/** * Returns whether the given {@link Resource} matches the given target {@link ResolvableType}. We inspect the * {@link Resource}'s value to determine the match. * * @param resource * @param target must not be {@literal null}. * @return whether the given {@link Resource} can be assigned to the given target {@link ResolvableType} */ private static boolean isValueTypeMatch(Resource<?> resource, ResolvableType target) { if (resource == null || !isRawTypeAssignable(target, resource.getClass())) { return false; } Object content = resource.getContent(); if (content == null) { return false; } ResolvableType type = findGenericType(target, Resource.class); return type != null && type.getGeneric(0).isAssignableFrom(ResolvableType.forClass(content.getClass())); }
private static ResolvableType findGenericType(ResolvableType source, Class<?> type) { Class<?> rawType = getRawType(source); if (Object.class.equals(rawType)) { return null; } if (rawType.equals(type)) { return source; } return findGenericType(source.getSuperType(), type); } }
@Override public boolean supports(ResolvableType type, Object value) { if (!ResourceProcessorHandlerMethodReturnValueHandler.RESOURCE_TYPE.isAssignableFrom(type)) { return false; } return super.supports(type, value) && isValueTypeMatch((Resource<?>) value, getTargetType()); }
private static ResolvableType findGenericType(ResolvableType source, Class<?> type) { Class<?> rawType = getRawType(source); if (Object.class.equals(rawType)) { return null; } if (rawType.equals(type)) { return source; } return findGenericType(source.getSuperType(), type); } }
@Override public boolean supports(ResolvableType type, Object value) { if (!ResourceProcessorHandlerMethodReturnValueHandler.RESOURCE_TYPE.isAssignableFrom(type)) { return false; } return super.supports(type, value) && isValueTypeMatch((Resource<?>) value, getTargetType()); }