use of io.requery.proxy.Property in project requery by requery.
the class EntityMetaGenerator method generateProperties.
private void generateProperties(AttributeDescriptor attribute, AttributeDescriptor parent, TypeMirror typeMirror, TypeName targetName, TypeName attributeName, CodeBlock.Builder block) {
String prefix = "";
if (parent != null) {
prefix = parent.getterName() + "().";
}
// boxed get/set using Objects
Class propertyClass = propertyClassFor(typeMirror);
ParameterizedTypeName propertyType = propertyName(propertyClass, targetName, attributeName);
TypeSpec.Builder builder = TypeSpec.anonymousClassBuilder("").addSuperinterface(propertyType);
boolean isNullable = typeMirror.getKind().isPrimitive() && attribute.isNullable();
boolean useGetter = entity.isUnimplementable() || entity.isImmutable();
boolean useSetter = entity.isUnimplementable();
String getName = prefix + (useGetter ? attribute.getterName() : attribute.fieldName());
String setName = prefix + (useSetter ? attribute.setterName() : attribute.fieldName());
new GeneratedProperty(getName, setName, targetName, attributeName).setNullable(isNullable).setReadOnly(entity.isImmutable()).setUseMethod(useGetter).build(builder);
// additional primitive get/set if the type is primitive
if (propertyClass != Property.class) {
TypeName primitiveType = TypeName.get(attribute.typeMirror());
String name = Names.upperCaseFirst(attribute.typeMirror().toString());
new GeneratedProperty(getName, setName, targetName, primitiveType).setMethodSuffix(name).setReadOnly(entity.isImmutable()).setUseMethod(useGetter).build(builder);
}
block.add(".setProperty($L)\n", builder.build());
block.add(".setPropertyName($S)\n", attribute.element().getSimpleName());
// property state get/set
if (!entity.isStateless()) {
ClassName stateClass = ClassName.get(PropertyState.class);
TypeSpec.Builder stateType = TypeSpec.anonymousClassBuilder("").addSuperinterface(parameterizedTypeName(Property.class, targetName, stateClass));
String fieldName = prefix + propertyStateFieldName(attribute);
new GeneratedProperty(fieldName, targetName, stateClass).build(stateType);
block.add(".setPropertyState($L)\n", stateType.build());
}
if (!entity.isImmutable()) {
return;
}
// if immutable add setter for the builder
String propertyName = attribute.fieldName();
TypeName builderName = typeName;
useSetter = false;
String parameterSuffix = null;
Optional<TypeMirror> builderType = entity.builderType();
if (builderType.isPresent()) {
parameterSuffix = ".builder";
if (parent != null) {
parameterSuffix = "." + parent.fieldName() + "Builder";
}
propertyName = attribute.setterName();
useSetter = true;
TypeElement element = elements.getTypeElement(builderType.get().toString());
if (element != null) {
for (ExecutableElement method : ElementFilter.methodsIn(element.getEnclosedElements())) {
List<? extends VariableElement> parameters = method.getParameters();
String name = Names.removeMethodPrefixes(method.getSimpleName());
// (some builders have with<Property> setters so strip that
if ((Names.matchesSetter("with", name)) || name.equalsIgnoreCase(attribute.fieldName()) && parameters.size() == 1) {
propertyName = method.getSimpleName().toString();
break;
} else if (Names.matchesSetter("set", name) || name.equalsIgnoreCase(attribute.fieldName()) && parameters.size() == 1) {
propertyName = method.getSimpleName().toString();
break;
}
}
} else {
// prefixed, prefix boolean types with is
if (ImmutableAnnotationKind.IMMUTABLE.isPresent(entity.element())) {
propertyName = attribute.fieldName();
String getterName = attribute.getterName().replaceFirst("get", "");
if (attribute.typeMirror().getKind() == TypeKind.BOOLEAN) {
propertyName = "is" + Names.upperCaseFirst(propertyName);
} else if (Names.isAllUpper(getterName)) {
propertyName = Names.lowerCaseFirst(getterName);
}
}
}
}
propertyType = propertyName(propertyClass, builderName, attributeName);
TypeSpec.Builder builderProperty = TypeSpec.anonymousClassBuilder("").addSuperinterface(propertyType);
new GeneratedProperty(propertyName, builderName, attributeName).setWriteOnly(true).setUseMethod(useSetter).setAccessSuffix(parameterSuffix).build(builderProperty);
if (propertyClass != Property.class) {
TypeName primitiveType = TypeName.get(attribute.typeMirror());
String name = Names.upperCaseFirst(attribute.typeMirror().toString());
new GeneratedProperty(propertyName, builderName, primitiveType).setMethodSuffix(name).setAccessSuffix(parameterSuffix).setUseMethod(useSetter).setWriteOnly(true).build(builderProperty);
}
block.add(".setBuilderProperty($L)\n", builderProperty.build());
}
Aggregations