test/tools/javac/tree/T8024415.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

     1 /*
     2  * Copyright (c) 2013, 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 8024415
    27  * @summary Pretty printing of JCConditional does not follow the precedence and
    28  *          associativity rules of JCConditional
    29  * @run testng T8024415
    30  */
    33 import static org.testng.Assert.assertEquals;
    35 import java.io.IOException;
    36 import java.io.StringWriter;
    38 import org.testng.annotations.Test;
    40 import com.sun.tools.javac.file.JavacFileManager;
    41 import com.sun.tools.javac.tree.JCTree;
    42 import com.sun.tools.javac.tree.JCTree.JCExpression;
    43 import com.sun.tools.javac.tree.Pretty;
    44 import com.sun.tools.javac.tree.TreeMaker;
    45 import com.sun.tools.javac.util.Context;
    46 import com.sun.tools.javac.util.Names;
    49 /*
    50  * Test verifies that the precedence rules of conditional expressions
    51  * (JCConditional) are correct.
    52  */
    53 @Test
    54 public class T8024415 {
    56     TreeMaker maker;
    57     JCExpression x;
    60     public T8024415() {
    61         Context ctx = new Context();
    62         JavacFileManager.preRegister(ctx);
    63         maker = TreeMaker.instance(ctx);
    64         Names names = Names.instance(ctx);
    65         x = maker.Ident(names.fromString("x"));
    66     }
    69     // JLS 15.25: The conditional operator is syntactically right-associative
    70     // (it groups right-to-left). Thus, a?b:c?d:e?f:g means the same as
    71     // a?b:(c?d:(e?f:g)).
    72     public void testAssociativity() throws IOException {
    74         JCTree left   = maker.Conditional(maker.Conditional(x, x, x), x, x);
    75         JCTree right  = maker.Conditional(x, x, maker.Conditional(x, x, x));
    77         String prettyLeft   = prettyPrint(left);
    78         String prettyRight  = prettyPrint(right);
    80         assertEquals(prettyLeft.replaceAll("\\s", ""),  "(x?x:x)?x:x");
    81         assertEquals(prettyRight.replaceAll("\\s", ""), "x?x:x?x:x");
    83     }
    86     // The true-part of of a conditional expression is surrounded by ? and :
    87     // and can thus always be parsed unambiguously without surrounding
    88     // parentheses.
    89     public void testPrecedence() throws IOException {
    91         JCTree left   = maker.Conditional(maker.Assign(x, x), x, x);
    92         JCTree middle = maker.Conditional(x, maker.Assign(x, x), x);
    93         JCTree right  = maker.Conditional(x, x, maker.Assign(x, x));
    95         String prettyLeft   = prettyPrint(left);
    96         String prettyMiddle = prettyPrint(middle);
    97         String prettyRight  = prettyPrint(right);
    99         assertEquals(prettyLeft.replaceAll("\\s", ""),   "(x=x)?x:x");
   100         assertEquals(prettyMiddle.replaceAll("\\s", ""), "x?x=x:x");
   101         assertEquals(prettyRight.replaceAll("\\s", ""),  "x?x:(x=x)");
   103     }
   106     // Helper method
   107     private static String prettyPrint(JCTree tree) throws IOException {
   108         StringWriter sw = new StringWriter();
   109         new Pretty(sw, true).printExpr(tree);
   110         return sw.toString();
   111     }
   113 }

mercurial