mirror of
https://github.com/querydsl/querydsl.git
synced 2026-07-03 21:07:49 +08:00
Provide access to enclosing type
This commit is contained in:
parent
a927b861eb
commit
5b6bcd325b
@ -35,7 +35,7 @@ public class ClassType implements Type {
|
||||
|
||||
private final List<Type> parameters;
|
||||
|
||||
private Type arrayType, componentType;
|
||||
private Type arrayType, componentType, enclosingType;
|
||||
|
||||
public ClassType(Class<?> javaClass, Type... parameters) {
|
||||
this(TypeCategory.SIMPLE, javaClass, Arrays.asList(parameters));
|
||||
@ -98,6 +98,17 @@ public class ClassType implements Type {
|
||||
return componentType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type getEnclosingType() {
|
||||
if (enclosingType == null) {
|
||||
Class<?> enclosingClass = javaClass.getEnclosingClass();
|
||||
if (enclosingClass != null) {
|
||||
enclosingType = new ClassType(enclosingClass);
|
||||
}
|
||||
}
|
||||
return enclosingType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFullName() {
|
||||
return className;
|
||||
|
||||
@ -43,7 +43,7 @@ public class SimpleType implements Type {
|
||||
|
||||
private final boolean primitiveClass, finalClass;
|
||||
|
||||
private Type arrayType, componentType;
|
||||
private Type arrayType, componentType, enclosingType;
|
||||
|
||||
private transient Class<?> javaClass;
|
||||
|
||||
@ -144,6 +144,20 @@ public class SimpleType implements Type {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type getEnclosingType() {
|
||||
if (enclosingType == null && localName.contains(".")) {
|
||||
String newLocalName = localName.substring(0, localName.lastIndexOf('.'));
|
||||
String newSimpleName = newLocalName.substring(newLocalName.lastIndexOf('.') + 1);
|
||||
String newFullName = newLocalName;
|
||||
if (packageName.length() > 0) {
|
||||
newFullName = packageName + "." + newLocalName;
|
||||
}
|
||||
enclosingType = new SimpleType(newFullName, packageName, newSimpleName);
|
||||
}
|
||||
return enclosingType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFullName() {
|
||||
return fullName;
|
||||
|
||||
@ -27,6 +27,8 @@ public interface Type {
|
||||
|
||||
Type getComponentType();
|
||||
|
||||
Type getEnclosingType();
|
||||
|
||||
TypeCategory getCategory();
|
||||
|
||||
String getFullName();
|
||||
|
||||
@ -45,6 +45,11 @@ public class TypeAdapter implements Type {
|
||||
return type.getComponentType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type getEnclosingType() {
|
||||
return type.getEnclosingType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return type.equals(o);
|
||||
|
||||
@ -5,9 +5,7 @@
|
||||
*/
|
||||
package com.mysema.codegen.model;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
@ -110,4 +108,10 @@ public class ClassTypeTest {
|
||||
// assertEquals("long", Types.LONG.getPrimitiveName());
|
||||
// assertEquals("short", Types.SHORT.getPrimitiveName());
|
||||
// }
|
||||
|
||||
@Test
|
||||
public void getEnclosingType() {
|
||||
assertEquals(new ClassType(ClassTypeTest.class), new ClassType(Inner.class).getEnclosingType());
|
||||
assertNull(new ClassType(ClassTypeTest.class).getEnclosingType());
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package com.mysema.codegen.model;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
@ -66,4 +67,11 @@ public class SimpleTypeTest {
|
||||
// assertEquals("int", new SimpleType(Types.INTEGER).getPrimitiveName());
|
||||
// }
|
||||
|
||||
@Test
|
||||
public void GetEnclosingType() {
|
||||
assertEquals(new SimpleType(new ClassType(SimpleTypeTest.class)),
|
||||
new SimpleType(new ClassType(Inner.class)).getEnclosingType());
|
||||
assertNull(new SimpleType(new ClassType(SimpleTypeTest.class)).getEnclosingType());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user