private Type createType(TypeElement typeElement, TypeCategory category, List<? extends TypeMirror> typeArgs, boolean deep) { String name = typeElement.getQualifiedName().toString(); String simpleName = typeElement.getSimpleName().toString(); String packageName = env.getElementUtils().getPackageOf(typeElement).getQualifiedName().toString(); Type[] params = new Type[typeArgs.size()]; for (int i = 0; i < params.length; i++) { params[i] = getType(typeArgs.get(i), deep); } return new SimpleType(category, name, packageName, simpleName, false, typeElement.getModifiers().contains(Modifier.FINAL), params); }
private Type asGeneric(Type type) { if (type.getParameters().size() == 0) { int count = type.getJavaClass().getTypeParameters().length; if (count > 0) { return new SimpleType(type, new Type[count]); } } return type; }
private Type getRaw(Type type) { if (type instanceof EntityType && type.getPackageName().startsWith("ext.java")) { return type; } else { return new SimpleType(type, type.getParameters()); } }
private Type createWithPackage(Type type) { String packageName = type.getPackageName(); String simpleName = prefix + normalizeName(type.getFullName() .substring(packageName.length() + 1)) + suffix; packageName = (packageName.startsWith("java") ? "ext." : "") + packageName + packageSuffix; return new SimpleType(type.getCategory(), packageName + "." + simpleName, packageName, simpleName, false, false); }
@Before public void setUp() { typeModel = new SimpleType(TypeCategory.ENTITY, "com.querydsl.DomainClass", "com.querydsl", "DomainClass", false,false); type = new EntityType(typeModel); }
@Before public void setUp() { typeModel = new SimpleType(TypeCategory.ENTITY, "com.querydsl.DomainClass", "com.querydsl", "DomainClass", false,false); type = new EntityType(typeModel); }
@Test public void superType() throws IOException { EntityType superType = new EntityType(new SimpleType(TypeCategory.ENTITY, "Entity2", "", "Entity2",false,false)); SimpleType type = new SimpleType(TypeCategory.ENTITY, "Entity", "", "Entity",false,false); EntityType entityType = new EntityType(type, Collections.singleton(new Supertype(superType, superType))); typeMappings.register(superType, queryTypeFactory.create(superType)); typeMappings.register(entityType, queryTypeFactory.create(entityType)); serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(writer.toString().contains("public final QEntity2 _super = new QEntity2(this);")); //CompileUtils.assertCompiles("QEntity", writer.toString()); }
@Test public void superType() throws IOException { EntityType superType = new EntityType(new SimpleType(TypeCategory.ENTITY, "Entity2", "", "Entity2",false,false)); SimpleType type = new SimpleType(TypeCategory.ENTITY, "Entity", "", "Entity",false,false); EntityType entityType = new EntityType(type, Collections.singleton(new Supertype(superType, superType))); typeMappings.register(superType, queryTypeFactory.create(superType)); typeMappings.register(entityType, queryTypeFactory.create(entityType)); serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(writer.toString().contains("public final QEntity2 _super = new QEntity2(this);")); //CompileUtils.assertCompiles("QEntity", writer.toString()); }
@Test public void correct_imports() throws IOException { SimpleType type = new SimpleType(TypeCategory.ENTITY, "test.Entity", "test", "Entity",false,false); EntityType entityType = new EntityType(type); typeMappings.register(entityType, queryTypeFactory.create(entityType)); serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(writer.toString().contains("import test.Entity;")); assertTrue(writer.toString().contains("public class QEntity extends EntityPathBase<Entity> {")); }
@Test public void escapedName() { Type typeModel = new SimpleType(TypeCategory.ENTITY, "com.querydsl.DomainClass", "com.querydsl", "DomainClass", false,false); EntityType type = new EntityType(typeModel); Property property = new Property(type, "boolean", type, Collections.<String>emptyList()); assertEquals("boolean$", property.getEscapedName()); }
@Test public void correct_superclass() throws IOException { SimpleType type = new SimpleType(TypeCategory.ENTITY, "java.util.Locale", "java.util", "Locale",false,false); EntityType entityType = new EntityType(type); typeMappings.register(entityType, queryTypeFactory.create(entityType)); serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(writer.toString().contains("public class QLocale extends EntityPathBase<Locale> {")); CompileUtils.assertCompiles("QLocale", writer.toString()); }
@Test public void no_package() throws IOException { SimpleType type = new SimpleType(TypeCategory.ENTITY, "Entity", "", "Entity",false,false); EntityType entityType = new EntityType(type); typeMappings.register(entityType, queryTypeFactory.create(entityType)); serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(writer.toString().contains("public class QEntity extends EntityPathBase<Entity> {")); CompileUtils.assertCompiles("QEntity", writer.toString()); }
@Test public void no_package() throws IOException { SimpleType type = new SimpleType(TypeCategory.ENTITY, "Entity", "", "Entity",false,false); EntityType entityType = new EntityType(type); typeMappings.register(entityType, queryTypeFactory.create(entityType)); serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(writer.toString().contains("public class QEntity extends BeanPath<Entity> {")); CompileUtils.assertCompiles("QEntity", writer.toString()); }
@Test public void correct_superclass() throws IOException { SimpleType type = new SimpleType(TypeCategory.ENTITY, "java.util.Locale", "java.util", "Locale",false,false); EntityType entityType = new EntityType(type); typeMappings.register(entityType, queryTypeFactory.create(entityType)); serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(writer.toString().contains("public class QLocale extends BeanPath<Locale> {")); CompileUtils.assertCompiles("QLocale", writer.toString()); }
@Test public void delegates() throws IOException { SimpleType type = new SimpleType(TypeCategory.ENTITY, "Entity", "", "Entity",false,false); EntityType entityType = new EntityType(type); Delegate delegate = new Delegate(type, type, "test", Collections.<Parameter>emptyList(), Types.STRING); entityType.addDelegate(delegate); typeMappings.register(entityType, queryTypeFactory.create(entityType)); serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(writer.toString().contains("return Entity.test(this);")); CompileUtils.assertCompiles("QEntity", writer.toString()); }
@Test public void delegates() throws IOException { SimpleType type = new SimpleType(TypeCategory.ENTITY, "Entity", "", "Entity",false,false); EntityType entityType = new EntityType(type); Delegate delegate = new Delegate(type, type, "test", Collections.<Parameter>emptyList(), Types.STRING); entityType.addDelegate(delegate); typeMappings.register(entityType, queryTypeFactory.create(entityType)); serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(writer.toString().contains("return Entity.test(this);")); CompileUtils.assertCompiles("QEntity", writer.toString()); }
@Test public void empty() throws IOException { SimpleType type = new SimpleType(TypeCategory.ENTITY, "Entity", "", "Entity",false,false); EntityType entityType = new EntityType(type); typeMappings.register(entityType, queryTypeFactory.create(entityType)); serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); CompileUtils.assertCompiles("QEntity", writer.toString()); }
@Test public void equals_and_hashCode() { Type typeModel = new SimpleType(TypeCategory.ENTITY, "com.querydsl.DomainClass", "com.querydsl", "DomainClass", false,false); EntityType type = new EntityType(typeModel); Property p1 = new Property(type, "property", type, Collections.<String>emptyList()); Property p2 = new Property(type, "property", type, Collections.<String>emptyList()); assertEquals(p1, p1); assertEquals(p1, p2); assertEquals(p1.hashCode(), p2.hashCode()); }
@Test public void primitive_array() throws IOException { SimpleType type = new SimpleType(TypeCategory.ENTITY, "Entity", "", "Entity",false,false); EntityType entityType = new EntityType(type); entityType.addProperty(new Property(entityType, "bytes", new ClassType(byte[].class))); typeMappings.register(entityType, queryTypeFactory.create(entityType)); serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(writer.toString().contains("public final SimplePath<byte[]> bytes")); CompileUtils.assertCompiles("QEntity", writer.toString()); }
@Test public void primitive_array() throws IOException { SimpleType type = new SimpleType(TypeCategory.ENTITY, "Entity", "", "Entity",false,false); EntityType entityType = new EntityType(type); entityType.addProperty(new Property(entityType, "bytes", new ClassType(byte[].class))); typeMappings.register(entityType, queryTypeFactory.create(entityType)); serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(writer.toString().contains("public final SimplePath<byte[]> bytes")); CompileUtils.assertCompiles("QEntity", writer.toString()); }