You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AjNaiveASTFlattener.java 46KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786
  1. /*******************************************************************************
  2. * Copyright (c) 2000, 2010 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v 2.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  7. *
  8. * Contributors:
  9. * IBM Corporation - initial API and implementation
  10. * Nieraj Singh
  11. *******************************************************************************/
  12. package org.aspectj.org.eclipse.jdt.core.dom;
  13. import java.util.Iterator;
  14. import java.util.List;
  15. /**
  16. * Internal AST visitor for serializing an AST in a quick and dirty fashion.
  17. * For various reasons the resulting string is not necessarily legal
  18. * Java code; and even if it is legal Java code, it is not necessarily the string
  19. * that corresponds to the given AST. Although useless for most purposes, it's
  20. * fine for generating debug print strings.
  21. * <p>
  22. * Example usage:
  23. * </p>
  24. * <pre>
  25. * <code>
  26. * NaiveASTFlattener p = new NaiveASTFlattener();
  27. * node.accept(p);
  28. * String result = p.getResult();
  29. * </code>
  30. * </pre>
  31. * <p>
  32. * Call the <code>reset</code> method to clear the previous result before reusing an
  33. * existing instance.
  34. * </p>
  35. *
  36. * @since 2.0
  37. */
  38. public class AjNaiveASTFlattener extends AjASTVisitor {
  39. /**
  40. * The string buffer into which the serialized representation of the AST is
  41. * written.
  42. */
  43. protected StringBuffer buffer;
  44. private int indent = 0;
  45. /**
  46. * Creates a new AST printer.
  47. */
  48. public AjNaiveASTFlattener() {
  49. this.buffer = new StringBuffer();
  50. }
  51. /**
  52. * Returns the string accumulated in the visit.
  53. *
  54. * @return the serialized
  55. */
  56. public String getResult() {
  57. return this.buffer.toString();
  58. }
  59. /**
  60. * Resets this printer so that it can be used again.
  61. */
  62. public void reset() {
  63. this.buffer.setLength(0);
  64. }
  65. void printIndent() {
  66. for (int i = 0; i < this.indent; i++)
  67. this.buffer.append(" "); //$NON-NLS-1$
  68. }
  69. /**
  70. * Appends the text representation of the given modifier flags, followed by a single space.
  71. * Used for 3.0 modifiers and annotations.
  72. *
  73. * @param ext the list of modifier and annotation nodes
  74. * (element type: <code>IExtendedModifiers</code>)
  75. */
  76. void printModifiers(List<ASTNode> ext) {
  77. for (ASTNode p : ext) {
  78. p.accept(this);
  79. this.buffer.append(" ");//$NON-NLS-1$
  80. }
  81. }
  82. /**
  83. * Appends the text representation of the given modifier flags, followed by a single space.
  84. * Used for JLS2 modifiers.
  85. *
  86. * @param modifiers the modifier flags
  87. */
  88. void printModifiers(int modifiers) {
  89. if (Modifier.isPublic(modifiers)) {
  90. this.buffer.append("public ");//$NON-NLS-1$
  91. }
  92. if (Modifier.isProtected(modifiers)) {
  93. this.buffer.append("protected ");//$NON-NLS-1$
  94. }
  95. if (Modifier.isPrivate(modifiers)) {
  96. this.buffer.append("private ");//$NON-NLS-1$
  97. }
  98. if (Modifier.isStatic(modifiers)) {
  99. this.buffer.append("static ");//$NON-NLS-1$
  100. }
  101. if (Modifier.isAbstract(modifiers)) {
  102. this.buffer.append("abstract ");//$NON-NLS-1$
  103. }
  104. if (Modifier.isFinal(modifiers)) {
  105. this.buffer.append("final ");//$NON-NLS-1$
  106. }
  107. if (Modifier.isSynchronized(modifiers)) {
  108. this.buffer.append("synchronized ");//$NON-NLS-1$
  109. }
  110. if (Modifier.isVolatile(modifiers)) {
  111. this.buffer.append("volatile ");//$NON-NLS-1$
  112. }
  113. if (Modifier.isNative(modifiers)) {
  114. this.buffer.append("native ");//$NON-NLS-1$
  115. }
  116. if (Modifier.isStrictfp(modifiers)) {
  117. this.buffer.append("strictfp ");//$NON-NLS-1$
  118. }
  119. if (Modifier.isTransient(modifiers)) {
  120. this.buffer.append("transient ");//$NON-NLS-1$
  121. }
  122. }
  123. /*
  124. * @see ASTVisitor#visit(AnnotationTypeDeclaration)
  125. * @since 3.1
  126. */
  127. public boolean visit(AnnotationTypeDeclaration node) {
  128. if (node.getJavadoc() != null) {
  129. node.getJavadoc().accept(this);
  130. }
  131. printIndent();
  132. printModifiers(node.modifiers());
  133. this.buffer.append("@interface ");//$NON-NLS-1$
  134. node.getName().accept(this);
  135. this.buffer.append(" {");//$NON-NLS-1$
  136. for (Object o : node.bodyDeclarations()) {
  137. BodyDeclaration d = (BodyDeclaration) o;
  138. d.accept(this);
  139. }
  140. this.buffer.append("}\n");//$NON-NLS-1$
  141. return false;
  142. }
  143. /*
  144. * @see ASTVisitor#visit(AnnotationTypeMemberDeclaration)
  145. * @since 3.1
  146. */
  147. public boolean visit(AnnotationTypeMemberDeclaration node) {
  148. if (node.getJavadoc() != null) {
  149. node.getJavadoc().accept(this);
  150. }
  151. printIndent();
  152. printModifiers(node.modifiers());
  153. node.getType().accept(this);
  154. this.buffer.append(" ");//$NON-NLS-1$
  155. node.getName().accept(this);
  156. this.buffer.append("()");//$NON-NLS-1$
  157. if (node.getDefault() != null) {
  158. this.buffer.append(" default ");//$NON-NLS-1$
  159. node.getDefault().accept(this);
  160. }
  161. this.buffer.append(";\n");//$NON-NLS-1$
  162. return false;
  163. }
  164. /*
  165. * @see ASTVisitor#visit(AnonymousClassDeclaration)
  166. */
  167. public boolean visit(AnonymousClassDeclaration node) {
  168. this.buffer.append("{\n");//$NON-NLS-1$
  169. this.indent++;
  170. for (Object o : node.bodyDeclarations()) {
  171. BodyDeclaration b = (BodyDeclaration) o;
  172. b.accept(this);
  173. }
  174. this.indent--;
  175. printIndent();
  176. this.buffer.append("}\n");//$NON-NLS-1$
  177. return false;
  178. }
  179. /*
  180. * @see ASTVisitor#visit(ArrayAccess)
  181. */
  182. public boolean visit(ArrayAccess node) {
  183. node.getArray().accept(this);
  184. this.buffer.append("[");//$NON-NLS-1$
  185. node.getIndex().accept(this);
  186. this.buffer.append("]");//$NON-NLS-1$
  187. return false;
  188. }
  189. /*
  190. * @see ASTVisitor#visit(ArrayCreation)
  191. */
  192. public boolean visit(ArrayCreation node) {
  193. this.buffer.append("new ");//$NON-NLS-1$
  194. ArrayType at = node.getType();
  195. int dims = at.getDimensions();
  196. Type elementType = at.getElementType();
  197. elementType.accept(this);
  198. for (Object o : node.dimensions()) {
  199. this.buffer.append("[");//$NON-NLS-1$
  200. Expression e = (Expression) o;
  201. e.accept(this);
  202. this.buffer.append("]");//$NON-NLS-1$
  203. dims--;
  204. }
  205. // add empty "[]" for each extra array dimension
  206. for (int i= 0; i < dims; i++) {
  207. this.buffer.append("[]");//$NON-NLS-1$
  208. }
  209. if (node.getInitializer() != null) {
  210. node.getInitializer().accept(this);
  211. }
  212. return false;
  213. }
  214. /*
  215. * @see ASTVisitor#visit(ArrayInitializer)
  216. */
  217. public boolean visit(ArrayInitializer node) {
  218. this.buffer.append("{");//$NON-NLS-1$
  219. for (Iterator it = node.expressions().iterator(); it.hasNext(); ) {
  220. Expression e = (Expression) it.next();
  221. e.accept(this);
  222. if (it.hasNext()) {
  223. this.buffer.append(",");//$NON-NLS-1$
  224. }
  225. }
  226. this.buffer.append("}");//$NON-NLS-1$
  227. return false;
  228. }
  229. /*
  230. * @see ASTVisitor#visit(ArrayType)
  231. */
  232. public boolean visit(ArrayType node) {
  233. node.getComponentType().accept(this);
  234. this.buffer.append("[]");//$NON-NLS-1$
  235. return false;
  236. }
  237. /*
  238. * @see ASTVisitor#visit(AssertStatement)
  239. */
  240. public boolean visit(AssertStatement node) {
  241. printIndent();
  242. this.buffer.append("assert ");//$NON-NLS-1$
  243. node.getExpression().accept(this);
  244. if (node.getMessage() != null) {
  245. this.buffer.append(" : ");//$NON-NLS-1$
  246. node.getMessage().accept(this);
  247. }
  248. this.buffer.append(";\n");//$NON-NLS-1$
  249. return false;
  250. }
  251. /*
  252. * @see ASTVisitor#visit(Assignment)
  253. */
  254. public boolean visit(Assignment node) {
  255. node.getLeftHandSide().accept(this);
  256. this.buffer.append(node.getOperator().toString());
  257. node.getRightHandSide().accept(this);
  258. return false;
  259. }
  260. /*
  261. * @see ASTVisitor#visit(Block)
  262. */
  263. public boolean visit(Block node) {
  264. this.buffer.append("{\n");//$NON-NLS-1$
  265. this.indent++;
  266. for (Object o : node.statements()) {
  267. Statement s = (Statement) o;
  268. s.accept(this);
  269. }
  270. this.indent--;
  271. printIndent();
  272. this.buffer.append("}\n");//$NON-NLS-1$
  273. return false;
  274. }
  275. /*
  276. * @see ASTVisitor#visit(BlockComment)
  277. * @since 3.0
  278. */
  279. public boolean visit(BlockComment node) {
  280. printIndent();
  281. this.buffer.append("/* */");//$NON-NLS-1$
  282. return false;
  283. }
  284. /*
  285. * @see ASTVisitor#visit(BooleanLiteral)
  286. */
  287. public boolean visit(BooleanLiteral node) {
  288. if (node.booleanValue() == true) {
  289. this.buffer.append("true");//$NON-NLS-1$
  290. } else {
  291. this.buffer.append("false");//$NON-NLS-1$
  292. }
  293. return false;
  294. }
  295. /*
  296. * @see ASTVisitor#visit(BreakStatement)
  297. */
  298. public boolean visit(BreakStatement node) {
  299. printIndent();
  300. this.buffer.append("break");//$NON-NLS-1$
  301. if (node.getLabel() != null) {
  302. this.buffer.append(" ");//$NON-NLS-1$
  303. node.getLabel().accept(this);
  304. }
  305. this.buffer.append(";\n");//$NON-NLS-1$
  306. return false;
  307. }
  308. /*
  309. * @see ASTVisitor#visit(CastExpression)
  310. */
  311. public boolean visit(CastExpression node) {
  312. this.buffer.append("(");//$NON-NLS-1$
  313. node.getType().accept(this);
  314. this.buffer.append(")");//$NON-NLS-1$
  315. node.getExpression().accept(this);
  316. return false;
  317. }
  318. /*
  319. * @see ASTVisitor#visit(CatchClause)
  320. */
  321. public boolean visit(CatchClause node) {
  322. this.buffer.append("catch (");//$NON-NLS-1$
  323. node.getException().accept(this);
  324. this.buffer.append(") ");//$NON-NLS-1$
  325. node.getBody().accept(this);
  326. return false;
  327. }
  328. /*
  329. * @see ASTVisitor#visit(CharacterLiteral)
  330. */
  331. public boolean visit(CharacterLiteral node) {
  332. this.buffer.append(node.getEscapedValue());
  333. return false;
  334. }
  335. /*
  336. * @see ASTVisitor#visit(ClassInstanceCreation)
  337. */
  338. public boolean visit(ClassInstanceCreation node) {
  339. if (node.getExpression() != null) {
  340. node.getExpression().accept(this);
  341. this.buffer.append(".");//$NON-NLS-1$
  342. }
  343. this.buffer.append("new ");//$NON-NLS-1$
  344. if (node.getAST().apiLevel() == AST.JLS2_INTERNAL) {
  345. node.internalGetName().accept(this);
  346. }
  347. if (node.getAST().apiLevel() >= AST.JLS3) {
  348. if (!node.typeArguments().isEmpty()) {
  349. this.buffer.append("<");//$NON-NLS-1$
  350. for (Iterator it = node.typeArguments().iterator(); it.hasNext(); ) {
  351. Type t = (Type) it.next();
  352. t.accept(this);
  353. if (it.hasNext()) {
  354. this.buffer.append(",");//$NON-NLS-1$
  355. }
  356. }
  357. this.buffer.append(">");//$NON-NLS-1$
  358. }
  359. node.getType().accept(this);
  360. }
  361. this.buffer.append("(");//$NON-NLS-1$
  362. for (Iterator it = node.arguments().iterator(); it.hasNext(); ) {
  363. Expression e = (Expression) it.next();
  364. e.accept(this);
  365. if (it.hasNext()) {
  366. this.buffer.append(",");//$NON-NLS-1$
  367. }
  368. }
  369. this.buffer.append(")");//$NON-NLS-1$
  370. if (node.getAnonymousClassDeclaration() != null) {
  371. node.getAnonymousClassDeclaration().accept(this);
  372. }
  373. return false;
  374. }
  375. /*
  376. * @see ASTVisitor#visit(CompilationUnit)
  377. */
  378. public boolean visit(CompilationUnit node) {
  379. if (node.getPackage() != null) {
  380. node.getPackage().accept(this);
  381. }
  382. for (Object value : node.imports()) {
  383. ImportDeclaration d = (ImportDeclaration) value;
  384. d.accept(this);
  385. }
  386. for (Object o : node.types()) {
  387. AbstractTypeDeclaration d = (AbstractTypeDeclaration) o;
  388. d.accept(this);
  389. }
  390. return false;
  391. }
  392. /*
  393. * @see ASTVisitor#visit(ConditionalExpression)
  394. */
  395. public boolean visit(ConditionalExpression node) {
  396. node.getExpression().accept(this);
  397. this.buffer.append(" ? ");//$NON-NLS-1$
  398. node.getThenExpression().accept(this);
  399. this.buffer.append(" : ");//$NON-NLS-1$
  400. node.getElseExpression().accept(this);
  401. return false;
  402. }
  403. /*
  404. * @see ASTVisitor#visit(ConstructorInvocation)
  405. */
  406. public boolean visit(ConstructorInvocation node) {
  407. printIndent();
  408. if (node.getAST().apiLevel() >= AST.JLS3) {
  409. if (!node.typeArguments().isEmpty()) {
  410. this.buffer.append("<");//$NON-NLS-1$
  411. for (Iterator it = node.typeArguments().iterator(); it.hasNext(); ) {
  412. Type t = (Type) it.next();
  413. t.accept(this);
  414. if (it.hasNext()) {
  415. this.buffer.append(",");//$NON-NLS-1$
  416. }
  417. }
  418. this.buffer.append(">");//$NON-NLS-1$
  419. }
  420. }
  421. this.buffer.append("this(");//$NON-NLS-1$
  422. for (Iterator it = node.arguments().iterator(); it.hasNext(); ) {
  423. Expression e = (Expression) it.next();
  424. e.accept(this);
  425. if (it.hasNext()) {
  426. this.buffer.append(",");//$NON-NLS-1$
  427. }
  428. }
  429. this.buffer.append(");\n");//$NON-NLS-1$
  430. return false;
  431. }
  432. /*
  433. * @see ASTVisitor#visit(ContinueStatement)
  434. */
  435. public boolean visit(ContinueStatement node) {
  436. printIndent();
  437. this.buffer.append("continue");//$NON-NLS-1$
  438. if (node.getLabel() != null) {
  439. this.buffer.append(" ");//$NON-NLS-1$
  440. node.getLabel().accept(this);
  441. }
  442. this.buffer.append(";\n");//$NON-NLS-1$
  443. return false;
  444. }
  445. /*
  446. * @see ASTVisitor#visit(DoStatement)
  447. */
  448. public boolean visit(DoStatement node) {
  449. printIndent();
  450. this.buffer.append("do ");//$NON-NLS-1$
  451. node.getBody().accept(this);
  452. this.buffer.append(" while (");//$NON-NLS-1$
  453. node.getExpression().accept(this);
  454. this.buffer.append(");\n");//$NON-NLS-1$
  455. return false;
  456. }
  457. /*
  458. * @see ASTVisitor#visit(EmptyStatement)
  459. */
  460. public boolean visit(EmptyStatement node) {
  461. printIndent();
  462. this.buffer.append(";\n");//$NON-NLS-1$
  463. return false;
  464. }
  465. /*
  466. * @see ASTVisitor#visit(EnhancedForStatement)
  467. * @since 3.1
  468. */
  469. public boolean visit(EnhancedForStatement node) {
  470. printIndent();
  471. this.buffer.append("for (");//$NON-NLS-1$
  472. node.getParameter().accept(this);
  473. this.buffer.append(" : ");//$NON-NLS-1$
  474. node.getExpression().accept(this);
  475. this.buffer.append(") ");//$NON-NLS-1$
  476. node.getBody().accept(this);
  477. return false;
  478. }
  479. public boolean visit(PointcutDeclaration node) {
  480. printIndent();
  481. buffer.append(" pointcut ");
  482. node.getName().accept(this);
  483. buffer.append("(");
  484. List parameters = node.parameters();
  485. for (Iterator iter = parameters.iterator(); iter.hasNext();) {
  486. SingleVariableDeclaration element = (SingleVariableDeclaration) iter.next();
  487. buffer.append(element.getType().toString()+" "+element.getName());
  488. if (iter.hasNext())
  489. buffer.append(", ");
  490. }
  491. buffer.append("):");
  492. node.getDesignator().accept(this);
  493. buffer.append(";\n");
  494. return false;
  495. }
  496. /*
  497. * @see ASTVisitor#visit(EnumConstantDeclaration)
  498. * @since 3.1
  499. */
  500. public boolean visit(EnumConstantDeclaration node) {
  501. if (node.getJavadoc() != null) {
  502. node.getJavadoc().accept(this);
  503. }
  504. printIndent();
  505. printModifiers(node.modifiers());
  506. node.getName().accept(this);
  507. if (!node.arguments().isEmpty()) {
  508. this.buffer.append("(");//$NON-NLS-1$
  509. for (Iterator it = node.arguments().iterator(); it.hasNext(); ) {
  510. Expression e = (Expression) it.next();
  511. e.accept(this);
  512. if (it.hasNext()) {
  513. this.buffer.append(",");//$NON-NLS-1$
  514. }
  515. }
  516. this.buffer.append(")");//$NON-NLS-1$
  517. }
  518. if (node.getAnonymousClassDeclaration() != null) {
  519. node.getAnonymousClassDeclaration().accept(this);
  520. }
  521. return false;
  522. }
  523. /*
  524. * @see ASTVisitor#visit(EnumDeclaration)
  525. * @since 3.1
  526. */
  527. public boolean visit(EnumDeclaration node) {
  528. if (node.getJavadoc() != null) {
  529. node.getJavadoc().accept(this);
  530. }
  531. printIndent();
  532. printModifiers(node.modifiers());
  533. this.buffer.append("enum ");//$NON-NLS-1$
  534. node.getName().accept(this);
  535. this.buffer.append(" ");//$NON-NLS-1$
  536. if (!node.superInterfaceTypes().isEmpty()) {
  537. this.buffer.append("implements ");//$NON-NLS-1$
  538. for (Iterator it = node.superInterfaceTypes().iterator(); it.hasNext(); ) {
  539. Type t = (Type) it.next();
  540. t.accept(this);
  541. if (it.hasNext()) {
  542. this.buffer.append(", ");//$NON-NLS-1$
  543. }
  544. }
  545. this.buffer.append(" ");//$NON-NLS-1$
  546. }
  547. this.buffer.append("{");//$NON-NLS-1$
  548. for (Iterator it = node.enumConstants().iterator(); it.hasNext(); ) {
  549. EnumConstantDeclaration d = (EnumConstantDeclaration) it.next();
  550. d.accept(this);
  551. // enum constant declarations do not include punctuation
  552. if (it.hasNext()) {
  553. // enum constant declarations are separated by commas
  554. this.buffer.append(", ");//$NON-NLS-1$
  555. }
  556. }
  557. if (!node.bodyDeclarations().isEmpty()) {
  558. this.buffer.append("; ");//$NON-NLS-1$
  559. for (Object o : node.bodyDeclarations()) {
  560. BodyDeclaration d = (BodyDeclaration) o;
  561. d.accept(this);
  562. // other body declarations include trailing punctuation
  563. }
  564. }
  565. this.buffer.append("}\n");//$NON-NLS-1$
  566. return false;
  567. }
  568. /*
  569. * @see ASTVisitor#visit(ExpressionStatement)
  570. */
  571. public boolean visit(ExpressionStatement node) {
  572. printIndent();
  573. node.getExpression().accept(this);
  574. this.buffer.append(";\n");//$NON-NLS-1$
  575. return false;
  576. }
  577. /*
  578. * @see ASTVisitor#visit(FieldAccess)
  579. */
  580. public boolean visit(FieldAccess node) {
  581. node.getExpression().accept(this);
  582. this.buffer.append(".");//$NON-NLS-1$
  583. node.getName().accept(this);
  584. return false;
  585. }
  586. /*
  587. * @see ASTVisitor#visit(FieldDeclaration)
  588. */
  589. public boolean visit(FieldDeclaration node) {
  590. if (node.getJavadoc() != null) {
  591. node.getJavadoc().accept(this);
  592. }
  593. printIndent();
  594. if (node.getAST().apiLevel() == AST.JLS2_INTERNAL) {
  595. printModifiers(node.getModifiers());
  596. }
  597. if (node.getAST().apiLevel() >= AST.JLS3) {
  598. printModifiers(node.modifiers());
  599. }
  600. node.getType().accept(this);
  601. this.buffer.append(" ");//$NON-NLS-1$
  602. for (Iterator it = node.fragments().iterator(); it.hasNext(); ) {
  603. VariableDeclarationFragment f = (VariableDeclarationFragment) it.next();
  604. f.accept(this);
  605. if (it.hasNext()) {
  606. this.buffer.append(", ");//$NON-NLS-1$
  607. }
  608. }
  609. this.buffer.append(";\n");//$NON-NLS-1$
  610. return false;
  611. }
  612. /*
  613. * @see ASTVisitor#visit(ForStatement)
  614. */
  615. public boolean visit(ForStatement node) {
  616. printIndent();
  617. this.buffer.append("for (");//$NON-NLS-1$
  618. for (Iterator it = node.initializers().iterator(); it.hasNext(); ) {
  619. Expression e = (Expression) it.next();
  620. e.accept(this);
  621. if (it.hasNext()) buffer.append(", ");//$NON-NLS-1$
  622. }
  623. this.buffer.append("; ");//$NON-NLS-1$
  624. if (node.getExpression() != null) {
  625. node.getExpression().accept(this);
  626. }
  627. this.buffer.append("; ");//$NON-NLS-1$
  628. for (Iterator it = node.updaters().iterator(); it.hasNext(); ) {
  629. Expression e = (Expression) it.next();
  630. e.accept(this);
  631. if (it.hasNext()) buffer.append(", ");//$NON-NLS-1$
  632. }
  633. this.buffer.append(") ");//$NON-NLS-1$
  634. node.getBody().accept(this);
  635. return false;
  636. }
  637. /*
  638. * @see ASTVisitor#visit(IfStatement)
  639. */
  640. public boolean visit(IfStatement node) {
  641. printIndent();
  642. this.buffer.append("if (");//$NON-NLS-1$
  643. node.getExpression().accept(this);
  644. this.buffer.append(") ");//$NON-NLS-1$
  645. node.getThenStatement().accept(this);
  646. if (node.getElseStatement() != null) {
  647. this.buffer.append(" else ");//$NON-NLS-1$
  648. node.getElseStatement().accept(this);
  649. }
  650. return false;
  651. }
  652. /*
  653. * @see ASTVisitor#visit(ImportDeclaration)
  654. */
  655. public boolean visit(ImportDeclaration node) {
  656. printIndent();
  657. this.buffer.append("import ");//$NON-NLS-1$
  658. if (node.getAST().apiLevel() >= AST.JLS3) {
  659. if (node.isStatic()) {
  660. this.buffer.append("static ");//$NON-NLS-1$
  661. }
  662. }
  663. node.getName().accept(this);
  664. if (node.isOnDemand()) {
  665. this.buffer.append(".*");//$NON-NLS-1$
  666. }
  667. this.buffer.append(";\n");//$NON-NLS-1$
  668. return false;
  669. }
  670. /*
  671. * @see ASTVisitor#visit(InfixExpression)
  672. */
  673. public boolean visit(InfixExpression node) {
  674. node.getLeftOperand().accept(this);
  675. this.buffer.append(' '); // for cases like x= i - -1; or x= i++ + ++i;
  676. this.buffer.append(node.getOperator().toString());
  677. this.buffer.append(' ');
  678. node.getRightOperand().accept(this);
  679. final List extendedOperands = node.extendedOperands();
  680. if (extendedOperands.size() != 0) {
  681. this.buffer.append(' ');
  682. for (Object extendedOperand : extendedOperands) {
  683. this.buffer.append(node.getOperator().toString()).append(' ');
  684. Expression e = (Expression) extendedOperand;
  685. e.accept(this);
  686. }
  687. }
  688. return false;
  689. }
  690. /*
  691. * @see ASTVisitor#visit(InstanceofExpression)
  692. */
  693. public boolean visit(InstanceofExpression node) {
  694. node.getLeftOperand().accept(this);
  695. this.buffer.append(" instanceof ");//$NON-NLS-1$
  696. node.getRightOperand().accept(this);
  697. return false;
  698. }
  699. /*
  700. * @see ASTVisitor#visit(Initializer)
  701. */
  702. public boolean visit(Initializer node) {
  703. if (node.getJavadoc() != null) {
  704. node.getJavadoc().accept(this);
  705. }
  706. if (node.getAST().apiLevel() == AST.JLS2_INTERNAL) {
  707. printModifiers(node.getModifiers());
  708. }
  709. if (node.getAST().apiLevel() >= AST.JLS3) {
  710. printModifiers(node.modifiers());
  711. }
  712. node.getBody().accept(this);
  713. return false;
  714. }
  715. /*
  716. * @see ASTVisitor#visit(Javadoc)
  717. */
  718. public boolean visit(Javadoc node) {
  719. printIndent();
  720. this.buffer.append("/** ");//$NON-NLS-1$
  721. for (Object o : node.tags()) {
  722. ASTNode e = (ASTNode) o;
  723. e.accept(this);
  724. }
  725. this.buffer.append("\n */\n");//$NON-NLS-1$
  726. return false;
  727. }
  728. /*
  729. * @see ASTVisitor#visit(LabeledStatement)
  730. */
  731. public boolean visit(LabeledStatement node) {
  732. printIndent();
  733. node.getLabel().accept(this);
  734. this.buffer.append(": ");//$NON-NLS-1$
  735. node.getBody().accept(this);
  736. return false;
  737. }
  738. /*
  739. * @see ASTVisitor#visit(LineComment)
  740. * @since 3.0
  741. */
  742. public boolean visit(LineComment node) {
  743. this.buffer.append("//\n");//$NON-NLS-1$
  744. return false;
  745. }
  746. /*
  747. * @see ASTVisitor#visit(MarkerAnnotation)
  748. * @since 3.1
  749. */
  750. public boolean visit(MarkerAnnotation node) {
  751. this.buffer.append("@");//$NON-NLS-1$
  752. node.getTypeName().accept(this);
  753. return false;
  754. }
  755. /*
  756. * @see ASTVisitor#visit(MemberRef)
  757. * @since 3.0
  758. */
  759. public boolean visit(MemberRef node) {
  760. if (node.getQualifier() != null) {
  761. node.getQualifier().accept(this);
  762. }
  763. this.buffer.append("#");//$NON-NLS-1$
  764. node.getName().accept(this);
  765. return false;
  766. }
  767. /*
  768. * @see ASTVisitor#visit(MemberValuePair)
  769. * @since 3.1
  770. */
  771. public boolean visit(MemberValuePair node) {
  772. node.getName().accept(this);
  773. this.buffer.append("=");//$NON-NLS-1$
  774. node.getValue().accept(this);
  775. return false;
  776. }
  777. /*
  778. * @see ASTVisitor#visit(MethodRef)
  779. * @since 3.0
  780. */
  781. public boolean visit(MethodRef node) {
  782. if (node.getQualifier() != null) {
  783. node.getQualifier().accept(this);
  784. }
  785. this.buffer.append("#");//$NON-NLS-1$
  786. node.getName().accept(this);
  787. this.buffer.append("(");//$NON-NLS-1$
  788. for (Iterator it = node.parameters().iterator(); it.hasNext(); ) {
  789. MethodRefParameter e = (MethodRefParameter) it.next();
  790. e.accept(this);
  791. if (it.hasNext()) {
  792. this.buffer.append(",");//$NON-NLS-1$
  793. }
  794. }
  795. this.buffer.append(")");//$NON-NLS-1$
  796. return false;
  797. }
  798. /*
  799. * @see ASTVisitor#visit(MethodRefParameter)
  800. * @since 3.0
  801. */
  802. public boolean visit(MethodRefParameter node) {
  803. node.getType().accept(this);
  804. if (node.getAST().apiLevel() >= AST.JLS3) {
  805. if (node.isVarargs()) {
  806. this.buffer.append("...");//$NON-NLS-1$
  807. }
  808. }
  809. if (node.getName() != null) {
  810. this.buffer.append(" ");//$NON-NLS-1$
  811. node.getName().accept(this);
  812. }
  813. return false;
  814. }
  815. /*
  816. * @see ASTVisitor#visit(MethodDeclaration)
  817. */
  818. public boolean visit(MethodDeclaration node) {
  819. if (node.getJavadoc() != null) {
  820. node.getJavadoc().accept(this);
  821. }
  822. printIndent();
  823. if (node.getAST().apiLevel() == AST.JLS2_INTERNAL) {
  824. printModifiers(node.getModifiers());
  825. }
  826. if (node.getAST().apiLevel() >= AST.JLS3) {
  827. printModifiers(node.modifiers());
  828. if (!node.typeParameters().isEmpty()) {
  829. this.buffer.append("<");//$NON-NLS-1$
  830. for (Iterator it = node.typeParameters().iterator(); it.hasNext(); ) {
  831. TypeParameter t = (TypeParameter) it.next();
  832. t.accept(this);
  833. if (it.hasNext()) {
  834. this.buffer.append(",");//$NON-NLS-1$
  835. }
  836. }
  837. this.buffer.append(">");//$NON-NLS-1$
  838. }
  839. }
  840. if (!node.isConstructor()) {
  841. if (node.getAST().apiLevel() == AST.JLS2_INTERNAL) {
  842. node.internalGetReturnType().accept(this);
  843. } else {
  844. if (node.getReturnType2() != null) {
  845. node.getReturnType2().accept(this);
  846. } else {
  847. // methods really ought to have a return type
  848. this.buffer.append("void");//$NON-NLS-1$
  849. }
  850. }
  851. this.buffer.append(" ");//$NON-NLS-1$
  852. }
  853. node.getName().accept(this);
  854. this.buffer.append("(");//$NON-NLS-1$
  855. for (Iterator it = node.parameters().iterator(); it.hasNext(); ) {
  856. SingleVariableDeclaration v = (SingleVariableDeclaration) it.next();
  857. v.accept(this);
  858. if (it.hasNext()) {
  859. this.buffer.append(",");//$NON-NLS-1$
  860. }
  861. }
  862. this.buffer.append(")");//$NON-NLS-1$
  863. for (int i = 0; i < node.getExtraDimensions(); i++) {
  864. this.buffer.append("[]"); //$NON-NLS-1$
  865. }
  866. if (!node.thrownExceptions().isEmpty()) {
  867. this.buffer.append(" throws ");//$NON-NLS-1$
  868. for (Iterator it = node.thrownExceptions().iterator(); it.hasNext(); ) {
  869. Name n = (Name) it.next();
  870. n.accept(this);
  871. if (it.hasNext()) {
  872. this.buffer.append(", ");//$NON-NLS-1$
  873. }
  874. }
  875. this.buffer.append(" ");//$NON-NLS-1$
  876. }
  877. if (node.getBody() == null) {
  878. this.buffer.append(";\n");//$NON-NLS-1$
  879. } else {
  880. node.getBody().accept(this);
  881. }
  882. return false;
  883. }
  884. /*
  885. * @see ASTVisitor#visit(MethodInvocation)
  886. */
  887. public boolean visit(MethodInvocation node) {
  888. if (node.getExpression() != null) {
  889. node.getExpression().accept(this);
  890. this.buffer.append(".");//$NON-NLS-1$
  891. }
  892. if (node.getAST().apiLevel() >= AST.JLS3) {
  893. if (!node.typeArguments().isEmpty()) {
  894. this.buffer.append("<");//$NON-NLS-1$
  895. for (Iterator it = node.typeArguments().iterator(); it.hasNext(); ) {
  896. Type t = (Type) it.next();
  897. t.accept(this);
  898. if (it.hasNext()) {
  899. this.buffer.append(",");//$NON-NLS-1$
  900. }
  901. }
  902. this.buffer.append(">");//$NON-NLS-1$
  903. }
  904. }
  905. node.getName().accept(this);
  906. this.buffer.append("(");//$NON-NLS-1$
  907. for (Iterator it = node.arguments().iterator(); it.hasNext(); ) {
  908. Expression e = (Expression) it.next();
  909. e.accept(this);
  910. if (it.hasNext()) {
  911. this.buffer.append(",");//$NON-NLS-1$
  912. }
  913. }
  914. this.buffer.append(")");//$NON-NLS-1$
  915. return false;
  916. }
  917. /*
  918. * @see ASTVisitor#visit(Modifier)
  919. * @since 3.1
  920. */
  921. public boolean visit(Modifier node) {
  922. this.buffer.append(node.getKeyword().toString());
  923. return false;
  924. }
  925. /*
  926. * @see ASTVisitor#visit(NormalAnnotation)
  927. * @since 3.1
  928. */
  929. public boolean visit(NormalAnnotation node) {
  930. this.buffer.append("@");//$NON-NLS-1$
  931. node.getTypeName().accept(this);
  932. this.buffer.append("(");//$NON-NLS-1$
  933. for (Iterator it = node.values().iterator(); it.hasNext(); ) {
  934. MemberValuePair p = (MemberValuePair) it.next();
  935. p.accept(this);
  936. if (it.hasNext()) {
  937. this.buffer.append(",");//$NON-NLS-1$
  938. }
  939. }
  940. this.buffer.append(")");//$NON-NLS-1$
  941. return false;
  942. }
  943. /*
  944. * @see ASTVisitor#visit(NullLiteral)
  945. */
  946. public boolean visit(NullLiteral node) {
  947. this.buffer.append("null");//$NON-NLS-1$
  948. return false;
  949. }
  950. /*
  951. * @see ASTVisitor#visit(NumberLiteral)
  952. */
  953. public boolean visit(NumberLiteral node) {
  954. this.buffer.append(node.getToken());
  955. return false;
  956. }
  957. /*
  958. * @see ASTVisitor#visit(PackageDeclaration)
  959. */
  960. public boolean visit(PackageDeclaration node) {
  961. if (node.getAST().apiLevel() >= AST.JLS3) {
  962. if (node.getJavadoc() != null) {
  963. node.getJavadoc().accept(this);
  964. }
  965. for (Object o : node.annotations()) {
  966. Annotation p = (Annotation) o;
  967. p.accept(this);
  968. this.buffer.append(" ");//$NON-NLS-1$
  969. }
  970. }
  971. printIndent();
  972. this.buffer.append("package ");//$NON-NLS-1$
  973. node.getName().accept(this);
  974. this.buffer.append(";\n");//$NON-NLS-1$
  975. return false;
  976. }
  977. /*
  978. * @see ASTVisitor#visit(ParameterizedType)
  979. * @since 3.1
  980. */
  981. public boolean visit(ParameterizedType node) {
  982. node.getType().accept(this);
  983. this.buffer.append("<");//$NON-NLS-1$
  984. for (Iterator it = node.typeArguments().iterator(); it.hasNext(); ) {
  985. Type t = (Type) it.next();
  986. t.accept(this);
  987. if (it.hasNext()) {
  988. this.buffer.append(",");//$NON-NLS-1$
  989. }
  990. }
  991. this.buffer.append(">");//$NON-NLS-1$
  992. return false;
  993. }
  994. /*
  995. * @see ASTVisitor#visit(ParenthesizedExpression)
  996. */
  997. public boolean visit(ParenthesizedExpression node) {
  998. this.buffer.append("(");//$NON-NLS-1$
  999. node.getExpression().accept(this);
  1000. this.buffer.append(")");//$NON-NLS-1$
  1001. return false;
  1002. }
  1003. /*
  1004. * @see ASTVisitor#visit(PostfixExpression)
  1005. */
  1006. public boolean visit(PostfixExpression node) {
  1007. node.getOperand().accept(this);
  1008. this.buffer.append(node.getOperator().toString());
  1009. return false;
  1010. }
  1011. /*
  1012. * @see ASTVisitor#visit(PrefixExpression)
  1013. */
  1014. public boolean visit(PrefixExpression node) {
  1015. this.buffer.append(node.getOperator().toString());
  1016. node.getOperand().accept(this);
  1017. return false;
  1018. }
  1019. /*
  1020. * @see ASTVisitor#visit(PrimitiveType)
  1021. */
  1022. public boolean visit(PrimitiveType node) {
  1023. this.buffer.append(node.getPrimitiveTypeCode().toString());
  1024. return false;
  1025. }
  1026. /*
  1027. * @see ASTVisitor#visit(QualifiedName)
  1028. */
  1029. public boolean visit(QualifiedName node) {
  1030. node.getQualifier().accept(this);
  1031. this.buffer.append(".");//$NON-NLS-1$
  1032. node.getName().accept(this);
  1033. return false;
  1034. }
  1035. /*
  1036. * @see ASTVisitor#visit(QualifiedType)
  1037. * @since 3.1
  1038. */
  1039. public boolean visit(QualifiedType node) {
  1040. node.getQualifier().accept(this);
  1041. this.buffer.append(".");//$NON-NLS-1$
  1042. node.getName().accept(this);
  1043. return false;
  1044. }
  1045. /*
  1046. * @see ASTVisitor#visit(ReturnStatement)
  1047. */
  1048. public boolean visit(ReturnStatement node) {
  1049. printIndent();
  1050. this.buffer.append("return");//$NON-NLS-1$
  1051. if (node.getExpression() != null) {
  1052. this.buffer.append(" ");//$NON-NLS-1$
  1053. node.getExpression().accept(this);
  1054. }
  1055. this.buffer.append(";\n");//$NON-NLS-1$
  1056. return false;
  1057. }
  1058. /*
  1059. * @see ASTVisitor#visit(SimpleName)
  1060. */
  1061. public boolean visit(SimpleName node) {
  1062. this.buffer.append(node.getIdentifier());
  1063. return false;
  1064. }
  1065. /*
  1066. * @see ASTVisitor#visit(SimpleType)
  1067. */
  1068. public boolean visit(SimpleType node) {
  1069. return true;
  1070. }
  1071. /*
  1072. * @see ASTVisitor#visit(SingleMemberAnnotation)
  1073. * @since 3.1
  1074. */
  1075. public boolean visit(SingleMemberAnnotation node) {
  1076. this.buffer.append("@");//$NON-NLS-1$
  1077. node.getTypeName().accept(this);
  1078. this.buffer.append("(");//$NON-NLS-1$
  1079. node.getValue().accept(this);
  1080. this.buffer.append(")");//$NON-NLS-1$
  1081. return false;
  1082. }
  1083. /*
  1084. * @see ASTVisitor#visit(SingleVariableDeclaration)
  1085. */
  1086. public boolean visit(SingleVariableDeclaration node) {
  1087. printIndent();
  1088. if (node.getAST().apiLevel() == AST.JLS2_INTERNAL) {
  1089. printModifiers(node.getModifiers());
  1090. }
  1091. if (node.getAST().apiLevel() >= AST.JLS3) {
  1092. printModifiers(node.modifiers());
  1093. }
  1094. node.getType().accept(this);
  1095. if (node.getAST().apiLevel() >= AST.JLS3) {
  1096. if (node.isVarargs()) {
  1097. this.buffer.append("...");//$NON-NLS-1$
  1098. }
  1099. }
  1100. this.buffer.append(" ");//$NON-NLS-1$
  1101. node.getName().accept(this);
  1102. for (int i = 0; i < node.getExtraDimensions(); i++) {
  1103. this.buffer.append("[]"); //$NON-NLS-1$
  1104. }
  1105. if (node.getInitializer() != null) {
  1106. this.buffer.append("=");//$NON-NLS-1$
  1107. node.getInitializer().accept(this);
  1108. }
  1109. return false;
  1110. }
  1111. /*
  1112. * @see ASTVisitor#visit(StringLiteral)
  1113. */
  1114. public boolean visit(StringLiteral node) {
  1115. this.buffer.append(node.getEscapedValue());
  1116. return false;
  1117. }
  1118. /*
  1119. * @see ASTVisitor#visit(SuperConstructorInvocation)
  1120. */
  1121. public boolean visit(SuperConstructorInvocation node) {
  1122. printIndent();
  1123. if (node.getExpression() != null) {
  1124. node.getExpression().accept(this);
  1125. this.buffer.append(".");//$NON-NLS-1$
  1126. }
  1127. if (node.getAST().apiLevel() >= AST.JLS3) {
  1128. if (!node.typeArguments().isEmpty()) {
  1129. this.buffer.append("<");//$NON-NLS-1$
  1130. for (Iterator it = node.typeArguments().iterator(); it.hasNext(); ) {
  1131. Type t = (Type) it.next();
  1132. t.accept(this);
  1133. if (it.hasNext()) {
  1134. this.buffer.append(",");//$NON-NLS-1$
  1135. }
  1136. }
  1137. this.buffer.append(">");//$NON-NLS-1$
  1138. }
  1139. }
  1140. this.buffer.append("super(");//$NON-NLS-1$
  1141. for (Iterator it = node.arguments().iterator(); it.hasNext(); ) {
  1142. Expression e = (Expression) it.next();
  1143. e.accept(this);
  1144. if (it.hasNext()) {
  1145. this.buffer.append(",");//$NON-NLS-1$
  1146. }
  1147. }
  1148. this.buffer.append(");\n");//$NON-NLS-1$
  1149. return false;
  1150. }
  1151. /*
  1152. * @see ASTVisitor#visit(SuperFieldAccess)
  1153. */
  1154. public boolean visit(SuperFieldAccess node) {
  1155. if (node.getQualifier() != null) {
  1156. node.getQualifier().accept(this);
  1157. this.buffer.append(".");//$NON-NLS-1$
  1158. }
  1159. this.buffer.append("super.");//$NON-NLS-1$
  1160. node.getName().accept(this);
  1161. return false;
  1162. }
  1163. /*
  1164. * @see ASTVisitor#visit(SuperMethodInvocation)
  1165. */
  1166. public boolean visit(SuperMethodInvocation node) {
  1167. if (node.getQualifier() != null) {
  1168. node.getQualifier().accept(this);
  1169. this.buffer.append(".");//$NON-NLS-1$
  1170. }
  1171. this.buffer.append("super.");//$NON-NLS-1$
  1172. if (node.getAST().apiLevel() >= AST.JLS3) {
  1173. if (!node.typeArguments().isEmpty()) {
  1174. this.buffer.append("<");//$NON-NLS-1$
  1175. for (Iterator it = node.typeArguments().iterator(); it.hasNext(); ) {
  1176. Type t = (Type) it.next();
  1177. t.accept(this);
  1178. if (it.hasNext()) {
  1179. this.buffer.append(",");//$NON-NLS-1$
  1180. }
  1181. }
  1182. this.buffer.append(">");//$NON-NLS-1$
  1183. }
  1184. }
  1185. node.getName().accept(this);
  1186. this.buffer.append("(");//$NON-NLS-1$
  1187. for (Iterator it = node.arguments().iterator(); it.hasNext(); ) {
  1188. Expression e = (Expression) it.next();
  1189. e.accept(this);
  1190. if (it.hasNext()) {
  1191. this.buffer.append(",");//$NON-NLS-1$
  1192. }
  1193. }
  1194. this.buffer.append(")");//$NON-NLS-1$
  1195. return false;
  1196. }
  1197. /*
  1198. * @see ASTVisitor#visit(SwitchCase)
  1199. */
  1200. public boolean visit(SwitchCase node) {
  1201. if (node.isDefault()) {
  1202. this.buffer.append("default :\n");//$NON-NLS-1$
  1203. } else {
  1204. this.buffer.append("case ");//$NON-NLS-1$
  1205. node.getExpression().accept(this);
  1206. this.buffer.append(":\n");//$NON-NLS-1$
  1207. }
  1208. this.indent++; //decremented in visit(SwitchStatement)
  1209. return false;
  1210. }
  1211. /*
  1212. * @see ASTVisitor#visit(SwitchStatement)
  1213. */
  1214. public boolean visit(SwitchStatement node) {
  1215. this.buffer.append("switch (");//$NON-NLS-1$
  1216. node.getExpression().accept(this);
  1217. this.buffer.append(") ");//$NON-NLS-1$
  1218. this.buffer.append("{\n");//$NON-NLS-1$
  1219. this.indent++;
  1220. for (Object o : node.statements()) {
  1221. Statement s = (Statement) o;
  1222. s.accept(this);
  1223. this.indent--; // incremented in visit(SwitchCase)
  1224. }
  1225. this.indent--;
  1226. printIndent();
  1227. this.buffer.append("}\n");//$NON-NLS-1$
  1228. return false;
  1229. }
  1230. /*
  1231. * @see ASTVisitor#visit(SynchronizedStatement)
  1232. */
  1233. public boolean visit(SynchronizedStatement node) {
  1234. this.buffer.append("synchronized (");//$NON-NLS-1$
  1235. node.getExpression().accept(this);
  1236. this.buffer.append(") ");//$NON-NLS-1$
  1237. node.getBody().accept(this);
  1238. return false;
  1239. }
  1240. /*
  1241. * @see ASTVisitor#visit(TagElement)
  1242. * @since 3.0
  1243. */
  1244. public boolean visit(TagElement node) {
  1245. if (node.isNested()) {
  1246. // nested tags are always enclosed in braces
  1247. this.buffer.append("{");//$NON-NLS-1$
  1248. } else {
  1249. // top-level tags always begin on a new line
  1250. this.buffer.append("\n * ");//$NON-NLS-1$
  1251. }
  1252. boolean previousRequiresWhiteSpace = false;
  1253. if (node.getTagName() != null) {
  1254. this.buffer.append(node.getTagName());
  1255. previousRequiresWhiteSpace = true;
  1256. }
  1257. boolean previousRequiresNewLine = false;
  1258. for (Object o : node.fragments()) {
  1259. ASTNode e = (ASTNode) o;
  1260. // assume text elements include necessary leading and trailing whitespace
  1261. // but Name, MemberRef, MethodRef, and nested TagElement do not include white space
  1262. boolean currentIncludesWhiteSpace = (e instanceof TextElement);
  1263. if (previousRequiresNewLine && currentIncludesWhiteSpace) {
  1264. this.buffer.append("\n * ");//$NON-NLS-1$
  1265. }
  1266. previousRequiresNewLine = currentIncludesWhiteSpace;
  1267. // add space if required to separate
  1268. if (previousRequiresWhiteSpace && !currentIncludesWhiteSpace) {
  1269. this.buffer.append(" "); //$NON-NLS-1$
  1270. }
  1271. e.accept(this);
  1272. previousRequiresWhiteSpace = !currentIncludesWhiteSpace && !(e instanceof TagElement);
  1273. }
  1274. if (node.isNested()) {
  1275. this.buffer.append("}");//$NON-NLS-1$
  1276. }
  1277. return false;
  1278. }
  1279. /*
  1280. * @see ASTVisitor#visit(TextElement)
  1281. * @since 3.0
  1282. */
  1283. public boolean visit(TextElement node) {
  1284. this.buffer.append(node.getText());
  1285. return false;
  1286. }
  1287. /*
  1288. * @see ASTVisitor#visit(ThisExpression)
  1289. */
  1290. public boolean visit(ThisExpression node) {
  1291. if (node.getQualifier() != null) {
  1292. node.getQualifier().accept(this);
  1293. this.buffer.append(".");//$NON-NLS-1$
  1294. }
  1295. this.buffer.append("this");//$NON-NLS-1$
  1296. return false;
  1297. }
  1298. /*
  1299. * @see ASTVisitor#visit(ThrowStatement)
  1300. */
  1301. public boolean visit(ThrowStatement node) {
  1302. printIndent();
  1303. this.buffer.append("throw ");//$NON-NLS-1$
  1304. node.getExpression().accept(this);
  1305. this.buffer.append(";\n");//$NON-NLS-1$
  1306. return false;
  1307. }
  1308. /*
  1309. * @see ASTVisitor#visit(TryStatement)
  1310. */
  1311. public boolean visit(TryStatement node) {
  1312. printIndent();
  1313. this.buffer.append("try ");//$NON-NLS-1$
  1314. node.getBody().accept(this);
  1315. this.buffer.append(" ");//$NON-NLS-1$
  1316. for (Object o : node.catchClauses()) {
  1317. CatchClause cc = (CatchClause) o;
  1318. cc.accept(this);
  1319. }
  1320. if (node.getFinally() != null) {
  1321. this.buffer.append(" finally ");//$NON-NLS-1$
  1322. node.getFinally().accept(this);
  1323. }
  1324. return false;
  1325. }
  1326. /*
  1327. * @see ASTVisitor#visit(TypeDeclaration)
  1328. */
  1329. public boolean visit(TypeDeclaration node) {
  1330. if (node.getJavadoc() != null) {
  1331. node.getJavadoc().accept(this);
  1332. }
  1333. if (node.getAST().apiLevel() == AST.JLS2_INTERNAL) {
  1334. printModifiers(node.getModifiers());
  1335. }
  1336. if (node.getAST().apiLevel() >= AST.JLS3) {
  1337. printModifiers(node.modifiers());
  1338. }
  1339. // this.buffer.append(node.isInterface() ? "interface " : "class");//$NON-NLS-2$//$NON-NLS-1$
  1340. if (node.isInterface()) {
  1341. this.buffer.append("interface ");//$NON-NLS-1$
  1342. } else if (((AjTypeDeclaration)node).isAspect()) {
  1343. this.buffer.append("aspect ");//$NON-NLS-1$
  1344. } else {
  1345. this.buffer.append("class ");//$NON-NLS-1$
  1346. }
  1347. node.getName().accept(this);
  1348. if (node.getAST().apiLevel() >= AST.JLS3) {
  1349. if (!node.typeParameters().isEmpty()) {
  1350. this.buffer.append("<");//$NON-NLS-1$
  1351. for (Iterator it = node.typeParameters().iterator(); it.hasNext(); ) {
  1352. TypeParameter t = (TypeParameter) it.next();
  1353. t.accept(this);
  1354. if (it.hasNext()) {
  1355. this.buffer.append(",");//$NON-NLS-1$
  1356. }
  1357. }
  1358. this.buffer.append(">");//$NON-NLS-1$
  1359. }
  1360. }
  1361. this.buffer.append(" ");//$NON-NLS-1$
  1362. if (node.getAST().apiLevel() == AST.JLS2_INTERNAL) {
  1363. if (node.internalGetSuperclass() != null) {
  1364. this.buffer.append("extends ");//$NON-NLS-1$
  1365. node.internalGetSuperclass().accept(this);
  1366. this.buffer.append(" ");//$NON-NLS-1$
  1367. }
  1368. if (!node.internalSuperInterfaces().isEmpty()) {
  1369. // this.buffer.append(node.isInterface() ? "extends " : "implements ");//$NON-NLS-2$//$NON-NLS-1$
  1370. if (node.isInterface()) {
  1371. this.buffer.append("extends ");//$NON-NLS-1$
  1372. } else {
  1373. this.buffer.append("implements ");//$NON-NLS-1$
  1374. }
  1375. for (Iterator it = node.internalSuperInterfaces().iterator(); it.hasNext(); ) {
  1376. Name n = (Name) it.next();
  1377. n.accept(this);
  1378. if (it.hasNext()) {
  1379. this.buffer.append(", ");//$NON-NLS-1$
  1380. }
  1381. }
  1382. this.buffer.append(" ");//$NON-NLS-1$
  1383. }
  1384. }
  1385. if (node.getAST().apiLevel() >= AST.JLS3) {
  1386. if (node.getSuperclassType() != null) {
  1387. this.buffer.append("extends ");//$NON-NLS-1$
  1388. node.getSuperclassType().accept(this);
  1389. this.buffer.append(" ");//$NON-NLS-1$
  1390. }
  1391. if (!node.superInterfaceTypes().isEmpty()) {
  1392. // this.buffer.append(node.isInterface() ? "extends " : "implements ");//$NON-NLS-2$//$NON-NLS-1$
  1393. if (node.isInterface()) {
  1394. this.buffer.append("extends ");//$NON-NLS-1$
  1395. } else {
  1396. this.buffer.append("implements ");//$NON-NLS-1$
  1397. }
  1398. for (Iterator it = node.superInterfaceTypes().iterator(); it.hasNext(); ) {
  1399. Type t = (Type) it.next();
  1400. t.accept(this);
  1401. if (it.hasNext()) {
  1402. this.buffer.append(", ");//$NON-NLS-1$
  1403. }
  1404. }
  1405. this.buffer.append(" ");//$NON-NLS-1$
  1406. }
  1407. }
  1408. this.buffer.append("{\n");//$NON-NLS-1$
  1409. this.indent++;
  1410. BodyDeclaration prev = null;
  1411. for (Object o : node.bodyDeclarations()) {
  1412. BodyDeclaration d = (BodyDeclaration) o;
  1413. if (prev instanceof EnumConstantDeclaration) {
  1414. // enum constant declarations do not include punctuation
  1415. if (d instanceof EnumConstantDeclaration) {
  1416. // enum constant declarations are separated by commas
  1417. this.buffer.append(", ");//$NON-NLS-1$
  1418. } else {
  1419. // semicolon separates last enum constant declaration from
  1420. // first class body declarations
  1421. this.buffer.append("; ");//$NON-NLS-1$
  1422. }
  1423. }
  1424. d.accept(this);
  1425. }
  1426. this.indent--;
  1427. printIndent();
  1428. this.buffer.append("}\n");//$NON-NLS-1$
  1429. return false;
  1430. }
  1431. /*
  1432. * @see ASTVisitor#visit(TypeDeclarationStatement)
  1433. */
  1434. public boolean visit(TypeDeclarationStatement node) {
  1435. if (node.getAST().apiLevel() == AST.JLS2_INTERNAL) {
  1436. node.internalGetTypeDeclaration().accept(this);
  1437. }
  1438. if (node.getAST().apiLevel() >= AST.JLS3) {
  1439. node.getDeclaration().accept(this);
  1440. }
  1441. return false;
  1442. }
  1443. /*
  1444. * @see ASTVisitor#visit(TypeLiteral)
  1445. */
  1446. public boolean visit(TypeLiteral node) {
  1447. node.getType().accept(this);
  1448. this.buffer.append(".class");//$NON-NLS-1$
  1449. return false;
  1450. }
  1451. /*
  1452. * @see ASTVisitor#visit(TypeParameter)
  1453. * @since 3.1
  1454. */
  1455. public boolean visit(TypeParameter node) {
  1456. node.getName().accept(this);
  1457. if (!node.typeBounds().isEmpty()) {
  1458. this.buffer.append(" extends ");//$NON-NLS-1$
  1459. for (Iterator it = node.typeBounds().iterator(); it.hasNext(); ) {
  1460. Type t = (Type) it.next();
  1461. t.accept(this);
  1462. if (it.hasNext()) {
  1463. this.buffer.append(" & ");//$NON-NLS-1$
  1464. }
  1465. }
  1466. }
  1467. return false;
  1468. }
  1469. /*
  1470. * @see ASTVisitor#visit(VariableDeclarationExpression)
  1471. */
  1472. public boolean visit(VariableDeclarationExpression node) {
  1473. if (node.getAST().apiLevel() == AST.JLS2_INTERNAL) {
  1474. printModifiers(node.getModifiers());
  1475. }
  1476. if (node.getAST().apiLevel() >= AST.JLS3) {
  1477. printModifiers(node.modifiers());
  1478. }
  1479. node.getType().accept(this);
  1480. this.buffer.append(" ");//$NON-NLS-1$
  1481. for (Iterator it = node.fragments().iterator(); it.hasNext(); ) {
  1482. VariableDeclarationFragment f = (VariableDeclarationFragment) it.next();
  1483. f.accept(this);
  1484. if (it.hasNext()) {
  1485. this.buffer.append(", ");//$NON-NLS-1$
  1486. }
  1487. }
  1488. return false;
  1489. }
  1490. /*
  1491. * @see ASTVisitor#visit(VariableDeclarationFragment)
  1492. */
  1493. public boolean visit(VariableDeclarationFragment node) {
  1494. node.getName().accept(this);
  1495. for (int i = 0; i < node.getExtraDimensions(); i++) {
  1496. this.buffer.append("[]");//$NON-NLS-1$
  1497. }
  1498. if (node.getInitializer() != null) {
  1499. this.buffer.append("=");//$NON-NLS-1$
  1500. node.getInitializer().accept(this);
  1501. }
  1502. return false;
  1503. }
  1504. /*
  1505. * @see ASTVisitor#visit(VariableDeclarationStatement)
  1506. */
  1507. public boolean visit(VariableDeclarationStatement node) {
  1508. printIndent();
  1509. if (node.getAST().apiLevel() == AST.JLS2_INTERNAL) {
  1510. printModifiers(node.getModifiers());
  1511. }
  1512. if (node.getAST().apiLevel() >= AST.JLS3) {
  1513. printModifiers(node.modifiers());
  1514. }
  1515. node.getType().accept(this);
  1516. this.buffer.append(" ");//$NON-NLS-1$
  1517. for (Iterator it = node.fragments().iterator(); it.hasNext(); ) {
  1518. VariableDeclarationFragment f = (VariableDeclarationFragment) it.next();
  1519. f.accept(this);
  1520. if (it.hasNext()) {
  1521. this.buffer.append(", ");//$NON-NLS-1$
  1522. }
  1523. }
  1524. this.buffer.append(";\n");//$NON-NLS-1$
  1525. return false;
  1526. }
  1527. /*
  1528. * @see ASTVisitor#visit(WildcardType)
  1529. * @since 3.1
  1530. */
  1531. public boolean visit(WildcardType node) {
  1532. this.buffer.append("?");//$NON-NLS-1$
  1533. Type bound = node.getBound();
  1534. if (bound != null) {
  1535. if (node.isUpperBound()) {
  1536. this.buffer.append(" extends ");//$NON-NLS-1$
  1537. } else {
  1538. this.buffer.append(" super ");//$NON-NLS-1$
  1539. }
  1540. bound.accept(this);
  1541. }
  1542. return false;
  1543. }
  1544. /*
  1545. * @see ASTVisitor#visit(WhileStatement)
  1546. */
  1547. public boolean visit(WhileStatement node) {
  1548. printIndent();
  1549. this.buffer.append("while (");//$NON-NLS-1$
  1550. node.getExpression().accept(this);
  1551. this.buffer.append(") ");//$NON-NLS-1$
  1552. node.getBody().accept(this);
  1553. return false;
  1554. }
  1555. public boolean visit(DeclareParentsDeclaration node) {
  1556. printIndent();
  1557. this.buffer.append("declare parents: ");
  1558. node.getChildTypePattern().accept(this);
  1559. if(node.isExtends()){
  1560. this.buffer.append(" extends ");
  1561. } else {
  1562. this.buffer.append(" implements ");
  1563. }
  1564. for (Iterator it = node.parentTypePatterns().iterator(); it.hasNext();) {
  1565. AbstractTypePattern typePat = (AbstractTypePattern) it.next();
  1566. typePat.accept(this);
  1567. if(it.hasNext()){
  1568. this.buffer.append(", ");
  1569. }
  1570. }
  1571. this.buffer.append(";\n");
  1572. return false;
  1573. }
  1574. public boolean visit(DeclareWarningDeclaration node) {
  1575. printIndent();
  1576. this.buffer.append("declare warning: ");
  1577. node.getPointcut().accept(this);
  1578. this.buffer.append(" : ");
  1579. node.getMessage().accept(this);
  1580. this.buffer.append(" ;\n");
  1581. return false;
  1582. }
  1583. public boolean visit(DeclareErrorDeclaration node) {
  1584. printIndent();
  1585. this.buffer.append("declare error: ");
  1586. node.getPointcut().accept(this);
  1587. this.buffer.append(" : ");
  1588. node.getMessage().accept(this);
  1589. this.buffer.append(" ;\n");
  1590. return false;
  1591. }
  1592. public boolean visit(DeclareSoftDeclaration node) {
  1593. printIndent();
  1594. this.buffer.append("declare soft: ");
  1595. node.getTypePattern().accept(this);
  1596. this.buffer.append(" : ");
  1597. node.getPointcut().accept(this);
  1598. this.buffer.append(" ;\n");
  1599. return false;
  1600. }
  1601. public boolean visit(DeclarePrecedenceDeclaration node) {
  1602. printIndent();
  1603. this.buffer.append("declare precedence: ");
  1604. for (Iterator it = node.typePatterns().iterator(); it.hasNext();) {
  1605. AbstractTypePattern typePat = (AbstractTypePattern) it.next();
  1606. typePat.accept(this);
  1607. if(it.hasNext()){
  1608. this.buffer.append(", ");
  1609. }
  1610. }
  1611. this.buffer.append(";\n");
  1612. return false;
  1613. }
  1614. public boolean visit(AbstractBooleanTypePattern node) {
  1615. // Flatten boolean expressions in order, meaning
  1616. // the left node needs to be appended first, followed by the
  1617. // boolean operator, followed by the right node.
  1618. node.getLeft().accept(this);
  1619. this.buffer.append(" ");
  1620. this.buffer.append(node.getTypePatternExpression());
  1621. this.buffer.append(" ");
  1622. node.getRight().accept(this);
  1623. // No need to visit the childrena, as they were already visited above
  1624. return false;
  1625. }
  1626. public boolean visit(AnyTypePattern node) {
  1627. this.buffer.append(node.getTypePatternExpression());
  1628. return false;
  1629. }
  1630. public boolean visit(AnyWithAnnotationTypePattern node) {
  1631. this.buffer.append(node.getTypePatternExpression());
  1632. return false;
  1633. }
  1634. public boolean visit(EllipsisTypePattern node) {
  1635. this.buffer.append(node.getTypePatternExpression());
  1636. return false;
  1637. }
  1638. public boolean visit(HasMemberTypePattern node) {
  1639. this.buffer.append(node.getTypePatternExpression());
  1640. return false;
  1641. }
  1642. public boolean visit(IdentifierTypePattern node) {
  1643. this.buffer.append(node.getTypePatternExpression());
  1644. return false;
  1645. }
  1646. public boolean visit(NotTypePattern node) {
  1647. this.buffer.append(node.getTypePatternExpression());
  1648. // Visit the children in this case, as the child of a not type pattern
  1649. // is the negated type pattern
  1650. return true;
  1651. }
  1652. public boolean visit(NoTypePattern node) {
  1653. this.buffer.append(node.getTypePatternExpression());
  1654. return false;
  1655. }
  1656. public boolean visit(TypeCategoryTypePattern node) {
  1657. this.buffer.append(node.getTypePatternExpression());
  1658. return false;
  1659. }
  1660. public boolean visit(DefaultPointcut node) {
  1661. this.buffer.append(node.getDetail());
  1662. return false;
  1663. }
  1664. }