1 /*
2  * Copyright (c) 2018, 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 8197439
27  * @summary Ensuring that unresolvable anonymous class is still attributed.
28  * @modules jdk.compiler
29  */
30 
31 import java.io.*;
32 import java.net.*;
33 import java.util.*;
34 
35 import javax.tools.*;
36 
37 import com.sun.source.tree.CompilationUnitTree;
38 import com.sun.source.tree.IdentifierTree;
39 import com.sun.source.tree.MethodTree;
40 import com.sun.source.tree.VariableTree;
41 import com.sun.source.util.*;
42 
43 public class BrokenAnonymous {
44 
45     static class JavaSource extends SimpleJavaFileObject {
46 
47         final static String source =
48                         "class C {\n" +
49                         "    Object o1 = new Undef1() {" +
50                         "        int i = 0;\n" +
51                         "        int m(int j) { return i + j; }\n" +
52                         "    }\n" +
53                         "    Object o2 = new Undef2<>() {" +
54                         "        int i = 0;\n" +
55                         "        int m(int j) { return i + j; }\n" +
56                         "    }\n" +
57                         "}";
58 
JavaSource()59         JavaSource() {
60             super(URI.create("myfo:/C.java"), JavaFileObject.Kind.SOURCE);
61         }
62 
63         @Override
getCharContent(boolean ignoreEncodingErrors)64         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
65             return source;
66         }
67     }
68 
main(String... args)69     public static void main(String... args) throws IOException {
70         new BrokenAnonymous().run();
71     }
72 
run()73     void run() throws IOException {
74         File destDir = new File("classes"); destDir.mkdir();
75         final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
76         JavaSource source = new JavaSource();
77         JavacTask ct = (JavacTask)tool.getTask(null, null, null,
78                 Arrays.asList("-d", destDir.getPath()),
79                 null,
80                 Arrays.asList(source));
81         CompilationUnitTree cut = ct.parse().iterator().next();
82         Trees trees = Trees.instance(ct);
83         ct.analyze();
84         new TreePathScanner<Void, Void>() {
85             @Override
86             public Void visitVariable(VariableTree node, Void p) {
87                 verifyElementType();
88                 return super.visitVariable(node, p);
89             }
90             @Override
91             public Void visitMethod(MethodTree node, Void p) {
92                 verifyElementType();
93                 return super.visitMethod(node, p);
94             }
95             @Override
96             public Void visitIdentifier(IdentifierTree node, Void p) {
97                 verifyElementType();
98                 return super.visitIdentifier(node, p);
99             }
100             private void verifyElementType() {
101                 TreePath tp = getCurrentPath();
102                 assertNotNull(trees.getElement(tp));
103                 assertNotNull(trees.getTypeMirror(tp));
104             }
105         }.scan(cut, null);
106     }
107 
assertNotNull(Object obj)108     private void assertNotNull(Object obj) {
109         if (obj == null) {
110             throw new AssertionError("Unexpected null value.");
111         }
112     }
113 }
114