use of org.jboss.forge.roaster.model.source.AnnotationElementSource in project morphia by mongodb.
the class AnnotationBuilders method setDefaults.
private void setDefaults(MethodSource<JavaClassSource> constructor, JavaAnnotationSource source) {
String body = "";
List<AnnotationElementSource> elements = source.getAnnotationElements();
for (AnnotationElementSource element : elements) {
DefaultValue defaultValue = element.getDefaultValue();
if (defaultValue != null) {
String literal = defaultValue.getLiteral();
var annot = defaultValue.getAnnotation();
if (annot != null) {
literal = format("%sBuilder.%s().build()", annot.getQualifiedName().replace(annot.getName(), "") + "builders." + annot.getName(), builderMethodName(annot.getName()));
} else if (literal != null && element.getType().isArray()) {
literal = format("new %s%s", element.getType().getName(), literal);
}
if (literal != null) {
body += format("annotation.%s = %s;%n", element.getName(), literal);
}
}
}
constructor.setBody(body);
}
use of org.jboss.forge.roaster.model.source.AnnotationElementSource in project morphia by mongodb.
the class AnnotationBuilders method equals.
private void equals(JavaClassSource annotation, List<AnnotationElementSource> elements) {
StringJoiner comparisons = new StringJoiner(" && ", "return ", ";");
for (AnnotationElementSource element : elements) {
String comparator;
if (element.getType().isArray()) {
builder.addImport(Arrays.class);
comparator = "Arrays";
} else {
comparator = "Objects";
}
comparisons.add(format("%s.equals(%s, that.%s)", comparator, element.getName(), element.getName()));
}
annotation.addMethod().setName("equals").setReturnType(boolean.class).setPublic().setBody("if (this == o) {\n" + " return true;\n" + "}\n" + "if (!(o instanceof " + annotation.getName() + ")) {\n" + " return false;\n" + "}\n" + "var that = (" + annotation.getName() + ") o;\n" + comparisons).addParameter("Object", "o");
}
use of org.jboss.forge.roaster.model.source.AnnotationElementSource in project morphia by mongodb.
the class AnnotationBuilders method hashCode.
private void hashCode(JavaClassSource annotation, List<AnnotationElementSource> elements) {
StringJoiner values = new StringJoiner(", ", "return Objects.hash(", ");");
for (AnnotationElementSource element : elements) {
values.add(element.getName());
}
annotation.addMethod().setName("hashCode").setReturnType(int.class).setPublic().setBody(values.toString());
}
use of org.jboss.forge.roaster.model.source.AnnotationElementSource in project morphia by mongodb.
the class AnnotationBuilders method emitBuilder.
private void emitBuilder(File sourceFile) throws IOException {
source = Roaster.parse(JavaAnnotationSource.class, sourceFile);
builder = Roaster.create(JavaClassSource.class).setName(source.getName() + "Builder").setPackage(source.getPackage() + ".builders").setFinal(true);
builder.addAnnotation("dev.morphia.annotations.internal.MorphiaInternal");
JavaDocSource<JavaClassSource> javaDoc = builder.getJavaDoc();
javaDoc.addTagValue("@since", "2.3");
javaDoc.addTagValue("@morphia.internal", "");
javaDoc.addTagValue("@morphia.experimental", "");
MethodSource<JavaClassSource> constructor = builder.addMethod().setConstructor(true).setPrivate();
setDefaults(constructor, source);
builder.addMethod().setPublic().setName("build").setReturnType(source.getName()).setBody(format("var anno = annotation; annotation = null; return anno;", builder.getName()));
builder.addMethod().setPublic().setStatic(true).setName(builderMethodName(source.getName())).setReturnType(builder.getName()).setBody(format("return new %s();", builder.getName()));
JavaClassSource morphiaAnnotation = annotationType(source, builder);
List<AnnotationElementSource> elements = source.getAnnotationElements();
if (!elements.isEmpty()) {
copyBuilder(source, elements);
equals(morphiaAnnotation, elements);
hashCode(morphiaAnnotation, elements);
}
for (AnnotationElementSource element : elements) {
String name = element.getName();
morphiaAnnotation.addField().setName(name).setType(element.getType().toString()).setPrivate();
morphiaAnnotation.addMethod().setPublic().setName(name).setReturnType(element.getType()).setBody(format("return %s;", name)).addAnnotation(Override.class);
builder.addMethod().setPublic().setName(name).setReturnType(builder.getName()).setBody(format("annotation.%s = %s; return this;", name, name)).addParameter(parameterType(element), name);
}
for (Import anImport : source.getImports()) {
builder.addImport(anImport);
}
builder.addImport(Objects.class);
output();
}
use of org.jboss.forge.roaster.model.source.AnnotationElementSource in project morphia by mongodb.
the class AnnotationBuilders method copyBuilder.
private void copyBuilder(JavaAnnotationSource source, List<AnnotationElementSource> elements) {
MethodSource<JavaClassSource> copier = builder.addMethod().setPublic().setStatic(true).setName(builderMethodName(source.getName())).setReturnType(builder.getName()).setBody(format("return new %s();", builder.getName()));
copier.addParameter(source.getName(), "source");
StringJoiner copies = new StringJoiner("\n");
copies.add(format("var builder = new %s();", builder.getName()));
for (AnnotationElementSource element : elements) {
String name = element.getName();
copies.add(format("builder.annotation.%s = source.%s();", name, name));
}
copies.add("return builder;");
copier.setBody(copies.toString());
}
Aggregations