Skip to content

Commit

Permalink
Improved query class detection.
Browse files Browse the repository at this point in the history
- made sure a static field is used
- corrected naming convention for inner classes
- altered and added test cases for those scenarios
  • Loading branch information
odrotbohm committed Jan 8, 2011
1 parent 2013edf commit 960bf09
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 16 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@


import java.io.Serializable; import java.io.Serializable;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.List; import java.util.List;


import javax.persistence.EntityManager; import javax.persistence.EntityManager;
Expand Down Expand Up @@ -282,16 +283,16 @@ public <T> EntityPath<T> createPath(Class<T> domainClass) {
Class<?> pathClass = Class<?> pathClass =
ClassUtils.forName(pathClassName, ClassUtils.forName(pathClassName,
QueryDslJpaRepository.class.getClassLoader()); QueryDslJpaRepository.class.getClassLoader());

Field field = getStaticFieldOfType(pathClass);
for (Field field : pathClass.getFields()) {
if (pathClass.equals(field.getType())) { if (field == null) {
return (EntityPath<T>) ReflectionUtils.getField(field, throw new IllegalStateException(String.format(
null); NO_FIELD_FOUND_TEMPLATE, pathClass));
} } else {
return (EntityPath<T>) ReflectionUtils
.getField(field, null);
} }


throw new IllegalStateException(String.format(
NO_FIELD_FOUND_TEMPLATE, pathClass));
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
throw new IllegalArgumentException(String.format( throw new IllegalArgumentException(String.format(
NO_CLASS_FOUND_TEMPLATE, pathClassName, NO_CLASS_FOUND_TEMPLATE, pathClassName,
Expand All @@ -300,6 +301,31 @@ public <T> EntityPath<T> createPath(Class<T> domainClass) {
} }




/**
* 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. * Returns the name of the query class for the given domain class.
* *
Expand All @@ -309,7 +335,8 @@ public <T> EntityPath<T> createPath(Class<T> domainClass) {
private String getQueryClassName(Class<?> domainClass) { private String getQueryClassName(Class<?> domainClass) {


String simpleClassName = ClassUtils.getShortName(domainClass); String simpleClassName = ClassUtils.getShortName(domainClass);
return String.format("%s%sQ%s", domainClass.getPackage().getName(), return String.format("%s.Q%s%s",
domainClass.getPackage().getName(),
getClassBase(simpleClassName), domainClass.getSimpleName()); getClassBase(simpleClassName), domainClass.getSimpleName());
} }


Expand All @@ -326,10 +353,10 @@ private String getClassBase(String shortName) {
String[] parts = shortName.split("\\."); String[] parts = shortName.split("\\.");


if (parts.length < 2) { if (parts.length < 2) {
return "."; return "";
} }


return "." + parts[0] + "$"; return parts[0] + "_";
} }
} }
} }
Original file line number Original file line Diff line number Diff line change
@@ -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.jpa.repository.support;

/**
* Stub for a generated QueryDsl query class.
*
* @author Oliver Gierke
*/
class QSimpleEntityPathResolverUnitTests_Sample {

public QSimpleEntityPathResolverUnitTests_Sample field;
}
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.junit.Test; import org.junit.Test;
import org.springframework.data.jpa.domain.sample.QUser; import org.springframework.data.jpa.domain.sample.QUser;
import org.springframework.data.jpa.domain.sample.User; import org.springframework.data.jpa.domain.sample.User;
import org.springframework.data.jpa.repository.support.JpaAnnotationEntityInformationUnitTests.FieldAnnotatedEntity;
import org.springframework.data.jpa.repository.support.QueryDslJpaRepository.EntityPathResolver; import org.springframework.data.jpa.repository.support.QueryDslJpaRepository.EntityPathResolver;
import org.springframework.data.jpa.repository.support.QueryDslJpaRepository.SimpleEntityPathResolver; import org.springframework.data.jpa.repository.support.QueryDslJpaRepository.SimpleEntityPathResolver;


Expand All @@ -43,6 +44,15 @@ public void createsRepositoryFromDomainClassCorrectly() throws Exception {
} }




@Test
public void resolvesEntityPathForInnerClassCorrectly() throws Exception {

assertThat(
resolver.createPath(FieldAnnotatedEntity.class),
is(QJpaAnnotationEntityInformationUnitTests_FieldAnnotatedEntity.class));
}


@Test(expected = IllegalStateException.class) @Test(expected = IllegalStateException.class)
public void rejectsFoundClassWithoutStaticFieldOfSameType() public void rejectsFoundClassWithoutStaticFieldOfSameType()
throws Exception { throws Exception {
Expand All @@ -55,14 +65,10 @@ public void rejectsFoundClassWithoutStaticFieldOfSameType()
public void rejectsClassWithoutQueryClassConfrmingToTheNamingScheme() public void rejectsClassWithoutQueryClassConfrmingToTheNamingScheme()
throws Exception { throws Exception {


resolver.createPath(QSample.class); resolver.createPath(QSimpleEntityPathResolverUnitTests_Sample.class);
} }


static class Sample { static class Sample {


} }

static class QSample {

}
} }

0 comments on commit 960bf09

Please sign in to comment.