1 /*
   2  * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  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 }