Skip to content
This repository was archived by the owner on Oct 10, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,30 @@
<version>4.6.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.4-android</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>0.39</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.testing.compile</groupId>
<artifactId>compile-testing</artifactId>
<version>0.19</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand Down
55 changes: 47 additions & 8 deletions src/main/java/com/squareup/javapoet/AnnotationSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,7 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
Expand Down Expand Up @@ -247,6 +240,51 @@ public AnnotationSpec build() {
}
}

/**
*
* @param builder The AnnotationSpec.Builder that you want to add in;
* @param name The name of the AnnotationSpec;
* @param format The format of the @name;
* @param specs The AnnotationSpec that be added in;
* @return the AnnotationSpec.Builder that be added with @specs
*/
public static AnnotationSpec.Builder addMembers(AnnotationSpec.Builder builder, String name, String format, AnnotationSpec... specs) {
final List<CodeBlock> codeBlocks = new ArrayList<>();
for (AnnotationSpec spec : specs) {
codeBlocks.add(CodeBlock.of(format, spec));
}
return addMembers(builder, name, codeBlocks);
}

/**
*
* @param builder The AnnotationSpec.Builder that you want to add in;
* @param name The name of the AnnotationSpec;
* @param format The format of the @name;
* @param specs The AnnotationSpec that be added in, speciticly, a Collection with AnnotationSpecs;
* @return the AnnotationSpec.Builder that be added with @specs
*/
public static AnnotationSpec.Builder addMembers(AnnotationSpec.Builder builder, String name, String format, Collection<AnnotationSpec> specs) {
final List<CodeBlock> codeBlocks = new ArrayList<>();
for (AnnotationSpec spec : specs) {
codeBlocks.add(CodeBlock.of(format, spec));
}
return addMembers(builder, name, codeBlocks);
}

/**
*
* @param builder The AnnotationSpec.Builder that you want to add in;
* @param name The name of the AnnotationSpec;
* @param codeBlocks The CodeBlocks that be added in, speciticly, a Collection with AnnotationSpecs;
* @return the AnnotationSpec.Builder that be added with @codeBlocks
*/
public static AnnotationSpec.Builder addMembers(AnnotationSpec.Builder builder, String name, Collection<CodeBlock> codeBlocks) {
final List<CodeBlock> values = builder.members.computeIfAbsent(name, k -> new ArrayList<>());
values.addAll(codeBlocks);
return builder;
}

/**
* Annotation value visitor adding members to the given builder instance.
*/
Expand Down Expand Up @@ -281,4 +319,5 @@ private static class Visitor extends SimpleAnnotationValueVisitor8<Builder, Stri
return builder;
}
}

}
Loading