Skip to content

DATAJPA-871 - Allow usage of composed annotations using @AliasFor. #166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATAJPA-871-SNAPSHOT</version>

<name>Spring Data JPA</name>
<description>Spring Data module for JPA repositories.</description>
Expand All @@ -26,7 +26,7 @@
<hsqldb1>1.8.0.10</hsqldb1>
<jpa>2.0.0</jpa>
<openjpa>2.4.1</openjpa>
<springdata.commons>1.12.0.BUILD-SNAPSHOT</springdata.commons>
<springdata.commons>1.12.0.DATACMNS-825-SNAPSHOT</springdata.commons>

<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2015 the original author or authors.
* Copyright 2014-2016 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.
Expand Down Expand Up @@ -30,6 +30,7 @@
*
* Since 1.9 we support the definition of dynamic {@link EntityGraph}s by allowing to customize the fetch-graph via
* via {@link #attributePaths()} ad-hoc fetch-graph configuration.
* @author Christoph Strobl
*
* If {@link #attributePaths()} are specified then we ignore the entity-graph name {@link #value()}
* and treat this {@link EntityGraph} as dynamic.
Expand All @@ -38,7 +39,7 @@
* @since 1.6
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@Documented
public @interface EntityGraph {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2008-2015 the original author or authors.
* Copyright 2008-2016 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.
Expand All @@ -25,9 +25,10 @@
* Indicates a method should be regarded as modifying query.
*
* @author Oliver Gierke
* @author Christoph Strobl
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@Documented
public @interface Modifying {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2008-2014 the original author or authors.
* Copyright 2008-2016 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.
Expand Down Expand Up @@ -28,9 +28,10 @@
*
* @author Oliver Gierke
* @author Thomas Darimont
* @author Christoph Strobl
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@QueryAnnotation
@Documented
public @interface Query {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2014 the original author or authors.
* Copyright 2013-2016 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.
Expand All @@ -17,13 +17,15 @@

import javax.persistence.Entity;

import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
* Default implementation for {@link JpaEntityMetadata}.
*
* @author Oliver Gierke
* @author Christoph Strobl
*/
public class DefaultJpaEntityMetadata<T> implements JpaEntityMetadata<T> {

Expand Down Expand Up @@ -55,7 +57,7 @@ public Class<T> getJavaType() {
*/
public String getEntityName() {

Entity entity = domainType.getAnnotation(Entity.class);
Entity entity = AnnotatedElementUtils.findMergedAnnotation(domainType, Entity.class);
boolean hasName = null != entity && StringUtils.hasText(entity.name());

return hasName ? entity.name() : domainType.getSimpleName();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2008-2015 the original author or authors.
* Copyright 2008-2016 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.
Expand All @@ -15,8 +15,7 @@
*/
package org.springframework.data.jpa.repository.query;

import static org.springframework.core.annotation.AnnotationUtils.*;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -28,6 +27,7 @@
import javax.persistence.LockModeType;
import javax.persistence.QueryHint;

import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.data.jpa.provider.QueryExtractor;
import org.springframework.data.jpa.repository.EntityGraph;
Expand All @@ -48,6 +48,7 @@
*
* @author Oliver Gierke
* @author Thomas Darimont
* @author Christoph Strobl
*/
public class JpaQueryMethod extends QueryMethod {

Expand Down Expand Up @@ -135,7 +136,7 @@ public JpaEntityMetadata<?> getEntityInformation() {
@Override
public boolean isModifyingQuery() {

return null != method.getAnnotation(Modifying.class);
return null != AnnotationUtils.findAnnotation(method, Modifying.class);
}

/**
Expand All @@ -147,7 +148,7 @@ List<QueryHint> getHints() {

List<QueryHint> result = new ArrayList<QueryHint>();

QueryHints hints = getAnnotation(method, QueryHints.class);
QueryHints hints = AnnotatedElementUtils.findMergedAnnotation(method, QueryHints.class);
if (hints != null) {
result.addAll(Arrays.asList(hints.value()));
}
Expand All @@ -162,7 +163,7 @@ List<QueryHint> getHints() {
*/
LockModeType getLockModeType() {

Lock annotation = findAnnotation(method, Lock.class);
Lock annotation = AnnotatedElementUtils.findMergedAnnotation(method, Lock.class);
return (LockModeType) AnnotationUtils.getValue(annotation);
}

Expand All @@ -174,7 +175,7 @@ LockModeType getLockModeType() {
*/
JpaEntityGraph getEntityGraph() {

EntityGraph annotation = findAnnotation(method, EntityGraph.class);
EntityGraph annotation = AnnotatedElementUtils.findMergedAnnotation(method, EntityGraph.class);
return annotation == null ? null : new JpaEntityGraph(annotation, getNamedQueryName());
}

Expand All @@ -186,7 +187,7 @@ JpaEntityGraph getEntityGraph() {
*/
boolean applyHintsToCountQuery() {

QueryHints hints = getAnnotation(method, QueryHints.class);
QueryHints hints = AnnotatedElementUtils.findMergedAnnotation(method, QueryHints.class);
return hints != null ? hints.forCounting() : false;
}

Expand Down Expand Up @@ -284,8 +285,7 @@ String getNamedCountQueryName() {
* @return
*/
boolean getClearAutomatically() {

return (Boolean) AnnotationUtils.getValue(method.getAnnotation(Modifying.class), "clearAutomatically");
return getMergedOrDefaultAnnotationValue("clearAutomatically", Modifying.class, Boolean.class);
}

/**
Expand All @@ -298,12 +298,18 @@ boolean getClearAutomatically() {
* @return
*/
private <T> T getAnnotationValue(String attribute, Class<T> type) {
return getMergedOrDefaultAnnotationValue(attribute, Query.class, type);
}

Query annotation = method.getAnnotation(Query.class);
Object value = annotation == null ? AnnotationUtils.getDefaultValue(Query.class, attribute)
: AnnotationUtils.getValue(annotation, attribute);
@SuppressWarnings({ "rawtypes", "unchecked" })
private <T> T getMergedOrDefaultAnnotationValue(String attribute, Class annotationType, Class<T> targetType) {

Annotation annotation = AnnotatedElementUtils.findMergedAnnotation(method, annotationType);
if (annotation == null) {
return targetType.cast(AnnotationUtils.getDefaultValue(annotationType, attribute));
}

return type.cast(value);
return targetType.cast(AnnotationUtils.getValue(annotation, attribute));
}

/*
Expand Down Expand Up @@ -339,7 +345,7 @@ public boolean isCollectionQuery() {
* @return
*/
public boolean isProcedureQuery() {
return method.getAnnotation(Procedure.class) != null;
return AnnotationUtils.findAnnotation(method, Procedure.class) != null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2016 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.
Expand All @@ -25,9 +25,10 @@
*
* @author Thomas Darimont
* @author Oliver Gierke
* @author Christoph Strobl
* @since 1.6
*/
@Target(ElementType.METHOD)
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface Procedure {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2015 the original author or authors.
* Copyright 2014-2016 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.
Expand All @@ -24,6 +24,7 @@
import javax.persistence.NamedStoredProcedureQuery;
import javax.persistence.StoredProcedureParameter;

import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

Expand All @@ -32,6 +33,7 @@
*
* @author Thomas Darimont
* @author Oliver Gierke
* @author Christoph Strobl
* @since 1.6
*/
enum StoredProcedureAttributeSource {
Expand All @@ -50,7 +52,7 @@ public StoredProcedureAttributes createFrom(Method method, JpaEntityMetadata<?>
Assert.notNull(method, "Method must not be null!");
Assert.notNull(entityMetadata, "EntityMetadata must not be null!");

Procedure procedure = method.getAnnotation(Procedure.class);
Procedure procedure = AnnotatedElementUtils.findMergedAnnotation(method, Procedure.class);
Assert.notNull(procedure, "Method must have an @Procedure annotation!");

NamedStoredProcedureQuery namedStoredProc = tryFindAnnotatedNamedStoredProcedureQuery(method, entityMetadata,
Expand Down Expand Up @@ -201,12 +203,14 @@ private List<NamedStoredProcedureQuery> collectNamedStoredProcedureQueriesFrom(C

List<NamedStoredProcedureQuery> queries = new ArrayList<NamedStoredProcedureQuery>();

NamedStoredProcedureQueries namedQueriesAnnotation = entityType.getAnnotation(NamedStoredProcedureQueries.class);
NamedStoredProcedureQueries namedQueriesAnnotation = AnnotatedElementUtils.findMergedAnnotation(entityType,
NamedStoredProcedureQueries.class);
if (namedQueriesAnnotation != null) {
queries.addAll(Arrays.asList(namedQueriesAnnotation.value()));
}

NamedStoredProcedureQuery namedQueryAnnotation = entityType.getAnnotation(NamedStoredProcedureQuery.class);
NamedStoredProcedureQuery namedQueryAnnotation = AnnotatedElementUtils.findMergedAnnotation(entityType,
NamedStoredProcedureQuery.class);
if (namedQueryAnnotation != null) {
queries.add(namedQueryAnnotation);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2015 the original author or authors.
* Copyright 2011-2016 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.
Expand Down Expand Up @@ -31,6 +31,7 @@
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.interceptor.ExposeInvocationInterceptor;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.Lock;
Expand All @@ -48,6 +49,7 @@
*
* @author Oliver Gierke
* @author Thomas Darimont
* @author Christoph Strobl
*/
class CrudMethodMetadataPostProcessor implements RepositoryProxyPostProcessor, BeanClassLoaderAware {

Expand Down Expand Up @@ -164,19 +166,19 @@ public DefaultCrudMethodMetadata(Method method) {
}

private static EntityGraph findEntityGraph(Method method) {
return AnnotationUtils.findAnnotation(method, EntityGraph.class);
return AnnotatedElementUtils.findMergedAnnotation(method, EntityGraph.class);
}

private static LockModeType findLockModeType(Method method) {

Lock annotation = AnnotationUtils.findAnnotation(method, Lock.class);
Lock annotation = AnnotatedElementUtils.findMergedAnnotation(method, Lock.class);
return annotation == null ? null : (LockModeType) AnnotationUtils.getValue(annotation);
}

private static Map<String, Object> findQueryHints(Method method) {

Map<String, Object> queryHints = new HashMap<String, Object>();
QueryHints queryHintsAnnotation = AnnotationUtils.findAnnotation(method, QueryHints.class);
QueryHints queryHintsAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, QueryHints.class);

if (queryHintsAnnotation != null) {

Expand Down
Loading