< prev index next >

src/hotspot/share/oops/array.hpp

Print this page

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2018, 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
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -130,11 +130,11 @@
 
     assert(words <= INT_MAX, "Overflow: " SIZE_FORMAT, words);
 
     return (int)words;
   }
-  int size() {
+  int size() const {
     return size(_length);
   }
 
   static int length_offset_in_bytes() { return (int) (offset_of(Array<T>, _length)); }
   // Note, this offset don't have to be wordSize aligned.

@@ -151,9 +151,28 @@
        st->print_cr("%d: " INTPTR_FORMAT, i, (intptr_t)at(i));
      }
   }
   void print() { print(tty); }
 #endif // PRODUCT
+
+  // Returns a "const" Array of S, where S is a supertype of T.
+  //
+  //     Array<InstanceKlass*>* ta = ... ;
+  //     Array<Klass*>* sa = ta->as_array_of<Klass*>();
+  //     Klass* k = sa->at(0);  /// OK to read
+  //     Klass* ak = k->array_klass(2);
+  //     sa->at_put(0, ak);     ///  NOT OK to write into it, or else
+  //                            /// ta would contain something unexpected.
+  //
+  // If you get a C compilation error, DO NOT simply cast the 'const' away!
+  template <typename S> const Array<S>* as_const_array_of() {
+#ifndef PRODUCT
+    T dummy = NULL;
+    S super_dummy = dummy; // must be down-castable
+    assert(sizeof(T) == sizeof(S), "must be");
+#endif 
+    return static_cast<Array<S>*>((void*)this);
+  }
 };
 
 
 #endif // SHARE_VM_OOPS_ARRAY_HPP
< prev index next >