test/tools/javac/parser/netbeans/JavacParserTest.java

Mon, 14 Nov 2011 15:11:10 -0800

author
ksrini
date
Mon, 14 Nov 2011 15:11:10 -0800
changeset 1138
7375d4979bd3
parent 1074
04f983e3e825
child 1147
abfa0d8ea803
permissions
-rw-r--r--

7106166: (javac) re-factor EndPos parser
Reviewed-by: jjg

     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  */
    24 /*
    25  * @test
    26  * @bug 7073631
    27  * @summary tests error and diagnostics positions
    28  * @author  jan.lahoda@oracle.com
    29  */
    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;
    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     }
    70     static class MyFileObject extends SimpleJavaFileObject {
    72         private String text;
    74         public MyFileObject(String text) {
    75             super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
    76             this.text = text;
    77         }
    79         @Override
    80         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
    81             return text;
    82         }
    83     }
    85     public void testPositionForSuperConstructorCalls() throws IOException {
    86         assert tool != null;
    88         String code = "package test; public class Test {public Test() {super();}}";
    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();
    95         MethodTree method =
    96                 (MethodTree) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);
    97         ExpressionStatementTree es =
    98                 (ExpressionStatementTree) method.getBody().getStatements().get(0);
   100         assertEquals("testPositionForSuperConstructorCalls",
   101                 72 - 24, pos.getStartPosition(cut, es));
   102         assertEquals("testPositionForSuperConstructorCalls",
   103                 80 - 24, pos.getEndPosition(cut, es));
   105         MethodInvocationTree mit = (MethodInvocationTree) es.getExpression();
   107         assertEquals("testPositionForSuperConstructorCalls",
   108                 72 - 24, pos.getStartPosition(cut, mit));
   109         assertEquals("testPositionForSuperConstructorCalls",
   110                 79 - 24, pos.getEndPosition(cut, mit));
   112         assertEquals("testPositionForSuperConstructorCalls",
   113                 72 - 24, pos.getStartPosition(cut, mit.getMethodSelect()));
   114         assertEquals("testPositionForSuperConstructorCalls",
   115                 77 - 24, pos.getEndPosition(cut, mit.getMethodSelect()));
   117     }
   119     public void testPositionForEnumModifiers() throws IOException {
   121         String code = "package test; public enum Test {A;}";
   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();
   128         ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
   129         ModifiersTree mt = clazz.getModifiers();
   131         assertEquals("testPositionForEnumModifiers",
   132                 38 - 24, pos.getStartPosition(cut, mt));
   133         assertEquals("testPositionForEnumModifiers",
   134                 44 - 24, pos.getEndPosition(cut, mt));
   135     }
   137     public void testNewClassWithEnclosing() throws IOException {
   140         String code = "package test; class Test { " +
   141                 "class d {} private void method() { " +
   142                 "Object o = Test.this.new d(); } }";
   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();
   149         ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
   150         ExpressionTree est =
   151                 ((VariableTree) ((MethodTree) clazz.getMembers().get(1)).getBody().getStatements().get(0)).getInitializer();
   153         assertEquals("testNewClassWithEnclosing",
   154                 97 - 24, pos.getStartPosition(cut, est));
   155         assertEquals("testNewClassWithEnclosing",
   156                 114 - 24, pos.getEndPosition(cut, est));
   157     }
   159     public void testPreferredPositionForBinaryOp() throws IOException {
   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() {}}";
   166         JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
   167                 null, Arrays.asList(new MyFileObject(code)));
   168         CompilationUnitTree cut = ct.parse().iterator().next();
   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();
   175         JCTree condJC = (JCTree) cond;
   177         assertEquals("testNewClassWithEnclosing",
   178                 117 - 24, condJC.pos);
   179     }
   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",};
   189         for (String command : commands) {
   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();
   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();
   208             StatementTree ret = statements.get(0);
   209             StatementTree block = statements.get(1);
   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     }
   220     public void testPositionBrokenSource126732b() throws IOException {
   221         String[] commands = new String[]{
   222             "break",
   223             "break A",
   224             "continue ",
   225             "continue A",};
   227         for (String command : commands) {
   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                     + "}";
   240             JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null,
   241                     null, null, Arrays.asList(new MyFileObject(code)));
   242             CompilationUnitTree cut = ct.parse().iterator().next();
   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();
   249             StatementTree ret = statements.get(0);
   250             StatementTree block = statements.get(1);
   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     }
   261     public void testErrorRecoveryForEnhancedForLoop142381() throws IOException {
   263         String code = "package test; class Test { " +
   264                 "private void method() { " +
   265                 "java.util.Set<String> s = null; for (a : s) {} } }";
   267         final List<Diagnostic<? extends JavaFileObject>> errors =
   268                 new LinkedList<Diagnostic<? extends JavaFileObject>>();
   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)));
   277         CompilationUnitTree cut = ct.parse().iterator().next();
   279         ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
   280         StatementTree forStatement =
   281                 ((MethodTree) clazz.getMembers().get(0)).getBody().getStatements().get(1);
   283         assertEquals("testErrorRecoveryForEnhancedForLoop142381",
   284                 Kind.ENHANCED_FOR_LOOP, forStatement.getKind());
   285         assertFalse("testErrorRecoveryForEnhancedForLoop142381", errors.isEmpty());
   286     }
   288     public void testPositionAnnotationNoPackage187551() throws IOException {
   290         String code = "\n@interface Test {}";
   292         JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
   293                 null, Arrays.asList(new MyFileObject(code)));
   295         CompilationUnitTree cut = ct.parse().iterator().next();
   296         ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
   297         Trees t = Trees.instance(ct);
   299         assertEquals("testPositionAnnotationNoPackage187551",
   300                 1, t.getSourcePositions().getStartPosition(cut, clazz));
   301     }
   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     }
   317     private void performPositionsSanityTest(String code) throws IOException {
   319         final List<Diagnostic<? extends JavaFileObject>> errors =
   320                 new LinkedList<Diagnostic<? extends JavaFileObject>>();
   322         JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null,
   323                 new DiagnosticListener<JavaFileObject>() {
   325             public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   326                 errors.add(diagnostic);
   327             }
   328         }, null, null, Arrays.asList(new MyFileObject(code)));
   330         final CompilationUnitTree cut = ct.parse().iterator().next();
   331         final Trees trees = Trees.instance(ct);
   333         new TreeScanner<Void, Void>() {
   335             private long parentStart = 0;
   336             private long parentEnd = Integer.MAX_VALUE;
   338             @Override
   339             public Void scan(Tree node, Void p) {
   340                 if (node == null) {
   341                     return null;
   342                 }
   344                 long start = trees.getSourcePositions().getStartPosition(cut, node);
   346                 if (start == (-1)) {
   347                     return null; //synthetic tree
   348                 }
   349                 assertTrue(node.toString() + ":" + start + "/" + parentStart,
   350                         parentStart <= start);
   352                 long prevParentStart = parentStart;
   354                 parentStart = start;
   356                 long end = trees.getSourcePositions().getEndPosition(cut, node);
   358                 assertTrue(node.toString() + ":" + end + "/" + parentEnd,
   359                         end <= parentEnd);
   361                 long prevParentEnd = parentEnd;
   363                 parentEnd = end;
   365                 super.scan(node, p);
   367                 parentStart = prevParentStart;
   368                 parentEnd = prevParentEnd;
   370                 return null;
   371             }
   373             private void assertTrue(String message, boolean b) {
   374                 if (!b) fail(message);
   375             }
   376         }.scan(cut, null);
   377     }
   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; } }",
   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; } }",
   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; } }",
   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; } }",
   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     }
   443     public void performWildcardPositionsTest(final String code,
   444             List<String> golden) throws IOException {
   446         final List<Diagnostic<? extends JavaFileObject>> errors =
   447                 new LinkedList<Diagnostic<? extends JavaFileObject>>();
   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)));
   456         final CompilationUnitTree cut = ct.parse().iterator().next();
   457         final List<String> content = new LinkedList<String>();
   458         final Trees trees = Trees.instance(ct);
   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);
   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);
   475                 return super.scan(node, p);
   476             }
   477         }.scan(((MethodTree) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0)).getBody().getStatements().get(0), null);
   479         assertEquals("performWildcardPositionsTest",golden.toString(),
   480                 content.toString());
   481     }
   483     public void testStartPositionForMethodWithoutModifiers() throws IOException {
   485         String code = "package t; class Test { <T> void t() {} }";
   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);
   496         assertEquals("testStartPositionForMethodWithoutModifiers",
   497                 "<T> void t() {}", code.substring(start, end));
   498     }
   500     public void testStartPositionEnumConstantInit() throws IOException {
   502         String code = "package t; enum Test { AAA; }";
   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());
   513         assertEquals("testStartPositionEnumConstantInit", -1, start);
   514     }
   516     public void testVariableInIfThen1() throws IOException {
   518         String code = "package t; class Test { " +
   519                 "private static void t(String name) { " +
   520                 "if (name != null) String nn = name.trim(); } }";
   522         DiagnosticCollector<JavaFileObject> coll =
   523                 new DiagnosticCollector<JavaFileObject>();
   525         JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null,
   526                 null, Arrays.asList(new MyFileObject(code)));
   528         ct.parse();
   530         List<String> codes = new LinkedList<String>();
   532         for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
   533             codes.add(d.getCode());
   534         }
   536         assertEquals("testVariableInIfThen1",
   537                 Arrays.<String>asList("compiler.err.variable.not.allowed"),
   538                 codes);
   539     }
   541     public void testVariableInIfThen2() throws IOException {
   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)));
   551         ct.parse();
   553         List<String> codes = new LinkedList<String>();
   555         for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
   556             codes.add(d.getCode());
   557         }
   559         assertEquals("testVariableInIfThen2",
   560                 Arrays.<String>asList("compiler.err.class.not.allowed"), codes);
   561     }
   563     public void testVariableInIfThen3() throws IOException {
   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)));
   573         ct.parse();
   575         List<String> codes = new LinkedList<String>();
   577         for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) {
   578             codes.add(d.getCode());
   579         }
   581         assertEquals("testVariableInIfThen3",
   582                 Arrays.<String>asList("compiler.err.illegal.start.of.expr"),
   583                 codes);
   584     }
   586     //see javac bug #6882235, NB bug #98234:
   587     public void testMissingExponent() throws IOException {
   589         String code = "\nclass Test { { System.err.println(0e); } }";
   591         JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
   592                 null, Arrays.asList(new MyFileObject(code)));
   594         assertNotNull(ct.parse().iterator().next());
   595     }
   597     public void testTryResourcePos() throws IOException {
   599         final String code = "package t; class Test { " +
   600                 "{ try (java.io.InputStream in = null) { } } }";
   602         JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
   603                 null, Arrays.asList(new MyFileObject(code)));
   604         CompilationUnitTree cut = ct.parse().iterator().next();
   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     }
   620     public void testVarPos() throws IOException {
   622         final String code = "package t; class Test { " +
   623                 "{ java.io.InputStream in = null; } }";
   625         JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
   626                 null, Arrays.asList(new MyFileObject(code)));
   627         CompilationUnitTree cut = ct.parse().iterator().next();
   629         new TreeScanner<Void, Void>() {
   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     }
   643     void testsNotWorking() throws IOException {
   645         // Fails with nb-javac, needs further investigation
   646         testPositionBrokenSource126732a();
   647         testPositionBrokenSource126732b();
   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     }
   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 }
   674 abstract class TestCase {
   676     void assertEquals(String message, int i, int pos) {
   677         if (i != pos) {
   678             fail(message);
   679         }
   680     }
   682     void assertFalse(String message, boolean empty) {
   683         throw new UnsupportedOperationException("Not yet implemented");
   684     }
   686     void assertEquals(String message, int i, long l) {
   687         if (i != l) {
   688             fail(message + ":" + i + ":" + l);
   689         }
   690     }
   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     }
   703     void assertNotNull(Object o) {
   704         if (o == null) {
   705             fail();
   706         }
   707     }
   709     void fail() {
   710         fail("test failed");
   711     }
   713     void fail(String message) {
   714         throw new RuntimeException(message);
   715     }
   716 }

mercurial