Source Code Cross Referenced for JavaPrinter.java in  » Parser » Rats-Parser-Generators » xtc » lang » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Parser » Rats Parser Generators » xtc.lang 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * xtc - The eXTensible Compiler
0003:         * Copyright (C) 2005-2007 Robert Grimm, New York University
0004:         *
0005:         * This program is free software; you can redistribute it and/or
0006:         * modify it under the terms of the GNU General Public License
0007:         * version 2 as published by the Free Software Foundation.
0008:         *
0009:         * This program is distributed in the hope that it will be useful,
0010:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012:         * GNU General Public License for more details.
0013:         *
0014:         * You should have received a copy of the GNU General Public License
0015:         * along with this program; if not, write to the Free Software
0016:         * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
0017:         * USA.
0018:         */
0019:        package xtc.lang;
0020:
0021:        import java.util.Iterator;
0022:
0023:        import xtc.tree.Comment;
0024:        import xtc.tree.Node;
0025:        import xtc.tree.GNode;
0026:        import xtc.tree.Printer;
0027:        import xtc.tree.Token;
0028:        import xtc.tree.Visitor;
0029:
0030:        /**
0031:         * A pretty printer for Java.
0032:         *
0033:         * <p />A note on operator precedence: This printer uses precedence
0034:         * levels to control when to print parentheses around expressions.
0035:         * The actual precedence values are the standard Java precedence
0036:         * levels multiplied by ten.
0037:         *
0038:         * @author Robert Grimm, Stacey Kuznetsov, Martin Hirzel
0039:         * @version $Revision: 1.69 $
0040:         */
0041:        public class JavaPrinter extends Visitor {
0042:
0043:            /**
0044:             * The base precedence level. This level corresponds to the
0045:             * expression nonterminal.
0046:             */
0047:            public static final int PREC_BASE = 0;
0048:
0049:            /**
0050:             * The list precedence level.  This level corresponds to the
0051:             * assignment expression nonterminal.
0052:             */
0053:            public static final int PREC_LIST = 10;
0054:
0055:            /**
0056:             * The constant precedence level.  This level corresponds to the
0057:             * conditional expression nonterminal.
0058:             */
0059:            public static final int PREC_CONSTANT = 1;
0060:
0061:            /** The flag for any statement besides an if or if-else statement. */
0062:            public static final int STMT_ANY = 0;
0063:
0064:            /** The flag for an if statement. */
0065:            public static final int STMT_IF = 1;
0066:
0067:            /** The flag for an if-else statement. */
0068:            public static final int STMT_IF_ELSE = 2;
0069:
0070:            /** The printer for this Java printer. */
0071:            protected final Printer printer;
0072:
0073:            /** The package for any previous import declaration. */
0074:            protected String packageName;
0075:
0076:            /** The flag for whether we just printed a declaration. */
0077:            protected boolean isDeclaration;
0078:
0079:            /** The flag for whether we just printed a statement. */
0080:            protected boolean isStatement;
0081:
0082:            /** The flag for whether the last statement ended with an open line. */
0083:            protected boolean isOpenLine;
0084:
0085:            /**
0086:             * The flag for whether the current statement requires nesting or
0087:             * for whether the current declaration is nested within a for
0088:             * statement.
0089:             */
0090:            protected boolean isNested;
0091:
0092:            /**
0093:             * The flag for whether this statement is the else clause of an
0094:             * if-else statement.
0095:             */
0096:            protected boolean isIfElse;
0097:
0098:            /** The operator precedence level for the current expression. */
0099:            protected int precedence;
0100:
0101:            /**
0102:             * Create a new Java printer.
0103:             *
0104:             * @param printer The printer.
0105:             */
0106:            public JavaPrinter(Printer printer) {
0107:                this .printer = printer;
0108:                printer.register(this );
0109:            }
0110:
0111:            /**
0112:             * Fold the specified qualified identifier.
0113:             *
0114:             * @param qid The qualified identifier.
0115:             * @param size Its size.
0116:             */
0117:            protected String fold(GNode qid, int size) {
0118:                StringBuilder buf = new StringBuilder();
0119:                for (int i = 0; i < size; i++) {
0120:                    buf.append(qid.getString(i));
0121:                    if (i < size - 1)
0122:                        buf.append('.');
0123:                }
0124:                return buf.toString();
0125:            }
0126:
0127:            /**
0128:             * Get the package name corresponding to the specified import
0129:             * declaration.
0130:             *
0131:             * @param n The import declaration node.
0132:             * @return The corresponding package name.
0133:             */
0134:            protected String getPackage(GNode n) {
0135:                assert n.hasName("ImportDeclaration");
0136:
0137:                GNode qid = n.getGeneric(1);
0138:                int size = qid.size();
0139:                if (null == n.get(2))
0140:                    size--;
0141:
0142:                return 0 >= size ? "" : fold(qid, size);
0143:            }
0144:
0145:            /** The actual implementation of {@link #containsLongExpression}. */
0146:            @SuppressWarnings("unused")
0147:            private static final Visitor containsLongExprVisitor = new Visitor() {
0148:                public Boolean visitBlock(GNode n) {
0149:                    return Boolean.TRUE;
0150:                }
0151:
0152:                public Boolean visitArrayInitializer(GNode n) {
0153:                    return Boolean.TRUE;
0154:                }
0155:
0156:                public Boolean visit(GNode n) {
0157:                    for (Object o : n) {
0158:                        if ((o instanceof  Node) && (Boolean) dispatch((Node) o)) {
0159:                            return Boolean.TRUE;
0160:                        }
0161:                    }
0162:                    return Boolean.FALSE;
0163:                }
0164:            };
0165:
0166:            /**
0167:             * Determine whether the specified node contains a long expression.
0168:             * This method considers blocks and array initializers to be long.
0169:             *
0170:             * @param n The node.
0171:             * @return <code>true</code> if the node contains a long expression.
0172:             */
0173:            protected boolean containsLongExpression(GNode n) {
0174:                return (Boolean) containsLongExprVisitor.dispatch(n);
0175:            }
0176:
0177:            /**
0178:             * Determine whether the specified declaration is long.  A long
0179:             * declaration requires multiple lines for readability.  Examples
0180:             * include declarations containing class bodies or blocks.
0181:             *
0182:             * @param decl The declaration.
0183:             * @return <code>true</code> if the specified declaration is long.
0184:             */
0185:            protected boolean isLongDeclaration(GNode decl) {
0186:                return (decl.hasName("ConstructorDeclaration")
0187:                        || decl.hasName("ClassDeclaration")
0188:                        || decl.hasName("InterfaceDeclaration")
0189:                        || decl.hasName("AnnotationDeclaration")
0190:                        || decl.hasName("EnumDeclaration")
0191:                        || decl.hasName("BlockDeclaration")
0192:                        || (decl.hasName("MethodDeclaration") && (null != decl
0193:                                .get(7)))
0194:                        || (decl.hasName("FieldDeclaration") && containsLongExpression(decl)) || (decl
0195:                        .hasName("AnnotationMethod") && containsLongExpression(decl)));
0196:            }
0197:
0198:            /**
0199:             * Print the specified node's children as declarations and/or
0200:             * statements.
0201:             *
0202:             * @param n The node.
0203:             */
0204:            protected void printDeclsAndStmts(GNode n) {
0205:                isOpenLine = false;
0206:                isNested = false;
0207:                isIfElse = false;
0208:                isDeclaration = false;
0209:                isStatement = false;
0210:                GNode previous = null;
0211:
0212:                for (Object o : n) {
0213:                    final Node node = (Node) o;
0214:                    if (null == node)
0215:                        continue;
0216:                    final GNode current = GNode.cast(node);
0217:
0218:                    // If there was a previous node and the previous node was a
0219:                    // block or long declaration, the current node is a block or
0220:                    // long declaration, or the previous node was a statement and
0221:                    // the current node is a declaration, then print an extra
0222:                    // newline.
0223:                    if ((null != previous)
0224:                            && (previous.hasName("Block")
0225:                                    || (isLongDeclaration(previous) && current
0226:                                            .getName().endsWith("Declaration"))
0227:                                    || current.hasName("Block")
0228:                                    || isLongDeclaration(current) || (!previous
0229:                                    .getName().endsWith("Declaration") && current
0230:                                    .getName().endsWith("Declaration")))) {
0231:                        printer.pln();
0232:                    }
0233:
0234:                    printer.p(node);
0235:
0236:                    if (isOpenLine)
0237:                        printer.pln();
0238:                    isOpenLine = false;
0239:                    previous = current;
0240:                }
0241:            }
0242:
0243:            /**
0244:             * Print an expression as a truth value.  This method parenthesizes
0245:             * assignment expressions.
0246:             *
0247:             * @param n The node to print.
0248:             */
0249:            protected void formatAsTruthValue(Node n) {
0250:                if (GNode.cast(n).hasName("AssignmentExpression")) {
0251:                    printer.p('(').p(n).p(')');
0252:                } else {
0253:                    printer.p(n);
0254:                }
0255:            }
0256:
0257:            /**
0258:             * Print empty square brackets for the given number of dimensions.
0259:             * 
0260:             * @param n Number of dimensions to print.
0261:             */
0262:            protected void formatDimensions(final int n) {
0263:                for (int i = 0; i < n; i++)
0264:                    printer.p("[]");
0265:            }
0266:
0267:            /**
0268:             * Start a new statement.  This method and the corresponding {@link
0269:             * #prepareNested()} and {@link #endStatement(boolean)} methods
0270:             * provide a reasonable default for newlines and indentation when
0271:             * printing statements.  They manage the {@link #isDeclaration},
0272:             * {@link #isStatement}, {@link #isOpenLine}, {@link #isNested}, and
0273:             * {@link #isIfElse} flags.
0274:             *
0275:             * @param kind The kind of statement, which must be one of the
0276:             *   three statement flags defined by this class.
0277:             * @return The flag for whether the current statement is nested.
0278:             */
0279:            protected boolean startStatement(int kind) {
0280:                if (isIfElse && ((STMT_IF == kind) || (STMT_IF_ELSE == kind))) {
0281:                    isNested = false;
0282:                } else {
0283:                    if (isOpenLine)
0284:                        printer.pln();
0285:                    if (isDeclaration)
0286:                        printer.pln();
0287:                    if (isNested)
0288:                        printer.incr();
0289:                }
0290:                isOpenLine = false;
0291:                boolean nested = isNested;
0292:                isNested = false;
0293:
0294:                return nested;
0295:            }
0296:
0297:            /**
0298:             * Prepare for a nested statement.
0299:             *
0300:             * @see #startStatement
0301:             */
0302:            protected void prepareNested() {
0303:                isDeclaration = false;
0304:                isStatement = false;
0305:                isOpenLine = true;
0306:                isNested = true;
0307:            }
0308:
0309:            /**
0310:             * End a statement.
0311:             *
0312:             * @see #startStatement
0313:             *
0314:             * @param nested The flag for whether the current statement is nested.
0315:             */
0316:            protected void endStatement(boolean nested) {
0317:                if (nested) {
0318:                    printer.decr();
0319:                }
0320:                isDeclaration = false;
0321:                isStatement = true;
0322:            }
0323:
0324:            /**
0325:             * Enter an expression context.  The new context has the specified
0326:             * precedence level.
0327:             *
0328:             * @see #exitContext(int)
0329:             *
0330:             * @param prec The precedence level for the expression context.
0331:             * @return The previous precedence level.
0332:             */
0333:            protected int enterContext(int prec) {
0334:                int old = precedence;
0335:                precedence = prec;
0336:                return old;
0337:            }
0338:
0339:            /**
0340:             * Enter an expression context.  The new context is appropriate for
0341:             * an operand opposite the associativity of the current operator.
0342:             * For example, when printing an additive expression, this method
0343:             * should be called before printing the second operand, as additive
0344:             * operators associate left-to-right.
0345:             *
0346:             * @see #exitContext(int)
0347:             *
0348:             * @return The previous precedence level.
0349:             */
0350:            protected int enterContext() {
0351:                int old = precedence;
0352:                precedence += 1;
0353:                return old;
0354:            }
0355:
0356:            /**
0357:             * Exit an expression context.
0358:             *
0359:             * @see #enterContext(int)
0360:             * @see #enterContext()
0361:             *
0362:             * @param prec The previous precedence level.
0363:             */
0364:            protected void exitContext(int prec) {
0365:                precedence = prec;
0366:            }
0367:
0368:            /**
0369:             * Start printing an expression at the specified operator precedence
0370:             * level.
0371:             *
0372:             * @see #endExpression(int)
0373:             *
0374:             * @param prec The expression's precedence level.
0375:             * @return The previous precedence level.
0376:             */
0377:            protected int startExpression(int prec) {
0378:                if (prec < precedence) {
0379:                    printer.p('(');
0380:                }
0381:
0382:                int old = precedence;
0383:                precedence = prec;
0384:                return old;
0385:            }
0386:
0387:            /**
0388:             * Stop printing an expression.
0389:             *
0390:             * @see #startExpression(int)
0391:             *
0392:             * @param prec The previous precedence level.
0393:             */
0394:            protected void endExpression(int prec) {
0395:                if (precedence < prec) {
0396:                    printer.p(')');
0397:                }
0398:                precedence = prec;
0399:            }
0400:
0401:            /** Visit the specified comment. */
0402:            public void visit(Comment c) {
0403:                printer.indent().p(c).p(c.getNode());
0404:            }
0405:
0406:            /** Visit the specified translation unit. */
0407:            public void visitCompilationUnit(GNode n) {
0408:                // Reset the state.
0409:                packageName = null;
0410:                isDeclaration = false;
0411:                isStatement = false;
0412:                isOpenLine = false;
0413:                isNested = false;
0414:                isIfElse = false;
0415:                precedence = PREC_BASE;
0416:
0417:                printDeclsAndStmts(n);
0418:            }
0419:
0420:            /** Visit the specified package declaration. */
0421:            public void visitPackageDeclaration(GNode n) {
0422:                GNode qid = n.getGeneric(1);
0423:                packageName = fold(qid, qid.size());
0424:
0425:                printer.indent().p(n.getNode(0)).p("package ").p(n.getNode(1))
0426:                        .pln(';');
0427:                isOpenLine = false;
0428:            }
0429:
0430:            /** Visit the specified import declaration. */
0431:            public void visitImportDeclaration(GNode n) {
0432:                String p = getPackage(n);
0433:                if ((null != packageName) && (!p.equals(packageName)))
0434:                    printer.pln();
0435:                packageName = p;
0436:
0437:                printer.indent().p("import ");
0438:                if (null != n.get(0))
0439:                    printer.p("static ");
0440:                printer.p(n.getNode(1));
0441:                if (null != n.get(2))
0442:                    printer.p(".*");
0443:                printer.pln(';');
0444:                isOpenLine = false;
0445:            }
0446:
0447:            /** Visit the specified modifiers. */
0448:            public void visitModifiers(GNode n) {
0449:                for (Object o : n)
0450:                    printer.p((Node) o).p(' ');
0451:            }
0452:
0453:            /** Visit the specified modifier. */
0454:            public void visitModifier(GNode n) {
0455:                printer.p(n.getString(0));
0456:            }
0457:
0458:            /** Visit the specified formal parameter. */
0459:            public void visitFormalParameter(GNode n) {
0460:                printer.p(n.getNode(0)).p(n.getNode(1));
0461:                if (null != n.get(2))
0462:                    printer.p(n.getString(2));
0463:                printer.p(' ').p(n.getString(3)).p(n.getNode(4));
0464:            }
0465:
0466:            /** Visit the specified final clause. */
0467:            public void visitFinalClause(GNode n) {
0468:                printer.p("final");
0469:            }
0470:
0471:            /** Visit the specified formal parameters. */
0472:            public void visitFormalParameters(GNode n) {
0473:                printer.p('(');
0474:                for (Iterator<Object> iter = n.iterator(); iter.hasNext();) {
0475:                    printer.p((Node) iter.next());
0476:                    if (iter.hasNext())
0477:                        printer.p(", ");
0478:                }
0479:                printer.p(')');
0480:            }
0481:
0482:            /** Visit the specified declarator. */
0483:            public void visitDeclarator(GNode n) {
0484:                printer.p(n.getString(0));
0485:                if (null != n.get(1)) {
0486:                    printer.p(' ');
0487:                    if (Token.test(n.get(1))) {
0488:                        formatDimensions(n.getString(1).length());
0489:                    } else {
0490:                        printer.p(n.getNode(1));
0491:                    }
0492:                }
0493:                if (null != n.get(2)) {
0494:                    printer.p(" = ").p(n.getNode(2));
0495:                }
0496:            }
0497:
0498:            /** Visit the specified declarators. */
0499:            public void visitDeclarators(GNode n) {
0500:                for (Iterator<Object> iter = n.iterator(); iter.hasNext();) {
0501:                    printer.p((Node) iter.next());
0502:                    if (iter.hasNext())
0503:                        printer.p(", ");
0504:                }
0505:            }
0506:
0507:            /** Visit the specified annotations. */
0508:            public void visitAnnotations(GNode n) {
0509:                for (Object o : n)
0510:                    printer.p((Node) o).p(' ');
0511:            }
0512:
0513:            /** Visit the specified annotation. */
0514:            public void visitAnnotation(GNode n) {
0515:                printer.p('@').p(n.getNode(0));
0516:                if (null != n.get(1))
0517:                    printer.p('(').p(n.getNode(1)).p(')');
0518:            }
0519:
0520:            /** Visit the specified element value pairs. */
0521:            public void visitElementValuePairs(GNode n) {
0522:                boolean first = true;
0523:                for (Object o : n) {
0524:                    if (first)
0525:                        first = false;
0526:                    else
0527:                        printer.p(", ");
0528:                    printer.p((Node) o);
0529:                }
0530:            }
0531:
0532:            /** Visit the specified element value pair. */
0533:            public void visitElementValuePair(GNode n) {
0534:                printer.p(n.getNode(0)).p(" = ").p(n.getNode(1));
0535:            }
0536:
0537:            /** Visit the specified default value. */
0538:            public void visitDefaultValue(GNode n) {
0539:                printer.p("default ").p(n.getNode(0));
0540:            }
0541:
0542:            /** Visit the specified class body. */
0543:            public void visitClassBody(GNode n) {
0544:                if (isOpenLine)
0545:                    printer.p(' ');
0546:                printer.pln('{').incr();
0547:
0548:                printDeclsAndStmts(n);
0549:
0550:                printer.decr().indent().p('}');
0551:                isOpenLine = true;
0552:                isNested = false;
0553:                isIfElse = false;
0554:            }
0555:
0556:            /** Visit the specified field declaration. */
0557:            public void visitFieldDeclaration(GNode n) {
0558:                printer.indent().p(n.getNode(0)).p(n.getNode(1)).p(' ').p(
0559:                        n.getNode(2)).p(';').pln();
0560:                isDeclaration = true;
0561:                isOpenLine = false;
0562:            }
0563:
0564:            /** Visit the specified method declaration. */
0565:            public void visitMethodDeclaration(GNode n) {
0566:                printer.indent().p(n.getNode(0));
0567:                if (null != n.get(1))
0568:                    printer.p(n.getNode(1)).p(' ');
0569:                printer.p(n.getNode(2));
0570:                if (!"<init>".equals(n.get(3))) {
0571:                    printer.p(' ').p(n.getString(3));
0572:                }
0573:                printer.p(n.getNode(4));
0574:                if (null != n.get(5)) {
0575:                    printer.p(' ').p(n.getNode(5));
0576:                }
0577:                if (null != n.get(6)) {
0578:                    printer.p(' ').p(n.getNode(6));
0579:                }
0580:                if (null != n.get(7)) {
0581:                    isOpenLine = true;
0582:                    printer.p(n.getNode(7)).pln();
0583:                } else {
0584:                    printer.pln(';');
0585:                }
0586:                isOpenLine = false;
0587:            }
0588:
0589:            /** Visit the specified constructor declaration. */
0590:            public void visitConstructorDeclaration(GNode n) {
0591:                printer.indent().p(n.getNode(0));
0592:                if (null != n.get(1))
0593:                    printer.p(n.getNode(1));
0594:                printer.p(n.getString(2)).p(n.getNode(3));
0595:                if (null != n.get(4)) {
0596:                    printer.p(n.getNode(4));
0597:                }
0598:                isOpenLine = true;
0599:                printer.p(n.getNode(5));
0600:            }
0601:
0602:            /** Visit the specified class declaration. */
0603:            public void visitClassDeclaration(GNode n) {
0604:                printer.indent().p(n.getNode(0)).p("class ").p(n.getString(1))
0605:                        .p(n.getNode(2));
0606:                if (null != n.get(3)) {
0607:                    printer.p(' ').p(n.getNode(3));
0608:                }
0609:                if (null != n.get(4)) {
0610:                    printer.p(' ').p(n.getNode(4));
0611:                }
0612:                isOpenLine = true;
0613:                printer.p(n.getNode(5)).pln();
0614:                isDeclaration = true;
0615:                isOpenLine = false;
0616:            }
0617:
0618:            /** Visit the specified interface declaration. */
0619:            public void visitInterfaceDeclaration(GNode n) {
0620:                printer.indent().p(n.getNode(0)).p("interface ").p(
0621:                        n.getString(1)).p(n.getNode(2));
0622:                if (null != n.get(3)) {
0623:                    printer.p(' ').p(n.getNode(3));
0624:                }
0625:                isOpenLine = true;
0626:                printer.p(n.getNode(4)).pln();
0627:                isDeclaration = true;
0628:                isOpenLine = false;
0629:            }
0630:
0631:            /** Visit the specified annotation declaration. */
0632:            public void visitAnnotationDeclaration(GNode n) {
0633:                printer.indent().p(n.getNode(0)).p("@interface ").p(
0634:                        n.getString(1));
0635:                isOpenLine = true;
0636:                printer.p(n.getNode(2)).pln();
0637:                isDeclaration = true;
0638:                isOpenLine = false;
0639:            }
0640:
0641:            /** Visit the specified annotation method. */
0642:            public void visitAnnotationMethod(GNode n) {
0643:                printer.indent().p(n.getNode(0)).p(n.getNode(1)).p(' ').p(
0644:                        n.getString(2)).p("()");
0645:                if (null != n.get(3))
0646:                    printer.p(" default ").p(n.getNode(3));
0647:                printer.pln(';');
0648:                isOpenLine = false;
0649:            }
0650:
0651:            /** Visit the specified enum declaration. */
0652:            public void visitEnumDeclaration(GNode n) {
0653:                printer.indent().p(n.getNode(0)).p("enum ").p(n.getString(1));
0654:                if (null != n.get(2)) {
0655:                    printer.p(' ').p(n.getNode(2));
0656:                }
0657:                printer.pln(" {").incr();
0658:
0659:                isOpenLine = false;
0660:                isNested = false;
0661:                isIfElse = false;
0662:                printer.p(n.getNode(3));
0663:
0664:                if (null != n.get(4)) {
0665:                    printer.pln(';').pln();
0666:                    isOpenLine = false;
0667:                    isNested = false;
0668:                    isIfElse = false;
0669:                    printer.p(n.getNode(4));
0670:                }
0671:
0672:                if (isOpenLine)
0673:                    printer.pln();
0674:                printer.decr().indent().pln('}');
0675:                isOpenLine = false;
0676:                isNested = false;
0677:                isIfElse = false;
0678:                isDeclaration = true;
0679:            }
0680:
0681:            /** Visit the specified enum constants. */
0682:            public void visitEnumConstants(GNode n) {
0683:                for (Iterator<Object> iter = n.iterator(); iter.hasNext();) {
0684:                    isDeclaration = false;
0685:                    printer.indent().p((Node) iter.next());
0686:                    if (iter.hasNext()) {
0687:                        printer.pln(',');
0688:                        if (isDeclaration)
0689:                            printer.pln();
0690:                    }
0691:                }
0692:                isOpenLine = true;
0693:            }
0694:
0695:            /** Visit the specified enum constant. */
0696:            public void visitEnumConstant(GNode n) {
0697:                printer.p(n.getNode(0)).p(n.getString(1)).p(n.getNode(2));
0698:                if (null != n.get(3)) {
0699:                    isOpenLine = true;
0700:                    printer.p(n.getNode(3));
0701:                    isDeclaration = true;
0702:                } else {
0703:                    isDeclaration = false;
0704:                }
0705:            }
0706:
0707:            /** Visit the specified enum members. */
0708:            public void visitEnumMembers(GNode n) {
0709:                printDeclsAndStmts(n);
0710:            }
0711:
0712:            /** Visit the specified block declaration. */
0713:            public void visitBlockDeclaration(GNode n) {
0714:                printer.indent();
0715:                if (null != n.get(0)) {
0716:                    printer.p(n.getString(0));
0717:                    isOpenLine = true;
0718:                }
0719:                printer.p(n.getNode(1)).pln();
0720:                isOpenLine = false;
0721:            }
0722:
0723:            /** Visit the specific empty declaration. */
0724:            public void visitEmptyDeclaration(GNode n) {
0725:                // Nothing to do.
0726:            }
0727:
0728:            /** Visit the specified throws clause. */
0729:            public void visitThrowsClause(GNode n) {
0730:                printer.p("throws ");
0731:                for (Iterator<Object> iter = n.iterator(); iter.hasNext();) {
0732:                    printer.p((Node) iter.next());
0733:                    if (iter.hasNext())
0734:                        printer.p(", ");
0735:                }
0736:            }
0737:
0738:            /** Visit the specified extension. */
0739:            public void visitExtension(GNode n) {
0740:                printer.p("extends ");
0741:                for (Iterator<Object> iter = n.iterator(); iter.hasNext();) {
0742:                    printer.p((Node) iter.next());
0743:                    if (iter.hasNext())
0744:                        printer.p(", ");
0745:                }
0746:            }
0747:
0748:            /** Visit the specified implementation. */
0749:            public void visitImplementation(GNode n) {
0750:                printer.p("implements ");
0751:                for (Iterator<Object> iter = n.iterator(); iter.hasNext();) {
0752:                    printer.p((Node) iter.next());
0753:                    if (iter.hasNext())
0754:                        printer.p(", ");
0755:                }
0756:            }
0757:
0758:            /** Visit the specified block. */
0759:            public void visitBlock(GNode n) {
0760:                if (isOpenLine) {
0761:                    printer.p(' ');
0762:                } else {
0763:                    printer.indent();
0764:                }
0765:                printer.pln('{').incr();
0766:
0767:                isOpenLine = false;
0768:                isNested = false;
0769:                isIfElse = false;
0770:                isDeclaration = false;
0771:                isStatement = false;
0772:
0773:                printDeclsAndStmts(n);
0774:
0775:                printer.decr().indent().p('}');
0776:                isOpenLine = true;
0777:                isNested = false;
0778:                isIfElse = false;
0779:            }
0780:
0781:            /** Visit the specified conditional statement. */
0782:            public void visitConditionalStatement(GNode n) {
0783:                final int flag = null == n.get(2) ? STMT_IF : STMT_IF_ELSE;
0784:                final boolean nested = startStatement(flag);
0785:                if (isIfElse) {
0786:                    printer.p(' ');
0787:                } else {
0788:                    printer.indent();
0789:                }
0790:                printer.p("if (").p(n.getNode(0)).p(')');
0791:                prepareNested();
0792:                printer.p(n.getNode(1));
0793:                if (null != n.get(2)) {
0794:                    if (isOpenLine) {
0795:                        printer.p(" else");
0796:                    } else {
0797:                        printer.indent().p("else");
0798:                    }
0799:                    prepareNested();
0800:                    boolean ifElse = isIfElse;
0801:                    isIfElse = true;
0802:                    printer.p(n.getNode(2));
0803:                    isIfElse = ifElse;
0804:                }
0805:                endStatement(nested);
0806:            }
0807:
0808:            /** Visit the specified for statement. */
0809:            public void visitForStatement(GNode n) {
0810:                final boolean nested = startStatement(STMT_ANY);
0811:
0812:                printer.indent().p("for (").p(n.getNode(0)).p(')');
0813:                prepareNested();
0814:                printer.p(n.getNode(1));
0815:
0816:                endStatement(nested);
0817:            }
0818:
0819:            /** Visit the specified basic for control. */
0820:            public void visitBasicForControl(GNode n) {
0821:                printer.p(n.getNode(0));
0822:                if (null != n.get(1))
0823:                    printer.p(n.getNode(1)).p(' ');
0824:
0825:                final int prec1 = enterContext(PREC_BASE);
0826:                printer.p(n.getNode(2)).p("; ");
0827:                exitContext(prec1);
0828:
0829:                if (null != n.get(3)) {
0830:                    final int prec2 = enterContext(PREC_BASE);
0831:                    formatAsTruthValue(n.getNode(3));
0832:                    exitContext(prec2);
0833:                }
0834:                printer.p("; ");
0835:
0836:                final int prec3 = enterContext(PREC_BASE);
0837:                printer.p(n.getNode(4));
0838:                exitContext(prec3);
0839:            }
0840:
0841:            /** Visit the specified enhanced for control. */
0842:            public void visitEnhancedForControl(GNode n) {
0843:                printer.p(n.getNode(0)).p(n.getNode(1)).p(' ')
0844:                        .p(n.getString(2)).p(" : ");
0845:
0846:                final int prec = enterContext(PREC_BASE);
0847:                printer.p(n.getNode(3));
0848:                exitContext(prec);
0849:            }
0850:
0851:            /** Visit the specified while statement. */
0852:            public void visitWhileStatement(GNode n) {
0853:                final boolean nested = startStatement(STMT_ANY);
0854:                printer.indent().p("while (").p(n.getNode(0)).p(')');
0855:                prepareNested();
0856:                printer.p(n.getNode(1));
0857:                endStatement(nested);
0858:            }
0859:
0860:            /** Visit the specified do while statement. */
0861:            public void visitDoWhileStatement(GNode n) {
0862:                final boolean nested = startStatement(STMT_ANY);
0863:                printer.indent().p("do");
0864:                prepareNested();
0865:                printer.p(n.getNode(0));
0866:                if (isOpenLine) {
0867:                    printer.p(' ');
0868:                } else {
0869:                    printer.indent();
0870:                }
0871:                printer.p("while (").p(n.getNode(1)).pln(");");
0872:                endStatement(nested);
0873:                isOpenLine = false;
0874:            }
0875:
0876:            /** Visit the specified try catch finally statement. */
0877:            public void visitTryCatchFinallyStatement(GNode n) {
0878:                final boolean nested = startStatement(STMT_ANY);
0879:
0880:                isOpenLine = true;
0881:                printer.indent().p("try").p(n.getNode(0)).p(' ');
0882:
0883:                final Iterator<Object> iter = n.iterator();
0884:                iter.next(); // Skip try block.
0885:                while (iter.hasNext()) {
0886:                    final GNode clause = GNode.cast(iter.next());
0887:
0888:                    isOpenLine = true;
0889:                    if (iter.hasNext()) {
0890:                        printer.p(clause).p(' ');
0891:                    } else if (null != clause) {
0892:                        printer.p("finally").p(clause);
0893:                    }
0894:                }
0895:
0896:                endStatement(nested);
0897:            }
0898:
0899:            /** Visit the specified catch clause. */
0900:            public void visitCatchClause(GNode n) {
0901:                printer.p("catch (").p(n.getNode(0)).p(")").p(n.getNode(1));
0902:            }
0903:
0904:            /** Visit the specified switch statement. */
0905:            public void visitSwitchStatement(GNode n) {
0906:                final boolean nested = startStatement(STMT_ANY);
0907:
0908:                final int prec = enterContext(PREC_CONSTANT);
0909:                printer.indent().p("switch (").p(n.getNode(0)).pln(") {")
0910:                        .incr();
0911:                exitContext(prec);
0912:
0913:                isOpenLine = false;
0914:                isNested = false;
0915:                isIfElse = false;
0916:                final Iterator<Object> iter = n.iterator();
0917:                iter.next(); // Skip switch expression.
0918:                while (iter.hasNext()) {
0919:                    printer.p((Node) iter.next());
0920:                }
0921:
0922:                if (isOpenLine) {
0923:                    printer.pln();
0924:                }
0925:                printer.decr().indent().p('}');
0926:
0927:                isOpenLine = true;
0928:                isNested = false;
0929:                isIfElse = false;
0930:                endStatement(nested);
0931:            }
0932:
0933:            /** Visit the specified case clause. */
0934:            public void visitCaseClause(GNode n) {
0935:                final boolean nested = startStatement(STMT_ANY);
0936:
0937:                final int prec = enterContext(PREC_CONSTANT);
0938:                printer.indentLess().p("case ").p(n.getNode(0)).pln(':');
0939:                exitContext(prec);
0940:
0941:                isOpenLine = false;
0942:                isNested = false;
0943:                isIfElse = false;
0944:                final Iterator<Object> iter = n.iterator();
0945:                iter.next(); // Skip case expression.
0946:                while (iter.hasNext()) {
0947:                    printer.p((Node) iter.next());
0948:                }
0949:
0950:                endStatement(nested);
0951:            }
0952:
0953:            /** Visit the specified default clause. */
0954:            public void visitDefaultClause(GNode n) {
0955:                final boolean nested = startStatement(STMT_ANY);
0956:                printer.indentLess().pln("default:");
0957:                isOpenLine = false;
0958:                isNested = false;
0959:                isIfElse = false;
0960:                for (Object o : n)
0961:                    printer.p((Node) o);
0962:                endStatement(nested);
0963:            }
0964:
0965:            /** Visit the specified synchronized statement. */
0966:            public void visitSynchronizedStatement(GNode n) {
0967:                final boolean nested = startStatement(STMT_ANY);
0968:                printer.indent().p("synchronized (").p(n.getNode(0)).p(')');
0969:                prepareNested();
0970:                printer.p(n.getNode(1));
0971:                endStatement(nested);
0972:            }
0973:
0974:            /** Visit the specified return statement. */
0975:            public void visitReturnStatement(GNode n) {
0976:                final boolean nested = startStatement(STMT_ANY);
0977:                printer.indent().p("return");
0978:                if (null != n.get(0)) {
0979:                    printer.p(' ').p(n.getNode(0));
0980:                }
0981:                printer.pln(';');
0982:                endStatement(nested);
0983:                isOpenLine = false;
0984:            }
0985:
0986:            /** Visit the specified throw statement. */
0987:            public void visitThrowStatement(GNode n) {
0988:                final boolean nested = startStatement(STMT_ANY);
0989:                printer.indent().p("throw").p(' ').p(n.getNode(0));
0990:                printer.pln(';');
0991:                endStatement(nested);
0992:                isOpenLine = false;
0993:            }
0994:
0995:            /** Visit the specified break statement. */
0996:            public void visitBreakStatement(GNode n) {
0997:                final boolean nested = startStatement(STMT_ANY);
0998:                printer.indent().p("break");
0999:                if ((n.getString(0)) != null) {
1000:                    printer.p(' ').p(n.getString(0));
1001:                }
1002:                printer.pln(';');
1003:                endStatement(nested);
1004:                isOpenLine = false;
1005:            }
1006:
1007:            /** Visit the specified continue statement. */
1008:            public void visitContinueStatement(GNode n) {
1009:                final boolean nested = startStatement(STMT_ANY);
1010:                printer.indent().p("continue");
1011:                if (null != n.getString(0)) {
1012:                    printer.p(' ').p(n.getString(0));
1013:                }
1014:                printer.p(';').pln();
1015:                endStatement(nested);
1016:                isOpenLine = false;
1017:            }
1018:
1019:            /** Visit the specified labeled statement. */
1020:            public void visitLabeledStatement(GNode n) {
1021:                final boolean nested = startStatement(STMT_ANY);
1022:                printer.indent().p(n.getString(0)).p(": ").p(n.getNode(1))
1023:                        .pln();
1024:                endStatement(nested);
1025:                isOpenLine = false;
1026:            }
1027:
1028:            /** Visit the specified expression statement. */
1029:            public void visitExpressionStatement(GNode n) {
1030:                final boolean nested = startStatement(STMT_ANY);
1031:                final int prec = enterContext(PREC_BASE);
1032:                printer.indent().p(n.getNode(0)).pln(';');
1033:                exitContext(prec);
1034:                endStatement(nested);
1035:                isOpenLine = false;
1036:            }
1037:
1038:            /** Visit the specified assert statement. */
1039:            public void visitAssertStatement(GNode n) {
1040:                final boolean nested = startStatement(STMT_ANY);
1041:                printer.indent().p("assert ").p(n.getNode(0));
1042:                if (null != n.get(1)) {
1043:                    printer.p(" : ").p(n.getNode(1));
1044:                }
1045:                printer.pln(';');
1046:                endStatement(nested);
1047:                isOpenLine = false;
1048:            }
1049:
1050:            /** Visit the specified empty statement. */
1051:            public void visitEmptyStatement(GNode n) {
1052:                final boolean nested = startStatement(STMT_ANY);
1053:                printer.indent().pln(';');
1054:                endStatement(nested);
1055:                isOpenLine = false;
1056:            }
1057:
1058:            /** Visit the specified expression list. */
1059:            public void visitExpressionList(GNode n) {
1060:                for (Iterator<Object> iter = n.iterator(); iter.hasNext();) {
1061:                    final int prec = enterContext(PREC_LIST);
1062:                    printer.p((Node) iter.next());
1063:                    exitContext(prec);
1064:                    if (iter.hasNext())
1065:                        printer.p(", ");
1066:                }
1067:            }
1068:
1069:            /** Visit the specified expression. */
1070:            public void visitExpression(GNode n) {
1071:                final int prec1 = startExpression(10);
1072:                final int prec2 = enterContext();
1073:                printer.p(n.getNode(0));
1074:                exitContext(prec2);
1075:
1076:                printer.p(' ').p(n.getString(1)).p(' ').p(n.getNode(2));
1077:                endExpression(prec1);
1078:            }
1079:
1080:            /** Visit the specified conditional expression. */
1081:            public void visitConditionalExpression(GNode n) {
1082:                final int prec1 = startExpression(20);
1083:
1084:                final int prec2 = enterContext();
1085:                printer.p(n.getNode(0)).p(" ? ");
1086:                exitContext(prec2);
1087:
1088:                final int prec3 = enterContext();
1089:                if (null != n.get(1)) {
1090:                    printer.p(n.getNode(1)).p(" : ");
1091:                } else {
1092:                    printer.p(" /* Empty */ : ");
1093:                }
1094:                exitContext(prec3);
1095:
1096:                printer.p(n.getNode(2));
1097:                endExpression(prec1);
1098:            }
1099:
1100:            /** Visit the specified logical or expression. */
1101:            public void visitLogicalOrExpression(GNode n) {
1102:                final int prec1 = startExpression(30);
1103:                printer.p(n.getNode(0));
1104:                printer.p(" || ");
1105:                final int prec2 = enterContext();
1106:                printer.p(n.getNode(1));
1107:                exitContext(prec2);
1108:                endExpression(prec1);
1109:            }
1110:
1111:            /** Visit the specified logical and expression. */
1112:            public void visitLogicalAndExpression(GNode n) {
1113:                final int prec1 = startExpression(40);
1114:                printer.p(n.getNode(0));
1115:                printer.p(" && ");
1116:                int prec2 = enterContext();
1117:                printer.p(n.getNode(1));
1118:                exitContext(prec2);
1119:                endExpression(prec1);
1120:            }
1121:
1122:            /** Visit the specified bitwise or expression. */
1123:            public void visitBitwiseOrExpression(GNode n) {
1124:                final int prec1 = startExpression(50);
1125:                printer.p(n.getNode(0));
1126:                printer.p(" | ");
1127:                final int prec2 = enterContext();
1128:                printer.p(n.getNode(1));
1129:                exitContext(prec2);
1130:                endExpression(prec1);
1131:            }
1132:
1133:            /** Visit the specified bitwise xor expression. */
1134:            public void visitBitwiseXorExpression(GNode n) {
1135:                final int prec1 = startExpression(60);
1136:                printer.p(n.getNode(0));
1137:                printer.p(" ^ ");
1138:                final int prec2 = enterContext();
1139:                printer.p(n.getNode(1));
1140:                exitContext(prec2);
1141:                endExpression(prec1);
1142:            }
1143:
1144:            /** Visit the specified bitwise and expression. */
1145:            public void visitBitwiseAndExpression(GNode n) {
1146:                final int prec1 = startExpression(70);
1147:                printer.p(n.getNode(0)).p(" & ");
1148:                final int prec2 = enterContext();
1149:                printer.p(n.getNode(1));
1150:                exitContext(prec2);
1151:                endExpression(prec1);
1152:            }
1153:
1154:            /** Visit the specified equality expression. */
1155:            public void visitEqualityExpression(GNode n) {
1156:                final int prec1 = startExpression(80);
1157:                printer.p(n.getNode(0)).p(' ').p(n.getString(1)).p(' ');
1158:                final int prec2 = enterContext();
1159:                printer.p(n.getNode(2));
1160:                exitContext(prec2);
1161:                endExpression(prec1);
1162:            }
1163:
1164:            /** Visit the specified instance of expression. */
1165:            public void visitInstanceOfExpression(GNode n) {
1166:                final int prec1 = startExpression(90);
1167:                printer.p(n.getNode(0)).p(' ').p("instanceof").p(' ');
1168:                final int prec2 = enterContext();
1169:                printer.p(n.getNode(1));
1170:                exitContext(prec2);
1171:                endExpression(prec1);
1172:            }
1173:
1174:            /** Visit the specified relational expression. */
1175:            public void visitRelationalExpression(GNode n) {
1176:                final int prec1 = startExpression(100);
1177:                printer.p(n.getNode(0)).p(' ').p(n.getString(1)).p(' ');
1178:                final int prec2 = enterContext();
1179:                printer.p(n.getNode(2));
1180:                exitContext(prec2);
1181:
1182:                endExpression(prec1);
1183:            }
1184:
1185:            /** Visit the specified shift expression. */
1186:            public void visitShiftExpression(GNode n) {
1187:                final int prec1 = startExpression(110);
1188:                printer.p(n.getNode(0));
1189:                printer.p(' ').p(n.getString(1)).p(' ');
1190:                final int prec2 = enterContext();
1191:                printer.p(n.getNode(2));
1192:                exitContext(prec2);
1193:                endExpression(prec1);
1194:            }
1195:
1196:            /** Visit the specified additive expression. */
1197:            public void visitAdditiveExpression(GNode n) {
1198:                final int prec1 = startExpression(120);
1199:                printer.p(n.getNode(0)).p(' ').p(n.getString(1)).p(' ');
1200:
1201:                final int prec2 = enterContext();
1202:                printer.p(n.getNode(2));
1203:                exitContext(prec2);
1204:
1205:                endExpression(prec1);
1206:            }
1207:
1208:            /** Visit the specified multiplicative expression. */
1209:            public void visitMultiplicativeExpression(GNode n) {
1210:                final int prec1 = startExpression(130);
1211:                printer.p(n.getNode(0)).p(' ').p(n.getString(1)).p(' ');
1212:
1213:                final int prec2 = enterContext();
1214:                printer.p(n.getNode(2));
1215:                exitContext(prec2);
1216:
1217:                endExpression(prec1);
1218:            }
1219:
1220:            /** Visit the specified unary expression. */
1221:            public void visitUnaryExpression(GNode n) {
1222:                final int prec = startExpression(150);
1223:                printer.p(n.getString(0)).p(n.getNode(1));
1224:                endExpression(prec);
1225:            }
1226:
1227:            /** Visit the specified bitwise negation expression. */
1228:            public void visitBitwiseNegationExpression(GNode n) {
1229:                final int prec = startExpression(150);
1230:                printer.p('~').p(n.getNode(0));
1231:                endExpression(prec);
1232:            }
1233:
1234:            /** Visit the specified logical negation expression. */
1235:            public void visitLogicalNegationExpression(GNode n) {
1236:                final int prec = startExpression(150);
1237:                printer.p('!').p(n.getNode(0));
1238:                endExpression(prec);
1239:            }
1240:
1241:            /** Visit the specified basic cast expression. */
1242:            public void visitBasicCastExpression(GNode n) {
1243:                final int prec = startExpression(140);
1244:                printer.p('(').p(n.getNode(0));
1245:                if (null != n.get(1)) {
1246:                    printer.p(n.getNode(1));
1247:                }
1248:                printer.p(')').p(n.getNode(2));
1249:
1250:                endExpression(prec);
1251:            }
1252:
1253:            /** Visit the specified cast expression. */
1254:            public void visitCastExpression(GNode n) {
1255:                final int prec = startExpression(140);
1256:                printer.p('(').p(n.getNode(0)).p(')').p(n.getNode(1));
1257:                endExpression(prec);
1258:            }
1259:
1260:            /** Visit the specified call expression. */
1261:            public void visitCallExpression(GNode n) {
1262:                final int prec = startExpression(160);
1263:                if (null != n.get(0))
1264:                    printer.p(n.getNode(0)).p('.');
1265:                printer.p(n.getNode(1)).p(n.getString(2)).p(n.getNode(3));
1266:                endExpression(prec);
1267:            }
1268:
1269:            /** Visit the specified selection expression. */
1270:            public void visitSelectionExpression(GNode n) {
1271:                final int prec = startExpression(160);
1272:                printer.p(n.getNode(0)).p('.').p(n.getString(1));
1273:                endExpression(prec);
1274:            }
1275:
1276:            /** Visit the specified subscript expression. */
1277:            public void visitSubscriptExpression(GNode n) {
1278:                final int prec1 = startExpression(160);
1279:                printer.p(n.getNode(0)).p('[');
1280:                final int prec2 = enterContext(PREC_BASE);
1281:                printer.p(n.getNode(1)).p(']');
1282:                exitContext(prec2);
1283:                endExpression(prec1);
1284:            }
1285:
1286:            /** Visit the specified postfix expression. */
1287:            public void visitPostfixExpression(GNode n) {
1288:                final int prec = startExpression(160);
1289:                printer.p(n.getNode(0)).p(n.getString(1));
1290:                endExpression(prec);
1291:            }
1292:
1293:            /** Visit the specified class literal expression. */
1294:            public void visitClassLiteralExpression(GNode n) {
1295:                final int prec = startExpression(160);
1296:                printer.p(n.getNode(0)).p(".class");
1297:                endExpression(prec);
1298:            }
1299:
1300:            /** Visit the specified this expression. */
1301:            public void visitThisExpression(GNode n) {
1302:                final int prec = startExpression(160);
1303:                if (null != n.get(0))
1304:                    printer.p(n.getNode(0)).p('.');
1305:                printer.p("this");
1306:                endExpression(prec);
1307:            }
1308:
1309:            /** Visit the specified super expression. */
1310:            public void visitSuperExpression(GNode n) {
1311:                final int prec = startExpression(160);
1312:                if (null != n.get(0))
1313:                    printer.p(n.getNode(0)).p('.');
1314:                printer.p("super");
1315:                endExpression(prec);
1316:            }
1317:
1318:            /** Visit the specified primary identifier. */
1319:            public void visitPrimaryIdentifier(GNode n) {
1320:                final int prec = startExpression(160);
1321:                printer.p(n.getString(0));
1322:                endExpression(prec);
1323:            }
1324:
1325:            /** Visit the specified new class expression. */
1326:            public void visitNewClassExpression(GNode n) {
1327:                final int prec = startExpression(160);
1328:                if (null != n.get(0))
1329:                    printer.p(n.getNode(0)).p('.');
1330:                printer.p("new ");
1331:                if (null != n.get(1))
1332:                    printer.p(n.getNode(1)).p(' ');
1333:                printer.p(n.getNode(2)).p(n.getNode(3));
1334:                if (null != n.get(4)) {
1335:                    prepareNested();
1336:                    printer.p(n.getNode(4));
1337:                }
1338:                endExpression(prec);
1339:            }
1340:
1341:            /** Visit the specified new array expression. */
1342:            public void visitNewArrayExpression(GNode n) {
1343:                final int prec = startExpression(160);
1344:                printer.p("new ").p(n.getNode(0)).p(n.getNode(1)).p(
1345:                        n.getNode(2));
1346:                if (null != n.get(3))
1347:                    printer.p(' ').p(n.getNode(3));
1348:                endExpression(prec);
1349:            }
1350:
1351:            /** Visit the specified concrete dimensions. */
1352:            public void visitConcreteDimensions(GNode n) {
1353:                for (Object o : n)
1354:                    printer.p('[').p((Node) o).p(']');
1355:            }
1356:
1357:            /** Visit the specified array initlizer. */
1358:            public void visitArrayInitializer(GNode n) {
1359:                if (!n.isEmpty()) {
1360:                    printer.pln('{').incr().indent();
1361:                    for (Iterator<Object> iter = n.iterator(); iter.hasNext();) {
1362:                        printer.buffer().p((Node) iter.next());
1363:                        if (iter.hasNext())
1364:                            printer.p(", ");
1365:                        printer.fit();
1366:                    }
1367:                    printer.pln().decr().indent().p('}');
1368:                } else {
1369:                    printer.p("{ }");
1370:                }
1371:            }
1372:
1373:            /** Visit the specified arguments. */
1374:            public void visitArguments(GNode n) {
1375:                printer.p('(');
1376:                for (Iterator<Object> iter = n.iterator(); iter.hasNext();) {
1377:                    final int prec = enterContext(PREC_LIST);
1378:                    printer.p((Node) iter.next());
1379:                    exitContext(prec);
1380:                    if (iter.hasNext())
1381:                        printer.p(", ");
1382:                }
1383:                printer.p(')');
1384:            }
1385:
1386:            /** Visit the specified void type specifier. */
1387:            public void visitVoidType(GNode n) {
1388:                printer.p("void");
1389:            }
1390:
1391:            /** Visit the specified type. */
1392:            public void visitType(GNode n) {
1393:                printer.p(n.getNode(0));
1394:                if (null != n.get(1)) {
1395:                    if (Token.test(n.get(1))) {
1396:                        formatDimensions(n.getString(1).length());
1397:                    } else {
1398:                        printer.p(' ').p(n.getNode(1)).p(' ');
1399:                    }
1400:                }
1401:            }
1402:
1403:            /** Visit the specified primitive type. */
1404:            public void visitPrimitiveType(GNode n) {
1405:                printer.p(n.getString(0));
1406:            }
1407:
1408:            /** Visit the secified reference type. */
1409:            public void visitInstantiatedType(GNode n) {
1410:                boolean first = true;
1411:                for (Object o : n) {
1412:                    if (first)
1413:                        first = false;
1414:                    else
1415:                        printer.p('.');
1416:                    printer.p((Node) o);
1417:                }
1418:            }
1419:
1420:            /** Visit the specified type instantiation. */
1421:            public void visitTypeInstantiation(GNode n) {
1422:                printer.p(n.getString(0)).p(n.getNode(1));
1423:            }
1424:
1425:            /** Visit the specified dimensions. */
1426:            public void visitDimensions(GNode n) {
1427:                for (int i = 0; i < n.size(); i++)
1428:                    printer.p("[]");
1429:            }
1430:
1431:            /** Visit the specified type parameters. */
1432:            public void visitTypeParameters(GNode n) {
1433:                printer.p('<');
1434:                for (Iterator<Object> iter = n.iterator(); iter.hasNext();) {
1435:                    printer.p((Node) iter.next());
1436:                    if (iter.hasNext())
1437:                        printer.p(", ");
1438:                }
1439:                printer.p('>');
1440:            }
1441:
1442:            /** Visit the specified type parameter. */
1443:            public void visitTypeParameter(GNode n) {
1444:                printer.p(n.getString(0));
1445:                if (null != n.get(1))
1446:                    printer.p(" extends ").p(n.getNode(1));
1447:            }
1448:
1449:            /** Visit the specified bound. */
1450:            public void visitBound(GNode n) {
1451:                for (Iterator<Object> iter = n.iterator(); iter.hasNext();) {
1452:                    printer.p((Node) iter.next());
1453:                    if (iter.hasNext())
1454:                        printer.p(" & ");
1455:                }
1456:            }
1457:
1458:            /** Visit the specified type arguments. */
1459:            public void visitTypeArguments(GNode n) {
1460:                printer.p('<');
1461:                for (Iterator<Object> iter = n.iterator(); iter.hasNext();) {
1462:                    printer.p((Node) iter.next());
1463:                    if (iter.hasNext())
1464:                        printer.p(", ");
1465:                }
1466:                printer.p('>');
1467:            }
1468:
1469:            /** Visit the specified wildcard. */
1470:            public void visitWildcard(GNode n) {
1471:                printer.p('?').p(n.getNode(0));
1472:            }
1473:
1474:            /** Visit the specified wildcard bound. */
1475:            public void visitWildcardBound(GNode n) {
1476:                printer.p(' ').p(n.getString(0)).p(' ').p(n.getNode(1));
1477:            }
1478:
1479:            /** Visit the specified integer literal. */
1480:            public void visitIntegerLiteral(GNode n) {
1481:                final int prec = startExpression(160);
1482:                printer.p(n.getString(0));
1483:                endExpression(prec);
1484:            }
1485:
1486:            /** Visit the specified floating point literal. */
1487:            public void visitFloatingPointLiteral(GNode n) {
1488:                final int prec = startExpression(160);
1489:                printer.p(n.getString(0));
1490:                endExpression(prec);
1491:            }
1492:
1493:            /** Visit the specified character literal. */
1494:            public void visitCharacterLiteral(GNode n) {
1495:                final int prec = startExpression(160);
1496:                printer.p(n.getString(0));
1497:                endExpression(prec);
1498:            }
1499:
1500:            /** Visit the specified string literal. */
1501:            public void visitStringLiteral(GNode n) {
1502:                final int prec = startExpression(160);
1503:                printer.p(n.getString(0));
1504:                endExpression(prec);
1505:            }
1506:
1507:            /** Visit the specified boolean literal. */
1508:            public void visitBooleanLiteral(GNode n) {
1509:                final int prec = startExpression(160);
1510:                printer.p(n.getString(0));
1511:                endExpression(prec);
1512:            }
1513:
1514:            /** Visit the specified null literal. */
1515:            public void visitNullLiteral(GNode n) {
1516:                final int prec = startExpression(160);
1517:                printer.p("null");
1518:                endExpression(prec);
1519:            }
1520:
1521:            /** Visit the specified qualified identifier. */
1522:            public void visitQualifiedIdentifier(GNode n) {
1523:                final int prec = startExpression(160);
1524:
1525:                if (1 == n.size()) {
1526:                    printer.p(n.getString(0));
1527:                } else {
1528:                    for (Iterator<Object> iter = n.iterator(); iter.hasNext();) {
1529:                        printer.p(Token.cast(iter.next()));
1530:                        if (iter.hasNext())
1531:                            printer.p('.');
1532:                    }
1533:                }
1534:
1535:                endExpression(prec);
1536:            }
1537:
1538:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.