Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8366584

Add an InstanceKlass::super() method that returns InstanceKlass*

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P4 P4
    • 26
    • None
    • hotspot
    • b14

      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().

            iklam Ioi Lam
            iklam Ioi Lam
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: