[seam-commits] Seam SVN: r13324 - sandbox/encore/src/test/java/org/jboss/encore/grammar/java.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Wed Jun 30 18:07:23 EDT 2010


Author: lincolnthree
Date: 2010-06-30 18:07:23 -0400 (Wed, 30 Jun 2010)
New Revision: 13324

Modified:
   sandbox/encore/src/test/java/org/jboss/encore/grammar/java/JavaParserTest.java
Log:
Working on tree manipulation.

Modified: sandbox/encore/src/test/java/org/jboss/encore/grammar/java/JavaParserTest.java
===================================================================
--- sandbox/encore/src/test/java/org/jboss/encore/grammar/java/JavaParserTest.java	2010-06-30 11:04:08 UTC (rev 13323)
+++ sandbox/encore/src/test/java/org/jboss/encore/grammar/java/JavaParserTest.java	2010-06-30 22:07:23 UTC (rev 13324)
@@ -1,21 +1,36 @@
 package org.jboss.encore.grammar.java;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 
 import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 
+import org.eclipse.jdt.core.dom.ASTNode;
+import org.eclipse.jdt.core.dom.ASTVisitor;
 import org.eclipse.jdt.core.dom.CompilationUnit;
 import org.eclipse.jdt.core.dom.ImportDeclaration;
+import org.eclipse.jdt.core.dom.MethodDeclaration;
+import org.eclipse.jdt.core.dom.TypeDeclaration;
+import org.junit.Before;
 import org.junit.Test;
 
 public class JavaParserTest
 {
+   private InputStream stream;
+
+   @Before
+   public void reset()
+   {
+      stream = JavaParserTest.class
+               .getResourceAsStream("/org/jboss/encore/grammar/java/MockClassFile.java");
+   }
+
    @Test
    public void testParse() throws Exception
    {
-      InputStream stream = JavaParserTest.class
-               .getResourceAsStream("/org/jboss/encore/grammar/java/MockClassFile.java");
       CompilationUnit unit = JavaParser.parse(stream);
 
       @SuppressWarnings("unchecked")
@@ -23,4 +38,47 @@
 
       assertEquals("java.net.URL", imports.get(0).getName().getFullyQualifiedName());
    }
+
+   @Test
+   public void testAddMethod() throws Exception
+   {
+      CompilationUnit unit = JavaParser.parse(stream);
+      MethodFinderVisitor visitor = new MethodFinderVisitor();
+      unit.accept(visitor);
+      List<MethodDeclaration> methods = visitor.getMethods();
+      TypeDeclaration parent = visitor.getParent();
+      MethodDeclaration newMethod = parent.getAST().newMethodDeclaration();
+      newMethod.setName(parent.getAST().newSimpleName("addedMethod"));
+      newMethod.setConstructor(false);
+      newMethod.setReturnType2(null);
+
+      // http://help.eclipse.org/ganymede/index.jsp?topic=/org.eclipse.jdt.doc.isv/reference/api/org/eclipse/jdt/core/dom/rewrite/package-summary.html
+      methods = visitor.getMethods();
+
+      assertTrue(methods.contains(newMethod));
+   }
+
+   public class MethodFinderVisitor extends ASTVisitor
+   {
+      private final List<MethodDeclaration> methods = new ArrayList<MethodDeclaration>();
+      private ASTNode parent;
+
+      @Override
+      public boolean visit(final TypeDeclaration node)
+      {
+         parent = node;
+         methods.addAll(Arrays.asList(node.getMethods()));
+         return super.visit(node);
+      }
+
+      public List<MethodDeclaration> getMethods()
+      {
+         return methods;
+      }
+
+      public TypeDeclaration getParent()
+      {
+         return (TypeDeclaration) parent;
+      }
+   }
 }



More information about the seam-commits mailing list