Skip to content

Commit 7278d2e

Browse files
committed
8372258: Improve TypeVariable support
Reviewed-by: liach
1 parent 84ffe87 commit 7278d2e

File tree

3 files changed

+84
-6
lines changed

3 files changed

+84
-6
lines changed

src/java.base/share/classes/java/lang/reflect/TypeVariable.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -72,7 +72,7 @@ public interface TypeVariable<D extends GenericDeclaration> extends Type, Annota
7272
Type[] getBounds();
7373

7474
/**
75-
* Returns the {@code GenericDeclaration} object representing the
75+
* Returns a {@code GenericDeclaration} object representing the
7676
* generic declaration declared for this type variable.
7777
*
7878
* @return the generic declaration declared for this type variable.

src/java.base/share/classes/sun/reflect/generics/reflectiveObjects/TypeVariableImpl.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -35,6 +35,8 @@
3535
import java.util.LinkedHashMap;
3636
import java.util.Map;
3737
import java.util.Objects;
38+
39+
import jdk.internal.reflect.ReflectionFactory;
3840
import sun.reflect.annotation.AnnotationSupport;
3941
import sun.reflect.annotation.TypeAnnotationParser;
4042
import sun.reflect.annotation.AnnotationType;
@@ -125,18 +127,24 @@ public Type[] getBounds() {
125127
}
126128

127129
/**
128-
* Returns the {@code GenericDeclaration} object representing the
130+
* Returns a {@code GenericDeclaration} object representing the
129131
* generic declaration that declared this type variable.
130132
*
131-
* @return the generic declaration that declared this type variable.
133+
* @return a generic declaration that declared this type variable.
132134
*
133135
* @since 1.5
134136
*/
137+
@SuppressWarnings("unchecked")
135138
public D getGenericDeclaration() {
136139
assert genericDeclaration instanceof Class<?> ||
137140
genericDeclaration instanceof Method ||
138141
genericDeclaration instanceof Constructor : "Unexpected kind of GenericDeclaration";
139-
return genericDeclaration;
142+
// If the `genericDeclaration` instance is mutable, we need to make a copy.
143+
return switch (genericDeclaration) {
144+
case Method method -> (D) ReflectionFactory.getReflectionFactory().copyMethod(method);
145+
case Constructor<?> ctor -> (D) ReflectionFactory.getReflectionFactory().copyConstructor(ctor);
146+
default -> genericDeclaration;
147+
};
140148
}
141149

142150

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2025, 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 8372258
27+
* @summary Test if a copy of the internal state is provided
28+
* @run junit ProtectInnerStateOfTypeVariableImplTest
29+
*/
30+
31+
import org.junit.jupiter.api.Test;
32+
33+
import java.lang.reflect.Constructor;
34+
import java.lang.reflect.Method;
35+
import java.lang.reflect.TypeVariable;
36+
37+
import static org.junit.jupiter.api.Assertions.*;
38+
39+
final class ProtectInnerStateOfTypeVariableImplTest {
40+
41+
static final class Foo {
42+
public <X> Foo() {
43+
}
44+
45+
<X> X x() {
46+
return null;
47+
}
48+
}
49+
50+
@Test
51+
void testMethod() throws NoSuchMethodException {
52+
Method method = Foo.class.getDeclaredMethod("x");
53+
TypeVariable<Method> tv = method.getTypeParameters()[0];
54+
55+
Method gd = tv.getGenericDeclaration();
56+
Method gd2 = tv.getGenericDeclaration();
57+
assertNotSame(gd, gd2);
58+
}
59+
60+
@Test
61+
void testConstructor() throws NoSuchMethodException {
62+
Constructor<?> ctor = Foo.class.getConstructor();
63+
TypeVariable<? extends Constructor<?>> tv = ctor.getTypeParameters()[0];
64+
65+
Constructor<?> gd = tv.getGenericDeclaration();
66+
Constructor<?> gd2 = tv.getGenericDeclaration();
67+
assertNotSame(gd, gd2);
68+
}
69+
70+
}

0 commit comments

Comments
 (0)