src/share/classes/com/sun/tools/javac/tree/Pretty.java

changeset 1347
1408af4cd8b0
parent 1326
30c36e23f154
child 1348
573ceb23beeb
     1.1 --- a/src/share/classes/com/sun/tools/javac/tree/Pretty.java	Sat Sep 29 09:00:58 2012 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/javac/tree/Pretty.java	Thu Oct 04 13:04:53 2012 +0100
     1.3 @@ -81,6 +81,12 @@
     1.4       */
     1.5      DocCommentTable docComments = null;
     1.6  
     1.7 +    /**
     1.8 +     * A string sequence to be used when Pretty output should be constrained
     1.9 +     * to fit into a given size
    1.10 +     */
    1.11 +    private final static String trimSequence = "[...]";
    1.12 +
    1.13      /** Align code to be indented to left margin.
    1.14       */
    1.15      void align() throws IOException {
    1.16 @@ -129,6 +135,27 @@
    1.17          out.write(lineSep);
    1.18      }
    1.19  
    1.20 +    public static String toSimpleString(JCTree tree, int maxLength) {
    1.21 +        StringWriter s = new StringWriter();
    1.22 +        try {
    1.23 +            new Pretty(s, false).printExpr(tree);
    1.24 +        }
    1.25 +        catch (IOException e) {
    1.26 +            // should never happen, because StringWriter is defined
    1.27 +            // never to throw any IOExceptions
    1.28 +            throw new AssertionError(e);
    1.29 +        }
    1.30 +        //we need to (i) replace all line terminators with a space and (ii) remove
    1.31 +        //occurrences of 'missing' in the Pretty output (generated when types are missing)
    1.32 +        String res = s.toString().replaceAll("\\s+", " ").replaceAll("/\\*missing\\*/", "");
    1.33 +        if (res.length() < maxLength) {
    1.34 +            return res;
    1.35 +        } else {
    1.36 +            int split = (maxLength - trimSequence.length()) * 2 / 3;
    1.37 +            return res.substring(0, split) + trimSequence + res.substring(split);
    1.38 +        }
    1.39 +    }
    1.40 +
    1.41      String lineSep = System.getProperty("line.separator");
    1.42  
    1.43      /**************************************************************************

mercurial