src/share/classes/sun/reflect/generics/repository/ClassRepository.java

Print this page
rev 7735 : 8064846: Lazy-init thread safety problems in core reflection


  23  * questions.
  24  */
  25 
  26 package sun.reflect.generics.repository;
  27 
  28 import sun.reflect.generics.factory.GenericsFactory;
  29 import sun.reflect.generics.tree.ClassSignature;
  30 import sun.reflect.generics.tree.TypeTree;
  31 import sun.reflect.generics.visitor.Reifier;
  32 import sun.reflect.generics.parser.SignatureParser;
  33 import java.lang.reflect.Type;
  34 
  35 
  36 /**
  37  * This class represents the generic type information for a class.
  38  * The code is not dependent on a particular reflective implementation.
  39  * It is designed to be used unchanged by at least core reflection and JDI.
  40  */
  41 public class ClassRepository extends GenericDeclRepository<ClassSignature> {
  42 
  43     private volatile Type superclass; // caches the generic superclass info
  44     private volatile Type[] superInterfaces; // caches the generic superinterface info



  45 
  46  // private, to enforce use of static factory
  47     private ClassRepository(String rawSig, GenericsFactory f) {
  48         super(rawSig, f);
  49     }
  50 
  51     protected ClassSignature parse(String s) {
  52         return SignatureParser.make().parseClassSig(s);
  53     }
  54 
  55     /**
  56      * Static factory method.
  57      * @param rawSig - the generic signature of the reflective object
  58      * that this repository is servicing
  59      * @param f - a factory that will provide instances of reflective
  60      * objects when this repository converts its AST
  61      * @return a <tt>ClassRepository</tt> that manages the generic type
  62      * information represented in the signature <tt>rawSig</tt>
  63      */
  64     public static ClassRepository make(String rawSig, GenericsFactory f) {
  65         return new ClassRepository(rawSig, f);
  66     }
  67 
  68     // public API
  69  /*
  70  * When queried for a particular piece of type information, the
  71  * general pattern is to consult the corresponding cached value.
  72  * If the corresponding field is non-null, it is returned.
  73  * If not, it is created lazily. This is done by selecting the appropriate
  74  * part of the tree and transforming it into a reflective object
  75  * using a visitor.
  76  * a visitor, which is created by feeding it the factory
  77  * with which the repository was created.
  78  */
  79 
  80     public Type getSuperclass(){
  81         Type superclass = this.superclass;
  82         if (superclass == null) { // lazily initialize superclass
  83             Reifier r = getReifier(); // obtain visitor
  84             // Extract superclass subtree from AST and reify
  85             getTree().getSuperclass().accept(r);
  86             // extract result from visitor and cache it
  87             superclass = r.getResult();
  88             this.superclass = superclass;
  89             }
  90         return superclass; // return cached result
  91     }
  92 
  93     public Type[] getSuperInterfaces(){
  94         Type[] superInterfaces = this.superInterfaces;
  95         if (superInterfaces == null) { // lazily initialize super interfaces
  96             // first, extract super interface subtree(s) from AST
  97             TypeTree[] ts  = getTree().getSuperInterfaces();
  98             // create array to store reified subtree(s)
  99             Type[] sis = new Type[ts.length];
 100             // reify all subtrees
 101             for (int i = 0; i < ts.length; i++) {
 102                 Reifier r = getReifier(); // obtain visitor
 103                 ts[i].accept(r);// reify subtree
 104                 // extract result from visitor and store it
 105                 sis[i] = r.getResult();
 106             }
 107             superInterfaces = sis; // cache overall result
 108             this.superInterfaces = superInterfaces;
 109         }
 110         return superInterfaces.clone(); // return cached result
 111     }
 112 }


  23  * questions.
  24  */
  25 
  26 package sun.reflect.generics.repository;
  27 
  28 import sun.reflect.generics.factory.GenericsFactory;
  29 import sun.reflect.generics.tree.ClassSignature;
  30 import sun.reflect.generics.tree.TypeTree;
  31 import sun.reflect.generics.visitor.Reifier;
  32 import sun.reflect.generics.parser.SignatureParser;
  33 import java.lang.reflect.Type;
  34 
  35 
  36 /**
  37  * This class represents the generic type information for a class.
  38  * The code is not dependent on a particular reflective implementation.
  39  * It is designed to be used unchanged by at least core reflection and JDI.
  40  */
  41 public class ClassRepository extends GenericDeclRepository<ClassSignature> {
  42 
  43     /** The generic superclass info.  Lazily initialized. */
  44     private volatile Type superclass;
  45 
  46     /** The generic superinterface info.  Lazily initialized. */
  47     private volatile Type[] superInterfaces;
  48 
  49  // private, to enforce use of static factory
  50     private ClassRepository(String rawSig, GenericsFactory f) {
  51         super(rawSig, f);
  52     }
  53 
  54     protected ClassSignature parse(String s) {
  55         return SignatureParser.make().parseClassSig(s);
  56     }
  57 
  58     /**
  59      * Static factory method.
  60      * @param rawSig - the generic signature of the reflective object
  61      * that this repository is servicing
  62      * @param f - a factory that will provide instances of reflective
  63      * objects when this repository converts its AST
  64      * @return a <tt>ClassRepository</tt> that manages the generic type
  65      * information represented in the signature <tt>rawSig</tt>
  66      */
  67     public static ClassRepository make(String rawSig, GenericsFactory f) {
  68         return new ClassRepository(rawSig, f);
  69     }
  70 
  71     // public API
  72  /*
  73  * When queried for a particular piece of type information, the
  74  * general pattern is to consult the corresponding cached value.
  75  * If the corresponding field is non-null, it is returned.
  76  * If not, it is created lazily. This is done by selecting the appropriate
  77  * part of the tree and transforming it into a reflective object
  78  * using a visitor.
  79  * a visitor, which is created by feeding it the factory
  80  * with which the repository was created.
  81  */
  82 
  83     public Type getSuperclass() {
  84         Type superclass = this.superclass;
  85         if (superclass == null) { // lazily initialize superclass
  86             Reifier r = getReifier(); // obtain visitor
  87             // Extract superclass subtree from AST and reify
  88             getTree().getSuperclass().accept(r);
  89             // extract result from visitor and cache it
  90             superclass = r.getResult();
  91             this.superclass = superclass;
  92         }
  93         return superclass; // return cached result
  94     }
  95 
  96     public Type[] getSuperInterfaces() {
  97         Type[] superInterfaces = this.superInterfaces;
  98         if (superInterfaces == null) { // lazily initialize super interfaces
  99             // first, extract super interface subtree(s) from AST
 100             TypeTree[] ts  = getTree().getSuperInterfaces();
 101             // create array to store reified subtree(s)
 102             superInterfaces = new Type[ts.length];
 103             // reify all subtrees
 104             for (int i = 0; i < ts.length; i++) {
 105                 Reifier r = getReifier(); // obtain visitor
 106                 ts[i].accept(r);// reify subtree
 107                 // extract result from visitor and store it
 108                 superInterfaces[i] = r.getResult();
 109             }

 110             this.superInterfaces = superInterfaces;
 111         }
 112         return superInterfaces.clone(); // return cached result
 113     }
 114 }