1 /* 2 * Copyright (c) 2003, 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.scope; 27 28 import java.lang.reflect.GenericDeclaration; 29 import java.lang.reflect.TypeVariable; 30 31 32 33 /** 34 * Abstract superclass for lazy scope objects, used when building 35 * factories for generic information repositories. 36 * The type parameter <tt>D</tt> represents the type of reflective 37 * object whose scope this class is representing. 38 * <p> To subclass this, all one needs to do is implement 39 * <tt>computeEnclosingScope</tt> and the subclass' constructor. 40 */ 41 public abstract class AbstractScope<D extends GenericDeclaration> 42 implements Scope { 43 44 private D recvr; // the declaration whose scope this instance represents 45 private Scope enclosingScope; // the enclosing scope of this scope 46 47 /** 48 * Constructor. Takes a reflective object whose scope the newly 49 * constructed instance will represent. 50 * @param D - A generic declaration whose scope the newly 51 * constructed instance will represent 52 */ 53 protected AbstractScope(D decl){ recvr = decl;} 54 55 /** 56 * Accessor for the receiver - the object whose scope this <tt>Scope</tt> 57 * object represents. 58 * @return The object whose scope this <tt>Scope</tt> object represents 59 */ 60 protected D getRecvr() {return recvr;} 61 62 /** This method must be implemented by any concrete subclass. 63 * It must return the enclosing scope of this scope. If this scope 64 * is a top-level scope, an instance of DummyScope must be returned. 65 * @return The enclosing scope of this scope 66 */ 67 protected abstract Scope computeEnclosingScope(); 68 69 /** 70 * Accessor for the enclosing scope, which is computed lazily and cached. 71 * @return the enclosing scope 72 */ 73 protected Scope getEnclosingScope(){ 74 if (enclosingScope == null) {enclosingScope = computeEnclosingScope();} 75 return enclosingScope; 76 } 77 78 /** 79 * Lookup a type variable in the scope, using its name. Returns null if 80 * no type variable with this name is declared in this scope or any of its 81 * surrounding scopes. 82 * @param name - the name of the type variable being looked up 83 * @return the requested type variable, if found 84 */ 85 public TypeVariable<?> lookup(String name) { 86 TypeVariable[] tas = getRecvr().getTypeParameters(); 87 for (TypeVariable/*<?>*/ tv : tas) { 88 if (tv.getName().equals(name)) {return tv;} 89 } 90 return getEnclosingScope().lookup(name); 91 } 92 }