1 /*
   2  * Copyright (c) 2003, 2020, 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 java.lang.reflect.Type;
  29 import sun.reflect.generics.factory.GenericsFactory;
  30 import sun.reflect.generics.tree.ClassSignature;
  31 import sun.reflect.generics.tree.TypeTree;
  32 import sun.reflect.generics.visitor.Reifier;
  33 import sun.reflect.generics.parser.SignatureParser;
  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     public static final ClassRepository NONE = ClassRepository.make("Ljava/lang/Object;", null);
  44 
  45     /** The generic superclass info.  Lazily initialized. */
  46     private volatile Type superclass;
  47 
  48     /** The generic superinterface info.  Lazily initialized. */
  49     private volatile Type[] superInterfaces;
  50 
  51     // private, to enforce use of static factory
  52     private ClassRepository(String rawSig, GenericsFactory f) {
  53         super(rawSig, f);
  54     }
  55 
  56     protected ClassSignature parse(String s) {
  57         return SignatureParser.make().parseClassSig(s);
  58     }
  59 
  60     /**
  61      * Static factory method.
  62      * @param rawSig - the generic signature of the reflective object
  63      * that this repository is servicing
  64      * @param f - a factory that will provide instances of reflective
  65      * objects when this repository converts its AST
  66      * @return a {@code ClassRepository} that manages the generic type
  67      * information represented in the signature {@code rawSig}
  68      */
  69     public static ClassRepository make(String rawSig, GenericsFactory f) {
  70         return new ClassRepository(rawSig, f);
  71     }
  72 
  73     /*
  74      * When queried for a particular piece of type information, the
  75      * general pattern is to consult the corresponding cached value.
  76      * If the corresponding field is non-null, it is returned.
  77      * If not, it is created lazily. This is done by selecting the appropriate
  78      * part of the tree and transforming it into a reflective object
  79      * using 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 value = superclass;
  85         if (value == null) {
  86             value = computeSuperclass();
  87             superclass = value;
  88         }
  89         return value;
  90     }
  91 
  92     public Type[] getSuperInterfaces() {
  93         Type[] value = superInterfaces;
  94         if (value == null) {
  95             value = computeSuperInterfaces();
  96             superInterfaces = value;
  97         }
  98         return value.clone();
  99     }
 100 
 101     private Type computeSuperclass() {
 102         Reifier r = getReifier(); // obtain visitor
 103         // Extract superclass subtree from AST and reify
 104         getTree().getSuperclass().accept(r);
 105         return r.getResult();
 106     }
 107 
 108     private Type[] computeSuperInterfaces() {
 109         // first, extract super interface subtree(s) from AST
 110         TypeTree[] ts = getTree().getSuperInterfaces();
 111         // create array to store reified subtree(s)
 112         int length = ts.length;
 113         Type[] superInterfaces = new Type[length];
 114         // reify all subtrees
 115         for (int i = 0; i < length; i++) {
 116             Reifier r = getReifier(); // obtain visitor
 117             ts[i].accept(r);// reify subtree
 118             // extract result from visitor and store it
 119             superInterfaces[i] = r.getResult();
 120         }
 121         return superInterfaces;
 122     }
 123 }