langtools/test/tools/javac/T6993301.java
author jjg
Fri, 22 Oct 2010 14:04:33 -0700
changeset 7073 c27644ae5855
child 30730 d3ce7619db2c
permissions -rw-r--r--
6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER) Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7073
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
     1
/*
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
     4
 *
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
     8
 *
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    13
 * accompanied this code).
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    14
 *
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    18
 *
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    21
 * questions.
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    22
 */
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    23
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    24
/*
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    25
 * @test
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    26
 * @bug 6993301
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    27
 * @summary catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    28
 */
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    29
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    30
import com.sun.source.tree.CompilationUnitTree;
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    31
import com.sun.source.tree.IdentifierTree;
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    32
import com.sun.source.tree.VariableTree;
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    33
import com.sun.source.util.TreePathScanner;
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    34
import com.sun.source.util.Trees;
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    35
import com.sun.tools.javac.api.JavacTaskImpl;
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    36
import java.io.IOException;
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    37
import java.net.URI;
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    38
import java.util.Arrays;
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    39
import javax.lang.model.element.Element;
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    40
import javax.lang.model.element.ElementKind;
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    41
import javax.tools.JavaCompiler;
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    42
import javax.tools.JavaFileObject;
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    43
import javax.tools.SimpleJavaFileObject;
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    44
import javax.tools.ToolProvider;
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    45
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    46
/**
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    47
 *
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    48
 * @author Jan Lahoda
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    49
 */
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    50
public class T6993301 {
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    51
    public static void main(String... args) throws Exception {
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    52
        new T6993301().testExceptionParameterCorrectKind();
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    53
    }
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    54
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    55
    static class MyFileObject extends SimpleJavaFileObject {
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    56
        private String text;
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    57
        public MyFileObject(String text) {
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    58
            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    59
            this.text = text;
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    60
        }
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    61
        @Override
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    62
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    63
            return text;
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    64
        }
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    65
    }
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    66
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    67
    public void testExceptionParameterCorrectKind() throws IOException {
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    68
        final String bootPath = System.getProperty("sun.boot.class.path");
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    69
        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    70
        assert tool != null;
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    71
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    72
        String code = "package test; public class Test { { try { } catch (NullPointerException ex) {} } }";
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    73
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    74
        final JavacTaskImpl ct = (JavacTaskImpl)tool.getTask(null, null, null,
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    75
                Arrays.asList("-bootclasspath",  bootPath),
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    76
                null, Arrays.asList(new MyFileObject(code)));
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    77
        CompilationUnitTree cut = ct.parse().iterator().next();
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    78
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    79
        ct.analyze();
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    80
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    81
        new TreePathScanner<Void, Void>() {
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    82
            @Override
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    83
            public Void visitVariable(VariableTree node, Void p) {
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    84
                Element el = Trees.instance(ct).getElement(getCurrentPath());
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    85
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    86
                assertNotNull(el);
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    87
                assertEquals(ElementKind.EXCEPTION_PARAMETER, el.getKind());
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    88
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    89
                return super.visitVariable(node, p);
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    90
            }
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    91
        }.scan(cut, null);
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    92
    }
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    93
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    94
    private void assertNotNull(Object o) {
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    95
        if (o == null)
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    96
            throw new AssertionError();
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    97
    }
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    98
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
    99
    private <T> void assertEquals(T expected, T actual) {
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
   100
        if (expected == null ? actual == null : expected.equals(actual))
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
   101
            return;
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
   102
        throw new AssertionError("expected: " + expected + ", actual: " + actual);
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
   103
    }
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
   104
c27644ae5855 6993301: catch parameters do not have correct kind (i.e. ElementKind.EXCEPTION_PARAMETER)
jjg
parents:
diff changeset
   105
}