From 97859f5dee6933abdc2025b52bd6aea4f309ec4a Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 20 Apr 2011 20:32:21 +0200 Subject: [PATCH] DATACMNS-32 - Removed Querydsl support classes and use the ones from Spring Data Commons Core. --- .../support/JpaRepositoryFactory.java | 7 +- .../support/QueryDslJpaRepository.java | 128 +----------------- .../SimpleEntityPathResolverUnitTests.java | 74 ---------- 3 files changed, 4 insertions(+), 205 deletions(-) delete mode 100644 src/test/java/org/springframework/data/jpa/repository/support/SimpleEntityPathResolverUnitTests.java diff --git a/src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactory.java b/src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactory.java index 176731b51..c197576fd 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactory.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactory.java @@ -15,6 +15,8 @@ */ package org.springframework.data.jpa.repository.support; +import static org.springframework.data.querydsl.QueryDslUtils.*; + import java.io.Serializable; import javax.persistence.EntityManager; @@ -28,7 +30,6 @@ import org.springframework.data.repository.query.QueryLookupStrategy.Key; import org.springframework.data.repository.support.RepositoryFactorySupport; import org.springframework.data.repository.support.RepositoryMetadata; import org.springframework.util.Assert; -import org.springframework.util.ClassUtils; /** @@ -38,10 +39,6 @@ import org.springframework.util.ClassUtils; */ public class JpaRepositoryFactory extends RepositoryFactorySupport { - private static final boolean QUERY_DSL_PRESENT = ClassUtils.isPresent( - "com.mysema.query.types.Predicate", - JpaRepositoryFactory.class.getClassLoader()); - private final EntityManager entityManager; private final QueryExtractor extractor; diff --git a/src/main/java/org/springframework/data/jpa/repository/support/QueryDslJpaRepository.java b/src/main/java/org/springframework/data/jpa/repository/support/QueryDslJpaRepository.java index 1cdaf7698..8ede79a12 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/QueryDslJpaRepository.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/QueryDslJpaRepository.java @@ -16,8 +16,6 @@ package org.springframework.data.jpa.repository.support; import java.io.Serializable; -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; import java.util.List; import javax.persistence.EntityManager; @@ -28,8 +26,8 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Order; import org.springframework.data.jpa.repository.QueryDslPredicateExecutor; -import org.springframework.util.ClassUtils; -import org.springframework.util.ReflectionUtils; +import org.springframework.data.querydsl.EntityPathResolver; +import org.springframework.data.querydsl.SimpleEntityPathResolver; import com.mysema.query.jpa.JPQLQuery; import com.mysema.query.jpa.impl.JPAQuery; @@ -223,126 +221,4 @@ public class QueryDslJpaRepository extends order.isAscending() ? com.mysema.query.types.Order.ASC : com.mysema.query.types.Order.DESC, property); } - - /** - * Strategy interface to abstract the ways to translate an plain domain - * class into a {@link EntityPath}. - * - * @author Oliver Gierke - */ - public static interface EntityPathResolver { - - EntityPath createPath(Class domainClass); - } - - /** - * Simple implementation of {@link EntityPathResolver} to lookup a query - * class by reflection and using the static field of the same type. - * - * @author Oliver Gierke - */ - static enum SimpleEntityPathResolver implements EntityPathResolver { - - INSTANCE; - - private static final String NO_CLASS_FOUND_TEMPLATE = - "Did not find a query class %s for domain class %s!"; - private static final String NO_FIELD_FOUND_TEMPLATE = - "Did not find a static field of the same type in %s!"; - - - /** - * Creates an {@link EntityPath} instance for the given domain class. - * Tries to lookup a class matching the naming convention (prepend Q to - * the simple name of the class, same package) and find a static field - * of the same type in it. - * - * @param domainClass - * @return - */ - @SuppressWarnings("unchecked") - public EntityPath createPath(Class domainClass) { - - String pathClassName = getQueryClassName(domainClass); - - try { - Class pathClass = - ClassUtils.forName(pathClassName, - QueryDslJpaRepository.class.getClassLoader()); - Field field = getStaticFieldOfType(pathClass); - - if (field == null) { - throw new IllegalStateException(String.format( - NO_FIELD_FOUND_TEMPLATE, pathClass)); - } else { - return (EntityPath) ReflectionUtils - .getField(field, null); - } - - } catch (ClassNotFoundException e) { - throw new IllegalArgumentException(String.format( - NO_CLASS_FOUND_TEMPLATE, pathClassName, - domainClass.getName()), e); - } - } - - - /** - * Returns the first static field of the given type inside the given - * type. - * - * @param type - * @return - */ - private Field getStaticFieldOfType(Class type) { - - for (Field field : type.getDeclaredFields()) { - - boolean isStatic = Modifier.isStatic(field.getModifiers()); - boolean hasSameType = type.equals(field.getType()); - - if (isStatic && hasSameType) { - return field; - } - } - - Class superclass = type.getSuperclass(); - return Object.class.equals(superclass) ? null - : getStaticFieldOfType(superclass); - } - - - /** - * Returns the name of the query class for the given domain class. - * - * @param domainClass - * @return - */ - private String getQueryClassName(Class domainClass) { - - String simpleClassName = ClassUtils.getShortName(domainClass); - return String.format("%s.Q%s%s", - domainClass.getPackage().getName(), - getClassBase(simpleClassName), domainClass.getSimpleName()); - } - - - /** - * Analyzes the short class name and potentially returns the outer - * class. - * - * @param shortName - * @return - */ - private String getClassBase(String shortName) { - - String[] parts = shortName.split("\\."); - - if (parts.length < 2) { - return ""; - } - - return parts[0] + "_"; - } - } } diff --git a/src/test/java/org/springframework/data/jpa/repository/support/SimpleEntityPathResolverUnitTests.java b/src/test/java/org/springframework/data/jpa/repository/support/SimpleEntityPathResolverUnitTests.java deleted file mode 100644 index 1745c2e53..000000000 --- a/src/test/java/org/springframework/data/jpa/repository/support/SimpleEntityPathResolverUnitTests.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright 2008-2011 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.jpa.repository.support; - -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; - -import org.junit.Test; -import org.springframework.data.jpa.domain.sample.QUser; -import org.springframework.data.jpa.domain.sample.User; -import org.springframework.data.jpa.repository.support.QueryDslJpaRepository.EntityPathResolver; -import org.springframework.data.jpa.repository.support.QueryDslJpaRepository.SimpleEntityPathResolver; -import org.springframework.data.jpa.repository.util.JpaClassUtilsUnitTests.NamedUser; -import org.springframework.data.jpa.repository.util.QJpaClassUtilsUnitTests_NamedUser; - - -/** - * Unit test for {@link SimpleEntityPathResolver}. - * - * @author Oliver Gierke - */ -public class SimpleEntityPathResolverUnitTests { - - EntityPathResolver resolver = - QueryDslJpaRepository.SimpleEntityPathResolver.INSTANCE; - - - @Test - public void createsRepositoryFromDomainClassCorrectly() throws Exception { - - assertThat(resolver.createPath(User.class), is(QUser.class)); - } - - - @Test - public void resolvesEntityPathForInnerClassCorrectly() throws Exception { - - assertThat(resolver.createPath(NamedUser.class), - is(QJpaClassUtilsUnitTests_NamedUser.class)); - } - - - @Test(expected = IllegalStateException.class) - public void rejectsFoundClassWithoutStaticFieldOfSameType() - throws Exception { - - resolver.createPath(Sample.class); - } - - - @Test(expected = IllegalArgumentException.class) - public void rejectsClassWithoutQueryClassConfrmingToTheNamingScheme() - throws Exception { - - resolver.createPath(QSimpleEntityPathResolverUnitTests_Sample.class); - } - - static class Sample { - - } -}