[seam-commits] Seam SVN: r13352 - in sandbox/encore/core/src: main/java/org/jboss/encore/grammar/java and 5 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Thu Jul 8 16:56:09 EDT 2010


Author: lincolnthree
Date: 2010-07-08 16:56:09 -0400 (Thu, 08 Jul 2010)
New Revision: 13352

Added:
   sandbox/encore/core/src/main/java/org/jboss/encore/grammar/Internal.java
   sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/Import.java
Removed:
   sandbox/encore/core/src/main/java/org/jboss/seam/encore/
   sandbox/encore/core/src/test/java/org/jboss/seam/encore/
Modified:
   sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/JavaClass.java
   sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/Method.java
   sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/ast/MethodFinderVisitor.java
   sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/ast/TypeDeclarationFinderVisitor.java
   sandbox/encore/core/src/main/java/org/jboss/encore/model/Mutable.java
   sandbox/encore/core/src/test/java/org/jboss/encore/grammar/java/JavaClassTest.java
Log:
Starting to standardize the graph, code is much cleaner, added Import object.

Added: sandbox/encore/core/src/main/java/org/jboss/encore/grammar/Internal.java
===================================================================
--- sandbox/encore/core/src/main/java/org/jboss/encore/grammar/Internal.java	                        (rev 0)
+++ sandbox/encore/core/src/main/java/org/jboss/encore/grammar/Internal.java	2010-07-08 20:56:09 UTC (rev 13352)
@@ -0,0 +1,39 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.encore.grammar;
+
+/**
+ * Represents an object that stores implementation-specific data. This data must be accessible to other objects sharing
+ * the implementation, but should not be referenced by end-users.
+ * 
+ * @author <a href="mailto:lincolnbaxter at gmail.com">Lincoln Baxter, III</a>
+ * 
+ */
+public interface Internal
+{
+   /**
+    * Returns the implementation-specific Object representing <code>this</code>. <b>Do not call this method</b> unless
+    * you are willing to risk breaking backwards compatibility if future versions do not use the same internal object
+    * implementations.
+    */
+   Object getInternal();
+}

Added: sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/Import.java
===================================================================
--- sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/Import.java	                        (rev 0)
+++ sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/Import.java	2010-07-08 20:56:09 UTC (rev 13352)
@@ -0,0 +1,100 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.encore.grammar.java;
+
+import org.eclipse.jdt.core.dom.AST;
+import org.eclipse.jdt.core.dom.CompilationUnit;
+import org.eclipse.jdt.core.dom.ImportDeclaration;
+import org.jboss.encore.grammar.Internal;
+import org.jboss.encore.model.Mutable;
+
+/**
+ * @author <a href="mailto:lincolnbaxter at gmail.com">Lincoln Baxter, III</a>
+ * 
+ */
+public class Import implements Internal, Mutable
+{
+
+   private JavaClass parent;
+   private AST ast = null;
+   private CompilationUnit cu = null;
+   private ImportDeclaration imprt = null;
+
+   private void init(final JavaClass parent)
+   {
+      this.parent = parent;
+      cu = (CompilationUnit) parent.getInternal();
+      ast = cu.getAST();
+   }
+
+   public Import(final JavaClass parent)
+   {
+      init(parent);
+      imprt = ast.newImportDeclaration();
+   }
+
+   public Import(final JavaClass parent, final Object internal)
+   {
+      init(parent);
+      imprt = (ImportDeclaration) internal;
+   }
+
+   public String getName()
+   {
+      return imprt.getName().getFullyQualifiedName();
+   }
+
+   public Import setName(final String name)
+   {
+      imprt.setName(ast.newName(tokenizeClassName(name)));
+      return this;
+   }
+
+   public boolean isStatic()
+   {
+      return imprt.isStatic();
+   }
+
+   public Import setStatic(final boolean value)
+   {
+      imprt.setStatic(value);
+      return this;
+   }
+
+   private String[] tokenizeClassName(final String className)
+   {
+      String[] result = className.split("\\.");
+      return result;
+   }
+
+   @Override
+   public void applyChanges()
+   {
+      parent.applyChanges();
+   }
+
+   @Override
+   public Object getInternal()
+   {
+      return imprt;
+   }
+}

Modified: sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/JavaClass.java
===================================================================
--- sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/JavaClass.java	2010-07-08 14:54:54 UTC (rev 13351)
+++ sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/JavaClass.java	2010-07-08 20:56:09 UTC (rev 13352)
@@ -1,3 +1,24 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
 package org.jboss.encore.grammar.java;
 
 import java.io.InputStream;
@@ -2,3 +23,2 @@
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.List;
@@ -13,18 +33,26 @@
 import org.eclipse.jdt.core.dom.MethodDeclaration;
 import org.eclipse.jdt.core.dom.TypeDeclaration;
 import org.eclipse.jdt.internal.compiler.util.Util;
+import org.eclipse.jface.text.BadLocationException;
 import org.eclipse.jface.text.Document;
+import org.eclipse.text.edits.MalformedTreeException;
 import org.eclipse.text.edits.TextEdit;
 import org.eclipse.text.edits.UndoEdit;
+import org.jboss.encore.grammar.Internal;
 import org.jboss.encore.grammar.java.ast.MethodFinderVisitor;
 import org.jboss.encore.grammar.java.ast.TypeDeclarationFinderVisitor;
+import org.jboss.encore.model.Mutable;
 
-public class JavaClass
+/**
+ * @author <a href="mailto:lincolnbaxter at gmail.com">Lincoln Baxter, III</a>
+ */
+public class JavaClass implements Internal, Mutable
 {
+   private final Stack<UndoEdit> undoStack = new Stack<UndoEdit>();
+
    private Document document;
+   private CompilationUnit unit;
 
-   private final Stack<UndoEdit> undoStack = new Stack<UndoEdit>();
-
    /**
     * Parses and process the java source code as a compilation unit and the result it abstract syntax tree (AST)
     * representation and this action uses the third edition of java Language Specification.
@@ -37,7 +65,7 @@
       try
       {
          char[] source = Util.getInputStreamAsCharArray(inputStream, inputStream.available(), "ISO8859_1");
-         document = new Document(new String(source));
+         init(source);
       }
       catch (Exception e)
       {
@@ -45,60 +73,56 @@
       }
    }
 
-   public JavaClass(final char[] source)
+   public JavaClass(final String source)
    {
-      document = new Document(new String(source));
+      this(source.toCharArray());
    }
 
-   public JavaClass(final String source)
+   public JavaClass(final char[] source)
    {
-      this(source.toCharArray());
+      init(source);
    }
 
-   public CompilationUnit parse()
+   private void init(final char[] source)
    {
+      document = new Document(new String(source));
       ASTParser parser = ASTParser.newParser(AST.JLS3);
       parser.setSource(document.get().toCharArray());
       parser.setResolveBindings(true);
       parser.setKind(ASTParser.K_COMPILATION_UNIT);
-      CompilationUnit cu = (CompilationUnit) parser.createAST(null);
-      return cu;
+      unit = (CompilationUnit) parser.createAST(null);
+      unit.recordModifications();
    }
 
+   @SuppressWarnings("unchecked")
    public Method addMethod()
    {
-      return new Method(this);
+      Method m = new Method(this);
+      getTypeDeclaration().bodyDeclarations().add(m.getInternal());
+      return m;
    }
 
    @SuppressWarnings("unchecked")
-   public JavaClass addImport(final String className)
+   public Method addMethod(final String method)
    {
-      CompilationUnit cu = parse();
-      try
-      {
-         AST ast = cu.getAST();
-         cu.recordModifications();
-         // ASTRewrite rewriter = ASTRewrite.create(ast);
-         // ListRewrite lrw = rewriter.getListRewrite(cu, CompilationUnit.IMPORTS_PROPERTY);
+      Method m = new Method(this, method);
+      getTypeDeclaration().bodyDeclarations().add(m.getInternal());
+      return m;
+   }
 
-         ImportDeclaration id = ast.newImportDeclaration();
-         id.setName(ast.newName(tokenizeClassName(className)));
-         cu.imports().add(id);
-         TextEdit edit = cu.rewrite(document, null);
-         UndoEdit undo = edit.apply(document);
+   @SuppressWarnings("unchecked")
+   public Import addImport(final String className)
+   {
+      Import imprt = new Import(this).setName(className);
+      unit.imports().add(imprt.getInternal());
+      return imprt;
+   }
 
-         // lrw.insertLast(id, null);
-         //
-         // TextEdit edits = rewriter.rewriteAST(document, null);
-         // UndoEdit undo = edits.apply(document);
-         undoStack.add(undo);
-
-         return this;
-      }
-      catch (Exception e)
+   public void addImports(final String... types)
+   {
+      for (String type : types)
       {
-         throw new RuntimeException("Could not add import: \"" + className +
-                  "\" to compilation unit: \"" + "\"", e);
+         addImport(type);
       }
    }
 
@@ -115,30 +139,28 @@
       }
    }
 
-   private String[] tokenizeClassName(final String className)
-   {
-      String[] result = className.split("\\.");
-      return result;
-   }
-
    public Stack<UndoEdit> getUndoStack()
    {
       return undoStack;
    }
 
    @SuppressWarnings("unchecked")
-   public List<ImportDeclaration> getImports()
+   public List<Import> getImports()
    {
-      CompilationUnit unit = parse();
-      List<ImportDeclaration> imports = unit.imports();
-      return Collections.unmodifiableList(imports);
+      List<Import> results = new ArrayList<Import>();
+
+      for (ImportDeclaration i : (List<ImportDeclaration>) unit.imports())
+      {
+         results.add(new Import(this, i));
+      }
+
+      return results;
    }
 
    public List<Method> getMethods()
    {
       List<Method> result = new ArrayList<Method>();
 
-      CompilationUnit unit = parse();
       MethodFinderVisitor methodFinderVisitor = new MethodFinderVisitor();
       unit.accept(methodFinderVisitor);
 
@@ -150,21 +172,55 @@
       return result;
    }
 
+   public void removeMethod(final Method method)
+   {
+      if (getMethods().contains(method))
+      {
+         getTypeDeclaration().bodyDeclarations().remove(method);
+      }
+   }
+
    public TypeDeclaration getTypeDeclaration()
    {
-      CompilationUnit unit = parse();
       TypeDeclarationFinderVisitor typeDeclarationFinder = new TypeDeclarationFinderVisitor();
       unit.accept(typeDeclarationFinder);
       return typeDeclarationFinder.getTypeDeclarations().get(0);
    }
 
-   public Document getDocument()
+   public String getName()
    {
-      return document;
+      return getTypeDeclaration().getName().getIdentifier();
    }
 
-   public String getName()
+   @Override
+   public String toString()
    {
-      return getTypeDeclaration().getName().getIdentifier();
+      return document.get();
    }
+
+   @Override
+   public Object getInternal()
+   {
+      return unit;
+   }
+
+   @Override
+   public void applyChanges()
+   {
+      try
+      {
+         TextEdit edit = unit.rewrite(document, null);
+         UndoEdit undo = edit.apply(document);
+         undoStack.add(undo);
+      }
+      catch (MalformedTreeException e)
+      {
+         throw new RuntimeException(e);
+      }
+      catch (BadLocationException e)
+      {
+         throw new RuntimeException(e);
+      }
+   }
+
 }

Modified: sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/Method.java
===================================================================
--- sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/Method.java	2010-07-08 14:54:54 UTC (rev 13351)
+++ sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/Method.java	2010-07-08 20:56:09 UTC (rev 13352)
@@ -21,102 +21,256 @@
  */
 package org.jboss.encore.grammar.java;
 
+import java.util.ArrayList;
 import java.util.List;
 
 import org.eclipse.jdt.core.dom.AST;
+import org.eclipse.jdt.core.dom.ASTNode;
 import org.eclipse.jdt.core.dom.Block;
 import org.eclipse.jdt.core.dom.CompilationUnit;
 import org.eclipse.jdt.core.dom.MethodDeclaration;
-import org.eclipse.jface.text.Document;
-import org.eclipse.text.edits.TextEdit;
-import org.eclipse.text.edits.UndoEdit;
+import org.eclipse.jdt.core.dom.Modifier;
+import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
+import org.eclipse.jdt.core.dom.Statement;
+import org.jboss.encore.grammar.Internal;
+import org.jboss.encore.model.Mutable;
 
 /**
  * @author <a href="mailto:lincolnbaxter at gmail.com">Lincoln Baxter, III</a>
  * 
  */
-public class Method
+public class Method implements Internal, Mutable
 {
-   private final JavaClass javaClass;
-   private final AST ast;
-   private final CompilationUnit cu;
-   private final Document document;
-   private MethodDeclaration method;
-   private String body;
+   private JavaClass parent = null;
+   private AST ast = null;
+   private CompilationUnit cu = null;
+   private final MethodDeclaration method;
 
-   public Method(final JavaClass javaClass)
+   private void init(final JavaClass parent)
    {
-      this.javaClass = javaClass;
-      cu = javaClass.parse();
-      cu.recordModifications();
-      document = javaClass.getDocument();
+      this.parent = parent;
+      cu = (CompilationUnit) parent.getInternal();
       ast = cu.getAST();
+   }
+
+   public Method(final JavaClass parent)
+   {
+      init(parent);
       method = ast.newMethodDeclaration();
       method.setConstructor(false);
    }
 
-   public Method(final JavaClass javaClass, final MethodDeclaration methodDeclaration)
+   public Method(final JavaClass parent, final Object internal)
    {
-      this(javaClass);
-      method = methodDeclaration;
+      init(parent);
+      method = (MethodDeclaration) internal;
    }
 
-   public Method name(final String name)
+   public Method(final JavaClass parent, final String method)
    {
-      method.setName(ast.newSimpleName(name));
+      init(parent);
+
+      String stub = "public class Stub { " + method + " }";
+      JavaClass temp = new JavaClass(stub);
+      List<Method> methods = temp.getMethods();
+      MethodDeclaration newMethod = methods.get(0).getMethodDeclaration();
+      MethodDeclaration subtree = (MethodDeclaration) ASTNode.copySubtree(cu.getAST(), newMethod);
+      this.method = subtree;
+   }
+
+   @SuppressWarnings("unchecked")
+   public String getBody()
+   {
+      String result = "";
+
+      List<Statement> statements = (List<Statement>) method.getBody().getStructuralProperty(Block.STATEMENTS_PROPERTY);
+      for (Statement statement : statements)
+      {
+         result += statement + " ";
+      }
+
+      return result;
+   }
+
+   public Method setBody(final String body)
+   {
+      String stub = "public class Stub { public void method() {" + body + "} }";
+      JavaClass temp = new JavaClass(stub);
+      List<Method> methods = temp.getMethods();
+      Block block = methods.get(0).getMethodDeclaration().getBody();
+
+      block = (Block) ASTNode.copySubtree(method.getAST(), block);
+      method.setBody(block);
+
       return this;
    }
 
-   public Method constructor(final boolean isConstructor)
+   public Method setConstructor(final boolean isConstructor)
    {
       method.setConstructor(isConstructor);
+      if (isConstructor())
+      {
+         method.setName(ast.newSimpleName(parent.getName()));
+      }
       return this;
    }
 
-   public Method returnType(final Class<?> type)
+   public boolean isConstructor()
    {
-      return returnType(type.getSimpleName());
+      return method.isConstructor();
    }
 
-   public Method returnType(final String type)
+   @SuppressWarnings("unchecked")
+   public Method setAbstract()
    {
-      method.setReturnType2(ast.newSimpleType(ast.newSimpleName(type)));
+      method.modifiers().add(ast.newModifier(ModifierKeyword.ABSTRACT_KEYWORD));
       return this;
    }
 
-   public Method body(final String body)
+   @SuppressWarnings("unchecked")
+   public Method setFinal()
    {
-      this.body = body;
-      String stub = "public class Stub { public void method() {" + body + "} }";
-      JavaClass temp = new JavaClass(stub);
-      List<Method> methods = temp.getMethods();
-      Block block = methods.get(0).getMethodDeclaration().getBody();
-      method.setBody(block);
+      method.modifiers().add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD));
       return this;
    }
 
-   private MethodDeclaration getMethodDeclaration()
+   public String getName()
    {
-      return method;
+      return method.getName().toString();
    }
 
+   public Method setName(final String name)
+   {
+      if (method.isConstructor())
+      {
+         throw new IllegalStateException("Cannot set the name of a constructor.");
+      }
+      method.setName(ast.newSimpleName(name));
+      return this;
+   }
+
+   public boolean isPackagePrivate()
+   {
+      return (!isPublic() && !isPrivate() && !isProtected());
+   }
+
    @SuppressWarnings("unchecked")
-   public JavaClass add()
+   public Method setPackagePrivate()
    {
-      try
+      List<Modifier> modifiers = method.modifiers();
+
+      List<Modifier> toBeRemoved = new ArrayList<Modifier>();
+      for (Modifier modifier : modifiers)
       {
-         boolean add = javaClass.getTypeDeclaration().bodyDeclarations().add(method);
+         if (modifier.isPrivate() || modifier.isProtected() || modifier.isPublic())
+         {
+            toBeRemoved.add(modifier);
+         }
+      }
 
-         TextEdit edit = cu.rewrite(document, null);
-         UndoEdit undo = edit.apply(document);
+      modifiers.removeAll(toBeRemoved);
 
-         javaClass.getUndoStack().add(undo);
-         return javaClass;
+      modifiers.add(ast.newModifier(ModifierKeyword.PRIVATE_KEYWORD));
+      return this;
+   }
+
+   public boolean isPublic()
+   {
+      return hasModifier(ModifierKeyword.PUBLIC_KEYWORD);
+   }
+
+   @SuppressWarnings("unchecked")
+   public Method setPublic()
+   {
+      method.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
+      return this;
+   }
+
+   public boolean isPrivate()
+   {
+      return hasModifier(ModifierKeyword.PRIVATE_KEYWORD);
+   }
+
+   @SuppressWarnings("unchecked")
+   public Method setPrivate()
+   {
+      method.modifiers().add(ast.newModifier(ModifierKeyword.PRIVATE_KEYWORD));
+      return this;
+   }
+
+   public boolean isProtected()
+   {
+      return hasModifier(ModifierKeyword.PROTECTED_KEYWORD);
+   }
+
+   @SuppressWarnings("unchecked")
+   public Method setProtected()
+   {
+      method.modifiers().add(ast.newModifier(ModifierKeyword.PROTECTED_KEYWORD));
+      return this;
+   }
+
+   public String getReturnType()
+   {
+      String result = null;
+      if (!isConstructor() && (method.getReturnType2() != null))
+      {
+         result = method.getReturnType2().toString();
       }
-      catch (Exception e)
+      return result;
+   }
+
+   public Method setReturnType(final Class<?> type)
+   {
+      return setReturnType(type.getSimpleName());
+   }
+
+   public Method setReturnType(final String type)
+   {
+      method.setReturnType2(ast.newSimpleType(ast.newSimpleName(type)));
+      return this;
+   }
+
+   public Method setReturnTypeVoid()
+   {
+      method.setReturnType2(null);
+      return this;
+   }
+
+   private MethodDeclaration getMethodDeclaration()
+   {
+      return method;
+   }
+
+   @Override
+   public String toString()
+   {
+      return method.toString();
+   }
+
+   @SuppressWarnings("unchecked")
+   private boolean hasModifier(final ModifierKeyword modifier)
+   {
+      boolean result = false;
+      List<Modifier> modifiers = method.modifiers();
+      for (Modifier m : modifiers)
       {
-         throw new RuntimeException("Could not add method: \"" +
-                  "\" to compilation unit: \"" + javaClass.getName() + "\"", e);
+         if (m.getKeyword() == modifier)
+         {
+            result = true;
+         }
       }
+      return result;
    }
+
+   @Override
+   public Object getInternal()
+   {
+      return method;
+   }
+
+   public void applyChanges()
+   {
+      parent.applyChanges();
+   }
 }

Modified: sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/ast/MethodFinderVisitor.java
===================================================================
--- sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/ast/MethodFinderVisitor.java	2010-07-08 14:54:54 UTC (rev 13351)
+++ sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/ast/MethodFinderVisitor.java	2010-07-08 20:56:09 UTC (rev 13352)
@@ -1,3 +1,24 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
 package org.jboss.encore.grammar.java.ast;
 
 import java.util.ArrayList;
@@ -9,6 +30,11 @@
 import org.eclipse.jdt.core.dom.MethodDeclaration;
 import org.eclipse.jdt.core.dom.TypeDeclaration;
 
+/**
+ * 
+ * @author <a href="mailto:lincolnbaxter at gmail.com">Lincoln Baxter, III</a>
+ * 
+ */
 public class MethodFinderVisitor extends ASTVisitor
 {
    private final List<MethodDeclaration> methods = new ArrayList<MethodDeclaration>();

Modified: sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/ast/TypeDeclarationFinderVisitor.java
===================================================================
--- sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/ast/TypeDeclarationFinderVisitor.java	2010-07-08 14:54:54 UTC (rev 13351)
+++ sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/ast/TypeDeclarationFinderVisitor.java	2010-07-08 20:56:09 UTC (rev 13352)
@@ -1,3 +1,24 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
 package org.jboss.encore.grammar.java.ast;
 
 import java.util.ArrayList;
@@ -7,6 +28,11 @@
 import org.eclipse.jdt.core.dom.ASTVisitor;
 import org.eclipse.jdt.core.dom.TypeDeclaration;
 
+/**
+ * 
+ * @author <a href="mailto:lincolnbaxter at gmail.com">Lincoln Baxter, III</a>
+ * 
+ */
 public class TypeDeclarationFinderVisitor extends ASTVisitor
 {
    private final List<TypeDeclaration> types = new ArrayList<TypeDeclaration>();

Modified: sandbox/encore/core/src/main/java/org/jboss/encore/model/Mutable.java
===================================================================
--- sandbox/encore/core/src/main/java/org/jboss/encore/model/Mutable.java	2010-07-08 14:54:54 UTC (rev 13351)
+++ sandbox/encore/core/src/main/java/org/jboss/encore/model/Mutable.java	2010-07-08 20:56:09 UTC (rev 13352)
@@ -22,10 +22,16 @@
 package org.jboss.encore.model;
 
 /**
+ * Represents an object that queues changes before making final modifications to a resource.
+ * 
  * @author <a href="mailto:lincolnbaxter at gmail.com">Lincoln Baxter, III</a>
- *
+ * 
  */
 public interface Mutable
 {
-
+   /**
+    * Apply all changes made to this or other objects to which this may belong. (Apply all pending changes in the object
+    * graph.)
+    */
+   void applyChanges();
 }

Modified: sandbox/encore/core/src/test/java/org/jboss/encore/grammar/java/JavaClassTest.java
===================================================================
--- sandbox/encore/core/src/test/java/org/jboss/encore/grammar/java/JavaClassTest.java	2010-07-08 14:54:54 UTC (rev 13351)
+++ sandbox/encore/core/src/test/java/org/jboss/encore/grammar/java/JavaClassTest.java	2010-07-08 20:56:09 UTC (rev 13352)
@@ -1,71 +1,136 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
 package org.jboss.encore.grammar.java;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 
 import java.io.InputStream;
 import java.net.URL;
 import java.util.List;
 import java.util.Map;
 
-import org.eclipse.jdt.core.dom.ImportDeclaration;
 import org.junit.Before;
 import org.junit.Test;
 
+/**
+ * @author <a href="mailto:lincolnbaxter at gmail.com">Lincoln Baxter, III</a>
+ */
 public class JavaClassTest
 {
    private InputStream stream;
+   private JavaClass javaClass;
 
    @Before
    public void reset()
    {
       stream = JavaClassTest.class
                .getResourceAsStream("/org/jboss/encore/grammar/java/MockClassFile.java");
+      javaClass = new JavaClass(stream);
    }
 
    @Test
    public void testParse() throws Exception
    {
-      JavaClass parser = new JavaClass(stream);
-      List<ImportDeclaration> imports = parser.getImports();
-
-      assertEquals(URL.class.getName(), imports.get(0).getName().getFullyQualifiedName());
+      List<Import> imports = javaClass.getImports();
+      assertEquals(URL.class.getName(), imports.get(0).getName());
    }
 
    @Test
    public void testAddImport() throws Exception
    {
-      JavaClass parser = new JavaClass(stream);
-      parser.addImport(List.class.getName());
-      List<ImportDeclaration> imports = parser.getImports();
+      javaClass.addImport(List.class.getName());
+      List<Import> imports = javaClass.getImports();
       assertEquals(2, imports.size());
-      assertEquals(URL.class.getName(), imports.get(0).getName().getFullyQualifiedName());
-      assertEquals(List.class.getName(), imports.get(1).getName().getFullyQualifiedName());
+      assertEquals(URL.class.getName(), imports.get(0).getName());
+      assertEquals(List.class.getName(), imports.get(1).getName());
    }
 
    @Test
    public void testAddImportsClasses() throws Exception
    {
-      JavaClass parser = new JavaClass(stream);
-
-      List<ImportDeclaration> imports = parser.getImports();
+      List<Import> imports = javaClass.getImports();
       assertEquals(1, imports.size());
 
-      parser.addImports(List.class, Map.class);
+      javaClass.addImports(List.class, Map.class);
 
-      imports = parser.getImports();
+      imports = javaClass.getImports();
       assertEquals(3, imports.size());
-      assertEquals(URL.class.getName(), imports.get(0).getName().getFullyQualifiedName());
-      assertEquals(List.class.getName(), imports.get(1).getName().getFullyQualifiedName());
-      assertEquals(Map.class.getName(), imports.get(2).getName().getFullyQualifiedName());
+      assertEquals(Map.class.getName(), imports.get(2).getName());
    }
 
    @Test
    public void testAddMethod() throws Exception
    {
-      JavaClass javaClass = new JavaClass(stream);
-      javaClass.addMethod().name("testMethod").returnType(String.class).add();
+      javaClass.addMethod().setName("testMethod").setReturnTypeVoid().setBody("").applyChanges();
       List<Method> methods = javaClass.getMethods();
       assertEquals(3, methods.size());
+      assertNull(methods.get(2).getReturnType());
    }
 
+   @Test
+   public void testAddMethodFromString() throws Exception
+   {
+      javaClass.addMethod("public URL rewriteURL(String pattern, String replacement) { return null; }")
+               .setPackagePrivate().applyChanges();
+      List<Method> methods = javaClass.getMethods();
+      assertEquals(3, methods.size());
+      assertEquals("URL", methods.get(2).getReturnType());
+      assertEquals("rewriteURL", methods.get(2).getName());
+
+      String body = methods.get(2).getBody();
+      assertEquals("return null;".replaceAll("\\s+", ""),
+                     body.replaceAll("\\s+", ""));
+   }
+
+   @Test
+   public void testAddConstructor() throws Exception
+   {
+      javaClass.addMethod().setName("testMethod").setConstructor(true).setProtected().setReturnType(String.class)
+               .setBody("System.out.println(\"I am a constructor!\");").applyChanges();
+      List<Method> methods = javaClass.getMethods();
+      assertEquals(3, methods.size());
+      assertTrue(methods.get(2).isProtected());
+      assertTrue(methods.get(2).isConstructor());
+      assertNull(methods.get(2).getReturnType());
+      String body = methods.get(2).getBody();
+      assertEquals("System.out.println(\"I am a constructor!\");".replaceAll("\\s+", ""),
+                     body.replaceAll("\\s+", ""));
+   }
+
+   @Test
+   public void testAddConstructorIngoresReturnTypeAndName() throws Exception
+   {
+      javaClass.addMethod().setName("testMethod").setConstructor(true).setPrivate().setReturnType(String.class)
+               .setBody("System.out.println(\"I am a constructor!\");").applyChanges();
+      List<Method> methods = javaClass.getMethods();
+      assertTrue(methods.get(2).isPrivate());
+      assertTrue(methods.get(2).isConstructor());
+      assertNull(methods.get(2).getReturnType());
+      assertEquals("MockClassFile", methods.get(2).getName());
+      String body = methods.get(2).getBody();
+      assertEquals("System.out.println(\"I am a constructor!\");".replaceAll("\\s+", ""),
+                     body.replaceAll("\\s+", ""));
+   }
+
 }



More information about the seam-commits mailing list