Monday, May 16, 2022

Inheritance chains

Every java.lang.Class is associated with a superClass in the inheritance hierarchy. With a little bit of recursion, this leads to a totally ordered set of classes starting at the current class and ending at java.lang.Object.
(defn inheritance-chain
  [class] 

  (if (= class Object) 
    (list class) 
    (cons class (inheritance-chain (.getSuperclass class)))))
In addition, every Object is associated with a Class by the getClass method. So combined with the inheritance-chain function this can be used to produce a totally ordered set of all parent classes for any object. I have in mind to use this on an atom.
(inheritance-chain (.getClass (atom 1)))
;=> (clojure.lang.Atom 
;    clojure.lang.ARef 
;    clojure.lang.AReference 
;    java.lang.Object)
I seem to recall that clojure.lang.Atom is related to java.util.concurrent.atomic.AtomicReference, as this shows they are not related by inheritance. As it turns out Atom simply uses an AtomicReference internally to define its state. Another thing is that both classes use compareAndSet.
(import java.util.concurrent.atomic.AtomicReference)

(def x (atom 1))
(.compareAndSet x 1 10)
(.deref x)
;=> 10

(def y (AtomicReference. 1))
(.compareAndSet y 1 10)
(.get y)
;=> 10
Beyond that basic point, there isn't much relationship between the two atomic classes because they don't inherit from one another. For example, the clojure.lang.Atom uses deref to get its value whilst java.util.concurrent.atomic.AtomicReference uses get in order to get its value.

Whenever you want to learn more about some class you can look up its Javadoc, which will have its inheritance chain, implemented classes, fields, methods, etc. Sometimes, however, it is nice to be able to get information directly from the JVM using reflection on the java.lang.Class instance. For example, it is useful to use reflection when you are creating your own JVM language and you want to make sure your compiler produces the right bytecode. The inheritance-chain method is an example of the utility of reflection.

No comments:

Post a Comment