use of io.requery.PropertyNameStyle in project requery by requery.
the class EntityGenerator method generateProxyMethods.
private void generateProxyMethods(TypeSpec.Builder builder) {
// add proxy field
TypeName entityType = entity.isEmbedded() ? nameResolver.typeNameOf(parent) : typeName;
TypeName proxyName = parameterizedTypeName(EntityProxy.class, entityType);
FieldSpec.Builder proxyField = FieldSpec.builder(proxyName, PROXY_NAME, Modifier.PRIVATE, Modifier.FINAL, Modifier.TRANSIENT);
if (!entity.isEmbedded()) {
proxyField.initializer("new $T(this, $L)", proxyName, TYPE_NAME);
}
builder.addField(proxyField.build());
for (AttributeDescriptor attribute : entity.attributes().values()) {
boolean useField = attribute.isTransient() || attribute.isEmbedded();
TypeMirror typeMirror = attribute.typeMirror();
TypeName unboxedTypeName;
if (attribute.isIterable()) {
unboxedTypeName = parameterizedCollectionName(typeMirror);
} else if (attribute.isOptional()) {
unboxedTypeName = TypeName.get(tryFirstTypeArgument(attribute.typeMirror()));
} else if (attribute.isEmbedded()) {
EntityDescriptor embedded = graph.embeddedDescriptorOf(attribute).orElseThrow(IllegalStateException::new);
unboxedTypeName = nameResolver.embeddedTypeNameOf(embedded, entity);
} else {
unboxedTypeName = nameResolver.tryGeneratedTypeName(typeMirror);
}
String attributeName = attribute.fieldName();
String getterName = attribute.getterName();
String fieldName = Names.upperCaseUnderscore(Names.removeMemberPrefixes(attributeName));
if (entity.isEmbedded()) {
fieldName = attributeFieldName(attribute);
}
// getter
MethodSpec.Builder getter = MethodSpec.methodBuilder(getterName).addModifiers(Modifier.PUBLIC).returns(attribute.isOptional() ? TypeName.get(typeMirror) : unboxedTypeName);
if (Mirrors.overridesMethod(types, typeElement, getterName)) {
getter.addAnnotation(Override.class);
}
memberExtensions.forEach(extension -> extension.addToGetter(attribute, getter));
if (useField) {
if (attribute.isEmbedded()) {
// have to cast to embedded type
getter.addStatement("return ($T)this.$L", unboxedTypeName, attributeName);
} else {
getter.addStatement("return this.$L", attributeName);
}
} else if (attribute.isOptional()) {
getter.addStatement("return $T.ofNullable($L.get($L))", Optional.class, PROXY_NAME, fieldName);
} else {
getter.addStatement("return $L.get($L)", PROXY_NAME, fieldName);
}
builder.addMethod(getter.build());
// setter
String setterName = attribute.setterName();
// if read only don't generate a public setter
boolean readOnly = entity.isReadOnly() || attribute.isReadOnly();
// edge case check if it's interface and we need to implement the setter
if (entity.element().getKind().isInterface() && ElementFilter.methodsIn(entity.element().getEnclosedElements()).stream().anyMatch(element -> element.getSimpleName().toString().equals(setterName))) {
readOnly = false;
}
if (!readOnly) {
TypeName setTypeName = unboxedTypeName;
boolean castType = false;
// use wildcard generic collection type if necessary
if (setTypeName instanceof ParameterizedTypeName) {
ParameterizedTypeName parameterizedName = (ParameterizedTypeName) setTypeName;
List<TypeName> arguments = parameterizedName.typeArguments;
List<TypeName> wildcards = new ArrayList<>();
for (TypeName argument : arguments) {
if (!(argument instanceof WildcardTypeName)) {
Elements elements = processingEnv.getElementUtils();
TypeElement element = elements.getTypeElement(argument.toString());
if (element != null && element.getKind() == ElementKind.INTERFACE) {
wildcards.add(WildcardTypeName.subtypeOf(argument));
} else {
wildcards.add(argument);
}
} else {
wildcards.add(argument);
}
}
TypeName[] array = new TypeName[wildcards.size()];
setTypeName = ParameterizedTypeName.get(parameterizedName.rawType, wildcards.toArray(array));
castType = true;
}
String paramName = Names.lowerCaseFirst(Names.removeMemberPrefixes(attributeName));
MethodSpec.Builder setter = MethodSpec.methodBuilder(setterName).addModifiers(Modifier.PUBLIC).addParameter(setTypeName, paramName);
if (useField) {
setter.addStatement("this.$L = $L", attributeName, paramName);
} else {
if (castType) {
setter.addAnnotation(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "unchecked").build());
setter.addStatement("$L.set($L, ($T)$L)", PROXY_NAME, fieldName, unboxedTypeName, paramName);
} else {
setter.addStatement("$L.set($L, $L)", PROXY_NAME, fieldName, paramName);
}
}
memberExtensions.forEach(extension -> extension.addToSetter(attribute, setter));
PropertyNameStyle style = entity.propertyNameStyle();
if (style == PropertyNameStyle.FLUENT || style == PropertyNameStyle.FLUENT_BEAN) {
setter.addStatement("return this");
setter.returns(typeName);
}
builder.addMethod(setter.build());
}
}
MethodSpec.Builder constructor = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC);
if (entity.isEmbedded()) {
constructor.addParameter(ParameterSpec.builder(proxyName, "proxy").build());
constructor.addStatement("this.$L = proxy", PROXY_NAME);
entity.attributes().values().stream().filter(attribute -> !attribute.isTransient()).forEach(attribute -> {
ParameterizedTypeName attributeType = ParameterizedTypeName.get(ClassName.get(Attribute.class), nameResolver.typeNameOf(parent), resolveAttributeType(attribute));
constructor.addParameter(ParameterSpec.builder(attributeType, attribute.name()).build());
constructor.addStatement("this.$L = $L", attributeFieldName(attribute), attribute.name());
});
}
generateListeners(constructor);
// initialize the generated embedded entities
entity.attributes().values().stream().filter(AttributeDescriptor::isEmbedded).forEach(attribute -> graph.embeddedDescriptorOf(attribute).ifPresent(embedded -> {
ClassName embeddedName = nameResolver.embeddedTypeNameOf(embedded, entity);
String format = embedded.attributes().values().stream().map(attr -> Names.upperCaseUnderscore(embeddedAttributeName(attribute, attr))).collect(Collectors.joining(", ", "$L = new $T($L, ", ")"));
constructor.addStatement(format, attribute.fieldName(), embeddedName, PROXY_NAME);
}));
builder.addMethod(constructor.build());
}
Aggregations