Merge pull request #35 from querydsl/enclosing-type

Provide access to enclosing type
This commit is contained in:
John Tims 2015-09-28 11:22:10 -04:00
commit d4a1801f9f
8 changed files with 127 additions and 29 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;
@ -173,6 +184,11 @@ public class ClassType implements Type {
return javaClass.isPrimitive();
}
@Override
public boolean isMember() {
return javaClass.getEnclosingClass() != null;
}
@Override
public String toString() {
return getGenericName(true);

View File

@ -41,9 +41,9 @@ public class SimpleType implements Type {
private final List<Type> parameters;
private final boolean primitiveClass, finalClass;
private final boolean primitiveClass, finalClass, memberClass;
private Type arrayType, componentType;
private Type arrayType, componentType, enclosingType;
private transient Class<?> javaClass;
@ -77,7 +77,7 @@ public class SimpleType implements Type {
} else {
this.localName = fullName;
}
if (fullName.substring(packageName.length() + 1).contains(".")) {
if (localName.contains(".")) {
this.outerClassName = fullName.substring(0, fullName.lastIndexOf('.'));
} else {
this.outerClassName = fullName;
@ -85,6 +85,7 @@ public class SimpleType implements Type {
this.primitiveClass = primitiveClass;
this.finalClass = finalClass;
this.parameters = parameters;
this.memberClass = localName.contains(".");
}
public SimpleType(TypeCategory typeCategory, String fullName, String packageName,
@ -144,6 +145,16 @@ public class SimpleType implements Type {
}
}
@Override
public Type getEnclosingType() {
if (enclosingType == null && memberClass) {
String newLocalName = localName.substring(0, localName.lastIndexOf('.'));
String newSimpleName = newLocalName.substring(newLocalName.lastIndexOf('.') + 1);
enclosingType = new SimpleType(outerClassName, packageName, newSimpleName);
}
return enclosingType;
}
@Override
public String getFullName() {
return fullName;
@ -217,8 +228,11 @@ public class SimpleType implements Type {
@Override
public String getRawName(Set<String> packages, Set<String> classes) {
if (packages.contains(packageName) || classes.contains(fullName)
|| classes.contains(outerClassName)) {
if (classes.contains(fullName)) {
return simpleName;
} else if (classes.contains(outerClassName)) {
return fullName.substring(outerClassName.lastIndexOf('.') + 1);
} else if (packages.contains(packageName)) {
return localName;
} else {
return fullName;
@ -245,6 +259,11 @@ public class SimpleType implements Type {
return primitiveClass;
}
@Override
public boolean isMember() {
return memberClass;
}
@Override
public String toString() {
return getGenericName(true);

View File

@ -27,6 +27,8 @@ public interface Type {
Type getComponentType();
Type getEnclosingType();
TypeCategory getCategory();
String getFullName();
@ -49,4 +51,6 @@ public interface Type {
boolean isPrimitive();
boolean isMember();
}

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);
@ -114,6 +119,11 @@ public class TypeAdapter implements Type {
return type.isPrimitive();
}
@Override
public boolean isMember() {
return type.isMember();
}
@Override
public String toString() {
return type.toString();

View File

@ -55,21 +55,19 @@ public final class ClassUtils {
public static String getName(Class<?> cl, Set<String> packages, Set<String> classes) {
if (cl.isArray()) {
return getName(cl.getComponentType(), packages, classes) + "[]";
}
if (cl.getName().indexOf('$') > 0) {
return getName(cl.getDeclaringClass(), packages, classes) + "." + cl.getSimpleName();
}
final String canonicalName = cl.getName();
final int i = canonicalName.lastIndexOf('.');
if (i == -1) {
return canonicalName;
} else {
final String packageName = canonicalName.substring(0, i);
if (packages.contains(packageName) || classes.contains(canonicalName)) {
return cl.getSimpleName();
} else {
return canonicalName;
}
final String canonicalName = cl.getName().replace('$', '.');
final int i = cl.getName().lastIndexOf('.');
if (classes.contains(canonicalName)) {
return cl.getSimpleName();
} else if (cl.getEnclosingClass() != null) {
return getName(cl.getEnclosingClass(), packages, classes) + "." + cl.getSimpleName();
} else if (i == -1) {
return canonicalName;
} else if (packages.contains(canonicalName.substring(0, i))) {
return canonicalName.substring(i + 1);
} else {
return canonicalName;
}
}

View File

@ -5,19 +5,22 @@
*/
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;
import org.junit.Test;
import com.google.common.collect.ImmutableSet;
public class ClassTypeTest {
public class Inner {
public class Inner2 {
public class Inner3 {
}
}
}
private final ClassType stringType = new ClassType(TypeCategory.STRING, String.class);
@ -110,4 +113,24 @@ public class ClassTypeTest {
// assertEquals("long", Types.LONG.getPrimitiveName());
// assertEquals("short", Types.SHORT.getPrimitiveName());
// }
@Test
public void GetEnclosingType() {
Type outer = new ClassType(ClassTypeTest.class);
Type inner = new ClassType(ClassTypeTest.Inner.class);
Type inner2 = new ClassType(ClassTypeTest.Inner.Inner2.class);
Type inner3 = new ClassType(ClassTypeTest.Inner.Inner2.Inner3.class);
assertEquals(inner2, inner3.getEnclosingType());
assertEquals(inner, inner2.getEnclosingType());
assertEquals(outer, inner.getEnclosingType());
assertNull(outer.getEnclosingType());
assertEquals("ClassTypeTest.Inner.Inner2.Inner3",
inner3.getRawName(ImmutableSet.of(outer.getPackageName()), ImmutableSet.<String>of()));
assertEquals("Inner2.Inner3",
inner3.getRawName(ImmutableSet.<String>of(), ImmutableSet.of(inner2.getFullName())));
assertEquals("Inner3",
inner3.getRawName(ImmutableSet.<String>of(), ImmutableSet.of(inner3.getFullName())));
}
}

View File

@ -1,15 +1,20 @@
package com.mysema.codegen.model;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;
import java.util.Collections;
import org.junit.Test;
import com.google.common.collect.ImmutableSet;
public class SimpleTypeTest {
public static class Inner {
public class Inner2 {
public class Inner3 {
}
}
}
@Test
@ -66,4 +71,29 @@ public class SimpleTypeTest {
// assertEquals("int", new SimpleType(Types.INTEGER).getPrimitiveName());
// }
@Test
public void GetEnclosingType() {
Type outer = new SimpleType(new ClassType(SimpleTypeTest.class));
Type inner = new SimpleType(new ClassType(SimpleTypeTest.Inner.class));
Type inner2 = new SimpleType(new ClassType(SimpleTypeTest.Inner.Inner2.class));
Type inner3 = new SimpleType(new ClassType(SimpleTypeTest.Inner.Inner2.Inner3.class));
assertEquals(inner2, inner3.getEnclosingType());
assertEquals(inner, inner2.getEnclosingType());
assertEquals(outer, inner.getEnclosingType());
assertNull(outer.getEnclosingType());
assertEquals("SimpleTypeTest.Inner.Inner2.Inner3",
inner3.getRawName(ImmutableSet.of(outer.getPackageName()), ImmutableSet.<String>of()));
assertEquals("Inner2.Inner3",
inner3.getRawName(ImmutableSet.<String>of(), ImmutableSet.of(inner2.getFullName())));
assertEquals("Inner3",
inner3.getRawName(ImmutableSet.<String>of(), ImmutableSet.of(inner3.getFullName())));
}
@Test
public void IsMember() {
assertTrue(new SimpleType(new ClassType(SimpleTypeTest.Inner.class)).isMember());
assertFalse(new SimpleType(new ClassType(SimpleType.class)).isMember());
}
}

View File

@ -19,9 +19,7 @@ public class ClassUtilsTest {
@Test
public void GetName() {
assertEquals("int", ClassUtils.getName(int.class));
assertEquals(
"int",
ClassUtils.getName(int.class, Collections.<String> emptySet(),
assertEquals("int", ClassUtils.getName(int.class, Collections.<String> emptySet(),
Collections.<String> emptySet()));
assertEquals("Object", ClassUtils.getName(Object.class));
assertEquals("Object[]", ClassUtils.getName(Object[].class));