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 /** The generic parameter types. Lazily initialized. */ 47 private volatile Type[] parameterTypes; 48 49 /** The generic exception types. Lazily initialized. */ 50 private volatile Type[] exceptionTypes; 51 52 // protected, to enforce use of static factory yet allow subclassing 53 protected ConstructorRepository(String rawSig, GenericsFactory f) { 54 super(rawSig, f); 55 } 56 57 protected MethodTypeSignature parse(String s) { 58 return SignatureParser.make().parseMethodSig(s); 59 } 60 61 /** 62 * Static factory method. 63 * @param rawSig - the generic signature of the reflective object 64 * that this repository is servicing 65 * @param f - a factory that will provide instances of reflective 66 * objects when this repository converts its AST 67 * @return a <tt>ConstructorRepository</tt> that manages the generic type 68 * information represented in the signature <tt>rawSig</tt> 69 */ 70 public static ConstructorRepository make(String rawSig, GenericsFactory f) { 71 return new ConstructorRepository(rawSig, f); 72 } 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, which is created by feeding it the factory 81 * with which the repository was created. 82 */ 83 84 public Type[] getParameterTypes() { 85 Type[] value = parameterTypes; 86 if (value == null) { 87 value = computeParameterTypes(); 88 parameterTypes = value; 89 } 90 return value.clone(); 91 } 92 93 public Type[] getExceptionTypes() { 94 Type[] value = exceptionTypes; 95 if (value == null) { 96 value = computeExceptionTypes(); 97 exceptionTypes = value; 98 } 99 return value.clone(); 100 } 101 102 private Type[] computeParameterTypes() { 103 // first, extract parameter type subtree(s) from AST 104 TypeSignature[] pts = getTree().getParameterTypes(); 105 // create array to store reified subtree(s) 106 int length = pts.length; 107 Type[] parameterTypes = new Type[length]; 108 // reify all subtrees 109 for (int i = 0; i < length; i++) { 110 Reifier r = getReifier(); // obtain visitor 111 pts[i].accept(r); // reify subtree 112 // extract result from visitor and store it 113 parameterTypes[i] = r.getResult(); 114 } 115 return parameterTypes; 116 } 117 118 private Type[] computeExceptionTypes() { 119 // first, extract exception type subtree(s) from AST 120 FieldTypeSignature[] ets = getTree().getExceptionTypes(); 121 // create array to store reified subtree(s) 122 int length = ets.length; 123 Type[] exceptionTypes = new Type[length]; 124 // reify all subtrees 125 for (int i = 0; i < length; i++) { 126 Reifier r = getReifier(); // obtain visitor 127 ets[i].accept(r); // reify subtree 128 // extract result from visitor and store it 129 exceptionTypes[i] = r.getResult(); 130 } 131 return exceptionTypes; 132 } 133 }