Provide access to enclosing type

This commit is contained in:
Timo Westkämper 2015-09-25 20:42:01 +03:00
parent a927b861eb
commit 5b6bcd325b
6 changed files with 49 additions and 5 deletions

View File

@ -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;

View File

@ -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;

View File

@ -27,6 +27,8 @@ public interface Type {
Type getComponentType();
Type getEnclosingType();
TypeCategory getCategory();
String getFullName();

View File

@ -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);

View File

@ -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());
}
}

View File

@ -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());
}
}