aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/java/com/squareup/javapoet/MethodSpec.java2
-rw-r--r--src/main/java/com/squareup/javapoet/Util.java4
-rw-r--r--src/test/java/com/squareup/javapoet/MethodSpecTest.java16
3 files changed, 19 insertions, 3 deletions
diff --git a/src/main/java/com/squareup/javapoet/MethodSpec.java b/src/main/java/com/squareup/javapoet/MethodSpec.java
index 2110f2e..1aa147f 100644
--- a/src/main/java/com/squareup/javapoet/MethodSpec.java
+++ b/src/main/java/com/squareup/javapoet/MethodSpec.java
@@ -287,7 +287,7 @@ public final class MethodSpec {
private List<TypeVariableName> typeVariables = new ArrayList<>();
private TypeName returnType;
private final List<ParameterSpec> parameters = new ArrayList<>();
- private final List<TypeName> exceptions = new ArrayList<>();
+ private final Set<TypeName> exceptions = new LinkedHashSet<>();
private final CodeBlock.Builder code = CodeBlock.builder();
private boolean varargs;
private CodeBlock defaultValue;
diff --git a/src/main/java/com/squareup/javapoet/Util.java b/src/main/java/com/squareup/javapoet/Util.java
index 864598e..5b9f557 100644
--- a/src/main/java/com/squareup/javapoet/Util.java
+++ b/src/main/java/com/squareup/javapoet/Util.java
@@ -73,8 +73,8 @@ final class Util {
if (!condition) throw new IllegalStateException(String.format(format, args));
}
- static <T> List<T> immutableList(List<T> list) {
- return Collections.unmodifiableList(new ArrayList<>(list));
+ static <T> List<T> immutableList(Collection<T> collection) {
+ return Collections.unmodifiableList(new ArrayList<>(collection));
}
static <T> Set<T> immutableSet(Collection<T> set) {
diff --git a/src/test/java/com/squareup/javapoet/MethodSpecTest.java b/src/test/java/com/squareup/javapoet/MethodSpecTest.java
index a826d2d..2a6cbb2 100644
--- a/src/test/java/com/squareup/javapoet/MethodSpecTest.java
+++ b/src/test/java/com/squareup/javapoet/MethodSpecTest.java
@@ -21,9 +21,11 @@ import java.io.Closeable;
import java.io.IOException;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
+import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
+import java.util.concurrent.TimeoutException;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
@@ -206,4 +208,18 @@ public final class MethodSpecTest {
assertThat(a.hashCode()).isEqualTo(b.hashCode());
}
+ @Test public void duplicateExceptionsIgnored() {
+ ClassName ioException = ClassName.get(IOException.class);
+ ClassName timeoutException = ClassName.get(TimeoutException.class);
+ MethodSpec methodSpec = MethodSpec.methodBuilder("duplicateExceptions")
+ .addException(ioException)
+ .addException(timeoutException)
+ .addException(timeoutException)
+ .addException(ioException)
+ .build();
+ assertThat(methodSpec.exceptions).isEqualTo(Arrays.asList(ioException, timeoutException));
+ assertThat(methodSpec.toBuilder().addException(ioException).build().exceptions)
+ .isEqualTo(Arrays.asList(ioException, timeoutException));
+ }
+
}