Skip to content

Commit 9dd9625

Browse files
raphwjddarcy
authored andcommittedApr 12, 2021
8263763: Synthetic constructor parameters of enum are not considered for annotation indices
Reviewed-by: darcy, jfranck
1 parent 1ee80e0 commit 9dd9625

File tree

4 files changed

+73
-9
lines changed

4 files changed

+73
-9
lines changed
 

‎src/java.base/share/classes/java/lang/reflect/Constructor.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -605,9 +605,14 @@ public Annotation[][] getParameterAnnotations() {
605605
}
606606

607607
@Override
608-
boolean handleParameterNumberMismatch(int resultLength, int numParameters) {
608+
boolean handleParameterNumberMismatch(int resultLength, Class<?>[] parameterTypes) {
609+
int numParameters = parameterTypes.length;
609610
Class<?> declaringClass = getDeclaringClass();
610-
if (declaringClass.isEnum() ||
611+
if (declaringClass.isEnum()) {
612+
return resultLength + 2 == numParameters &&
613+
parameterTypes[0] == String.class &&
614+
parameterTypes[1] == int.class;
615+
} else if (
611616
declaringClass.isAnonymousClass() ||
612617
declaringClass.isLocalClass() )
613618
return false; // Can't do reliable parameter counting

‎src/java.base/share/classes/java/lang/reflect/Executable.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -566,17 +566,19 @@ Annotation[][] sharedGetParameterAnnotations(Class<?>[] parameterTypes,
566566
Annotation[][] result = parseParameterAnnotations(parameterAnnotations);
567567

568568
if (result.length != numParameters &&
569-
handleParameterNumberMismatch(result.length, numParameters)) {
570-
Annotation[][] tmp = new Annotation[result.length+1][];
571-
// Shift annotations down one to account for an implicit leading parameter
572-
System.arraycopy(result, 0, tmp, 1, result.length);
573-
tmp[0] = new Annotation[0];
569+
handleParameterNumberMismatch(result.length, parameterTypes)) {
570+
Annotation[][] tmp = new Annotation[numParameters][];
571+
// Shift annotations down to account for any implicit leading parameters
572+
System.arraycopy(result, 0, tmp, numParameters - result.length, result.length);
573+
for (int i = 0; i < numParameters - result.length; i++) {
574+
tmp[i] = new Annotation[0];
575+
}
574576
result = tmp;
575577
}
576578
return result;
577579
}
578580

579-
abstract boolean handleParameterNumberMismatch(int resultLength, int numParameters);
581+
abstract boolean handleParameterNumberMismatch(int resultLength, Class<?>[] parameterTypes);
580582

581583
/**
582584
* {@inheritDoc}

‎src/java.base/share/classes/java/lang/reflect/Method.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ public AnnotatedType getAnnotatedReturnType() {
760760
}
761761

762762
@Override
763-
boolean handleParameterNumberMismatch(int resultLength, int numParameters) {
763+
boolean handleParameterNumberMismatch(int resultLength, Class<?>[] parameterTypes) {
764764
throw new AnnotationFormatError("Parameter annotations don't match number of parameters");
765765
}
766766
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8263763
27+
* @summary Check that annotations on an enum constructor are indexed correctly.
28+
*/
29+
30+
import java.lang.annotation.Annotation;
31+
import java.lang.annotation.Retention;
32+
import java.lang.annotation.RetentionPolicy;
33+
import java.lang.reflect.Constructor;
34+
import java.util.Arrays;
35+
36+
public class EnumConstructorAnnotation {
37+
38+
public static void main(String[] args) {
39+
Constructor<?> c = SampleEnum.class.getDeclaredConstructors()[0];
40+
Annotation[] a1 = c.getParameters()[2].getAnnotations(), a2 = c.getParameterAnnotations()[2];
41+
for (Annotation[] a : Arrays.asList(a1, a2)) {
42+
if (a.length != 1) {
43+
throw new RuntimeException("Unexpected length " + a.length);
44+
} else if (a[0].annotationType() != SampleAnnotation.class) {
45+
throw new RuntimeException("Unexpected type " + a[0]);
46+
}
47+
}
48+
}
49+
50+
enum SampleEnum {
51+
INSTANCE("foo");
52+
SampleEnum(@SampleAnnotation String value) { }
53+
}
54+
55+
@Retention(RetentionPolicy.RUNTIME)
56+
@interface SampleAnnotation { }
57+
}

0 commit comments

Comments
 (0)