1 /*
2  * Copyright (c) 2013, 2015, 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     8027730
27  * @summary Test visitor support for intersection types
28  * @modules java.compiler
29  *          jdk.compiler
30  */
31 
32 import java.lang.annotation.Annotation;
33 import java.util.List;
34 import javax.lang.model.element.*;
35 import javax.lang.model.type.*;
36 import javax.lang.model.util.*;
37 
38 public class TestIntersectionTypeVisitors {
main(String... args)39     public static void main(String... args) throws Exception {
40         IntersectionType it = new TestIntersectionType();
41 
42         boolean result = it.accept(new TypeKindVisitor8Child(), null) &&
43             it.accept(new SimpleTypeVisitor8Child(), null) &&
44             it.accept(new SimpleTypeVisitor6Child(), null);
45 
46         if (!result)
47             throw new RuntimeException();
48     }
49 
50     static class TestIntersectionType implements IntersectionType {
TestIntersectionType()51         TestIntersectionType() {}
52 
53         @Override
getBounds()54         public List<? extends TypeMirror> getBounds() {
55             throw new UnsupportedOperationException();
56         }
57 
58         @Override
accept(TypeVisitor<R,P> v, P p)59         public <R,P> R accept(TypeVisitor<R,P> v,
60                        P p) {
61             return v.visitIntersection(this, p);
62         }
63 
64         @Override
getKind()65         public TypeKind getKind() {
66             return TypeKind.INTERSECTION;
67         }
68 
69         @Override
getAnnotation(Class<A> annotationType)70         public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
71             throw new UnsupportedOperationException();
72         }
73 
74         @Override
getAnnotationMirrors()75         public List<? extends AnnotationMirror> getAnnotationMirrors() {
76             throw new UnsupportedOperationException();
77         }
78 
79         @Override
getAnnotationsByType(Class<A> annotationType)80         public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType) {
81             throw new UnsupportedOperationException();
82         }
83     }
84 
85     static class TypeKindVisitor8Child extends TypeKindVisitor8<Boolean, Void> {
TypeKindVisitor8Child()86         TypeKindVisitor8Child() {
87             super(false);
88         }
89 
90         @Override
visitIntersection(IntersectionType t, Void p)91         public Boolean visitIntersection(IntersectionType t, Void p) {
92             super.visitIntersection(t, p); // Make sure overridden method doesn't throw an exception
93             return true;
94         }
95     }
96 
97     static class SimpleTypeVisitor8Child extends SimpleTypeVisitor8<Boolean, Void> {
SimpleTypeVisitor8Child()98         SimpleTypeVisitor8Child() {
99             super(false);
100         }
101 
102         @Override
visitIntersection(IntersectionType t, Void p)103         public Boolean visitIntersection(IntersectionType t, Void p) {
104             super.visitIntersection(t, p);  // Make sure overridden method doesn't throw an exception
105             return true;
106         }
107     }
108 
109     static class SimpleTypeVisitor6Child extends SimpleTypeVisitor6<Boolean, Void> {
SimpleTypeVisitor6Child()110         SimpleTypeVisitor6Child() {
111             super(false);
112         }
113 
114         @Override
visitIntersection(IntersectionType t, Void p)115         public Boolean visitIntersection(IntersectionType t, Void p) {
116             try {
117                 super.visitIntersection(t, p);
118                 return false;
119             } catch (UnknownTypeException ute) {
120                 return true; // Expected
121             }
122         }
123     }
124 }
125