Skip to content

Commit

Permalink
Merge pull request #3 from xenit-eu/multiline-wrapper-annotations
Browse files Browse the repository at this point in the history
Override multiline settings for wrapper annotations
  • Loading branch information
rschev authored Dec 6, 2023
2 parents 9c66092 + ee25fc7 commit 6351ce1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'eu.xenit.enterprise-conventions.oss' version '0.2.0'
id 'eu.xenit.enterprise-conventions.oss' version '0.4.0'
}

rootProject.name = 'bard'
11 changes: 10 additions & 1 deletion src/main/java/eu/xenit/contentcloud/bard/AnnotationSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ void emit(CodeWriter codeWriter, boolean inline) throws IOException {
} else if (members.size() == 1 && members.containsKey("value")) {
// @Named("foo")
codeWriter.emit("@$T(", type);
emitAnnotationValues(codeWriter, whitespace, memberSeparator, members.get("value"));
if (hasOnlyAnnotations(members.get("value"))) {
// override inlining logic when emitting annotation that wraps array of annotations
emitAnnotationValues(codeWriter, "\n", ",\n", members.get("value"));
} else {
emitAnnotationValues(codeWriter, whitespace, memberSeparator, members.get("value"));
}
codeWriter.emit(")");
} else {
// Inline:
Expand All @@ -84,6 +89,10 @@ void emit(CodeWriter codeWriter, boolean inline) throws IOException {
}
}

private boolean hasOnlyAnnotations(List<CodeBlock> blocks) {
return blocks.stream().allMatch(b -> b.toString().trim().startsWith("@"));
}

private void emitAnnotationValues(CodeWriter codeWriter, String whitespace,
String memberSeparator, List<CodeBlock> values) throws IOException {
if (values.size() == 1) {
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/eu/xenit/contentcloud/bard/AnnotationSpecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,27 @@ public class IsAnnotated {
private String toString(TypeSpec typeSpec) {
return JavaFile.builder("com.squareup.tacos", typeSpec).build().toString();
}

@Retention(RetentionPolicy.RUNTIME)
public @interface SubAnnotation {
String name() default "foo";
}

@Retention(RetentionPolicy.RUNTIME)
public @interface HasSubAnnotations {
SubAnnotation[] value();
}

@Test public void arrayOfAnnotations() {
AnnotationSpec.Builder builder = AnnotationSpec.builder(HasSubAnnotations.class);
builder.addMember("value", "$L", AnnotationSpec.builder(SubAnnotation.class).addMember("name", "$S", "bar").build());
builder.addMember("value", "$L", AnnotationSpec.builder(SubAnnotation.class).addMember("name", "$S", "baz").build());
builder.addMember("value", "$L", AnnotationSpec.builder(SubAnnotation.class).addMember("name", "$S", "baq").build());
assertThat(builder.build().toString()).isEqualTo(
"@eu.xenit.contentcloud.bard.AnnotationSpecTest.HasSubAnnotations({\n"
+ " @eu.xenit.contentcloud.bard.AnnotationSpecTest.SubAnnotation(name = \"bar\"),\n"
+ " @eu.xenit.contentcloud.bard.AnnotationSpecTest.SubAnnotation(name = \"baz\"),\n"
+ " @eu.xenit.contentcloud.bard.AnnotationSpecTest.SubAnnotation(name = \"baq\")\n"
+ "})");
}
}

0 comments on commit 6351ce1

Please sign in to comment.