-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#60 First crack at PathHelper for java config
- Loading branch information
Taylor Wicksell
committed
May 27, 2014
1 parent
ae23c88
commit 4e50648
Showing
4 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
validation/src/main/java/org/springjutsu/validation/dsl/PathHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package org.springjutsu.validation.dsl; | ||
|
||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Modifier; | ||
import java.lang.reflect.ParameterizedType; | ||
import java.util.Collection; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
import org.springframework.cglib.proxy.Enhancer; | ||
import org.springframework.cglib.proxy.InvocationHandler; | ||
|
||
/** | ||
* Will generate dynamic proxies along a call chain. Does not support | ||
* abstract/final types. Does not support classes with no default constructor. | ||
* Internally, overrides the toString method on each proxy to return the current path. | ||
* @author twicksell | ||
* | ||
*/ | ||
public class PathHelper { | ||
|
||
public static <T> T forEntity(Class<T> clazz) | ||
{ | ||
return forEntity(clazz, "", null); | ||
} | ||
|
||
protected static <T> T forEntity(Class<T> clazz, String path, Class parameterizedType) | ||
{ | ||
Enhancer enhancer = new Enhancer(); | ||
return (T) enhancer.create(clazz, new PathBuildingInvocationHandler(path, parameterizedType)); | ||
} | ||
|
||
|
||
} | ||
|
||
class PathBuildingInvocationHandler extends PathHelper implements InvocationHandler | ||
{ | ||
|
||
String path; | ||
Class parameterizedType; | ||
public PathBuildingInvocationHandler(String path, Class parameterizedType) { | ||
this.path=path; | ||
this.parameterizedType=parameterizedType; | ||
} | ||
@Override | ||
public Object invoke(Object proxy, Method method, Object[] arguments) throws Throwable { | ||
Class returnType = (parameterizedType != null) ? parameterizedType : method.getReturnType(); | ||
Class parameterType = null; | ||
if(method.getName().equals("toString")) | ||
return path; | ||
if(Collection.class.isAssignableFrom(method.getReturnType())) | ||
{ | ||
parameterType = Class.forName(((ParameterizedType)method.getGenericReturnType()).getActualTypeArguments()[0].getTypeName()); | ||
} | ||
|
||
|
||
if(!path.isEmpty() && parameterizedType == null) | ||
path+="."; | ||
path+=StringUtils.uncapitalize(method.getName().replace("get", "")); | ||
if(method.getReturnType().equals(String.class)) | ||
return path; | ||
if(Modifier.isFinal(method.getReturnType().getModifiers())) | ||
throw new RuntimeException("Cannot help with final or abstract return types: "+method.getReturnType()); | ||
|
||
return PathHelper.forEntity(returnType, path, parameterType); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
validation/src/test/java/org/springjutsu/validation/dsl/PathHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.springjutsu.validation.dsl; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.springjutsu.validation.test.entities.Company; | ||
import org.springjutsu.validation.test.entities.Customer; | ||
|
||
public class PathHelperTest { | ||
|
||
@Test | ||
public void test() | ||
{ | ||
Assert.assertEquals("address", PathHelper.forEntity(Customer.class).getAddress().toString()); | ||
Assert.assertEquals("address.lineOne", PathHelper.forEntity(Customer.class).getAddress().getLineOne()); | ||
Assert.assertEquals("customers.firstName", PathHelper.forEntity(Company.class).getCustomers().get(0).getFirstName()); | ||
Assert.assertEquals("customers", PathHelper.forEntity(Company.class).getCustomers().toString()); | ||
} | ||
|
||
} |