PROBLEM:
The return type of Klass::super() is a Klass*. See
https://github.com/openjdk/jdk/blob/b06459d3a83c13c0fbc7a0a7698435f17265982e/src/hotspot/share/oops/klass.hpp#L218-L221
// Klass::super() cannot be InstanceKlass* -- Java arrays are covariant, and _super is used
// to implement that. NB: the _super of "[Ljava/lang/Integer;" is "[Ljava/lang/Number;"
// If this is not what your code expects, you're probably looking for Klass::java_super().
However, if you already have an InstanceKlass* ik, it's guaranteed that the object returned by ik->super() must be an InstanceKlass* (or nullptr).
In the past, we had unnecessary casts:
InstanceKlass* s = InstanceKlass::cast(ik->super());
JDK-8366024 changed that to
InstanceKlass* s = ik->java_super().
However, this style is still confusing, and forces people to know the difference between Klass::super() and Klass::java_super(). Also, java_super() is a virtual method so it's a bit slower.
PROPOSAL
Add the following function to InstanceKlass to shadow the method of the same name in Klass.
class InstanceKlass : public Klass {
public:
InstanceKlass* super() const {
return (Klass::super() == nullptr) ? nullptr : InstanceKlass::cast(Klass::super());
}
};
If you already have a InstanceKlass* ik, the code can simply call:
InstanceKlass* s = k->java_super().
The return type of Klass::super() is a Klass*. See
https://github.com/openjdk/jdk/blob/b06459d3a83c13c0fbc7a0a7698435f17265982e/src/hotspot/share/oops/klass.hpp#L218-L221
// Klass::super() cannot be InstanceKlass* -- Java arrays are covariant, and _super is used
// to implement that. NB: the _super of "[Ljava/lang/Integer;" is "[Ljava/lang/Number;"
// If this is not what your code expects, you're probably looking for Klass::java_super().
However, if you already have an InstanceKlass* ik, it's guaranteed that the object returned by ik->super() must be an InstanceKlass* (or nullptr).
In the past, we had unnecessary casts:
InstanceKlass* s = InstanceKlass::cast(ik->super());
InstanceKlass* s = ik->java_super().
However, this style is still confusing, and forces people to know the difference between Klass::super() and Klass::java_super(). Also, java_super() is a virtual method so it's a bit slower.
PROPOSAL
Add the following function to InstanceKlass to shadow the method of the same name in Klass.
class InstanceKlass : public Klass {
public:
InstanceKlass* super() const {
return (Klass::super() == nullptr) ? nullptr : InstanceKlass::cast(Klass::super());
}
};
If you already have a InstanceKlass* ik, the code can simply call:
InstanceKlass* s = k->java_super().
- relates to
-
JDK-8366024 Remove unnecessary InstanceKlass::cast()
-
- Resolved
-
- links to
-
Commit(master) openjdk/jdk/f4d73d2a
-
Review(master) openjdk/jdk/27037