test/tools/javac/api/6557752/T6557752.java

Mon, 14 Mar 2011 11:33:33 -0700

author
jjg
date
Mon, 14 Mar 2011 11:33:33 -0700
changeset 928
307b065ff2af
parent 904
4baab658f357
child 2525
2eb010b6cb22
permissions
-rw-r--r--

7026414: Types.asElement() returns null for ErrorType
Reviewed-by: darcy

     1 /*
     2  * Copyright (c) 2006, 2011, 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  */
    25 /*
    26  * @test
    27  * @bug     6557752
    28  * @summary Test for wrapping the original type in ErrorType.
    29  * @library ../lib
    30  * @compile T6557752.java
    31  * @run main T6557752
    32  */
    34 import com.sun.source.tree.AssignmentTree;
    35 import com.sun.source.tree.CompilationUnitTree;
    36 import com.sun.source.tree.MethodInvocationTree;
    37 import com.sun.source.util.JavacTask;
    38 import com.sun.source.util.TreePath;
    39 import com.sun.source.util.TreePathScanner;
    40 import com.sun.source.util.Trees;
    41 import com.sun.tools.javac.api.JavacTaskImpl;
    42 import com.sun.tools.javac.util.List;
    43 import java.io.IOException;
    44 import java.net.URI;
    45 import javax.lang.model.type.ErrorType;
    46 import javax.lang.model.type.TypeKind;
    47 import javax.lang.model.type.TypeMirror;
    48 import javax.tools.JavaCompiler;
    49 import javax.tools.JavaFileObject;
    50 import javax.tools.SimpleJavaFileObject;
    51 import javax.tools.ToolProvider;
    52 import javax.lang.model.util.Types;
    54 public class T6557752 {
    55     static class MyFileObject extends SimpleJavaFileObject {
    56         public MyFileObject() {
    57             super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
    58         }
    59         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
    60             return "import java.util.*;\n"
    61                 + "public class Test {\n"
    62                 + "    void foobar() {\n"
    63                 + "        Iterator<Number> itr = null;\n"
    64                 + "        String str = itr.next();\n"
    65                 + "        FooBar fooBar = FooBar.foobar();\n"
    66                 + "    }\n"
    67                 + "}";
    68         }
    69     }
    70     static Trees trees;
    71     static JavacTask task = null;
    72     public static void main(String[] args) throws IOException {
    73         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    74         task = (JavacTask) compiler.getTask(null, null, null, null, null, List.of(new MyFileObject()));
    75         Iterable<? extends CompilationUnitTree> asts = task.parse();
    76         task.analyze();
    77         trees = Trees.instance(task);
    78         MyVisitor myVisitor = new MyVisitor();
    79         for (CompilationUnitTree ast : asts) {
    80             myVisitor.compilationUnit = ast;
    81             myVisitor.scan(ast, null);
    82         }
    84         if (!myVisitor.foundError) {
    85             throw new AssertionError("Expected error not found!");
    86         }
    87     }
    89     static class MyVisitor extends TreePathScanner<Void,Void> {
    90         public boolean foundError = false;
    91         CompilationUnitTree compilationUnit = null;
    92         int i = 0;
    93         @Override
    94         public Void visitMethodInvocation(MethodInvocationTree node, Void ignored) {
    95             TreePath path = TreePath.getPath(compilationUnit, node);
    96             TypeMirror typeMirror = trees.getTypeMirror(path);
    97             if (typeMirror.getKind() == TypeKind.ERROR) {
    98               if (i == 0) {
    99                 String str1 = trees.getOriginalType((ErrorType)typeMirror).toString();
   100                 if (!str1.equals("java.lang.Number")) {
   101                     throw new AssertionError("Trees.getOriginalType() error!");
   102                 }
   104                 Types types = task.getTypes();
   106                 str1 = types.asElement(trees.getOriginalType((ErrorType)typeMirror)).toString();
   107                 if (!str1.equals("java.lang.Number")) {
   108                     throw new AssertionError("Types.asElement() error!");
   109                 }
   111                 i++;
   112               }
   113               else if (i == 1) {
   114                 String str1 = trees.getOriginalType((ErrorType)typeMirror).toString();
   115                 if (!str1.equals("FooBar")) {
   116                     throw new AssertionError("Trees.getOriginalType() error!");
   117                 }
   119                 Types types = task.getTypes();
   121                 str1 = types.asElement(trees.getOriginalType((ErrorType)typeMirror)).toString();
   122                 if (!str1.equals("FooBar")) {
   123                     throw new AssertionError("Types.asElement() error!");
   124                 }
   125                 foundError = true;
   126               }
   127             }
   130             return null;
   131         }
   133     }
   134 }

mercurial