Skip to content

Commit 30eeb19

Browse files
committed
Fix NPE thrown on FileClauses when there are Diagnostics that aren't associated with any element.
------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=66118534
1 parent 440f9b7 commit 30eeb19

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

src/main/java/com/google/testing/compile/JavaSourcesSubject.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ public LineClause in(final JavaFileObject file) {
164164
diagnosticsWithMessage.filter(new Predicate<Diagnostic<? extends FileObject>>() {
165165
@Override
166166
public boolean apply(Diagnostic<? extends FileObject> input) {
167-
return file.toUri().getPath().equals(input.getSource().toUri().getPath());
167+
return ((input.getSource() != null)
168+
&& file.toUri().getPath().equals(input.getSource().toUri().getPath()));
168169
}
169170
});
170171
if (diagnosticsInFile.isEmpty()) {
@@ -173,9 +174,11 @@ public boolean apply(Diagnostic<? extends FileObject> input) {
173174
diagnosticsWithMessage.transform(
174175
new Function<Diagnostic<? extends FileObject>, String>() {
175176
@Override public String apply(Diagnostic<? extends FileObject> input) {
176-
return input.getSource().getName();
177+
return (input.getSource() != null) ? input.getSource().getName()
178+
: "(no associated file)";
177179
}
178-
})));
180+
})
181+
.toSet()));
179182
}
180183
return new LineClause() {
181184
@Override public UnsuccessfulCompilationClause and() {
@@ -194,11 +197,14 @@ public boolean apply(Diagnostic<?> input) {
194197
failureStrategy.fail(String.format(
195198
"Expected an error on line %d of %s, but only found errors on line(s) %s",
196199
lineNumber, file.getName(), diagnosticsInFile.transform(
197-
new Function<Diagnostic<?>, Long>() {
198-
@Override public Long apply(Diagnostic<?> input) {
199-
return input.getLineNumber();
200+
new Function<Diagnostic<?>, String>() {
201+
@Override public String apply(Diagnostic<?> input) {
202+
long errLine = input.getLineNumber();
203+
return (errLine != Diagnostic.NOPOS) ? errLine + ""
204+
: "(no associated position)";
200205
}
201-
})));
206+
})
207+
.toSet()));
202208
}
203209
return new ColumnClause() {
204210
@Override
@@ -220,11 +226,14 @@ public boolean apply(Diagnostic<?> input) {
220226
failureStrategy.fail(String.format(
221227
"Expected an error at %d:%d of %s, but only found errors at column(s) %s",
222228
lineNumber, columnNumber, file.getName(), diagnosticsOnLine.transform(
223-
new Function<Diagnostic<?>, Long>() {
224-
@Override public Long apply(Diagnostic<?> input) {
225-
return input.getColumnNumber();
229+
new Function<Diagnostic<?>, String>() {
230+
@Override public String apply(Diagnostic<?> input) {
231+
long errCol = input.getColumnNumber();
232+
return (errCol != Diagnostic.NOPOS) ? errCol + ""
233+
: "(no associated position)";
226234
}
227-
})));
235+
})
236+
.toSet()));
228237
}
229238
return new ChainingClause<UnsuccessfulCompilationClause>() {
230239
@Override public UnsuccessfulCompilationClause and() {

src/test/java/com/google/testing/compile/JavaSourcesSubjectFactoryTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import javax.lang.model.SourceVersion;
4141
import javax.lang.model.element.Element;
4242
import javax.lang.model.element.TypeElement;
43+
import javax.tools.Diagnostic;
4344
import javax.tools.Diagnostic.Kind;
4445
import javax.tools.JavaFileObject;
4546

@@ -157,8 +158,9 @@ public void failsToCompile_throwsNotInFile() {
157158
fail();
158159
} catch (VerificationException expected) {
159160
ASSERT.that(expected.getMessage())
160-
.isEqualTo(String.format("Expected an error in %s, but only found errors in [%s]",
161-
otherFileObject.getName(), fileObject.getName()));
161+
.isEqualTo(String.format("Expected an error in %s, but only found errors in %s",
162+
otherFileObject.getName(), ImmutableSet.of(fileObject.getName(),
163+
"(no associated file)")));
162164
}
163165
}
164166

@@ -365,6 +367,7 @@ public synchronized void init(ProcessingEnvironment processingEnv) {
365367
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
366368
for (Element element : roundEnv.getRootElements()) {
367369
messager.printMessage(Kind.ERROR, "expected error!", element);
370+
messager.printMessage(Kind.ERROR, "another expected error!");
368371
}
369372
return false;
370373
}

0 commit comments

Comments
 (0)