mirror of
https://github.com/querydsl/querydsl.git
synced 2026-07-03 21:07:49 +08:00
added tests for EConstructor
added static create method for EConstructor
This commit is contained in:
parent
8c798277e9
commit
d02c6befcc
@ -10,6 +10,8 @@ import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.lang.ClassUtils;
|
||||
|
||||
import com.mysema.query.types.Visitor;
|
||||
|
||||
/**
|
||||
@ -25,6 +27,27 @@ public class EConstructor<D> extends Expr<D> {
|
||||
private final List<Expr<?>> args;
|
||||
|
||||
private final Class<?>[] parameterTypes;
|
||||
|
||||
public static <D> EConstructor<D> create(Class<D> type, Expr<?>... args){
|
||||
for (Constructor<?> c : type.getConstructors()){
|
||||
Class<?>[] paramTypes = c.getParameterTypes();
|
||||
if (paramTypes.length == args.length){
|
||||
boolean found = true;
|
||||
for (int i = 0; i < paramTypes.length; i++){
|
||||
Class<?> paramType = paramTypes[i];
|
||||
if (paramType.isPrimitive()) paramType = ClassUtils.primitiveToWrapper(paramType);
|
||||
if (!paramType.isAssignableFrom(args[i].getType())){
|
||||
found = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (found){
|
||||
return new EConstructor<D>(type, paramTypes, args);
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("Got no matching constructor");
|
||||
}
|
||||
|
||||
public EConstructor(Class<D> type, Class<?>[] paramTypes, Expr<?>... args) {
|
||||
super(type);
|
||||
|
||||
@ -0,0 +1,56 @@
|
||||
package com.mysema.query.types.expr;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class EConstructorTest {
|
||||
|
||||
public static class Projection{
|
||||
|
||||
public Projection(){
|
||||
|
||||
}
|
||||
|
||||
public Projection(Long id){
|
||||
|
||||
}
|
||||
|
||||
public Projection(long id, String text){
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_Constructor(){
|
||||
ENumber<Long> longVal = ENumberConst.create(1l);
|
||||
EString stringVal = EStringConst.create("");
|
||||
new EConstructor<Projection>(Projection.class, new Class[]{long.class, String.class}, longVal, stringVal)
|
||||
.getJavaConstructor();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_create(){
|
||||
ENumber<Long> longVal = ENumberConst.create(1l);
|
||||
EString stringVal = EStringConst.create("");
|
||||
Constructor<?> c = EConstructor.create(Projection.class, longVal, stringVal).getJavaConstructor();
|
||||
assertEquals(long.class, c.getParameterTypes()[0]);
|
||||
assertEquals(String.class, c.getParameterTypes()[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_create2(){
|
||||
ENumber<Long> longVal = ENumberConst.create(1l);
|
||||
Constructor<?> c = EConstructor.create(Projection.class, longVal).getJavaConstructor();
|
||||
assertEquals(Long.class, c.getParameterTypes()[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_create3(){
|
||||
Constructor<?> c = EConstructor.create(Projection.class).getJavaConstructor();
|
||||
assertEquals(0, c.getParameterTypes().length);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user