-
Type:
CSR
-
Resolution: Unresolved
-
Priority:
P2
-
Component/s: core-libs
-
None
-
behavioral
-
minimal
-
Clients may rely on the same object being returned.
-
Java API
-
SE
Summary
Return a copy of an underlying object and change specification from "returns the" to "returns a" so that it becomes allowed to return any representation of an object.
Problem
The TypeVaiableImpl held and returned the underlying object and since Method and Constructor are mutable, mutating a returned object also mutated the internally held variable so that other callers would now see and share the same instance.
Solution
Make an internal copy of the underlying object and return that instead.
Specification
diff --git a/src/java.base/share/classes/sun/reflect/generics/reflectiveObjects/TypeVariableImpl.java b/src/java.base/share/classes/sun/reflect/generics/reflectiveObjects/TypeVariableImpl.java
index 75750d38f2fab..560e9a4f7c970 100644
--- a/src/java.base/share/classes/sun/reflect/generics/reflectiveObjects/TypeVariableImpl.java
+++ b/src/java.base/share/classes/sun/reflect/generics/reflectiveObjects/TypeVariableImpl.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2025, 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
@@ -35,6 +35,8 @@
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
+
+import jdk.internal.reflect.ReflectionFactory;
import sun.reflect.annotation.AnnotationSupport;
import sun.reflect.annotation.TypeAnnotationParser;
import sun.reflect.annotation.AnnotationType;
@@ -125,18 +127,24 @@ public Type[] getBounds() {
}
/**
- * Returns the {@code GenericDeclaration} object representing the
+ * Returns a {@code GenericDeclaration} object representing the
* generic declaration that declared this type variable.
*
- * @return the generic declaration that declared this type variable.
+ * @return a generic declaration that declared this type variable.
*
* @since 1.5
*/
+ @SuppressWarnings("unchecked")
public D getGenericDeclaration() {
assert genericDeclaration instanceof Class<?> ||
genericDeclaration instanceof Method ||
genericDeclaration instanceof Constructor : "Unexpected kind of GenericDeclaration";
- return genericDeclaration;
+ // If the `genericDeclaration` instance is mutable, we need to make a copy.
+ return switch (genericDeclaration) {
+ case Method method -> (D) ReflectionFactory.getReflectionFactory().copyMethod(method);
+ case Constructor<?> ctor -> (D) ReflectionFactory.getReflectionFactory().copyConstructor(ctor);
+ default -> genericDeclaration;
+ };
}
- csr of
-
JDK-8372258 Improve TypeVariable support
-
- In Progress
-