test/tools/javac/processing/model/TestSymtabItems.java

changeset 900
938dda0bec17
child 983
fb84cfca28a1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/processing/model/TestSymtabItems.java	Tue Mar 01 12:00:06 2011 -0800
     1.3 @@ -0,0 +1,221 @@
     1.4 +/*
     1.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 7021183
    1.30 + * @summary 269: assertion failure getting enclosing element of an undefined name
    1.31 + */
    1.32 +
    1.33 +import java.lang.reflect.Field;
    1.34 +import javax.lang.model.element.Element;
    1.35 +import javax.lang.model.element.ExecutableElement;
    1.36 +import javax.lang.model.element.PackageElement;
    1.37 +import javax.lang.model.element.TypeElement;
    1.38 +import javax.lang.model.element.TypeParameterElement;
    1.39 +import javax.lang.model.element.UnknownElementException;
    1.40 +import javax.lang.model.element.VariableElement;
    1.41 +import javax.lang.model.type.TypeMirror;
    1.42 +import javax.lang.model.type.UnknownTypeException;
    1.43 +import javax.lang.model.util.ElementScanner7;
    1.44 +import javax.lang.model.util.SimpleTypeVisitor7;
    1.45 +import javax.lang.model.util.Types;
    1.46 +
    1.47 +import com.sun.tools.javac.code.Symbol.ClassSymbol;
    1.48 +import com.sun.tools.javac.code.Symtab;
    1.49 +import com.sun.tools.javac.file.JavacFileManager;
    1.50 +import com.sun.tools.javac.model.JavacTypes;
    1.51 +import com.sun.tools.javac.util.Context;
    1.52 +
    1.53 +/**
    1.54 + * Scan javac Symtab looking for TypeMirrors and Elements, and ensure that
    1.55 + * no exceptions are thrown when used with javax.lang.model visitors.
    1.56 + *
    1.57 + */
    1.58 +public class TestSymtabItems {
    1.59 +    public static void main(String... args) throws Exception {
    1.60 +        new TestSymtabItems().run();
    1.61 +    }
    1.62 +
    1.63 +    void run() throws Exception {
    1.64 +        Context c = new Context();
    1.65 +        JavacFileManager.preRegister(c);
    1.66 +        Symtab syms = Symtab.instance(c);
    1.67 +        JavacTypes types = JavacTypes.instance(c);
    1.68 +
    1.69 +//        print("noSymbol", syms.noSymbol);
    1.70 +//        print("errSymbol", syms.errSymbol);
    1.71 +//        print("unknownSymbol", syms.unknownSymbol);
    1.72 +//        print("botType", syms.botType);
    1.73 +//        print("errType", syms.errType);
    1.74 +//        print("unknownType", syms.unknownType);
    1.75 +
    1.76 +        for (Field f: Symtab.class.getDeclaredFields()) {
    1.77 +//            System.err.println(f.getType() + " " + f.getName());
    1.78 +
    1.79 +            // Temporarily ignore methodHandle and transientMethodHandle
    1.80 +            // during API evolution
    1.81 +            if (f.getName().toLowerCase().contains("methodhandle"))
    1.82 +                continue;
    1.83 +
    1.84 +            Class<?> ft = f.getType();
    1.85 +            if (TypeMirror.class.isAssignableFrom(ft))
    1.86 +                print(f.getName(), (TypeMirror) f.get(syms), types);
    1.87 +            else if(Element.class.isAssignableFrom(ft))
    1.88 +                print(f.getName(), (Element) f.get(syms));
    1.89 +        }
    1.90 +
    1.91 +        if (errors > 0)
    1.92 +            throw new Exception(errors + " errors occurred");
    1.93 +    }
    1.94 +
    1.95 +    void print(String label, Element e) {
    1.96 +        ElemPrinter ep = new ElemPrinter();
    1.97 +        System.err.println("Test " + label);
    1.98 +        ep.visit(e);
    1.99 +        System.err.println();
   1.100 +    }
   1.101 +
   1.102 +    void print(String label, TypeMirror t, Types types) {
   1.103 +        TypePrinter tp = new TypePrinter();
   1.104 +        System.err.println("Test " + label);
   1.105 +        tp.visit(t, types);
   1.106 +        System.err.println();
   1.107 +    }
   1.108 +
   1.109 +    void error(String msg) {
   1.110 +        System.err.println("Error: " + msg);
   1.111 +        errors++;
   1.112 +    }
   1.113 +
   1.114 +    int errors;
   1.115 +
   1.116 +    class ElemPrinter extends ElementScanner7<Void, Void> {
   1.117 +        @Override
   1.118 +        public Void visitPackage(PackageElement e, Void p) {
   1.119 +            show("package", e);
   1.120 +            indent(+1);
   1.121 +            super.visitPackage(e, p);
   1.122 +            indent(-1);
   1.123 +            return null;
   1.124 +        }
   1.125 +
   1.126 +        @Override
   1.127 +        public Void visitType(TypeElement e, Void p) {
   1.128 +            show("type", e);
   1.129 +            indent(+1);
   1.130 +            super.visitType(e, p);
   1.131 +            indent(-1);
   1.132 +            return null;
   1.133 +        }
   1.134 +
   1.135 +        @Override
   1.136 +        public Void visitVariable(VariableElement e, Void p) {
   1.137 +            show("variable", e);
   1.138 +            indent(+1);
   1.139 +            super.visitVariable(e, p);
   1.140 +            indent(-1);
   1.141 +            return null;
   1.142 +        }
   1.143 +
   1.144 +        @Override
   1.145 +        public Void visitExecutable(ExecutableElement e, Void p) {
   1.146 +            show("executable", e);
   1.147 +            indent(+1);
   1.148 +            super.visitExecutable(e, p);
   1.149 +            indent(-1);
   1.150 +            return null;
   1.151 +        }
   1.152 +
   1.153 +        @Override
   1.154 +        public Void visitTypeParameter(TypeParameterElement e, Void p) {
   1.155 +            show("type parameter", e);
   1.156 +            indent(+1);
   1.157 +            super.visitTypeParameter(e, p);
   1.158 +            indent(-1);
   1.159 +            return null;
   1.160 +        }
   1.161 +
   1.162 +        @Override
   1.163 +        public Void visitUnknown(Element e, Void p) {
   1.164 +            show("unknown", e);
   1.165 +            indent(+1);
   1.166 +            try {
   1.167 +                super.visitUnknown(e, p);
   1.168 +            } catch (UnknownElementException ex) {
   1.169 +                System.err.println("caught " + ex);
   1.170 +            }
   1.171 +            indent(-1);
   1.172 +            return null;
   1.173 +        }
   1.174 +
   1.175 +        void indent(int i) {
   1.176 +            indent += i;
   1.177 +        }
   1.178 +
   1.179 +        String sp(int w) {
   1.180 +            StringBuilder sb = new StringBuilder();
   1.181 +            for (int i = 0; i < w; i++)
   1.182 +                sb.append("    ");
   1.183 +            return sb.toString();
   1.184 +        }
   1.185 +
   1.186 +        void show(String label, Element e) {
   1.187 +            System.err.println(sp(indent) + label
   1.188 +                    + ": mods:" + e.getModifiers()
   1.189 +                    + " " + e.getSimpleName()
   1.190 +                    + ", kind: " + e.getKind()
   1.191 +                    + ", type: " + e.asType()
   1.192 +                    + ", encl: " + e.getEnclosingElement());
   1.193 +
   1.194 +            // The following checks help establish why NPE might occur when trying to scan children
   1.195 +            if (e instanceof ClassSymbol) {
   1.196 +                ClassSymbol csym = (ClassSymbol) e;
   1.197 +                if (csym.members_field == null)
   1.198 +                    error("members_field is null");
   1.199 +                if (csym.type == null)
   1.200 +                    System.err.println("type is null");
   1.201 +            }
   1.202 +        }
   1.203 +
   1.204 +        int indent;
   1.205 +    };
   1.206 +
   1.207 +    class TypePrinter extends SimpleTypeVisitor7<Void, Types> {
   1.208 +        @Override
   1.209 +        public Void defaultAction(TypeMirror m, Types types) {
   1.210 +            System.err.println(m.getKind() + " " + m + " " + types.asElement(m));
   1.211 +            return null;
   1.212 +        }
   1.213 +
   1.214 +        @Override
   1.215 +        public Void visitUnknown(TypeMirror t, Types types) {
   1.216 +            try {
   1.217 +                return super.visitUnknown(t, types);
   1.218 +            } catch (UnknownTypeException ex) {
   1.219 +                System.err.println("caught " + ex);
   1.220 +                return null;
   1.221 +            }
   1.222 +        }
   1.223 +    };
   1.224 +}

mercurial