langtools/src/java.compiler/share/classes/javax/lang/model/element/ElementVisitor.java
author alanb
Fri, 10 Feb 2017 09:06:10 +0000
changeset 43767 9cff98a149cb
parent 43260 f5b52e571607
permissions -rw-r--r--
8173393: Module system implementation refresh (2/2017) Reviewed-by: mcimadamore, mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
43147
823bfbf9e914 8172531: Correct misstatements in javax.lang.model visitor documentation
darcy
parents: 43133
diff changeset
     2
 * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package javax.lang.model.element;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import javax.lang.model.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
 * A visitor of program elements, in the style of the visitor design
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
 * pattern.  Classes implementing this interface are used to operate
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
 * on an element when the kind of element is unknown at compile time.
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
 * When a visitor is passed to an element's {@link Element#accept
43147
823bfbf9e914 8172531: Correct misstatements in javax.lang.model visitor documentation
darcy
parents: 43133
diff changeset
    35
 * accept} method, the <code>visit<i>Xyz</i></code> method most applicable
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 * to that element is invoked.
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 * <p> Classes implementing this interface may or may not throw a
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 * {@code NullPointerException} if the additional parameter {@code p}
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 * is {@code null}; see documentation of the implementing class for
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 * details.
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 * <p> <b>WARNING:</b> It is possible that methods will be added to
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 * this interface to accommodate new, currently unknown, language
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 * structures added to future versions of the Java&trade; programming
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 * language.  Therefore, visitor classes directly implementing this
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 * interface may be source incompatible with future versions of the
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 * platform.  To avoid this source incompatibility, visitor
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 * implementations are encouraged to instead extend the appropriate
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
 * abstract visitor class that implements this interface.  However, an
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
 * API should generally use this visitor interface as the type for
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
 * parameters, return type, etc. rather than one of the abstract
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
 * classes.
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
 *
15386
92bc08d96f0c 8006264: Add explanation of why default methods cannot be used in JDK 8 javax.lang.model
darcy
parents: 7681
diff changeset
    55
 * <p>Note that methods to accommodate new language constructs could
92bc08d96f0c 8006264: Add explanation of why default methods cannot be used in JDK 8 javax.lang.model
darcy
parents: 7681
diff changeset
    56
 * be added in a source <em>compatible</em> way if they were added as
92bc08d96f0c 8006264: Add explanation of why default methods cannot be used in JDK 8 javax.lang.model
darcy
parents: 7681
diff changeset
    57
 * <em>default methods</em>.  However, default methods are only
92bc08d96f0c 8006264: Add explanation of why default methods cannot be used in JDK 8 javax.lang.model
darcy
parents: 7681
diff changeset
    58
 * available on Java SE 8 and higher releases and the {@code
43147
823bfbf9e914 8172531: Correct misstatements in javax.lang.model visitor documentation
darcy
parents: 43133
diff changeset
    59
 * javax.lang.model.*} packages bundled in Java SE 8 were required to
15386
92bc08d96f0c 8006264: Add explanation of why default methods cannot be used in JDK 8 javax.lang.model
darcy
parents: 7681
diff changeset
    60
 * also be runnable on Java SE 7.  Therefore, default methods
43147
823bfbf9e914 8172531: Correct misstatements in javax.lang.model visitor documentation
darcy
parents: 43133
diff changeset
    61
 * were <em>not</em> used when extending {@code javax.lang.model.*}
43260
f5b52e571607 8172910: Use default methods as appropriate for language model visitors
darcy
parents: 43147
diff changeset
    62
 * to cover Java SE 8 language features.  However, default methods
f5b52e571607 8172910: Use default methods as appropriate for language model visitors
darcy
parents: 43147
diff changeset
    63
 * are used in subsequent revisions of the {@code javax.lang.model.*}
15386
92bc08d96f0c 8006264: Add explanation of why default methods cannot be used in JDK 8 javax.lang.model
darcy
parents: 7681
diff changeset
    64
 * packages that are only required to run on Java SE 8 and higher
92bc08d96f0c 8006264: Add explanation of why default methods cannot be used in JDK 8 javax.lang.model
darcy
parents: 7681
diff changeset
    65
 * platform versions.
92bc08d96f0c 8006264: Add explanation of why default methods cannot be used in JDK 8 javax.lang.model
darcy
parents: 7681
diff changeset
    66
 *
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
 * @param <R> the return type of this visitor's methods.  Use {@link
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
 *            Void} for visitors that do not need to return results.
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
 * @param <P> the type of the additional parameter to this visitor's
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
 *            methods.  Use {@code Void} for visitors that do not need an
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
 *            additional parameter.
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
 * @author Joseph D. Darcy
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
 * @author Scott Seligman
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
 * @author Peter von der Ah&eacute;
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
 * @since 1.6
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
public interface ElementVisitor<R, P> {
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
     * Visits an element.
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
     * @param e  the element to visit
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
     * @param p  a visitor-specified parameter
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
     * @return a visitor-specified result
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    R visit(Element e, P p);
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    /**
43260
f5b52e571607 8172910: Use default methods as appropriate for language model visitors
darcy
parents: 43147
diff changeset
    88
     * A convenience method equivalent to {@code visit(e, null)}.
f5b52e571607 8172910: Use default methods as appropriate for language model visitors
darcy
parents: 43147
diff changeset
    89
     *
f5b52e571607 8172910: Use default methods as appropriate for language model visitors
darcy
parents: 43147
diff changeset
    90
     * @implSpec The default implementation is {@code visit(e, null)}.
f5b52e571607 8172910: Use default methods as appropriate for language model visitors
darcy
parents: 43147
diff changeset
    91
     *
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
     * @param e  the element to visit
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
     * @return a visitor-specified result
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
     */
43260
f5b52e571607 8172910: Use default methods as appropriate for language model visitors
darcy
parents: 43147
diff changeset
    95
    default R visit(Element e) {
f5b52e571607 8172910: Use default methods as appropriate for language model visitors
darcy
parents: 43147
diff changeset
    96
        return visit(e, null);
f5b52e571607 8172910: Use default methods as appropriate for language model visitors
darcy
parents: 43147
diff changeset
    97
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
     * Visits a package element.
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
     * @param e  the element to visit
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
     * @param p  a visitor-specified parameter
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
     * @return a visitor-specified result
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    R visitPackage(PackageElement e, P p);
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
     * Visits a type element.
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
     * @param e  the element to visit
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
     * @param p  a visitor-specified parameter
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
     * @return a visitor-specified result
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
    R visitType(TypeElement e, P p);
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
     * Visits a variable element.
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
     * @param e  the element to visit
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
     * @param p  a visitor-specified parameter
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
     * @return a visitor-specified result
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    R visitVariable(VariableElement e, P p);
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
     * Visits an executable element.
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
     * @param e  the element to visit
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
     * @param p  a visitor-specified parameter
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
     * @return a visitor-specified result
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
    R visitExecutable(ExecutableElement e, P p);
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
     * Visits a type parameter element.
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
     * @param e  the element to visit
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
     * @param p  a visitor-specified parameter
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
     * @return a visitor-specified result
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
    R visitTypeParameter(TypeParameterElement e, P p);
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
     * Visits an unknown kind of element.
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
     * This can occur if the language evolves and new kinds
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
     * of elements are added to the {@code Element} hierarchy.
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
     * @param e  the element to visit
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
     * @param p  a visitor-specified parameter
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
     * @return a visitor-specified result
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
     * @throws UnknownElementException
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
     *  a visitor implementation may optionally throw this exception
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
    R visitUnknown(Element e, P p);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 25874
diff changeset
   151
3b41f1c69604 8142968: Module System implementation
alanb
parents: 25874
diff changeset
   152
    /**
3b41f1c69604 8142968: Module System implementation
alanb
parents: 25874
diff changeset
   153
     * Visits a module element.
43260
f5b52e571607 8172910: Use default methods as appropriate for language model visitors
darcy
parents: 43147
diff changeset
   154
     *
f5b52e571607 8172910: Use default methods as appropriate for language model visitors
darcy
parents: 43147
diff changeset
   155
     * @implSpec Visits a {@code ModuleElement} by calling {@code
f5b52e571607 8172910: Use default methods as appropriate for language model visitors
darcy
parents: 43147
diff changeset
   156
     * visitUnknown(e, p)}.
f5b52e571607 8172910: Use default methods as appropriate for language model visitors
darcy
parents: 43147
diff changeset
   157
     *
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 25874
diff changeset
   158
     * @param e  the element to visit
3b41f1c69604 8142968: Module System implementation
alanb
parents: 25874
diff changeset
   159
     * @param p  a visitor-specified parameter
3b41f1c69604 8142968: Module System implementation
alanb
parents: 25874
diff changeset
   160
     * @return a visitor-specified result
3b41f1c69604 8142968: Module System implementation
alanb
parents: 25874
diff changeset
   161
     * @since 9
43767
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43260
diff changeset
   162
     * @spec JPMS
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 25874
diff changeset
   163
     */
43260
f5b52e571607 8172910: Use default methods as appropriate for language model visitors
darcy
parents: 43147
diff changeset
   164
    default R visitModule(ModuleElement e, P p) {
f5b52e571607 8172910: Use default methods as appropriate for language model visitors
darcy
parents: 43147
diff changeset
   165
        return visitUnknown(e, p);
f5b52e571607 8172910: Use default methods as appropriate for language model visitors
darcy
parents: 43147
diff changeset
   166
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
}