T6852595.java revision 3294:9adfb22ff08f
1169092Sdeischen/*
2169092Sdeischen * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
3169092Sdeischen * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4174618Sdas *
5174618Sdas * This code is free software; you can redistribute it and/or modify it
6157196Sdeischen * under the terms of the GNU General Public License version 2 only, as
7157196Sdeischen * published by the Free Software Foundation.
8157196Sdeischen *
9157196Sdeischen * This code is distributed in the hope that it will be useful, but WITHOUT
10157196Sdeischen * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11157196Sdeischen * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12157196Sdeischen * version 2 for more details (a copy is included in the LICENSE file that
13157196Sdeischen * accompanied this code).
14157196Sdeischen *
15157196Sdeischen * You should have received a copy of the GNU General Public License version
16157196Sdeischen * 2 along with this work; if not, write to the Free Software Foundation,
17157196Sdeischen * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18157196Sdeischen *
19157196Sdeischen * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20157196Sdeischen * or visit www.oracle.com if you need additional information or have any
21157196Sdeischen * questions.
22157196Sdeischen */
23157196Sdeischen
24157196Sdeischen/*
25157196Sdeischen * @test
26157196Sdeischen * @bug     6852595
27157196Sdeischen * @summary Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
28157196Sdeischen * @author  mcimadamore
29157196Sdeischen * @modules jdk.compiler/com.sun.tools.javac.tree
30157196Sdeischen */
31157196Sdeischen
32157196Sdeischenimport java.io.IOException;
33157196Sdeischenimport java.net.URI;
34157196Sdeischenimport java.util.Arrays;
35157196Sdeischenimport java.util.List;
36157196Sdeischenimport javax.tools.JavaCompiler;
37157196Sdeischenimport javax.tools.JavaFileObject;
38157196Sdeischenimport javax.tools.SimpleJavaFileObject;
39157196Sdeischenimport javax.tools.ToolProvider;
40157196Sdeischen
41157196Sdeischenimport com.sun.source.util.JavacTask;
42157196Sdeischenimport com.sun.source.tree.*;
43157196Sdeischenimport com.sun.source.util.TreePath;
44157196Sdeischenimport com.sun.source.util.Trees;
45157196Sdeischenimport com.sun.tools.javac.tree.JCTree.*;
46157196Sdeischen
47157196Sdeischenimport static javax.tools.JavaFileObject.Kind;
48157196Sdeischen
49157196Sdeischenpublic class T6852595 {
50157196Sdeischen    public static void main(String[] args) throws IOException {
51157196Sdeischen        JavaFileObject sfo = new SimpleJavaFileObject(URI.create("myfo:/Test.java"),Kind.SOURCE) {
52157196Sdeischen            public CharSequence getCharContent(boolean ignoreEncodingErrors) {
53157196Sdeischen                return "class BadName { Object o = j; }";
54157196Sdeischen            }
55157196Sdeischen        };
56157196Sdeischen        List<? extends JavaFileObject> files = Arrays.asList(sfo);
57157196Sdeischen        JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
58157196Sdeischen        JavacTask ct = (JavacTask)tool.getTask(null, null, null, null, null, files);
59157196Sdeischen        Iterable<? extends CompilationUnitTree> compUnits = ct.parse();
60157196Sdeischen        CompilationUnitTree cu = compUnits.iterator().next();
61157196Sdeischen        ClassTree cdef = (ClassTree)cu.getTypeDecls().get(0);
62157196Sdeischen        JCVariableDecl vdef = (JCVariableDecl)cdef.getMembers().get(0);
63157196Sdeischen        TreePath path = TreePath.getPath(cu, vdef.init);
64157196Sdeischen        Trees.instance(ct).getScope(path);
65157196Sdeischen    }
66157196Sdeischen}
67157196Sdeischen