diff -r 20e4a54b1629 -r 1408af4cd8b0 src/share/classes/com/sun/tools/javac/tree/Pretty.java --- a/src/share/classes/com/sun/tools/javac/tree/Pretty.java Sat Sep 29 09:00:58 2012 -0700 +++ b/src/share/classes/com/sun/tools/javac/tree/Pretty.java Thu Oct 04 13:04:53 2012 +0100 @@ -81,6 +81,12 @@ */ DocCommentTable docComments = null; + /** + * A string sequence to be used when Pretty output should be constrained + * to fit into a given size + */ + private final static String trimSequence = "[...]"; + /** Align code to be indented to left margin. */ void align() throws IOException { @@ -129,6 +135,27 @@ out.write(lineSep); } + public static String toSimpleString(JCTree tree, int maxLength) { + StringWriter s = new StringWriter(); + try { + new Pretty(s, false).printExpr(tree); + } + catch (IOException e) { + // should never happen, because StringWriter is defined + // never to throw any IOExceptions + throw new AssertionError(e); + } + //we need to (i) replace all line terminators with a space and (ii) remove + //occurrences of 'missing' in the Pretty output (generated when types are missing) + String res = s.toString().replaceAll("\\s+", " ").replaceAll("/\\*missing\\*/", ""); + if (res.length() < maxLength) { + return res; + } else { + int split = (maxLength - trimSequence.length()) * 2 / 3; + return res.substring(0, split) + trimSequence + res.substring(split); + } + } + String lineSep = System.getProperty("line.separator"); /**************************************************************************