|
| 1 | +/* |
| 2 | + * Copyright 2014-2015 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.projection; |
| 17 | + |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.Collection; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.Map.Entry; |
| 23 | + |
| 24 | +import org.aopalliance.intercept.MethodInterceptor; |
| 25 | +import org.aopalliance.intercept.MethodInvocation; |
| 26 | +import org.springframework.core.CollectionFactory; |
| 27 | +import org.springframework.data.util.ClassTypeInformation; |
| 28 | +import org.springframework.data.util.TypeInformation; |
| 29 | +import org.springframework.util.Assert; |
| 30 | +import org.springframework.util.ClassUtils; |
| 31 | + |
| 32 | +/** |
| 33 | + * {@link MethodInterceptor} to delegate the invocation to a different {@link MethodInterceptor} but creating a |
| 34 | + * projecting proxy in case the returned value is not of the return type of the invoked method. |
| 35 | + * |
| 36 | + * @author Oliver Gierke |
| 37 | + * @since 1.10 |
| 38 | + */ |
| 39 | +class ProjectingMethodInterceptor implements MethodInterceptor { |
| 40 | + |
| 41 | + private final ProjectionFactory factory; |
| 42 | + private final MethodInterceptor delegate; |
| 43 | + |
| 44 | + /** |
| 45 | + * Creates a new {@link ProjectingMethodInterceptor} using the given {@link ProjectionFactory} and delegate |
| 46 | + * {@link MethodInterceptor}. |
| 47 | + * |
| 48 | + * @param factory the {@link ProjectionFactory} to use to create projections if types do not match, must not be |
| 49 | + * {@literal null}.. |
| 50 | + * @param delegate the {@link MethodInterceptor} to trigger to create the source value, must not be {@literal null}.. |
| 51 | + */ |
| 52 | + public ProjectingMethodInterceptor(ProjectionFactory factory, MethodInterceptor delegate) { |
| 53 | + |
| 54 | + Assert.notNull(factory, "ProjectionFactory must not be null!"); |
| 55 | + Assert.notNull(delegate, "Delegate MethodInterceptor must not be null!"); |
| 56 | + |
| 57 | + this.factory = factory; |
| 58 | + this.delegate = delegate; |
| 59 | + } |
| 60 | + |
| 61 | + /* |
| 62 | + * (non-Javadoc) |
| 63 | + * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation) |
| 64 | + */ |
| 65 | + @Override |
| 66 | + public Object invoke(MethodInvocation invocation) throws Throwable { |
| 67 | + |
| 68 | + Object result = delegate.invoke(invocation); |
| 69 | + |
| 70 | + if (result == null) { |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
| 74 | + TypeInformation<?> type = ClassTypeInformation.fromReturnTypeOf(invocation.getMethod()); |
| 75 | + |
| 76 | + if (type.isCollectionLike()) { |
| 77 | + return projectCollectionElements(asCollection(result), type); |
| 78 | + } else if (type.isMap()) { |
| 79 | + return projectMapValues((Map<?, ?>) result, type); |
| 80 | + } else { |
| 81 | + return getProjection(result, type.getType()); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Creates projections of the given {@link Collection}'s elements if necessary and returns a new collection containing |
| 87 | + * the projection results. |
| 88 | + * |
| 89 | + * @param sources must not be {@literal null}. |
| 90 | + * @param type must not be {@literal null}. |
| 91 | + * @return |
| 92 | + */ |
| 93 | + private Collection<Object> projectCollectionElements(Collection<?> sources, TypeInformation<?> type) { |
| 94 | + |
| 95 | + Collection<Object> result = CollectionFactory.createCollection(type.getType(), sources.size()); |
| 96 | + |
| 97 | + for (Object source : sources) { |
| 98 | + result.add(getProjection(source, type.getComponentType().getType())); |
| 99 | + } |
| 100 | + |
| 101 | + return result; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Creates projections of the given {@link Map}'s values if necessary and returns an new {@link Map} with the handled |
| 106 | + * values. |
| 107 | + * |
| 108 | + * @param sources must not be {@literal null}. |
| 109 | + * @param type must not be {@literal null}. |
| 110 | + * @return |
| 111 | + */ |
| 112 | + private Map<Object, Object> projectMapValues(Map<?, ?> sources, TypeInformation<?> type) { |
| 113 | + |
| 114 | + Map<Object, Object> result = CollectionFactory.createMap(type.getType(), sources.size()); |
| 115 | + |
| 116 | + for (Entry<?, ?> source : sources.entrySet()) { |
| 117 | + result.put(source.getKey(), getProjection(source.getValue(), type.getMapValueType().getType())); |
| 118 | + } |
| 119 | + |
| 120 | + return result; |
| 121 | + } |
| 122 | + |
| 123 | + private Object getProjection(Object result, Class<?> returnType) { |
| 124 | + return ClassUtils.isAssignable(returnType, result.getClass()) ? result : factory.createProjection(returnType, |
| 125 | + result); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Turns the given value into a {@link Collection}. Will turn an array into a collection an wrap all other values into |
| 130 | + * a single-element collection. |
| 131 | + * |
| 132 | + * @param source must not be {@literal null}. |
| 133 | + * @return |
| 134 | + */ |
| 135 | + private static Collection<?> asCollection(Object source) { |
| 136 | + |
| 137 | + Assert.notNull(source, "Source object must not be null!"); |
| 138 | + |
| 139 | + if (source instanceof Collection) { |
| 140 | + return (Collection<?>) source; |
| 141 | + } else if (source.getClass().isArray()) { |
| 142 | + return Arrays.asList((Object[]) source); |
| 143 | + } else { |
| 144 | + return Collections.singleton(source); |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments