Thu, 10 Jan 2013 15:48:46 -0800
8006033: bug in Pretty.toSimpleString
Reviewed-by: darcy
1.1 --- a/src/share/classes/com/sun/tools/javac/tree/Pretty.java Thu Jan 10 14:09:33 2013 -0800 1.2 +++ b/src/share/classes/com/sun/tools/javac/tree/Pretty.java Thu Jan 10 15:48:46 2013 -0800 1.3 @@ -1,5 +1,5 @@ 1.4 /* 1.5 - * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. 1.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.8 * 1.9 * This code is free software; you can redistribute it and/or modify it 1.10 @@ -154,12 +154,13 @@ 1.11 } 1.12 //we need to (i) replace all line terminators with a space and (ii) remove 1.13 //occurrences of 'missing' in the Pretty output (generated when types are missing) 1.14 - String res = s.toString().replaceAll("\\s+", " ").replaceAll("/\\*missing\\*/", ""); 1.15 + String res = s.toString().trim().replaceAll("\\s+", " ").replaceAll("/\\*missing\\*/", ""); 1.16 if (res.length() < maxLength) { 1.17 return res; 1.18 } else { 1.19 - int split = (maxLength - trimSequence.length()) * 2 / 3; 1.20 - return res.substring(0, split) + trimSequence + res.substring(split); 1.21 + int head = (maxLength - trimSequence.length()) * 2 / 3; 1.22 + int tail = maxLength - trimSequence.length() - head; 1.23 + return res.substring(0, head) + trimSequence + res.substring(res.length() - tail); 1.24 } 1.25 } 1.26
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/tree/PrettySimpleStringTest.java Thu Jan 10 15:48:46 2013 -0800 2.3 @@ -0,0 +1,73 @@ 2.4 +/* 2.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 + * 2.8 + * This code is free software; you can redistribute it and/or modify it 2.9 + * under the terms of the GNU General Public License version 2 only, as 2.10 + * published by the Free Software Foundation. 2.11 + * 2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.15 + * version 2 for more details (a copy is included in the LICENSE file that 2.16 + * accompanied this code). 2.17 + * 2.18 + * You should have received a copy of the GNU General Public License version 2.19 + * 2 along with this work; if not, write to the Free Software Foundation, 2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.21 + * 2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2.23 + * or visit www.oracle.com if you need additional information or have any 2.24 + * questions. 2.25 + */ 2.26 + 2.27 +/* 2.28 + * @test 2.29 + * @bug 8006033 2.30 + * @summary bug in Pretty.toSimpleString 2.31 + */ 2.32 + 2.33 +import java.io.File; 2.34 + 2.35 +import javax.tools.StandardJavaFileManager; 2.36 + 2.37 +import com.sun.source.tree.CompilationUnitTree; 2.38 +import com.sun.source.util.JavacTask; 2.39 +import com.sun.tools.javac.api.JavacTool; 2.40 +import com.sun.tools.javac.tree.JCTree; 2.41 +import com.sun.tools.javac.tree.Pretty; 2.42 + 2.43 +public class PrettySimpleStringTest { 2.44 + public static void main(String... args) throws Exception { 2.45 + new PrettySimpleStringTest().run(); 2.46 + } 2.47 + 2.48 + void run() throws Exception { 2.49 + File testSrc = new File(System.getProperty("test.src")); 2.50 + File thisFile = new File(testSrc, getClass().getName() + ".java"); 2.51 + JavacTool tool = JavacTool.create(); 2.52 + StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); 2.53 + JavacTask task = tool.getTask(null, fm, null, null, null, 2.54 + fm.getJavaFileObjects(thisFile)); 2.55 + Iterable<? extends CompilationUnitTree> trees = task.parse(); 2.56 + CompilationUnitTree thisTree = trees.iterator().next(); 2.57 + 2.58 + { // test default 2.59 + String thisSrc = Pretty.toSimpleString((JCTree) thisTree); 2.60 + System.err.println(thisSrc); 2.61 + String expect = "import jav[...]} } }"; 2.62 + if (!thisSrc.equals(expect)) { 2.63 + throw new Exception("unexpected result"); 2.64 + } 2.65 + } 2.66 + 2.67 + { // test explicit length 2.68 + String thisSrc = Pretty.toSimpleString((JCTree) thisTree, 32); 2.69 + System.err.println(thisSrc); 2.70 + String expect = "import java.io.Fil[...]; } } } }"; 2.71 + if (!thisSrc.equals(expect)) { 2.72 + throw new Exception("unexpected result"); 2.73 + } 2.74 + } 2.75 + } 2.76 +}