DATACMNS-32 - Refactored Querydsl support code from JPA and Mongo module into core.

This commit is contained in:
Oliver Gierke
2011-04-20 20:30:00 +02:00
parent c2b8a1cf1a
commit b6845e7310
9 changed files with 340 additions and 0 deletions

View File

@@ -10,6 +10,11 @@
<artifactId>spring-data-commons-core</artifactId>
<packaging>jar</packaging>
<name>Spring Data Commons Core</name>
<properties>
<querydsl.version>2.1.1</querydsl.version>
</properties>
<dependencies>
<!-- Spring -->
@@ -69,6 +74,26 @@
<version>1.6</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-mongodb</artifactId>
<version>${querydsl.version}</version>
<optional>true</optional>
<exclusions>
<exclusion>
<groupId>com.google.code.morphia</groupId>
<artifactId>morphia</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
@@ -77,6 +102,23 @@
<groupId>com.springsource.bundlor</groupId>
<artifactId>com.springsource.bundlor.maven</artifactId>
</plugin>
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>maven-apt-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>generate-test-sources</phase>
<goals>
<goal>test-process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/test-annotations</outputDirectory>
<processor>com.mysema.query.apt.QuerydslAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,28 @@
/*
* Copyright 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.querydsl;
import com.mysema.query.types.EntityPath;
/**
* Strategy interface to abstract the ways to translate an plain domain class into a {@link EntityPath}.
*
* @author Oliver Gierke
*/
public interface EntityPathResolver {
<T> EntityPath<T> createPath(Class<T> domainClass);
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright 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.querydsl;
/**
*
* @author Oliver Gierke
*/
public class QueryDslUtils {
public static final boolean QUERY_DSL_PRESENT = org.springframework.util.ClassUtils.isPresent(
"com.mysema.query.types.Predicate", QueryDslUtils.class.getClassLoader());
}

View File

@@ -0,0 +1,135 @@
/*
* Copyright 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.querydsl;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import com.mysema.query.types.EntityPath;
/**
* Simple implementation of {@link EntityPathResolver} to lookup a query
* class by reflection and using the static field of the same type.
*
* @author Oliver Gierke
*/
public 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 <T> EntityPath<T> createPath(Class<T> domainClass) {
String pathClassName = getQueryClassName(domainClass);
try {
Class<?> pathClass =
ClassUtils.forName(pathClassName,
SimpleEntityPathResolver.class.getClassLoader());
Field field = getStaticFieldOfType(pathClass);
if (field == null) {
throw new IllegalStateException(String.format(
NO_FIELD_FOUND_TEMPLATE, pathClass));
} else {
return (EntityPath<T>) 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] + "_";
}
}

View File

@@ -0,0 +1,72 @@
/*
* Copyright 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.querydsl;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.data.querydsl.EntityPathResolver;
import org.springframework.data.querydsl.SimpleEntityPathResolver;
import org.springframework.data.querydsl.QSimpleEntityPathResolverUnitTests_NamedUser;
import org.springframework.data.querydsl.QSimpleEntityPathResolverUnitTests_Sample;
import org.springframework.data.querydsl.QUser;
import com.mysema.query.annotations.QueryEntity;
/**
* Unit test for {@link SimpleEntityPathResolver}.
*
* @author Oliver Gierke
*/
public class SimpleEntityPathResolverUnitTests {
EntityPathResolver resolver = 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(QSimpleEntityPathResolverUnitTests_NamedUser.class));
}
@Test(expected = IllegalArgumentException.class)
public void rejectsClassWithoutQueryClassConfrmingToTheNamingScheme()
throws Exception {
resolver.createPath(QSimpleEntityPathResolverUnitTests_Sample.class);
}
@QueryEntity
static class Sample {
}
@QueryEntity
static class NamedUser {
}
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 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.querydsl;
import com.mysema.query.annotations.QueryEntity;
/**
* @author Oliver Gierke
*/
@QueryEntity
public class User {
}

View File

@@ -5,6 +5,7 @@ Bundle-ManifestVersion: 2
Import-Package:
sun.reflect;version="0";resolution:=optional
Import-Template:
com.mysema.query.*;version="${querydsl.version:[=.=.=,+1.0.0)}";resolution:=optional,
org.springframework.aop.*;version="${org.springframework.version:[=.=.=,+1.0.0)}",
org.springframework.beans.*;version="${org.springframework.version:[=.=.=,+1.0.0)}",
org.springframework.core.*;version="${org.springframework.version:[=.=.=,+1.0.0)}",

View File

@@ -361,6 +361,14 @@
<name>Spring Framework Maven Snapshot Repository</name>
<url>http://maven.springframework.org/snapshot</url>
</repository>
<repository>
<id>querydsl</id>
<name>Mysema QueryDsl</name>
<url>http://source.mysema.com/maven2/releases</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<reporting>
<plugins>

View File

@@ -5,6 +5,7 @@ Changes in version 1.0.1
----------------------------------------
General
* Extracted Querydsl support code from JPA and Mongo modules (DATACMNS-32)
* Implementations of Page, Pageable and Sort are now serializable (DATACMNS-30)
Changes in version 1.0.0 (2011-04-18)