From a4fa9682be28072f1a0d9a100f0b2664bd9eb40e Mon Sep 17 00:00:00 2001 From: Stephan Herrmann Date: Sun, 23 Jun 2024 13:22:09 +0200 Subject: [PATCH] Bug 545605 - Adjust ecj to change in JVMS 4.7.18./19. (#2628) Accept class files with mismatching parameter count vis-a-vis parameter annotations (which is legal since Java 9). Also introduces system property jdt.reject.parameterAnnotations.countMismatch to get back the previous error reporting. fixes https://github.com/eclipse-jdt/eclipse.jdt.core/issues/2625 --- .../MethodInfoWithParameterAnnotations.java | 40 ++++++++++-------- .../regression/ClassFileReaderTest_1_8.java | 28 ++++++++++++ .../workspace/TestGH2625.jar | Bin 0 -> 719 bytes 3 files changed, 50 insertions(+), 18 deletions(-) create mode 100644 org.eclipse.jdt.core.tests.compiler/workspace/TestGH2625.jar diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/classfmt/MethodInfoWithParameterAnnotations.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/classfmt/MethodInfoWithParameterAnnotations.java index 79bce007add..daf79f786c3 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/classfmt/MethodInfoWithParameterAnnotations.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/classfmt/MethodInfoWithParameterAnnotations.java @@ -26,25 +26,29 @@ class MethodInfoWithParameterAnnotations extends MethodInfoWithAnnotations { } @Override public IBinaryAnnotation[] getParameterAnnotations(int index, char[] classFileName) { - try { - return this.parameterAnnotations == null ? null : this.parameterAnnotations[index]; - } catch (ArrayIndexOutOfBoundsException aioobe) { - // detailed reporting to track down https://bugs.eclipse.org/474081 - StringBuilder message = new StringBuilder("Mismatching number of parameter annotations, "); //$NON-NLS-1$ - message.append(index); - message.append('>'); - message.append(this.parameterAnnotations.length-1); - message.append(" in "); //$NON-NLS-1$ - message.append(getSelector()); - char[] desc = getGenericSignature(); - if (desc != null) - message.append(desc); - else - message.append(getMethodDescriptor()); - if (classFileName != null) - message.append(" in ").append(classFileName); //$NON-NLS-1$ - throw new IllegalStateException(message.toString(), aioobe); + if (this.parameterAnnotations != null) { + if (index < this.parameterAnnotations.length) { + return this.parameterAnnotations[index]; + } + if (Boolean.getBoolean("jdt.reject.parameterAnnotations.countMismatch")) { //$NON-NLS-1$ + // detailed reporting to track down https://bugs.eclipse.org/474081 + StringBuilder message = new StringBuilder("Mismatching number of parameter annotations, "); //$NON-NLS-1$ + message.append(index); + message.append('>'); + message.append(this.parameterAnnotations.length-1); + message.append(" in "); //$NON-NLS-1$ + message.append(getSelector()); + char[] desc = getGenericSignature(); + if (desc != null) + message.append(desc); + else + message.append(getMethodDescriptor()); + if (classFileName != null) + message.append(" in ").append(classFileName); //$NON-NLS-1$ + throw new IllegalStateException(message.toString()); + } } + return null; } @Override public int getAnnotatedParametersCount() { diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ClassFileReaderTest_1_8.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ClassFileReaderTest_1_8.java index 695daeef763..45559f7b3a3 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ClassFileReaderTest_1_8.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ClassFileReaderTest_1_8.java @@ -31,6 +31,7 @@ @SuppressWarnings({ "rawtypes" }) public class ClassFileReaderTest_1_8 extends AbstractRegressionTest { static { +// TESTS_NAMES = new String[] { "testGH2625" }; } public static Test suite() { @@ -470,6 +471,33 @@ public void testBug548596() { ); } + public void testGH2625() { + String[] libs = getDefaultClassPaths(); + int len = libs.length; + System.arraycopy(libs, 0, libs = new String[len+1], 0, len); + // in this jar the num_parameters field of RuntimeInvisibleParameterAnnotations has been manually set to 2 + // (annotations on the 3-arg method a(Function,Object,long)): + libs[libs.length-1] = this.getCompilerTestsPluginDirectoryPath() + File.separator + "workspace" + File.separator + "TestGH2625.jar"; + + runConformTest( + new String[] { + "Test.java", + """ + import a.A; + public class Test { + void test(A a) { + a.m(null, null, 0L); + } + } + """ + }, + "", + libs, + false, + null + ); + } + /** * Produce a nicely formatted type annotation for testing. Exercises the API for type annotations. * Output examples:
diff --git a/org.eclipse.jdt.core.tests.compiler/workspace/TestGH2625.jar b/org.eclipse.jdt.core.tests.compiler/workspace/TestGH2625.jar new file mode 100644 index 0000000000000000000000000000000000000000..698dfacf66191c4082af65cdfe62263059bcf836 GIT binary patch literal 719 zcmWIWW@h1HVBlb2NSk&of&mF|GO#fCx`sIFdiuHP|2xIN5CBvv!ob17fuU3cs12^v z*U`_@%{4eg&)4m<@0rs+-nx1hdA)VD&Yd~GImqCO@q?#DdS1Rdp1v1LS8WM0FbcTv zLEp>goM&jkIeo1Y`d&JWT34Ptee{$GXdydChG=7_Jgq^kU253*1YE!c*nAYmYznG1xHYVTr)C6_-w&sn!WiN?Ew) zr2pgg*TsKMKV){+p0x7OvMVk-|4%oFQgm_ip8D^Q%zxA8FSdoaJxkJ>K51ReVbk-i zf>r^m4E8+Im;4rXNIYqw>!IEmKY8A+&?r?>+f=nicut6!Oj8}l)lDlF&sQwXEO(e1 zm@BtG>2a&nsmWIN+n30eXzqw)-+p27t&LgH`