Path: blob/master/src/java.base/share/classes/sun/reflect/generics/scope/AbstractScope.java
41161 views
/*1* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.reflect.generics.scope;2627import java.lang.reflect.GenericDeclaration;28import java.lang.reflect.TypeVariable;293031/**32* Abstract superclass for lazy scope objects, used when building33* factories for generic information repositories.34* The type parameter {@code D} represents the type of reflective35* object whose scope this class is representing.36* <p> To subclass this, all one needs to do is implement37* {@code computeEnclosingScope} and the subclass' constructor.38*/39public abstract class AbstractScope<D extends GenericDeclaration>40implements Scope {4142private final D recvr; // the declaration whose scope this instance represents4344/** The enclosing scope of this scope. Lazily initialized. */45private volatile Scope enclosingScope;4647/**48* Constructor. Takes a reflective object whose scope the newly49* constructed instance will represent.50* @param decl - A generic declaration whose scope the newly51* constructed instance will represent52*/53protected AbstractScope(D decl){ recvr = decl;}5455/**56* Accessor for the receiver - the object whose scope this {@code Scope}57* object represents.58* @return The object whose scope this {@code Scope} object represents59*/60protected D getRecvr() {return recvr;}6162/** This method must be implemented by any concrete subclass.63* It must return the enclosing scope of this scope. If this scope64* is a top-level scope, an instance of DummyScope must be returned.65* @return The enclosing scope of this scope66*/67protected abstract Scope computeEnclosingScope();6869/**70* Accessor for the enclosing scope, which is computed lazily and cached.71* @return the enclosing scope72*/73protected Scope getEnclosingScope() {74Scope value = enclosingScope;75if (value == null) {76value = computeEnclosingScope();77enclosingScope = value;78}79return value;80}8182/**83* Lookup a type variable in the scope, using its name. Returns null if84* no type variable with this name is declared in this scope or any of its85* surrounding scopes.86* @param name - the name of the type variable being looked up87* @return the requested type variable, if found88*/89public TypeVariable<?> lookup(String name) {90TypeVariable<?>[] tas = getRecvr().getTypeParameters();91for (TypeVariable<?> tv : tas) {92if (tv.getName().equals(name)) {return tv;}93}94return getEnclosingScope().lookup(name);95}96}979899