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

Avoid lots of casting in <xxxKlass>::cast()

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Won't Fix
    • Icon: P4 P4
    • 20
    • 19
    • hotspot

      We have similar pattern in {instanceKlass, typeArrayKlass, objArrayKlass, etc}.hpp::

        static ObjArrayKlass* cast(Klass* k) {
          return const_cast<ObjArrayKlass*>(cast(const_cast<const Klass*>(k)));
        }
        static const ObjArrayKlass* cast(const Klass* k) {
          assert(k->is_objArray_klass(), "cast to ObjArrayKlass");
          return static_cast<const ObjArrayKlass*>(k);
        }

      Seeing the word "cast" three times on a single line is confusing.

      Proposed cleanup:

        template <typename O, typename K>
        static O* cast_impl(K* k) {
          assert(k->is_objArray_klass(), "cast to ObjArrayKlass");
          return static_cast<O*>(k);
        }

       static ObjArrayKlass* cast(Klass* k) {
          return cast_impl<ObjArrayKlass>(k);
        }

        static const ObjArrayKlass* cast(const Klass* k) {
          return cast_impl<const ObjArrayKlass>(k);
        }


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

              Created:
              Updated:
              Resolved: