public Property createCopy(EntityType targetModel) { if (!declaringType.getParameters().isEmpty()) { Type newType = TypeResolver.resolve(type, declaringType, targetModel); if (!newType.equals(type) || !newType.getClass().equals(type.getClass())) { return new Property(targetModel, name, newType, inits, false); } else { return new Property(targetModel, name, type, inits, targetModel.getSuperType() != null); } } else { return new Property(targetModel, name, type, inits, targetModel.getSuperType() != null); } }
protected void addToString(EntityType model, CodeWriter writer) throws IOException { writer.line("@Override"); writer.beginPublicMethod(Types.STRING, "toString"); StringBuilder builder = new StringBuilder(); for (Property property : model.getProperties()) { String propertyName = property.getEscapedName(); if (builder.length() > 0) { builder.append(" + \", "); } else { builder.append("\""); } builder.append(propertyName + " = \" + "); if (property.getType().getCategory() == TypeCategory.ARRAY) { builder.append("Arrays.toString(" + propertyName + ")"); } else { builder.append(propertyName); } } writer.line(" return ", builder.toString(), ";"); writer.end(); }
private Set<String> getAnnotationTypes(EntityType model) { Set<String> imports = new HashSet<String>(); for (Annotation annotation : model.getAnnotations()) { imports.add(annotation.annotationType().getName()); } if (propertyAnnotations) { for (Property property : model.getProperties()) { for (Annotation annotation : property.getAnnotations()) { imports.add(annotation.annotationType().getName()); } } } return imports; }
public void addProperty(Property field) { if (!propertyNames.contains(field.getName())) { propertyNames.add(field.getName()); escapedPropertyNames.add(field.getEscapedName()); properties.add(validateField(field)); } }
for (Annotation annotation : property.getAnnotations()) { writer.annotation(annotation); writer.field(property.getType(), property.getEscapedName());
@Test public void annotated_property() throws IOException { Property property = new Property(type, "entityField", type); property.addAnnotation(new QueryEntityImpl()); type.addProperty(property); BeanSerializer serializer = new BeanSerializer(); serializer.serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer)); String str = writer.toString(); assertTrue(str.contains("import com.querydsl.core.annotations.QueryEntity;")); assertTrue(str.contains("@QueryEntity")); }
protected void initEntityField(CodeWriter writer, SerializerConfig config, EntityType model, Property field) throws IOException { Type queryType = typeMappings.getPathType(field.getType(), model, false); if (!field.isInherited()) { boolean hasEntityFields = field.getType() instanceof EntityType && ((EntityType) field.getType()).hasEntityFields(); writer.line("this." + field.getEscapedName() + ASSIGN, "inits.isInitialized(\"" + field.getName() + "\") ? ", NEW + writer.getRawName(queryType) + "(forProperty(\"" + field.getName() + "\")", hasEntityFields ? (", inits.get(\"" + field.getName() + "\")") : EMPTY, ") : null;"); } else if (!config.useEntityAccessors()) { writer.line("this.", field.getEscapedName(), ASSIGN, "_super.", field.getEscapedName(), SEMICOLON); } }
@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\"));")); }
protected void entityField(EntityType model, Property field, SerializerConfig config, CodeWriter writer) throws IOException { Type queryType = typeMappings.getPathType(field.getType(), model, false); if (field.isInherited()) { writer.line("// inherited"); } if (config.useEntityAccessors()) { writer.protectedField(queryType, field.getEscapedName()); } else { writer.publicFinal(queryType, field.getEscapedName()); } }
@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()); }
protected void entityAccessor(EntityType model, Property field, CodeWriter writer) throws IOException { Type queryType = typeMappings.getPathType(field.getType(), model, false); writer.beginPublicMethod(queryType, field.getEscapedName()); writer.line("if (", field.getEscapedName(), " == null) {"); writer.line(" ", field.getEscapedName(), " = new ", writer.getRawName(queryType), "(forProperty(\"", field.getName(), "\"));"); writer.line("}"); writer.line(RETURN, field.getEscapedName(), SEMICOLON); writer.end(); }
property.addAnnotation(new IdImpl()); property.addAnnotation(new GeneratedValueImpl()); property.addAnnotation(new ElementCollectionImpl()); property.getData().put("COLUMN", column); if (columnAnnotations) { property.addAnnotation(new ColumnImpl(normalizedColumnName)); property.addAnnotation(new NotNullImpl()); property.addAnnotation(new SizeImpl(0, size));
private boolean hasPropertyWithType(TypeCategory category) { for (Property property : properties) { if (property.getType().getCategory() == category) { return true; } } return false; }
protected void validateInits(EntityType entityType, Property property) { for (String init : property.getInits()) { if (!init.startsWith("*") && property.getType() instanceof EntityType) { String initProperty = init.contains(".") ? init.substring(0, init.indexOf('.')) : init; Set<String> propertyNames = ((EntityType) property.getType()).getPropertyNames(); if (!propertyNames.contains(initProperty)) { processingEnv.getMessager().printMessage(Kind.ERROR, "Illegal inits of " + entityType.getFullName() + "." + property.getName() + ": " + initProperty + " not found in " + propertyNames); } } } } private void serializeMetaTypes() {
@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()); }
protected void addFullConstructor(EntityType model, CodeWriter writer) throws IOException { // public empty constructor writer.beginConstructor(); writer.end(); // full constructor writer.beginConstructor(model.getProperties(), propertyToParameter); for (Property property : model.getProperties()) { writer.line("this.", property.getEscapedName(), " = ", property.getEscapedName(), ";"); } writer.end(); }
protected boolean hasOwnEntityProperties(EntityType model) { if (model.hasEntityFields()) { for (Property property : model.getProperties()) { if (!property.isInherited() && property.getType().getCategory() == TypeCategory.ENTITY) { return true; } } } return false; }
protected void serialize(EntityType model, Property field, Type type, CodeWriter writer, String factoryMethod, String... args) throws IOException { Supertype superType = model.getSuperType(); // construct value StringBuilder value = new StringBuilder(); if (field.isInherited() && superType != null) { if (!superType.getEntityType().hasEntityFields()) { value.append("_super." + field.getEscapedName()); } } else { value.append(factoryMethod + "(\"" + field.getName() + QUOTE); for (String arg : args) { value.append(COMMA + arg); } value.append(")"); } // serialize it if (field.isInherited()) { writer.line("//inherited"); } if (value.length() > 0) { writer.publicFinal(type, field.getEscapedName(), value.toString()); } else { writer.publicFinal(type, field.getEscapedName()); } }