/* * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
@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.