1/*
2 * Copyright (c) 2010, 2015, 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 6993301
27 * @summary catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
28 * @modules jdk.compiler/com.sun.tools.javac.api
29 */
30
31import com.sun.source.tree.CompilationUnitTree;
32import com.sun.source.tree.IdentifierTree;
33import com.sun.source.tree.VariableTree;
34import com.sun.source.util.TreePathScanner;
35import com.sun.source.util.Trees;
36import com.sun.tools.javac.api.JavacTaskImpl;
37import java.io.IOException;
38import java.net.URI;
39import java.util.Arrays;
40import javax.lang.model.element.Element;
41import javax.lang.model.element.ElementKind;
42import javax.tools.JavaCompiler;
43import javax.tools.JavaFileObject;
44import javax.tools.SimpleJavaFileObject;
45import javax.tools.ToolProvider;
46
47/**
48 * @author Jan Lahoda
49 */
50public class T6993301 {
51    public static void main(String... args) throws Exception {
52        new T6993301().testExceptionParameterCorrectKind();
53    }
54
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    }
66
67    public void testExceptionParameterCorrectKind() throws IOException {
68        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
69        assert tool != null;
70
71        String code = "package test; public class Test { { try { } catch (NullPointerException ex) {} } }";
72
73        final JavacTaskImpl ct = (JavacTaskImpl)tool.getTask(null, null, null,
74                null, null, Arrays.asList(new MyFileObject(code)));
75        CompilationUnitTree cut = ct.parse().iterator().next();
76
77        ct.analyze();
78
79        new TreePathScanner<Void, Void>() {
80            @Override
81            public Void visitVariable(VariableTree node, Void p) {
82                Element el = Trees.instance(ct).getElement(getCurrentPath());
83
84                assertNotNull(el);
85                assertEquals(ElementKind.EXCEPTION_PARAMETER, el.getKind());
86
87                return super.visitVariable(node, p);
88            }
89        }.scan(cut, null);
90    }
91
92    private void assertNotNull(Object o) {
93        if (o == null)
94            throw new AssertionError();
95    }
96
97    private <T> void assertEquals(T expected, T actual) {
98        if (expected == null ? actual == null : expected.equals(actual))
99            return;
100        throw new AssertionError("expected: " + expected + ", actual: " + actual);
101    }
102
103}
104