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