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