@Test publicvoid testUnreachableCatchMulti() throws Exception {
runDiagSpanTest(""" class Test { publicvoid exception(boolean b) { try { if (b) thrownew Sub1(); else thrownew Sub2();
} catch(Sub1 exc) {
} catch(Sub2 exc) {
} catch(IllegalStateException | /^Base1/ | /^Base2/ exc) { }
}
} class Base1 extends Exception {} class Sub1 extends Base1 {} class Base2 extends Exception {} class Sub2 extends Base2 {} """, '/', '^');
}
@Test publicvoid testSubtypeMulti1() throws Exception {
runDiagSpanTest(""" class Test { publicvoid exception(boolean b) { try { thrownew Sub1();
} catch(Base1 | /^Sub1/ exc) { }
}
} class Base1 extends Exception {} class Sub1 extends Base1 {} """, '/', '^');
}
@Test publicvoid testSubtypeMulti2() throws Exception {
runDiagSpanTest(""" class Test { publicvoid exception(boolean b) { try { thrownew Sub1();
} catch(Sub1 | /^Base1/ exc) { }
}
} class Base1 extends Exception {} class Sub1 extends Base1 {} """, '/', '^');
}
privatevoid runDiagSpanTest(String code, char spanMarker, char prefMarker) throws Exception { var realCode = new StringBuilder(); var expectedError = new ArrayList<String>(); int startPos = -1; int preferedPos = -1; int pos = 0;
for (int i = 0; i < code.length(); i++) { char c = code.charAt(i); if (c == spanMarker) { if (startPos == (-1)) {
startPos = pos;
} else {
expectedError.add("" + startPos + ":" + pos + ":" + preferedPos);
startPos = (-1);
preferedPos = (-1);
}
} elseif (c == prefMarker) { if (preferedPos == (-1)) {
preferedPos = pos;
} else { thrownew AssertionError("Too many markers!");
}
} else {
realCode.append(c);
pos++;
}
}
if (startPos != (-1) || preferedPos != (-1)) { thrownew AssertionError("Incorrect number of markers!");
}
var compiler = ToolProvider.getSystemJavaCompiler(); var actualErrors = new ArrayList<String>();
DiagnosticListener<JavaFileObject> dl = d -> {
System.err.println("d=" + d);
actualErrors.add("" + d.getStartPosition() + ":" + d.getEndPosition() + ":" + d.getPosition());
}; var sourceFiles = List.of(new JFOImpl(realCode.toString())); var task = compiler.getTask(null, null, dl, null, null, sourceFiles);
task.call(); if (!Objects.equals(expectedError, actualErrors)) { thrownew AssertionError("Expected error spans not found, expected: " +
expectedError + ", actual: " + actualErrors);
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.