1 /*
   2  * Copyright (c) 2017, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8172910
  27  * @summary Test behavior of default methods on visitors.
  28  * @modules java.compiler
  29  */
  30 
  31 import javax.lang.model.SourceVersion;
  32 import javax.lang.model.element.*;
  33 import javax.lang.model.type.*;
  34 import javax.lang.model.util.*;
  35 
  36 /**
  37  * Verify expected behavior of default methods on visitors.
  38  */
  39 public class TestVisitorDefaults {
  40     public static void main(String... args) {
  41         DirectElementVisitorChild dvc = new DirectElementVisitorChild();
  42         if (!"visitUnknown".equals(dvc.visitModule(null, null))) {
  43             throw new RuntimeException("Problem with DirectElementVisitorChild");
  44         }
  45         if (!"visit".equals(dvc.visit(null))) {
  46             throw new RuntimeException("Problem with DirectElementVisitorChild");
  47         }
  48 
  49         IndirectElementVisitorChild ivc = new IndirectElementVisitorChild();
  50         if (!"visitUnknown".equals(ivc.visitModule(null, null))) {
  51             throw new RuntimeException("Problem with IndirectElementVisitorChild");
  52         }
  53 
  54         DirectTypeVisitorChild dtvc = new DirectTypeVisitorChild();
  55         if (!"visit".equals(dtvc.visit(null))) {
  56             throw new RuntimeException("Problem with DirectTypeVisitorChild");
  57         }
  58 
  59         DirectAnnotationVisitorChild davc = new DirectAnnotationVisitorChild();
  60         if (!"visit".equals(davc.visit(null))) {
  61             throw new RuntimeException("Problem with DirectAnnotationVisitorChild");
  62         }
  63     }
  64 
  65     private static class DirectElementVisitorChild
  66         implements ElementVisitor<String, Object> {
  67 
  68         public DirectElementVisitorChild() {
  69             super();
  70         }
  71 
  72         @Override
  73         public String visitModule(ModuleElement e, Object o) {
  74             return ElementVisitor.super.visitModule(e, null);
  75         }
  76 
  77         @Override
  78         public String visitUnknown(Element e, Object o) {
  79             return "visitUnknown";
  80         }
  81 
  82         @Override
  83         public String visit(Element e) {
  84             return ElementVisitor.super.visit(e);
  85         }
  86 
  87         @Override
  88         public String visit(Element e, Object o) {
  89             return "visit";
  90         }
  91 
  92         @Override
  93         public String visitExecutable(ExecutableElement e, Object o)       { return throwUOE(); }
  94         @Override
  95         public String visitPackage(PackageElement e, Object o)             { return throwUOE(); }
  96         @Override
  97         public String visitType(TypeElement e, Object o)                   { return throwUOE(); }
  98         @Override
  99         public String visitTypeParameter(TypeParameterElement e, Object o) { return throwUOE(); }
 100         @Override
 101         public String visitVariable(VariableElement e, Object o)           { return throwUOE(); }
 102     }
 103 
 104     private static class IndirectElementVisitorChild
 105         extends AbstractElementVisitor6<String, Object> {
 106 
 107         public IndirectElementVisitorChild() {
 108             super();
 109         }
 110 
 111         @Override
 112         public String visitModule(ModuleElement e, Object o) {
 113             return super.visitModule(e, o);
 114         }
 115 
 116 
 117         @Override
 118         public String visitUnknown(Element e, Object o) {
 119             return "visitUnknown";
 120         }
 121 
 122         @Override
 123         public String visitExecutable(ExecutableElement e, Object o)       { return throwUOE(); }
 124         @Override
 125         public String visitPackage(PackageElement e, Object o)             { return throwUOE(); }
 126         @Override
 127         public String visitType(TypeElement e, Object o)                   { return throwUOE(); }
 128         @Override
 129         public String visitTypeParameter(TypeParameterElement e, Object o) { return throwUOE(); }
 130         @Override
 131         public String visitVariable(VariableElement e, Object o)           { return throwUOE(); }
 132     }
 133 
 134 
 135     private static class DirectTypeVisitorChild
 136         implements TypeVisitor<String, Object> {
 137 
 138         public DirectTypeVisitorChild() {
 139             super();
 140         }
 141 
 142         @Override
 143         public String visit(TypeMirror t) {
 144             return TypeVisitor.super.visit(t);
 145         }
 146 
 147         @Override
 148         public String visit(TypeMirror t, Object o) {
 149             return "visit";
 150         }
 151 
 152         @Override
 153         public String visitUnknown(TypeMirror t, Object o)            { return throwUOE(); }
 154         @Override
 155         public String visitArray(ArrayType t, Object o)               { return throwUOE(); }
 156         @Override
 157         public String visitDeclared(DeclaredType t, Object o)         { return throwUOE(); }
 158         @Override
 159         public String visitError(ErrorType t, Object o)               { return throwUOE(); }
 160         @Override
 161         public String visitExecutable(ExecutableType t, Object o)     { return throwUOE(); }
 162         @Override
 163         public String visitIntersection(IntersectionType t, Object o) { return throwUOE(); }
 164         @Override
 165         public String visitNoType(NoType t, Object o)                 { return throwUOE(); }
 166         @Override
 167         public String visitNull(NullType t, Object o)                 { return throwUOE(); }
 168         @Override
 169         public String visitPrimitive(PrimitiveType t, Object o)       { return throwUOE(); }
 170         @Override
 171         public String visitTypeVariable(TypeVariable t, Object o)     { return throwUOE(); }
 172         @Override
 173         public String visitUnion(UnionType t, Object o)               { return throwUOE(); }
 174         @Override
 175         public String visitWildcard(WildcardType t, Object o)         { return throwUOE(); }
 176     }
 177 
 178     private static class DirectAnnotationVisitorChild
 179         implements AnnotationValueVisitor<String, Object> {
 180 
 181         @Override
 182         public String visit(AnnotationValue av) {
 183             return AnnotationValueVisitor.super.visit(av);
 184         }
 185 
 186         @Override
 187         public String visit(AnnotationValue av, Object o) {
 188             return "visit";
 189         }
 190 
 191         @Override
 192         public String visitAnnotation(AnnotationMirror a, Object o)    { return throwUOE(); }
 193         @Override
 194         public String visitArray(List<? extends AnnotationValue> vals,
 195                                    Object o)                           { return throwUOE(); }
 196         @Override
 197         public String visitBoolean(boolean b, Object o)                { return throwUOE(); }
 198         @Override
 199         public String visitByte(byte b, Object o)                      { return throwUOE(); }
 200         @Override
 201         public String visitChar(char c, Object o)                      { return throwUOE(); }
 202         @Override
 203         public String visitDouble(double d, Object o)                  { return throwUOE(); }
 204         @Override
 205         public String visitEnumConstant(VariableElement c, Object o)   { return throwUOE(); }
 206         @Override
 207         public String visitFloat(float f, Object o)                    { return throwUOE(); }
 208         @Override
 209         public String visitInt(int i, Object o)                        { return throwUOE(); }
 210         @Override
 211         public String visitLong(long i, Object o)                      { return throwUOE(); }
 212         @Override
 213         public String visitShort(short s, Object o)                    { return throwUOE(); }
 214         @Override
 215         public String visitString(String s, Object o)                  { return throwUOE(); }
 216         @Override
 217         public String visitType(TypeMirror t, Object o)                { return throwUOE(); }
 218         @Override
 219         public String visitUnknown(AnnotationValue av, Object o)       { return throwUOE(); }
 220     }
 221 
 222     private static String throwUOE() {
 223         throw new UnsupportedOperationException();
 224     }
 225 }