<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#ffffff" text="#000000">
    Martin,<br>
    <br>
    The pretty printer is primarily a debugging aid, to accurately show
    the contents of the syntax tree. To assist in that task, a more
    recent development has been to try and make the output be compilable
    as well.<br>
    <br>
    The difficulty is that, despite first appearances, the AST is not a
    direct, literal representation of the source text and some
    transformations on the tree have already occurred by the time the
    pretty printer gets invoked.<br>
    <br>
    The use of comments has been introduced as a way of showing the
    parts of the AST which are present but which prevent compilation.&nbsp;&nbsp;
    It would seem you have found a couple of corner cases that need to
    be fixed.<br>
    <br>
    -- Jon<br>
    <br>
    <br>
    On 01/29/2012 06:07 AM, Crazy Java wrote:
    <blockquote
cite="mid:CAK1A17m+PYM-CU=1P+O=VvCgaMKSvta+X+pE=3Bp78nXo6LhZQ@mail.gmail.com"
      type="cite">It looks like the Enum problem should alreadz be fixed
      according to bug 6902720 (<a moz-do-not-send="true"
        href="http://hg.openjdk.java.net/jdk7/tl/langtools/rev/b1bb8164a9bd">http://hg.openjdk.java.net/jdk7/tl/langtools/rev/b1bb8164a9bd</a>,
      <a moz-do-not-send="true"
        href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6902720">http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6902720</a>),
      but it is not.<br>
      <br>
      <div class="gmail_quote">2012/1/29 R&eacute;mi Forax <span dir="ltr">&lt;<a
            moz-do-not-send="true" href="mailto:forax@univ-mlv.fr">forax@univ-mlv.fr</a>&gt;</span><br>
        <blockquote style="margin: 0px 0px 0px 0.8ex; padding-left: 1ex;
          border-left: 1px solid rgb(204, 204, 204);"
          class="gmail_quote">
          <div>
            <div class="h5">On 01/29/2012 01:40 PM, Crazy Java wrote:<br>
              <blockquote style="margin: 0px 0px 0px 0.8ex;
                padding-left: 1ex; border-left: 1px solid rgb(204, 204,
                204);" class="gmail_quote">
                Hi,<br>
                as part of the javac AST Visualizer project, I have some
                difficulties with javac's pretty printer. The problem is
                the fact, that I am doing double compilation (after
                first compilation I get the textual representation of
                produced AST and that is in fact used for visualization
                purposes). Double compilation is needed because I want
                to visualize all the compiler sugar (default
                constructor, effectively final variables in ARM, code
                generated for enum, ...).<br>
                The problem is that javac's pretty printer (I guess it
                is pretty printer) after calling
                CompilationUnitTree.toString() produces in some specific
                situations code that is not able to compile:<br>
                1.) it transforms<br>
                public class FullClassUsage {<br>
                &nbsp; &nbsp;{<br>
                &nbsp; &nbsp; &nbsp; &nbsp;new &lt;Object&gt; ArrayList&lt;String&gt;(10) {<br>
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{<br>
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;add("123");<br>
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br>
                &nbsp; &nbsp; &nbsp; &nbsp;};<br>
                &nbsp; &nbsp;}<br>
                }<br>
                into<br>
                public class FullClassUsage {<br>
                <br>
                &nbsp; &nbsp;public FullClassUsage() {<br>
                &nbsp; &nbsp; &nbsp; &nbsp;super();<br>
                &nbsp; &nbsp;}<br>
                &nbsp; &nbsp;{<br>
                &nbsp; &nbsp; &nbsp; &nbsp;new &lt;Object&gt;ArrayList&lt;String&gt;(10){<br>
                <br>
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(int x0) {<br>
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;super(x0);<br>
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br>
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{<br>
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;add("123");<br>
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br>
                &nbsp; &nbsp; &nbsp; &nbsp;};<br>
                &nbsp; &nbsp;}<br>
                }<br>
                the (int x0) is the problem.<br>
                2.) when using together with enum types it transforms:<br>
                public enum SimpleEnum {<br>
                &nbsp; &nbsp;ENUM_CONSTANT_1,<br>
                &nbsp; &nbsp;ENUM_CONSTANT_2<br>
                }<br>
                into<br>
                public enum SimpleEnum {<br>
                &nbsp; &nbsp;/*public static final*/ ENUM_CONSTANT_1 /* = new
                SimpleEnum() */,<br>
                &nbsp; &nbsp;/*public static final*/ ENUM_CONSTANT_2 /* = new
                SimpleEnum() */;<br>
                <br>
                &nbsp; &nbsp;private SimpleEnum() {<br>
                &nbsp; &nbsp; &nbsp; &nbsp;super();<br>
                &nbsp; &nbsp;}<br>
                }<br>
                that is really weird. Not only it generates comments ???
                but also a super call causing the code not to compile. I
                would also assume that it will generate all the enum
                type code (constructors with n+2 arguments, valueOf()
                factory method, extending java.lang.Enum, ...).<br>
                Is there any reason why it produces that weird code?<br>
              </blockquote>
              <br>
            </div>
          </div>
          Java language != bytecode,<br>
          at the end javac will generate bytecodes and there are a lot
          of things that are allowed<br>
          in bytecode that are not allowed in Java.<br>
          <br>
          The pretty printer is something used to debug and not
          something that will generate fully compatible Java code<br>
          because some de-sugared code are only valid in bytecode but
          not in Java.
          <div class="im"><br>
            <br>
            <blockquote style="margin: 0px 0px 0px 0.8ex; padding-left:
              1ex; border-left: 1px solid rgb(204, 204, 204);"
              class="gmail_quote">
              Is there any better way to get the Textual representation
              of CompilationUnitTree (using pretty printer) that will
              compile in both of these examples ?<br>
            </blockquote>
            <br>
          </div>
          write your own :)<br>
          <br>
          <blockquote style="margin: 0px 0px 0px 0.8ex; padding-left:
            1ex; border-left: 1px solid rgb(204, 204, 204);"
            class="gmail_quote">
            //Martin<br>
          </blockquote>
          <font color="#888888">
            <br>
            R&eacute;mi<br>
            <br>
          </font></blockquote>
      </div>
      <br>
    </blockquote>
    <br>
  </body>
</html>