Skip to content

Commit

Permalink
Fail fast with null AnnotationSpec on type.
Browse files Browse the repository at this point in the history
  • Loading branch information
NightlyNexus committed Mar 12, 2018
1 parent d91f8e1 commit f24801f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/squareup/javapoet/TypeSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ public Builder addAnnotations(Iterable<AnnotationSpec> annotationSpecs) {
}

public Builder addAnnotation(AnnotationSpec annotationSpec) {
checkNotNull(annotationSpec, "annotationSpec == null");
this.annotations.add(annotationSpec);
return this;
}
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/com/squareup/javapoet/TypeSpecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,27 @@ private TypeElement getElement(Class<?> clazz) {
+ "}\n");
}

@Test public void addAnnotationDisallowsNull() {
try {
TypeSpec.classBuilder("Foo").addAnnotation((AnnotationSpec) null);
fail();
} catch (NullPointerException expected) {
assertThat(expected).hasMessageThat().isEqualTo("annotationSpec == null");
}
try {
TypeSpec.classBuilder("Foo").addAnnotation((ClassName) null);
fail();
} catch (NullPointerException expected) {
assertThat(expected).hasMessageThat().isEqualTo("type == null");
}
try {
TypeSpec.classBuilder("Foo").addAnnotation((Class<?>) null);
fail();
} catch (NullPointerException expected) {
assertThat(expected).hasMessageThat().isEqualTo("clazz == null");
}
}

@Test public void enumWithSubclassing() throws Exception {
TypeSpec roshambo = TypeSpec.enumBuilder("Roshambo")
.addModifiers(Modifier.PUBLIC)
Expand Down

0 comments on commit f24801f

Please sign in to comment.