TestSymtabItems.java revision 1053:111bbf1ad913
1/*
2 * Copyright (c) 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.
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 7021183 7025809
27 * @summary 269: assertion failure getting enclosing element of an undefined name
28 */
29
30import java.lang.reflect.Field;
31import javax.lang.model.element.Element;
32import javax.lang.model.element.ExecutableElement;
33import javax.lang.model.element.PackageElement;
34import javax.lang.model.element.TypeElement;
35import javax.lang.model.element.TypeParameterElement;
36import javax.lang.model.element.UnknownElementException;
37import javax.lang.model.element.VariableElement;
38import javax.lang.model.type.TypeMirror;
39import javax.lang.model.type.UnknownTypeException;
40import javax.lang.model.util.*;
41
42import com.sun.tools.javac.code.Symbol.ClassSymbol;
43import com.sun.tools.javac.code.Symtab;
44import com.sun.tools.javac.file.JavacFileManager;
45import com.sun.tools.javac.main.JavaCompiler;
46import com.sun.tools.javac.model.JavacTypes;
47import com.sun.tools.javac.util.Context;
48
49/**
50 * Scan javac Symtab looking for TypeMirrors and Elements, and ensure that
51 * no exceptions are thrown when used with javax.lang.model visitors.
52 *
53 */
54public class TestSymtabItems {
55    public static void main(String... args) throws Exception {
56        new TestSymtabItems().run();
57    }
58
59    void run() throws Exception {
60        Context c = new Context();
61        JavacFileManager.preRegister(c);
62        Symtab syms = Symtab.instance(c);
63        JavacTypes types = JavacTypes.instance(c);
64        JavaCompiler.instance(c);  // will init ClassReader.sourceCompleter
65
66//        print("noSymbol", syms.noSymbol);
67//        print("errSymbol", syms.errSymbol);
68//        print("unknownSymbol", syms.unknownSymbol);
69//        print("botType", syms.botType);
70//        print("errType", syms.errType);
71//        print("unknownType", syms.unknownType);
72
73        for (Field f: Symtab.class.getDeclaredFields()) {
74//            System.err.println(f.getType() + " " + f.getName());
75
76            // Temporarily ignore methodHandle and transientMethodHandle
77            // during API evolution
78            if (f.getName().toLowerCase().contains("methodhandle"))
79                continue;
80
81            Class<?> ft = f.getType();
82            if (TypeMirror.class.isAssignableFrom(ft))
83                print(f.getName(), (TypeMirror) f.get(syms), types);
84            else if(Element.class.isAssignableFrom(ft))
85                print(f.getName(), (Element) f.get(syms));
86        }
87
88        if (errors > 0)
89            throw new Exception(errors + " errors occurred");
90    }
91
92    void print(String label, Element e) {
93        ElemPrinter ep = new ElemPrinter();
94        System.err.println("Test " + label);
95        ep.visit(e);
96        System.err.println();
97    }
98
99    void print(String label, TypeMirror t, Types types) {
100        TypePrinter tp = new TypePrinter();
101        System.err.println("Test " + label);
102        tp.visit(t, types);
103        System.err.println();
104    }
105
106    void error(String msg) {
107        System.err.println("Error: " + msg);
108        errors++;
109    }
110
111    int errors;
112
113    class ElemPrinter extends ElementScanner8<Void, Void> {
114        @Override
115        public Void visitPackage(PackageElement e, Void p) {
116            show("package", e);
117            indent(+1);
118            super.visitPackage(e, p);
119            indent(-1);
120            return null;
121        }
122
123        @Override
124        public Void visitType(TypeElement e, Void p) {
125            show("type", e);
126            indent(+1);
127            super.visitType(e, p);
128            indent(-1);
129            return null;
130        }
131
132        @Override
133        public Void visitVariable(VariableElement e, Void p) {
134            show("variable", e);
135            indent(+1);
136            super.visitVariable(e, p);
137            indent(-1);
138            return null;
139        }
140
141        @Override
142        public Void visitExecutable(ExecutableElement e, Void p) {
143            show("executable", e);
144            indent(+1);
145            super.visitExecutable(e, p);
146            indent(-1);
147            return null;
148        }
149
150        @Override
151        public Void visitTypeParameter(TypeParameterElement e, Void p) {
152            show("type parameter", e);
153            indent(+1);
154            super.visitTypeParameter(e, p);
155            indent(-1);
156            return null;
157        }
158
159        @Override
160        public Void visitUnknown(Element e, Void p) {
161            show("unknown", e);
162            indent(+1);
163            try {
164                super.visitUnknown(e, p);
165            } catch (UnknownElementException ex) {
166                System.err.println("caught " + ex);
167            }
168            indent(-1);
169            return null;
170        }
171
172        void indent(int i) {
173            indent += i;
174        }
175
176        String sp(int w) {
177            StringBuilder sb = new StringBuilder();
178            for (int i = 0; i < w; i++)
179                sb.append("    ");
180            return sb.toString();
181        }
182
183        void show(String label, Element e) {
184            System.err.println(sp(indent) + label
185                    + ": mods:" + e.getModifiers()
186                    + " " + e.getSimpleName()
187                    + ", kind: " + e.getKind()
188                    + ", type: " + e.asType()
189                    + ", encl: " + e.getEnclosingElement());
190
191            // The following checks help establish why NPE might occur when trying to scan children
192            if (e instanceof ClassSymbol) {
193                ClassSymbol csym = (ClassSymbol) e;
194                if (csym.members_field == null)
195                    error("members_field is null");
196                if (csym.type == null)
197                    System.err.println("type is null");
198            }
199        }
200
201        int indent;
202    };
203
204    class TypePrinter extends SimpleTypeVisitor7<Void, Types> {
205        @Override
206        public Void defaultAction(TypeMirror m, Types types) {
207            System.err.println(m.getKind() + " " + m + " " + types.asElement(m));
208            return null;
209        }
210
211        @Override
212        public Void visitUnknown(TypeMirror t, Types types) {
213            try {
214                return super.visitUnknown(t, types);
215            } catch (UnknownTypeException ex) {
216                System.err.println("caught " + ex);
217                return null;
218            }
219        }
220    };
221}
222