langtools/test/tools/javac/api/6557752/T6557752.java
author never
Fri, 25 Mar 2011 18:50:10 -0700
changeset 8867 6febcc945802
parent 8625 6b51ef804d49
child 8842 d532916921b7
permissions -rw-r--r--
7022204: LogFile wildcarding should use %p instead of star Reviewed-by: coleenp, jrose
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
     1
/*
8625
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 5520
diff changeset
     2
 * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
     4
 *
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
     8
 *
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    13
 * accompanied this code).
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    14
 *
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    18
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
    21
 * questions.
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    22
 */
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    23
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    24
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    25
/*
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    26
 * @test
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    27
 * @bug     6557752
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    28
 * @summary Test for wrapping the original type in ErrorType.
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    29
 * @library ../lib
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    30
 * @compile T6557752.java
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    31
 * @run main T6557752
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    32
 */
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    33
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    34
import com.sun.source.tree.AssignmentTree;
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    35
import com.sun.source.tree.CompilationUnitTree;
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    36
import com.sun.source.tree.MethodInvocationTree;
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    37
import com.sun.source.util.JavacTask;
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    38
import com.sun.source.util.TreePath;
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    39
import com.sun.source.util.TreePathScanner;
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    40
import com.sun.source.util.Trees;
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    41
import com.sun.tools.javac.api.JavacTaskImpl;
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    42
import com.sun.tools.javac.util.List;
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    43
import java.io.IOException;
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    44
import java.net.URI;
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    45
import javax.lang.model.type.ErrorType;
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    46
import javax.lang.model.type.TypeKind;
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    47
import javax.lang.model.type.TypeMirror;
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    48
import javax.tools.JavaCompiler;
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    49
import javax.tools.JavaFileObject;
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    50
import javax.tools.SimpleJavaFileObject;
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    51
import javax.tools.ToolProvider;
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    52
import javax.lang.model.util.Types;
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    53
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    54
public class T6557752 {
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    55
    static class MyFileObject extends SimpleJavaFileObject {
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    56
        public MyFileObject() {
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    57
            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    58
        }
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    59
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    60
            return "import java.util.*;\n"
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    61
                + "public class Test {\n"
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    62
                + "    void foobar() {\n"
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    63
                + "        Iterator<Number> itr = null;\n"
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    64
                + "        String str = itr.next();\n"
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    65
                + "        FooBar fooBar = FooBar.foobar();\n"
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    66
                + "    }\n"
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    67
                + "}";
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    68
        }
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    69
    }
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    70
    static Trees trees;
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    71
    static JavacTask task = null;
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    72
    public static void main(String[] args) throws IOException {
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    73
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    74
        task = (JavacTask) compiler.getTask(null, null, null, null, null, List.of(new MyFileObject()));
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    75
        Iterable<? extends CompilationUnitTree> asts = task.parse();
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    76
        task.analyze();
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    77
        trees = Trees.instance(task);
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    78
        MyVisitor myVisitor = new MyVisitor();
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    79
        for (CompilationUnitTree ast : asts) {
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    80
            myVisitor.compilationUnit = ast;
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    81
            myVisitor.scan(ast, null);
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    82
        }
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    83
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    84
        if (!myVisitor.foundError) {
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    85
            throw new AssertionError("Expected error not found!");
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    86
        }
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    87
    }
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    88
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    89
    static class MyVisitor extends TreePathScanner<Void,Void> {
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    90
        public boolean foundError = false;
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    91
        CompilationUnitTree compilationUnit = null;
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    92
        int i = 0;
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    93
        @Override
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    94
        public Void visitMethodInvocation(MethodInvocationTree node, Void ignored) {
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    95
            TreePath path = TreePath.getPath(compilationUnit, node);
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    96
            TypeMirror typeMirror = trees.getTypeMirror(path);
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    97
            if (typeMirror.getKind() == TypeKind.ERROR) {
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    98
              if (i == 0) {
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
    99
                String str1 = trees.getOriginalType((ErrorType)typeMirror).toString();
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   100
                if (!str1.equals("java.lang.Number")) {
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   101
                    throw new AssertionError("Trees.getOriginalType() error!");
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   102
                }
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   103
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   104
                Types types = task.getTypes();
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   105
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   106
                str1 = types.asElement(trees.getOriginalType((ErrorType)typeMirror)).toString();
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   107
                if (!str1.equals("java.lang.Number")) {
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   108
                    throw new AssertionError("Types.asElement() error!");
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   109
                }
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   110
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   111
                i++;
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   112
              }
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   113
              else if (i == 1) {
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   114
                String str1 = trees.getOriginalType((ErrorType)typeMirror).toString();
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   115
                if (!str1.equals("FooBar")) {
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   116
                    throw new AssertionError("Trees.getOriginalType() error!");
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   117
                }
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   118
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   119
                Types types = task.getTypes();
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   120
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   121
                if (types.asElement(trees.getOriginalType((ErrorType)typeMirror)) != null) {
8625
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 5520
diff changeset
   122
                    throw new AssertionError("Types.asElement() error!");
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   123
                }
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   124
                foundError = true;
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   125
              }
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   126
            }
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   127
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   128
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   129
            return null;
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   130
        }
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   131
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   132
    }
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
diff changeset
   133
}