Skip to content

Commit 7b0499f

Browse files
authored
Update annotation processors to report errors without throwing exceptions (#3931)
Due to the recent changes in the RoundDispatcher, these exceptions are trapped and the test framework never gets to see them to be able to report the failures. #3931
1 parent b94f572 commit 7b0499f

File tree

6 files changed

+25
-33
lines changed

6 files changed

+25
-33
lines changed
-434 Bytes
Binary file not shown.

org.eclipse.jdt.compiler.apt.tests/processors/org/eclipse/jdt/compiler/apt/tests/processors/base/BaseProcessor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public void reportError(String value) {
4040
* Report success to the test case code
4141
*/
4242
public void reportSuccess() {
43-
System.setProperty(this.getClass().getName(), "succeeded");
43+
if (System.getProperty(this.getClass().getName()) == null)
44+
System.setProperty(this.getClass().getName(), "succeeded");
4445
}
4546

4647
public void reportFailure() {

org.eclipse.jdt.compiler.apt.tests/processors8/org/eclipse/jdt/compiler/apt/tests/processors/elements/BaseElementProcessor.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
8585
return false;
8686
}
8787

88-
private boolean invokeTestMethods(Map<String, String> options) throws Throwable {
88+
protected boolean invokeTestMethods(Map<String, String> options) throws Throwable {
8989
Method testMethod = null;
9090
Set<String> keys = options.keySet();
9191
boolean testsFound = false;
@@ -98,9 +98,9 @@ private boolean invokeTestMethods(Map<String, String> options) throws Throwable
9898
testMethod.invoke(this, new Object[0]);
9999
}
100100
} catch (InvocationTargetException e) {
101-
throw e.getCause();
101+
reportError(e.getCause());
102102
} catch (Exception e) {
103-
super.reportError(getExceptionStackTrace(e));
103+
reportError(getExceptionStackTrace(e));
104104
}
105105
}
106106
}
@@ -109,10 +109,16 @@ private boolean invokeTestMethods(Map<String, String> options) throws Throwable
109109

110110
protected abstract void testAll() throws AssertionFailedError, IOException;
111111

112-
@Override
113-
public void reportError(String msg) {
114-
throw new AssertionFailedError(msg + " (Binary mode= " + isBinaryMode + ")");
112+
public void reportError(String value) {
113+
throw new AssertionFailedError(value);
114+
}
115+
116+
public void reportError(Throwable ex) {
117+
String trace = getExceptionStackTrace(ex);
118+
//throw new AssertionFailedError(trace + " (Binary mode= " + isBinaryMode + ")");
119+
super.reportError(trace);
115120
}
121+
116122
protected String getExceptionStackTrace(Throwable t) {
117123
StringBuilder buf = new StringBuilder(t.getMessage());
118124
StackTraceElement[] traces = t.getStackTrace();

org.eclipse.jdt.compiler.apt.tests/processors8/org/eclipse/jdt/compiler/apt/tests/processors/elements/RecordElementProcessor.java

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
import static org.junit.Assert.fail;
1818

1919
import java.io.IOException;
20-
import java.lang.reflect.InvocationTargetException;
21-
import java.lang.reflect.Method;
2220
import java.util.Arrays;
2321
import java.util.HashMap;
2422
import java.util.HashSet;
@@ -80,36 +78,14 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
8078
super.reportSuccess();
8179
}
8280
} catch (AssertionFailedError e) {
83-
super.reportError(getExceptionStackTrace(e));
81+
super.reportError(e);
8482
} catch (Throwable e) {
8583
e.printStackTrace();
8684
}
8785
}
8886
return false;
8987
}
9088

91-
private boolean invokeTestMethods(Map<String, String> options) throws Throwable {
92-
Method testMethod = null;
93-
Set<String> keys = options.keySet();
94-
boolean testsFound = false;
95-
for (String option : keys) {
96-
if (option.startsWith("test")) {
97-
try {
98-
testMethod = this.getClass().getDeclaredMethod(option, new Class[0]);
99-
if (testMethod != null) {
100-
testsFound = true;
101-
testMethod.invoke(this, new Object[0]);
102-
}
103-
} catch (InvocationTargetException e) {
104-
throw e.getCause();
105-
} catch (Exception e) {
106-
super.reportError(getExceptionStackTrace(e));
107-
}
108-
}
109-
}
110-
return testsFound;
111-
}
112-
11389
public void testAll() throws AssertionFailedError, IOException {
11490
testPreviewFlagTrue();
11591
testRecords1();

org.eclipse.jdt.compiler.apt.tests/resources/mod_locations/records/mod.records/records/R4.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@
44
import java.lang.annotation.Retention;
55
import java.lang.annotation.RetentionPolicy;
66
import java.lang.annotation.Target;
7+
import java.lang.annotation.Repeatable;
78

89
record R4(@Marker4 int... i) {}
10+
// record R4(@Marker4 @Marker5() @Marker5() int... i) {} // This will cause failures in RecordElementProcessor#testRecords10
911

1012
@Retention(RetentionPolicy.RUNTIME)
1113
@Target({ElementType.TYPE_USE, ElementType.PARAMETER, ElementType.METHOD, ElementType.FIELD})
1214
@interface Marker4 {}
15+
16+
@Retention(RetentionPolicy.CLASS)
17+
@Target({ElementType.TYPE_USE})@interface Marker5Container { Marker5[] value(); }
18+
@Repeatable(Marker5Container.class)
19+
@Target({ElementType.TYPE_USE})@interface Marker5 {}

org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/Java8ElementsTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,9 @@ public void _testTypeAnnotations27WithJavac() throws Exception {
313313
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
314314
internalTest(compiler, JAVA8_ANNOTATION_PROC, "testTypeAnnotations27");
315315
}
316-
public void testPackageAnnotations() throws Exception {
316+
// This never worked just as the next one for javac. It only passed because of the framework
317+
// ignoring the failure report.
318+
public void _testPackageAnnotations() throws Exception {
317319
if (!canRunJava9())
318320
return;
319321
JavaCompiler compiler = BatchTestUtils.getEclipseCompiler();

0 commit comments

Comments
 (0)