Mon, 03 Oct 2016 11:21:14 -0700
Added tag jdk8u122-b02 for changeset 2baeb96fa198
1 /*
2 * Copyright (c) 2010, 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 */
24 /*
25 * @test
26 * @bug 6993301
27 * @summary catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
28 */
30 import com.sun.source.tree.CompilationUnitTree;
31 import com.sun.source.tree.IdentifierTree;
32 import com.sun.source.tree.VariableTree;
33 import com.sun.source.util.TreePathScanner;
34 import com.sun.source.util.Trees;
35 import com.sun.tools.javac.api.JavacTaskImpl;
36 import java.io.IOException;
37 import java.net.URI;
38 import java.util.Arrays;
39 import javax.lang.model.element.Element;
40 import javax.lang.model.element.ElementKind;
41 import javax.tools.JavaCompiler;
42 import javax.tools.JavaFileObject;
43 import javax.tools.SimpleJavaFileObject;
44 import javax.tools.ToolProvider;
46 /**
47 *
48 * @author Jan Lahoda
49 */
50 public class T6993301 {
51 public static void main(String... args) throws Exception {
52 new T6993301().testExceptionParameterCorrectKind();
53 }
55 static class MyFileObject extends SimpleJavaFileObject {
56 private String text;
57 public MyFileObject(String text) {
58 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
59 this.text = text;
60 }
61 @Override
62 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
63 return text;
64 }
65 }
67 public void testExceptionParameterCorrectKind() throws IOException {
68 final String bootPath = System.getProperty("sun.boot.class.path");
69 final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
70 assert tool != null;
72 String code = "package test; public class Test { { try { } catch (NullPointerException ex) {} } }";
74 final JavacTaskImpl ct = (JavacTaskImpl)tool.getTask(null, null, null,
75 Arrays.asList("-bootclasspath", bootPath),
76 null, Arrays.asList(new MyFileObject(code)));
77 CompilationUnitTree cut = ct.parse().iterator().next();
79 ct.analyze();
81 new TreePathScanner<Void, Void>() {
82 @Override
83 public Void visitVariable(VariableTree node, Void p) {
84 Element el = Trees.instance(ct).getElement(getCurrentPath());
86 assertNotNull(el);
87 assertEquals(ElementKind.EXCEPTION_PARAMETER, el.getKind());
89 return super.visitVariable(node, p);
90 }
91 }.scan(cut, null);
92 }
94 private void assertNotNull(Object o) {
95 if (o == null)
96 throw new AssertionError();
97 }
99 private <T> void assertEquals(T expected, T actual) {
100 if (expected == null ? actual == null : expected.equals(actual))
101 return;
102 throw new AssertionError("expected: " + expected + ", actual: " + actual);
103 }
105 }