public void addInterface(Class<?> iface) { interfaces.add(new ClassType(iface)); }
@SuppressWarnings("rawtypes") public void register(TypeCategory category, @Nullable Class<? extends Expression> expr, @Nullable Class<? extends Path> path, @Nullable Class<? extends TemplateExpression> template) { if (expr != null) { exprTypes.put(category, new ClassType(expr)); } if (path != null) { pathTypes.put(category, new ClassType(path)); } if (template != null) { templateTypes.put(category, new ClassType(template)); } }
protected void listAccessor(EntityType model, Property field, CodeWriter writer) throws IOException { String escapedName = field.getEscapedName(); Type queryType = typeMappings.getPathType(field.getParameter(0), model, false); writer.beginPublicMethod(queryType, escapedName, new Parameter("index", Types.INT)); writer.line(RETURN + escapedName + ".get(index);").end(); writer.beginPublicMethod(queryType, escapedName, new Parameter("index", new ClassType(Expression.class, Types.INTEGER))); writer.line(RETURN + escapedName + ".get(index);").end(); }
@Override public Parameter apply(Parameter p) { Type type; if (!asExpr) { type = typeMappings.getExprType( p.getType(), model, false, false, true); } else if (p.getType().isFinal()) { type = new ClassType(Expression.class, p.getType()); } else { type = new ClassType(Expression.class, new TypeExtends(p.getType())); } return new Parameter(p.getName(), type); } });
@Override public Parameter apply(Parameter p) { Type type; if (!asExpr) { type = typeMappings.getExprType(p.getType(), model, false, false, true); } else if (p.getType().isFinal()) { type = new ClassType(Expression.class, p.getType()); } else { type = new ClassType(Expression.class, new TypeExtends(p.getType())); } return new Parameter(p.getName(), type); } });
@Test public void getPathType_of_innerClass() { TypeMappings typeMappings = new JavaTypeMappings(); EntityType model = new EntityType(new ClassType(TypeMappingsTest.class)); EntityType type = new EntityType(new ClassType(Entity.class)); typeMappings.register(type, new QueryTypeFactoryImpl("Q","","").create(type)); Type pathType = typeMappings.getPathType(type, model, false); assertEquals("QTypeMappingsTest_Entity", pathType.getSimpleName()); }
@Test public void javadocs_for_innerClass() throws IOException { EntityType entityType = new EntityType(new ClassType(Entity.class)); typeMappings.register(entityType, queryTypeFactory.create(entityType)); serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(writer.toString().contains("QEntitySerializerTest_Entity is a Querydsl query type for Entity")); CompileUtils.assertCompiles("QEntitySerializerTest_Entity", writer.toString()); }
protected void mapAccessor(EntityType model, Property field, CodeWriter writer) throws IOException { String escapedName = field.getEscapedName(); Type queryType = typeMappings.getPathType(field.getParameter(1), model, false); writer.beginPublicMethod(queryType, escapedName, new Parameter("key", field.getParameter(0))); writer.line(RETURN + escapedName + ".get(key);").end(); writer.beginPublicMethod(queryType, escapedName, new Parameter("key", new ClassType(Expression.class, field.getParameter(0)))); writer.line(RETURN + escapedName + ".get(key);").end(); }
@Test public void different_package() throws IOException { queryTypeFactory = new QueryTypeFactoryImpl("Q", "", ".gen"); EntityType entityType = new EntityType(new ClassType(Entity.class)); typeMappings.register(entityType, queryTypeFactory.create(entityType)); serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(writer.toString().contains("public class QEntitySerializerTest_Entity " + "extends EntityPathBase<EntitySerializerTest.Entity>")); CompileUtils.assertCompiles("QEntitySerializerTest_Entity", writer.toString()); }
@Test public void customType() throws IOException { SimpleType type = new SimpleType(TypeCategory.ENTITY, "Entity", "", "Entity",false,false); EntityType entityType = new EntityType(type); entityType.addProperty(new Property(entityType, "property", new ClassType(Double[].class))); typeMappings.register(new ClassType(Double[].class), new ClassType(Point.class)); typeMappings.register(entityType, queryTypeFactory.create(entityType)); assertTrue(typeMappings.isRegistered(entityType.getProperties().iterator().next().getType())); serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(writer.toString().contains( "public final com.querydsl.codegen.Point property = " + "new com.querydsl.codegen.Point(forProperty(\"property\"));")); }
@Test public void interfaces() throws IOException { BeanSerializer serializer = new BeanSerializer(); serializer.addInterface(new ClassType(Serializable.class)); serializer.serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(writer.toString().contains("public class DomainClass implements Serializable {")); }
@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()); }
@Test public void uncapSimpleName_escaped3() { ClassType typeModel = new ClassType(TypeCategory.ENTITY, Void.class); EntityType entityModel = new EntityType(typeModel); assertEquals("void$", entityModel.getModifiedSimpleName()); }
@Test public void uncapSimpleName_escaped2() { ClassType typeModel = new ClassType(TypeCategory.ENTITY, Object.class); EntityType entityModel = new EntityType(typeModel); assertEquals("object", entityModel.getModifiedSimpleName()); entityModel.addProperty(new Property(entityModel, "OBJECT", "object", typeModel, Collections.<String> emptyList(), false)); assertEquals("object1", entityModel.getModifiedSimpleName()); }
@Test public void uncapSimpleName_escaped() { ClassType typeModel = new ClassType(TypeCategory.ENTITY, Object.class); EntityType entityModel = new EntityType(typeModel); assertEquals("object", entityModel.getModifiedSimpleName()); entityModel.addProperty(new Property(entityModel, "object", typeModel)); assertEquals("object1", entityModel.getModifiedSimpleName()); }
@Test public void toString_() throws IOException { // property type.addProperty(new Property(type, "entityField", type)); type.addProperty(new Property(type, "collection", new SimpleType(Types.COLLECTION, typeModel))); type.addProperty(new Property(type, "listField", new SimpleType(Types.LIST, typeModel))); type.addProperty(new Property(type, "setField", new SimpleType(Types.SET, typeModel))); type.addProperty(new Property(type, "arrayField", new ClassType(TypeCategory.ARRAY, String[].class))); type.addProperty(new Property(type, "mapField", new SimpleType(Types.MAP, typeModel, typeModel))); BeanSerializer serializer = new BeanSerializer(); serializer.setAddToString(true); serializer.serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); assertTrue(String.valueOf(writer).contains( " @Override\n" + " public String toString()")); }
@Test public void fullConstructor() throws IOException { // property type.addProperty(new Property(type, "entityField", type)); type.addProperty(new Property(type, "collection", new SimpleType(Types.COLLECTION, typeModel))); type.addProperty(new Property(type, "listField", new SimpleType(Types.LIST, typeModel))); type.addProperty(new Property(type, "setField", new SimpleType(Types.SET, typeModel))); type.addProperty(new Property(type, "arrayField", new ClassType(TypeCategory.ARRAY, String[].class))); type.addProperty(new Property(type, "mapField", new SimpleType(Types.MAP, typeModel, typeModel))); BeanSerializer serializer = new BeanSerializer(); serializer.setAddFullConstructor(true); serializer.serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); //System.out.println(writer.toString()); }