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 java.lang.reflect.Type; 29 import sun.reflect.generics.factory.GenericsFactory; 30 import sun.reflect.generics.parser.SignatureParser; 31 import sun.reflect.generics.tree.FieldTypeSignature; 32 import sun.reflect.generics.tree.MethodTypeSignature; 33 import sun.reflect.generics.tree.TypeSignature; 34 import sun.reflect.generics.visitor.Reifier; 35 36 37 38 /** 39 * This class represents the generic type information for a constructor. 40 * The code is not dependent on a particular reflective implementation. 41 * It is designed to be used unchanged by at least core reflection and JDI. 42 */ 43 public class ConstructorRepository 44 extends GenericDeclRepository<MethodTypeSignature> { 45 46 private Type[] paramTypes; // caches the generic parameter types info 47 private Type[] exceptionTypes; // caches the generic exception types info 48 49 // protected, to enforce use of static factory yet allow subclassing 50 protected ConstructorRepository(String rawSig, GenericsFactory f) { 51 super(rawSig, f); 52 } 53 54 protected MethodTypeSignature parse(String s) { 55 return SignatureParser.make().parseMethodSig(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>ConstructorRepository</tt> that manages the generic type 65 * information represented in the signature <tt>rawSig</tt> 66 */ 67 public static ConstructorRepository make(String rawSig, 68 GenericsFactory f) { 69 return new ConstructorRepository(rawSig, f); 70 } 71 72 // public API 73 74 /* 75 * When queried for a particular piece of type information, the 76 * general pattern is to consult the corresponding cached value. 77 * If the corresponding field is non-null, it is returned. 78 * If not, it is created lazily. This is done by selecting the appropriate 79 * part of the tree and transforming it into a reflective object 80 * using a visitor. 81 * a visitor, which is created by feeding it the factory 82 * with which the repository was created. 83 */ 84 85 public Type[] getParameterTypes(){ 86 if (paramTypes == null) { // lazily initialize parameter types 87 // first, extract parameter type subtree(s) from AST 88 TypeSignature[] pts = getTree().getParameterTypes(); 89 // create array to store reified subtree(s) 90 Type[] ps = new Type[pts.length]; 91 // reify all subtrees 92 for (int i = 0; i < pts.length; i++) { 93 Reifier r = getReifier(); // obtain visitor 94 pts[i].accept(r); // reify subtree 95 // extract result from visitor and store it 96 ps[i] = r.getResult(); 97 } 98 paramTypes = ps; // cache overall result 99 } 100 return paramTypes.clone(); // return cached result 101 } 102 103 public Type[] getExceptionTypes(){ 104 if (exceptionTypes == null) { // lazily initialize exception types 105 // first, extract exception type subtree(s) from AST 106 FieldTypeSignature[] ets = getTree().getExceptionTypes(); 107 // create array to store reified subtree(s) 108 Type[] es = new Type[ets.length]; 109 // reify all subtrees 110 for (int i = 0; i < ets.length; i++) { 111 Reifier r = getReifier(); // obtain visitor 112 ets[i].accept(r); // reify subtree 113 // extract result from visitor and store it 114 es[i] = r.getResult(); 115 } 116 exceptionTypes = es; // cache overall result 117 } 118 return exceptionTypes.clone(); // return cached result 119 } 120 }