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

@@ -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 {
}