langtools/test/tools/javac/parser/netbeans/JavacParserTest.java
changeset 11303 5f48992867e6
parent 11302 a6305295d4d9
parent 11239 885050364691
child 11304 5d3d2bd1dfd1
equal deleted inserted replaced
11302:a6305295d4d9 11303:5f48992867e6
     1 /*
       
     2  * Copyright (c) 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  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 7073631
       
    27  * @summary tests error and diagnostics positions
       
    28  * @author  jan.lahoda@oracle.com
       
    29  */
       
    30 
       
    31 import com.sun.source.tree.BinaryTree;
       
    32 import com.sun.source.tree.BlockTree;
       
    33 import com.sun.source.tree.ClassTree;
       
    34 import com.sun.source.tree.CompilationUnitTree;
       
    35 import com.sun.source.tree.ExpressionStatementTree;
       
    36 import com.sun.source.tree.ExpressionTree;
       
    37 import com.sun.source.tree.MethodInvocationTree;
       
    38 import com.sun.source.tree.MethodTree;
       
    39 import com.sun.source.tree.ModifiersTree;
       
    40 import com.sun.source.tree.StatementTree;
       
    41 import com.sun.source.tree.Tree;
       
    42 import com.sun.source.tree.Tree.Kind;
       
    43 import com.sun.source.tree.VariableTree;
       
    44 import com.sun.source.tree.WhileLoopTree;
       
    45 import com.sun.source.util.SourcePositions;
       
    46 import com.sun.source.util.TreeScanner;
       
    47 import com.sun.source.util.Trees;
       
    48 import com.sun.tools.javac.api.JavacTaskImpl;
       
    49 import com.sun.tools.javac.tree.JCTree;
       
    50 import java.io.IOException;
       
    51 import java.net.URI;
       
    52 import java.util.Arrays;
       
    53 import java.util.LinkedList;
       
    54 import java.util.List;
       
    55 import javax.tools.Diagnostic;
       
    56 import javax.tools.DiagnosticCollector;
       
    57 import javax.tools.DiagnosticListener;
       
    58 import javax.tools.JavaCompiler;
       
    59 import javax.tools.JavaFileObject;
       
    60 import javax.tools.SimpleJavaFileObject;
       
    61 import javax.tools.ToolProvider;
       
    62 
       
    63 public class JavacParserTest extends TestCase {
       
    64     final JavaCompiler tool;
       
    65     public JavacParserTest(String testName) {
       
    66         tool = ToolProvider.getSystemJavaCompiler();
       
    67         System.out.println("java.home=" + System.getProperty("java.home"));
       
    68     }
       
    69 
       
    70     static class MyFileObject extends SimpleJavaFileObject {
       
    71 
       
    72         private String text;
       
    73 
       
    74         public MyFileObject(String text) {
       
    75             super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
       
    76             this.text = text;
       
    77         }
       
    78 
       
    79         @Override
       
    80         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
       
    81             return text;
       
    82         }
       
    83     }
       
    84 
       
    85     public void testPositionForSuperConstructorCalls() throws IOException {
       
    86         assert tool != null;
       
    87 
       
    88         String code = "package test; public class Test {public Test() {super();}}";
       
    89 
       
    90         JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
       
    91                 null, Arrays.asList(new MyFileObject(code)));
       
    92         CompilationUnitTree cut = ct.parse().iterator().next();
       
    93         SourcePositions pos = Trees.instance(ct).getSourcePositions();
       
    94 
       
    95         MethodTree method =
       
    96                 (MethodTree) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);
       
    97         ExpressionStatementTree es =
       
    98                 (ExpressionStatementTree) method.getBody().getStatements().get(0);
       
    99 
       
   100         assertEquals("testPositionForSuperConstructorCalls",
       
   101                 72 - 24, pos.getStartPosition(cut, es));
       
   102         assertEquals("testPositionForSuperConstructorCalls",
       
   103                 80 - 24, pos.getEndPosition(cut, es));
       
   104 
       
   105         MethodInvocationTree mit = (MethodInvocationTree) es.getExpression();
       
   106 
       
   107         assertEquals("testPositionForSuperConstructorCalls",
       
   108                 72 - 24, pos.getStartPosition(cut, mit));
       
   109         assertEquals("testPositionForSuperConstructorCalls",
       
   110                 79 - 24, pos.getEndPosition(cut, mit));
       
   111 
       
   112         assertEquals("testPositionForSuperConstructorCalls",
       
   113                 72 - 24, pos.getStartPosition(cut, mit.getMethodSelect()));
       
   114         assertEquals("testPositionForSuperConstructorCalls",
       
   115                 77 - 24, pos.getEndPosition(cut, mit.getMethodSelect()));
       
   116 
       
   117     }
       
   118 
       
   119     public void testPositionForEnumModifiers() throws IOException {
       
   120 
       
   121         String code = "package test; public enum Test {A;}";
       
   122 
       
   123         JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
       
   124                 null, Arrays.asList(new MyFileObject(code)));
       
   125         CompilationUnitTree cut = ct.parse().iterator().next();
       
   126         SourcePositions pos = Trees.instance(ct).getSourcePositions();
       
   127 
       
   128         ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
       
   129         ModifiersTree mt = clazz.getModifiers();
       
   130 
       
   131         assertEquals("testPositionForEnumModifiers",
       
   132                 38 - 24, pos.getStartPosition(cut, mt));
       
   133         assertEquals("testPositionForEnumModifiers",
       
   134                 44 - 24, pos.getEndPosition(cut, mt));
       
   135     }
       
   136 
       
   137     public void testNewClassWithEnclosing() throws IOException {
       
   138 
       
   139 
       
   140         String code = "package test; class Test { " +
       
   141                 "class d {} private void method() { " +
       
   142                 "Object o = Test.this.new d(); } }";
       
   143 
       
   144         JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
       
   145                 null, Arrays.asList(new MyFileObject(code)));
       
   146         CompilationUnitTree cut = ct.parse().iterator().next();
       
   147         SourcePositions pos = Trees.instance(ct).getSourcePositions();
       
   148 
       
   149         ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
       
   150         ExpressionTree est =
       
   151                 ((VariableTree) ((MethodTree) clazz.getMembers().get(1)).getBody().getStatements().get(0)).getInitializer();
       
   152 
       
   153         assertEquals("testNewClassWithEnclosing",
       
   154                 97 - 24, pos.getStartPosition(cut, est));
       
   155         assertEquals("testNewClassWithEnclosing",
       
   156                 114 - 24, pos.getEndPosition(cut, est));
       
   157     }
       
   158 
       
   159     public void testPreferredPositionForBinaryOp() throws IOException {
       
   160 
       
   161         String code = "package test; public class Test {" +
       
   162                 "private void test() {" +
       
   163                 "Object o = null; boolean b = o != null && o instanceof String;" +
       
   164                 "} private Test() {}}";
       
   165 
       
   166         JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
       
   167                 null, Arrays.asList(new MyFileObject(code)));
       
   168         CompilationUnitTree cut = ct.parse().iterator().next();
       
   169 
       
   170         ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
       
   171         MethodTree method = (MethodTree) clazz.getMembers().get(0);
       
   172         VariableTree condSt = (VariableTree) method.getBody().getStatements().get(1);
       
   173         BinaryTree cond = (BinaryTree) condSt.getInitializer();
       
   174 
       
   175         JCTree condJC = (JCTree) cond;
       
   176 
       
   177         assertEquals("testNewClassWithEnclosing",
       
   178                 117 - 24, condJC.pos);
       
   179     }
       
   180 
       
   181     public void testPositionBrokenSource126732a() throws IOException {
       
   182         String[] commands = new String[]{
       
   183             "return Runnable()",
       
   184             "do { } while (true)",
       
   185             "throw UnsupportedOperationException()",
       
   186             "assert true",
       
   187             "1 + 1",};
       
   188 
       
   189         for (String command : commands) {
       
   190 
       
   191             String code = "package test;\n"
       
   192                     + "public class Test {\n"
       
   193                     + "    public static void test() {\n"
       
   194                     + "        " + command + " {\n"
       
   195                     + "                new Runnable() {\n"
       
   196                     + "        };\n"
       
   197                     + "    }\n"
       
   198                     + "}";
       
   199             JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null,
       
   200                     null, null, Arrays.asList(new MyFileObject(code)));
       
   201             CompilationUnitTree cut = ct.parse().iterator().next();
       
   202 
       
   203             ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
       
   204             MethodTree method = (MethodTree) clazz.getMembers().get(0);
       
   205             List<? extends StatementTree> statements =
       
   206                     method.getBody().getStatements();
       
   207 
       
   208             StatementTree ret = statements.get(0);
       
   209             StatementTree block = statements.get(1);
       
   210 
       
   211             Trees t = Trees.instance(ct);
       
   212             int len = code.indexOf(command + " {") + (command + " ").length();
       
   213             assertEquals(command, len,
       
   214                     t.getSourcePositions().getEndPosition(cut, ret));
       
   215             assertEquals(command, len,
       
   216                     t.getSourcePositions().getStartPosition(cut, block));
       
   217         }
       
   218     }
       
   219 
       
   220     public void testPositionBrokenSource126732b() throws IOException {
       
   221         String[] commands = new String[]{
       
   222             "break",
       
   223             "break A",
       
   224             "continue ",
       
   225             "continue A",};
       
   226 
       
   227         for (String command : commands) {
       
   228 
       
   229             String code = "package test;\n"
       
   230                     + "public class Test {\n"
       
   231                     + "    public static void test() {\n"
       
   232                     + "        while (true) {\n"
       
   233                     + "            " + command + " {\n"
       
   234                     + "                new Runnable() {\n"
       
   235                     + "        };\n"
       
   236                     + "        }\n"
       
   237                     + "    }\n"
       
   238                     + "}";
       
   239 
       
   240             JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null,
       
   241                     null, null, Arrays.asList(new MyFileObject(code)));
       
   242             CompilationUnitTree cut = ct.parse().iterator().next();
       
   243 
       
   244             ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
       
   245             MethodTree method = (MethodTree) clazz.getMembers().get(0);
       
   246             List<? extends StatementTree> statements =
       
   247                     ((BlockTree) ((WhileLoopTree) method.getBody().getStatements().get(0)).getStatement()).getStatements();
       
   248 
       
   249             StatementTree ret = statements.get(0);
       
   250             StatementTree block = statements.get(1);
       
   251 
       
   252             Trees t = Trees.instance(ct);
       
   253             int len = code.indexOf(command + " {") + (command + " ").length();
       
   254             assertEquals(command, len,
       
   255                     t.getSourcePositions().getEndPosition(cut, ret));
       
   256             assertEquals(command, len,
       
   257                     t.getSourcePositions().getStartPosition(cut, block));
       
   258         }
       
   259     }
       
   260 
       
   261     public void testErrorRecoveryForEnhancedForLoop142381() throws IOException {
       
   262 
       
   263         String code = "package test; class Test { " +
       
   264                 "private void method() { " +
       
   265                 "java.util.Set<String> s = null; for (a : s) {} } }";
       
   266 
       
   267         final List<Diagnostic<? extends JavaFileObject>> errors =
       
   268                 new LinkedList<Diagnostic<? extends JavaFileObject>>();
       
   269 
       
   270         JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null,
       
   271                 new DiagnosticListener<JavaFileObject>() {
       
   272             public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
       
   273                 errors.add(diagnostic);
       
   274             }
       
   275         }, null, null, Arrays.asList(new MyFileObject(code)));
       
   276 
       
   277         CompilationUnitTree cut = ct.parse().iterator().next();
       
   278 
       
   279         ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
       
   280         StatementTree forStatement =
       
   281                 ((MethodTree) clazz.getMembers().get(0)).getBody().getStatements().get(1);
       
   282 
       
   283         assertEquals("testErrorRecoveryForEnhancedForLoop142381",
       
   284                 Kind.ENHANCED_FOR_LOOP, forStatement.getKind());
       
   285         assertFalse("testErrorRecoveryForEnhancedForLoop142381", errors.isEmpty());
       
   286     }
       
   287 
       
   288     public void testPositionAnnotationNoPackage187551() throws IOException {
       
   289 
       
   290         String code = "\n@interface Test {}";
       
   291 
       
   292         JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
       
   293                 null, Arrays.asList(new MyFileObject(code)));
       
   294 
       
   295         CompilationUnitTree cut = ct.parse().iterator().next();
       
   296         ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
       
   297         Trees t = Trees.instance(ct);
       
   298 
       
   299         assertEquals("testPositionAnnotationNoPackage187551",
       
   300                 1, t.getSourcePositions().getStartPosition(cut, clazz));
       
   301     }
       
   302 
       
   303     public void testPositionsSane() throws IOException {
       
   304         performPositionsSanityTest("package test; class Test { " +
       
   305                 "private void method() { " +
       
   306                 "java.util.List<? extends java.util.List<? extends String>> l; " +
       
   307                 "} }");
       
   308         performPositionsSanityTest("package test; class Test { " +
       
   309                 "private void method() { " +
       
   310                 "java.util.List<? super java.util.List<? super String>> l; " +
       
   311                 "} }");
       
   312         performPositionsSanityTest("package test; class Test { " +
       
   313                 "private void method() { " +
       
   314                 "java.util.List<? super java.util.List<?>> l; } }");
       
   315     }
       
   316 
       
   317     private void performPositionsSanityTest(String code) throws IOException {
       
   318 
       
   319         final List<Diagnostic<? extends JavaFileObject>> errors =
       
   320                 new LinkedList<Diagnostic<? extends JavaFileObject>>();
       
   321 
       
   322         JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null,
       
   323                 new DiagnosticListener<JavaFileObject>() {
       
   324 
       
   325             public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
       
   326                 errors.add(diagnostic);
       
   327             }
       
   328         }, null, null, Arrays.asList(new MyFileObject(code)));
       
   329 
       
   330         final CompilationUnitTree cut = ct.parse().iterator().next();
       
   331         final Trees trees = Trees.instance(ct);
       
   332 
       
   333         new TreeScanner<Void, Void>() {
       
   334 
       
   335             private long parentStart = 0;
       
   336             private long parentEnd = Integer.MAX_VALUE;
       
   337 
       
   338             @Override
       
   339             public Void scan(Tree node, Void p) {
       
   340                 if (node == null) {
       
   341                     return null;
       
   342                 }
       
   343 
       
   344                 long start = trees.getSourcePositions().getStartPosition(cut, node);
       
   345 
       
   346                 if (start == (-1)) {
       
   347                     return null; //synthetic tree
       
   348                 }
       
   349                 assertTrue(node.toString() + ":" + start + "/" + parentStart,
       
   350                         parentStart <= start);
       
   351 
       
   352                 long prevParentStart = parentStart;
       
   353 
       
   354                 parentStart = start;
       
   355 
       
   356                 long end = trees.getSourcePositions().getEndPosition(cut, node);
       
   357 
       
   358                 assertTrue(node.toString() + ":" + end + "/" + parentEnd,
       
   359                         end <= parentEnd);
       
   360 
       
   361                 long prevParentEnd = parentEnd;
       
   362 
       
   363                 parentEnd = end;
       
   364 
       
   365                 super.scan(node, p);
       
   366 
       
   367                 parentStart = prevParentStart;
       
   368                 parentEnd = prevParentEnd;
       
   369 
       
   370                 return null;
       
   371             }
       
   372 
       
   373             private void assertTrue(String message, boolean b) {
       
   374                 if (!b) fail(message);
       
   375             }
       
   376         }.scan(cut, null);
       
   377     }
       
   378 
       
   379     public void testCorrectWilcardPositions() throws IOException {
       
   380         performWildcardPositionsTest("package test; import java.util.List; " +
       
   381                 "class Test { private void method() { List<? extends List<? extends String>> l; } }",
       
   382 
       
   383                 Arrays.asList("List<? extends List<? extends String>> l;",
       
   384                 "List<? extends List<? extends String>>",
       
   385                 "List",
       
   386                 "? extends List<? extends String>",
       
   387                 "List<? extends String>",
       
   388                 "List",
       
   389                 "? extends String",
       
   390                 "String"));
       
   391         performWildcardPositionsTest("package test; import java.util.List; " +
       
   392                 "class Test { private void method() { List<? super List<? super String>> l; } }",
       
   393 
       
   394                 Arrays.asList("List<? super List<? super String>> l;",
       
   395                 "List<? super List<? super String>>",
       
   396                 "List",
       
   397                 "? super List<? super String>",
       
   398                 "List<? super String>",
       
   399                 "List",
       
   400                 "? super String",
       
   401                 "String"));
       
   402         performWildcardPositionsTest("package test; import java.util.List; " +
       
   403                 "class Test { private void method() { List<? super List<?>> l; } }",
       
   404 
       
   405                 Arrays.asList("List<? super List<?>> l;",
       
   406                 "List<? super List<?>>",
       
   407                 "List",
       
   408                 "? super List<?>",
       
   409                 "List<?>",
       
   410                 "List",
       
   411                 "?"));
       
   412         performWildcardPositionsTest("package test; import java.util.List; " +
       
   413                 "class Test { private void method() { " +
       
   414                 "List<? extends List<? extends List<? extends String>>> l; } }",
       
   415 
       
   416                 Arrays.asList("List<? extends List<? extends List<? extends String>>> l;",
       
   417                 "List<? extends List<? extends List<? extends String>>>",
       
   418                 "List",
       
   419                 "? extends List<? extends List<? extends String>>",
       
   420                 "List<? extends List<? extends String>>",
       
   421                 "List",
       
   422                 "? extends List<? extends String>",
       
   423                 "List<? extends String>",
       
   424                 "List",
       
   425                 "? extends String",
       
   426                 "String"));
       
   427         performWildcardPositionsTest("package test; import java.util.List; " +
       
   428                 "class Test { private void method() { " +
       
   429                 "List<? extends List<? extends List<? extends String   >>> l; } }",
       
   430                 Arrays.asList("List<? extends List<? extends List<? extends String   >>> l;",
       
   431                 "List<? extends List<? extends List<? extends String   >>>",
       
   432                 "List",
       
   433                 "? extends List<? extends List<? extends String   >>",
       
   434                 "List<? extends List<? extends String   >>",
       
   435                 "List",
       
   436                 "? extends List<? extends String   >",
       
   437                 "List<? extends String   >",
       
   438                 "List",
       
   439                 "? extends String",
       
   440                 "String"));
       
   441     }
       
   442 
       
   443     public void performWildcardPositionsTest(final String code,
       
   444             List<String> golden) throws IOException {
       
   445 
       
   446         final List<Diagnostic<? extends JavaFileObject>> errors =
       
   447                 new LinkedList<Diagnostic<? extends JavaFileObject>>();
       
   448 
       
   449         JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null,
       
   450                 new DiagnosticListener<JavaFileObject>() {
       
   451                     public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
       
   452                         errors.add(diagnostic);
       
   453                     }
       
   454                 }, null, null, Arrays.asList(new MyFileObject(code)));
       
   455 
       
   456         final CompilationUnitTree cut = ct.parse().iterator().next();
       
   457         final List<String> content = new LinkedList<String>();
       
   458         final Trees trees = Trees.instance(ct);
       
   459 
       
   460         new TreeScanner<Void, Void>() {
       
   461             @Override
       
   462             public Void scan(Tree node, Void p) {
       
   463                 if (node == null) {
       
   464                     return null;
       
   465                 }
       
   466                 long start = trees.getSourcePositions().getStartPosition(cut, node);
       
   467 
       
   468                 if (start == (-1)) {
       
   469                     return null; //synthetic tree
       
   470                 }
       
   471                 long end = trees.getSourcePositions().getEndPosition(cut, node);
       
   472                 String s = code.substring((int) start, (int) end);
       
   473                 content.add(s);
       
   474 
       
   475                 return super.scan(node, p);
       
   476             }
       
   477         }.scan(((MethodTree) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0)).getBody().getStatements().get(0), null);
       
   478 
       
   479         assertEquals("performWildcardPositionsTest",golden.toString(),
       
   480                 content.toString());
       
   481     }
       
   482 
       
   483     public void testStartPositionForMethodWithoutModifiers() throws IOException {
       
   484 
       
   485         String code = "package t; class Test { <T> void t() {} }";
       
   486 
       
   487         JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
       
   488                 null, Arrays.asList(new MyFileObject(code)));
       
   489         CompilationUnitTree cut = ct.parse().iterator().next();
       
   490         ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
       
   491         MethodTree mt = (MethodTree) clazz.getMembers().get(0);
       
   492         Trees t = Trees.instance(ct);
       
   493         int start = (int) t.getSourcePositions().getStartPosition(cut, mt);
       
   494         int end = (int) t.getSourcePositions().getEndPosition(cut, mt);
       
   495 
       
   496         assertEquals("testStartPositionForMethodWithoutModifiers",
       
   497                 "<T> void t() {}", code.substring(start, end));
       
   498     }
       
   499 
       
   500     public void testStartPositionEnumConstantInit() throws IOException {
       
   501 
       
   502         String code = "package t; enum Test { AAA; }";
       
   503 
       
   504         JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
       
   505                 null, Arrays.asList(new MyFileObject(code)));
       
   506         CompilationUnitTree cut = ct.parse().iterator().next();
       
   507         ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
       
   508         VariableTree enumAAA = (VariableTree) clazz.getMembers().get(0);
       
   509         Trees t = Trees.instance(ct);
       
   510         int start = (int) t.getSourcePositions().getStartPosition(cut,
       
   511                 enumAAA.getInitializer());
       
   512 
       
   513         assertEquals("testStartPositionEnumConstantInit", -1, start);
       
   514     }
       
   515 
       
   516     public void testVariableInIfThen1() throws IOException {
       
   517 
       
   518         String code = "package t; class Test { " +
       
   519                 "private static void t(String name) { " +
       
   520                 "if (name != null) String nn = name.trim(); } }";
       
   521 
       
   522         DiagnosticCollector<JavaFileObject> coll =
       
   523                 new DiagnosticCollector<JavaFileObject>();
       
   524 
       
   525         JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null,
       
   526                 null, Arrays.asList(new MyFileObject(code)));
       
   527 
       
   528         ct.parse();
       
   529 
       
   530         List<String> codes = new LinkedList<String>();
       
   531 
       
   532         for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
       
   533             codes.add(d.getCode());
       
   534         }
       
   535 
       
   536         assertEquals("testVariableInIfThen1",
       
   537                 Arrays.<String>asList("compiler.err.variable.not.allowed"),
       
   538                 codes);
       
   539     }
       
   540 
       
   541     public void testVariableInIfThen2() throws IOException {
       
   542 
       
   543         String code = "package t; class Test { " +
       
   544                 "private static void t(String name) { " +
       
   545                 "if (name != null) class X {} } }";
       
   546         DiagnosticCollector<JavaFileObject> coll =
       
   547                 new DiagnosticCollector<JavaFileObject>();
       
   548         JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null,
       
   549                 null, Arrays.asList(new MyFileObject(code)));
       
   550 
       
   551         ct.parse();
       
   552 
       
   553         List<String> codes = new LinkedList<String>();
       
   554 
       
   555         for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
       
   556             codes.add(d.getCode());
       
   557         }
       
   558 
       
   559         assertEquals("testVariableInIfThen2",
       
   560                 Arrays.<String>asList("compiler.err.class.not.allowed"), codes);
       
   561     }
       
   562 
       
   563     public void testVariableInIfThen3() throws IOException {
       
   564 
       
   565         String code = "package t; class Test { "+
       
   566                 "private static void t(String name) { " +
       
   567                 "if (name != null) abstract } }";
       
   568         DiagnosticCollector<JavaFileObject> coll =
       
   569                 new DiagnosticCollector<JavaFileObject>();
       
   570         JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null,
       
   571                 null, Arrays.asList(new MyFileObject(code)));
       
   572 
       
   573         ct.parse();
       
   574 
       
   575         List<String> codes = new LinkedList<String>();
       
   576 
       
   577         for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
       
   578             codes.add(d.getCode());
       
   579         }
       
   580 
       
   581         assertEquals("testVariableInIfThen3",
       
   582                 Arrays.<String>asList("compiler.err.illegal.start.of.expr"),
       
   583                 codes);
       
   584     }
       
   585 
       
   586     //see javac bug #6882235, NB bug #98234:
       
   587     public void testMissingExponent() throws IOException {
       
   588 
       
   589         String code = "\nclass Test { { System.err.println(0e); } }";
       
   590 
       
   591         JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
       
   592                 null, Arrays.asList(new MyFileObject(code)));
       
   593 
       
   594         assertNotNull(ct.parse().iterator().next());
       
   595     }
       
   596 
       
   597     public void testTryResourcePos() throws IOException {
       
   598 
       
   599         final String code = "package t; class Test { " +
       
   600                 "{ try (java.io.InputStream in = null) { } } }";
       
   601 
       
   602         JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
       
   603                 null, Arrays.asList(new MyFileObject(code)));
       
   604         CompilationUnitTree cut = ct.parse().iterator().next();
       
   605 
       
   606         new TreeScanner<Void, Void>() {
       
   607             @Override
       
   608             public Void visitVariable(VariableTree node, Void p) {
       
   609                 if ("in".contentEquals(node.getName())) {
       
   610                     JCTree.JCVariableDecl var = (JCTree.JCVariableDecl) node;
       
   611                     System.out.println(node.getName() + "," + var.pos);
       
   612                     assertEquals("testTryResourcePos", "in = null) { } } }",
       
   613                             code.substring(var.pos));
       
   614                 }
       
   615                 return super.visitVariable(node, p);
       
   616             }
       
   617         }.scan(cut, null);
       
   618     }
       
   619 
       
   620     public void testVarPos() throws IOException {
       
   621 
       
   622         final String code = "package t; class Test { " +
       
   623                 "{ java.io.InputStream in = null; } }";
       
   624 
       
   625         JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
       
   626                 null, Arrays.asList(new MyFileObject(code)));
       
   627         CompilationUnitTree cut = ct.parse().iterator().next();
       
   628 
       
   629         new TreeScanner<Void, Void>() {
       
   630 
       
   631             @Override
       
   632             public Void visitVariable(VariableTree node, Void p) {
       
   633                 if ("in".contentEquals(node.getName())) {
       
   634                     JCTree.JCVariableDecl var = (JCTree.JCVariableDecl) node;
       
   635                     assertEquals("testVarPos","in = null; } }",
       
   636                             code.substring(var.pos));
       
   637                 }
       
   638                 return super.visitVariable(node, p);
       
   639             }
       
   640         }.scan(cut, null);
       
   641     }
       
   642 
       
   643     void testsNotWorking() throws IOException {
       
   644 
       
   645         // Fails with nb-javac, needs further investigation
       
   646         testPositionBrokenSource126732a();
       
   647         testPositionBrokenSource126732b();
       
   648 
       
   649         // Fails, these tests yet to be addressed
       
   650         testVariableInIfThen1();
       
   651         testVariableInIfThen2();
       
   652         testPositionForEnumModifiers();
       
   653         testStartPositionEnumConstantInit();
       
   654     }
       
   655     void testPositions() throws IOException {
       
   656         testPositionsSane();
       
   657         testCorrectWilcardPositions();
       
   658         testPositionAnnotationNoPackage187551();
       
   659         testPositionForSuperConstructorCalls();
       
   660         testPreferredPositionForBinaryOp();
       
   661         testStartPositionForMethodWithoutModifiers();
       
   662         testVarPos();
       
   663         testVariableInIfThen3();
       
   664         testTryResourcePos();
       
   665     }
       
   666 
       
   667     public static void main(String... args) throws IOException {
       
   668         JavacParserTest jpt = new JavacParserTest("JavacParserTest");
       
   669         jpt.testPositions();
       
   670         System.out.println("PASS");
       
   671     }
       
   672 }
       
   673 
       
   674 abstract class TestCase {
       
   675 
       
   676     void assertEquals(String message, int i, int pos) {
       
   677         if (i != pos) {
       
   678             fail(message);
       
   679         }
       
   680     }
       
   681 
       
   682     void assertFalse(String message, boolean empty) {
       
   683         throw new UnsupportedOperationException("Not yet implemented");
       
   684     }
       
   685 
       
   686     void assertEquals(String message, int i, long l) {
       
   687         if (i != l) {
       
   688             fail(message + ":" + i + ":" + l);
       
   689         }
       
   690     }
       
   691 
       
   692     void assertEquals(String message, Object o1, Object o2) {
       
   693         System.out.println(o1);
       
   694         System.out.println(o2);
       
   695         if (o1 != null && o2 != null && !o1.equals(o2)) {
       
   696             fail(message);
       
   697         }
       
   698         if (o1 == null && o2 != null) {
       
   699             fail(message);
       
   700         }
       
   701     }
       
   702 
       
   703     void assertNotNull(Object o) {
       
   704         if (o == null) {
       
   705             fail();
       
   706         }
       
   707     }
       
   708 
       
   709     void fail() {
       
   710         fail("test failed");
       
   711     }
       
   712 
       
   713     void fail(String message) {
       
   714         throw new RuntimeException(message);
       
   715     }
       
   716 }