diff --git a/src/main/java/org/springframework/data/repository/augment/DefaultMethodMetadata.java b/src/main/java/org/springframework/data/repository/augment/DefaultMethodMetadata.java new file mode 100644 index 000000000..a7d29ef64 --- /dev/null +++ b/src/main/java/org/springframework/data/repository/augment/DefaultMethodMetadata.java @@ -0,0 +1,73 @@ +/* + * Copyright 2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.repository.augment; + +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.aopalliance.intercept.MethodInvocation; +import org.springframework.aop.framework.Advised; +import org.springframework.aop.framework.ReflectiveMethodInvocation; +import org.springframework.aop.interceptor.ExposeInvocationInterceptor; + +/** + * Default implementation of {@link MethodMetadata}. + * + * @author Oliver Gierke + * @since 1.12 + */ +enum DefaultMethodMetadata implements MethodMetadata { + + INSTANCE; + + private MethodInvocation getMethodInvocation() { + return ExposeInvocationInterceptor.currentInvocation(); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.repository.core.support.MethodMetadata#getInvocationTargetType() + */ + public List> getInvocationTargetType() { + + MethodInvocation invocation = getMethodInvocation(); + + if (invocation instanceof ReflectiveMethodInvocation) { + Advised proxy = (Advised) ((ReflectiveMethodInvocation) invocation).getProxy(); + return Arrays.asList(proxy.getProxiedInterfaces()); + } + + return Collections.> singletonList(getMethodInvocation().getThis().getClass()); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.repository.core.support.MethodMetadata#getInvocationArguments() + */ + public Object[] getInvocationArguments() { + return getMethodInvocation().getArguments(); + } + + /* + * (non-Javadoc) + * @see org.springframework.data.repository.core.support.MethodMetadata#getMethod() + */ + public Method getMethod() { + return getMethodInvocation().getMethod(); + } +} diff --git a/src/main/java/org/springframework/data/repository/augment/QueryAugmentationEngine.java b/src/main/java/org/springframework/data/repository/augment/QueryAugmentationEngine.java index 1f1c3effe..d6aabb43c 100644 --- a/src/main/java/org/springframework/data/repository/augment/QueryAugmentationEngine.java +++ b/src/main/java/org/springframework/data/repository/augment/QueryAugmentationEngine.java @@ -40,7 +40,7 @@ public class QueryAugmentationEngine { private static final Iterable, ? extends QueryContext, ? extends UpdateContext>> NO_AUGMENTORS = Collections .emptySet(); - public static final QueryAugmentationEngine NONE = new QueryAugmentationEngine(NO_AUGMENTORS, null, false); + public static final QueryAugmentationEngine NONE = new QueryAugmentationEngine(NO_AUGMENTORS); private static final Logger LOGGER = LoggerFactory.getLogger(QueryAugmentationEngine.class); private static final Comparator COMPARATOR = new AnnotationAwareOrderComparator(); @@ -54,34 +54,14 @@ public class QueryAugmentationEngine { * Creates a new {@link QueryAugmentationEngine} by inspecting the given {@link QueryAugmentor}s. * * @param augmentors must not be {@literal null}. - * @param metadataProvider must not be {@literal null}. - */ - public QueryAugmentationEngine( - Iterable, ? extends QueryContext, ? extends UpdateContext>> augmentors, - MethodMetadata metadataProvider) { - this(augmentors, metadataProvider, true); - } - - /** - * Internal constructor to allow {@link #NONE} being created with a {@literal null} {@link MethodMetadata} which - * actually must not be null otherwise. - * - * @param augmentors the {@link QueryAugmentor}s to register. - * @param methodMetadata - * @param checkNull whether to check the {@link MethodMetadata} for {@literal null}. */ @SuppressWarnings("unchecked") - private QueryAugmentationEngine( - Iterable, ? extends QueryContext, ? extends UpdateContext>> augmentors, - MethodMetadata methodMetadata, boolean checkNull) { + public QueryAugmentationEngine( + Iterable, ? extends QueryContext, ? extends UpdateContext>> augmentors) { Assert.notNull(augmentors, "QueryAugmentors must not be null!"); - if (checkNull) { - Assert.notNull(methodMetadata, "MethodMetadata must not be null!"); - } - - this.methodMetadata = methodMetadata; + this.methodMetadata = DefaultMethodMetadata.INSTANCE; for (QueryAugmentor, ? extends QueryContext, ? extends UpdateContext> augmentor : augmentors) { diff --git a/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java b/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java index eff6a0530..ad6448326 100644 --- a/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java +++ b/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java @@ -19,8 +19,6 @@ import java.io.Serializable; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -28,10 +26,7 @@ import java.util.concurrent.ConcurrentHashMap; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; -import org.springframework.aop.ProxyMethodInvocation; -import org.springframework.aop.framework.Advised; import org.springframework.aop.framework.ProxyFactory; -import org.springframework.aop.framework.ReflectiveMethodInvocation; import org.springframework.aop.interceptor.ExposeInvocationInterceptor; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.BeanClassLoaderAware; @@ -40,7 +35,6 @@ import org.springframework.core.MethodParameter; import org.springframework.core.convert.TypeDescriptor; import org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor; import org.springframework.data.repository.Repository; -import org.springframework.data.repository.augment.MethodMetadata; import org.springframework.data.repository.augment.QueryAugmentationEngine; import org.springframework.data.repository.augment.QueryAugmentationEngineAware; import org.springframework.data.repository.augment.QueryAugmentor; @@ -137,7 +131,7 @@ public abstract class RepositoryFactorySupport implements BeanClassLoaderAware { */ public void setQueryAugmentors( List, ? extends QueryContext, ? extends UpdateContext>> augmentors) { - this.augmentationEngine = new QueryAugmentationEngine(augmentors, DefaultMethodMetadata.INSTANCE); + this.augmentationEngine = new QueryAugmentationEngine(augmentors); } /** @@ -334,9 +328,9 @@ public abstract class RepositoryFactorySupport implements BeanClassLoaderAware { if (null == customImplementation && repositoryInformation.hasCustomMethod()) { - throw new IllegalArgumentException(String.format( - "You have custom methods in %s but not provided a custom implementation!", - repositoryInformation.getRepositoryInterface())); + throw new IllegalArgumentException( + String.format("You have custom methods in %s but not provided a custom implementation!", + repositoryInformation.getRepositoryInterface())); } validate(repositoryInformation); @@ -647,50 +641,4 @@ public abstract class RepositoryFactorySupport implements BeanClassLoaderAware { factory.addAdvice(ExposeInvocationInterceptor.INSTANCE); } } - - /** - * Default implementation of {@link MethodMetadata}. - * - * @author Oliver Gierke - */ - private static enum DefaultMethodMetadata implements MethodMetadata { - - INSTANCE; - - private MethodInvocation getMethodInvocation() { - return ExposeInvocationInterceptor.currentInvocation(); - } - - /* - * (non-Javadoc) - * @see org.springframework.data.repository.core.support.MethodMetadata#getInvocationTargetType() - */ - public List> getInvocationTargetType() { - - MethodInvocation invocation = getMethodInvocation(); - - if (invocation instanceof ReflectiveMethodInvocation) { - Advised proxy = (Advised) ((ReflectiveMethodInvocation) invocation).getProxy(); - return Arrays.asList(proxy.getProxiedInterfaces()); - } - - return Collections.> singletonList(getMethodInvocation().getThis().getClass()); - } - - /* - * (non-Javadoc) - * @see org.springframework.data.repository.core.support.MethodMetadata#getInvocationArguments() - */ - public Object[] getInvocationArguments() { - return getMethodInvocation().getArguments(); - } - - /* - * (non-Javadoc) - * @see org.springframework.data.repository.core.support.MethodMetadata#getMethod() - */ - public Method getMethod() { - return getMethodInvocation().getMethod(); - } - } }