1 /*
   2  * Copyright (c) 2011, 2019, 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 javax.lang.model.util;
  27 
  28 import javax.lang.model.element.*;
  29 import javax.annotation.processing.SupportedSourceVersion;
  30 import static javax.lang.model.SourceVersion.*;
  31 import javax.lang.model.SourceVersion;
  32 
  33 /**
  34  * A visitor of program elements based on their {@linkplain
  35  * ElementKind kind} with default behavior appropriate for source
  36  * versions with records.
  37  *
  38  * For {@linkplain
  39  * Element elements} <code><i>Xyz</i></code> that may have more than one
  40  * kind, the <code>visit<i>Xyz</i></code> methods in this class delegate
  41  * to the <code>visit<i>Xyz</i>As<i>Kind</i></code> method corresponding to the
  42  * first argument's kind.  The <code>visit<i>Xyz</i>As<i>Kind</i></code> methods
  43  * call {@link #defaultAction defaultAction}, passing their arguments
  44  * to {@code defaultAction}'s corresponding parameters.
  45  *
  46  * <p> Methods in this class may be overridden subject to their
  47  * general contract.  Note that annotating methods in concrete
  48  * subclasses with {@link java.lang.Override @Override} will help
  49  * ensure that methods are overridden as intended.
  50  *
  51  * <p> <b>WARNING:</b> The {@code ElementVisitor} interface
  52  * implemented by this class may have methods added to it or the
  53  * {@code ElementKind} {@code enum} used in this case may have
  54  * constants added to it in the future to accommodate new, currently
  55  * unknown, language structures added to future versions of the
  56  * Java&trade; programming language.  Therefore, methods whose names
  57  * begin with {@code "visit"} may be added to this class in the
  58  * future; to avoid incompatibilities, classes which extend this class
  59  * should not declare any instance methods with names beginning with
  60  * {@code "visit"}.
  61  *
  62  * <p>When such a new visit method is added, the default
  63  * implementation in this class will be to call the {@link
  64  * #visitUnknown visitUnknown} method.  A new abstract element kind
  65  * visitor class will also be introduced to correspond to the new
  66  * language level; this visitor will have different default behavior
  67  * for the visit method in question.  When the new visitor is
  68  * introduced, all or portions of this visitor may be deprecated.
  69  *
  70  * @param <R> the return type of this visitor's methods.  Use {@link
  71  *            Void} for visitors that do not need to return results.
  72  * @param <P> the type of the additional parameter to this visitor's
  73  *            methods.  Use {@code Void} for visitors that do not need an
  74  *            additional parameter.
  75  *
  76  * @see ElementKindVisitor6
  77  * @see ElementKindVisitor7
  78  * @see ElementKindVisitor8
  79  * @since amber
  80  */
  81 @SupportedSourceVersion(RELEASE_13)
  82 public class ElementKindVisitorRecord<R, P> extends ElementKindVisitor9<R, P> {
  83     /**
  84      * Constructor for concrete subclasses; uses {@code null} for the
  85      * default value.
  86      */
  87     protected ElementKindVisitorRecord() {
  88         super(null);
  89     }
  90 
  91     /**
  92      * Constructor for concrete subclasses; uses the argument for the
  93      * default value.
  94      *
  95      * @param defaultValue the value to assign to {@link #DEFAULT_VALUE}
  96      */
  97     protected ElementKindVisitorRecord(R defaultValue) {
  98         super(defaultValue);
  99     }
 100 
 101     /**
 102      * {@inheritDoc}
 103      *
 104      * @implSpec This implementation calls {@code defaultAction}.
 105      *.
 106      * @param e the element to visit
 107      * @param p a visitor-specified parameter
 108      * @return  the result of {@code defaultAction}
 109      *
 110      * @since amber
 111      */
 112     @Override
 113     public R visitTypeAsRecord(TypeElement e, P p) {
 114         return defaultAction(e, p);
 115     }
 116 
 117     /**
 118      * {@inheritDoc}
 119      *
 120      * @implSpec This implementation calls {@code defaultAction}.
 121      *.
 122      * @param e the element to visit
 123      * @param p a visitor-specified parameter
 124      * @return  the result of {@code defaultAction}
 125      *
 126      * @since amber
 127      */
 128     @Override
 129     public R visitVariableAsStateComponent(VariableElement e, P p) {
 130         return defaultAction(e, p);
 131     }
 132 }