[seam-commits] Seam SVN: r13520 - in sandbox/encore: core/src/main/java/org/jboss/encore/grammar and 15 other directories.
seam-commits at lists.jboss.org
seam-commits at lists.jboss.org
Tue Jul 27 16:04:12 EDT 2010
Author: lincolnthree
Date: 2010-07-27 16:04:10 -0400 (Tue, 27 Jul 2010)
New Revision: 13520
Added:
sandbox/encore/core/src/main/java/org/jboss/encore/grammar/Mutable.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/ModifierAccessor.java
sandbox/encore/core/src/test/java/org/jboss/encore/grammar/java/MethodTest.java
Removed:
sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/Method.java
sandbox/encore/core/src/main/java/org/jboss/encore/model/Mutable.java
sandbox/encore/core/src/main/java/org/jboss/encore/model/Project.java
sandbox/encore/core/src/main/java/org/jboss/encore/model/Stateful.java
sandbox/encore/core/src/main/java/org/jboss/encore/model/java/
sandbox/encore/core/src/main/java/org/jboss/seam/
sandbox/encore/core/src/test/java/org/jboss/seam/
sandbox/encore/src/main/java/org/jboss/encore/grammar/java/
sandbox/encore/src/main/java/org/jboss/encore/model/java/
sandbox/encore/src/main/java/org/jboss/encore/mojo/
sandbox/encore/src/main/java/org/jboss/seam/encore/
sandbox/encore/src/test/java/org/jboss/encore/grammar/java/
sandbox/encore/src/test/java/org/jboss/seam/encore/
sandbox/encore/src/test/resources/org/
Modified:
sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/Import.java
sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/JavaClass.java
sandbox/encore/core/src/test/java/org/jboss/encore/grammar/java/JavaClassTest.java
sandbox/encore/model/pom.xml
sandbox/encore/model/src/main/java/org/jboss/encore/model/AbstractProject.java
sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/builtin/HelpPlugin.java
Log:
Java parser enhancements (can change class and method modifiers, set class and method names and visibility, and remove imports)
Copied: sandbox/encore/core/src/main/java/org/jboss/encore/grammar/Mutable.java (from rev 13512, sandbox/encore/core/src/main/java/org/jboss/encore/model/Mutable.java)
===================================================================
--- sandbox/encore/core/src/main/java/org/jboss/encore/grammar/Mutable.java (rev 0)
+++ sandbox/encore/core/src/main/java/org/jboss/encore/grammar/Mutable.java 2010-07-27 20:04:10 UTC (rev 13520)
@@ -0,0 +1,37 @@
+/*
+ * 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 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/main/java/org/jboss/encore/grammar/java/Import.java
===================================================================
--- sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/Import.java 2010-07-27 19:11:53 UTC (rev 13519)
+++ sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/Import.java 2010-07-27 20:04:10 UTC (rev 13520)
@@ -25,7 +25,7 @@
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;
+import org.jboss.encore.grammar.Mutable;
/**
* @author <a href="mailto:lincolnbaxter at gmail.com">Lincoln Baxter, III</a>
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-27 19:11:53 UTC (rev 13519)
+++ sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/JavaClass.java 2010-07-27 20:04:10 UTC (rev 13520)
@@ -31,6 +31,8 @@
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.Modifier.ModifierKeyword;
+import org.eclipse.jdt.core.dom.PackageDeclaration;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.internal.compiler.util.Util;
import org.eclipse.jface.text.BadLocationException;
@@ -39,9 +41,10 @@
import org.eclipse.text.edits.TextEdit;
import org.eclipse.text.edits.UndoEdit;
import org.jboss.encore.grammar.Internal;
+import org.jboss.encore.grammar.Mutable;
import org.jboss.encore.grammar.java.ast.MethodFinderVisitor;
+import org.jboss.encore.grammar.java.ast.ModifierAccessor;
import org.jboss.encore.grammar.java.ast.TypeDeclarationFinderVisitor;
-import org.jboss.encore.model.Mutable;
/**
* @author <a href="mailto:lincolnbaxter at gmail.com">Lincoln Baxter, III</a>
@@ -52,13 +55,17 @@
private Document document;
private CompilationUnit unit;
+ private final ModifierAccessor ma = new ModifierAccessor();
/**
- * 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.
+ * 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.
*
- * @param source - the java source to be parsed (i.e. the char[] contains Java source).
- * @return CompilationUnit Abstract syntax tree representation of a java source file.
+ * @param source - the java source to be parsed (i.e. the char[] contains
+ * Java source).
+ * @return CompilationUnit Abstract syntax tree representation of a java
+ * source file.
*/
public JavaClass(final InputStream inputStream)
{
@@ -94,56 +101,74 @@
unit.recordModifications();
}
+ /*
+ * Import modifiers
+ */
+
@SuppressWarnings("unchecked")
- public Method addMethod()
+ public Import addImport(final String className)
{
- Method m = new Method(this);
- getTypeDeclaration().bodyDeclarations().add(m.getInternal());
- return m;
+ Import imprt = new Import(this).setName(className);
+ unit.imports().add(imprt.getInternal());
+ return imprt;
}
- @SuppressWarnings("unchecked")
- public Method addMethod(final String method)
+ public Import addImport(final Class<?> type)
{
- Method m = new Method(this, method);
- getTypeDeclaration().bodyDeclarations().add(m.getInternal());
- return m;
+ return addImport(type.getName());
}
- @SuppressWarnings("unchecked")
- public Import addImport(final String className)
+ public JavaClass addImports(final Class<?>... types)
{
- Import imprt = new Import(this).setName(className);
- unit.imports().add(imprt.getInternal());
- return imprt;
+ for (Class<?> type : types)
+ {
+ addImport(type.getName());
+ }
+ return this;
}
- public void addImports(final String... types)
+ public JavaClass addImports(final String... types)
{
for (String type : types)
{
addImport(type);
}
+ return this;
}
- public void addImport(final Class<?> type)
+ public JavaClass removeImport(String name)
{
- addImport(type.getName());
+ for (Import i : getImports())
+ {
+ if (i.getName().equals(name))
+ {
+ removeImport(i);
+ break;
+ }
+ }
+ return this;
}
- public void addImports(final Class<?>... types)
+ public JavaClass removeImport(Class<?> clazz)
{
- for (Class<?> type : types)
- {
- addImport(type.getName());
- }
+ return removeImport(clazz.getName());
}
- public Stack<UndoEdit> getUndoStack()
+ public JavaClass removeImport(Import imprt)
{
- return undoStack;
+ Object internal = imprt.getInternal();
+ if (unit.imports().contains(internal))
+ {
+ unit.imports().remove(internal);
+ }
+ return this;
}
+ /**
+ * Get a list of the {@link Import}s contained within this {@link JavaClass}.
+ * Note that modification of this list does not affect internal state, but
+ * modification of individual {@link Import} objects will.
+ */
@SuppressWarnings("unchecked")
public List<Import> getImports()
{
@@ -157,6 +182,31 @@
return results;
}
+ /*
+ * Method modifiers
+ */
+
+ @SuppressWarnings("unchecked")
+ public Method addMethod()
+ {
+ Method m = new Method(this);
+ getTypeDeclaration().bodyDeclarations().add(m.getInternal());
+ return m;
+ }
+
+ @SuppressWarnings("unchecked")
+ public Method addMethod(final String method)
+ {
+ Method m = new Method(this, method);
+ getTypeDeclaration().bodyDeclarations().add(m.getInternal());
+ return m;
+ }
+
+ /**
+ * Get a list of the {@link Method}s contained within this {@link JavaClass}.
+ * Note that modification of this list does not affect internal state, but
+ * modification of individual {@link Method} objects will.
+ */
public List<Method> getMethods()
{
List<Method> result = new ArrayList<Method>();
@@ -181,18 +231,138 @@
return this;
}
- public TypeDeclaration getTypeDeclaration()
+ private TypeDeclaration getTypeDeclaration()
{
TypeDeclarationFinderVisitor typeDeclarationFinder = new TypeDeclarationFinderVisitor();
unit.accept(typeDeclarationFinder);
return typeDeclarationFinder.getTypeDeclarations().get(0);
}
+ /*
+ * Name modifiers
+ */
+
public String getName()
{
return getTypeDeclaration().getName().getIdentifier();
}
+ public JavaClass setName(String name)
+ {
+ getTypeDeclaration().setName(unit.getAST().newSimpleName(name));
+ return this;
+ }
+
+ /*
+ * Package modifiers
+ */
+
+ public String getPackage()
+ {
+ PackageDeclaration pkg = unit.getPackage();
+ if (pkg != null)
+ {
+ return pkg.getName().getFullyQualifiedName();
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ public JavaClass setPackage(String name)
+ {
+ unit.getPackage().setName(unit.getAST().newName(name));
+ return this;
+ }
+
+ public JavaClass setDefaultPackage()
+ {
+ unit.setPackage(null);
+ return this;
+ }
+
+ public boolean isDefaultPackage()
+ {
+ return unit.getPackage() == null;
+ }
+
+ /*
+ * Visibility modifiers
+ */
+ public boolean isPackagePrivate()
+ {
+ return (!isPublic() && !isPrivate() && !isProtected());
+ }
+
+ public JavaClass setPackagePrivate()
+ {
+ ma.clearVisibility(getTypeDeclaration());
+ return this;
+ }
+
+ public boolean isPublic()
+ {
+ return ma.hasModifier(getTypeDeclaration(), ModifierKeyword.PUBLIC_KEYWORD);
+ }
+
+ public JavaClass setPublic()
+ {
+ ma.clearVisibility(getTypeDeclaration());
+ ma.addModifier(getTypeDeclaration(), ModifierKeyword.PUBLIC_KEYWORD);
+ return this;
+ }
+
+ public boolean isPrivate()
+ {
+ return ma.hasModifier(getTypeDeclaration(), ModifierKeyword.PRIVATE_KEYWORD);
+ }
+
+ public JavaClass setPrivate()
+ {
+ ma.clearVisibility(getTypeDeclaration());
+ ma.addModifier(getTypeDeclaration(), ModifierKeyword.PRIVATE_KEYWORD);
+ return this;
+ }
+
+ public boolean isProtected()
+ {
+ return ma.hasModifier(getTypeDeclaration(), ModifierKeyword.PROTECTED_KEYWORD);
+ }
+
+ public JavaClass setProtected()
+ {
+ ma.clearVisibility(getTypeDeclaration());
+ ma.addModifier(getTypeDeclaration(), ModifierKeyword.PROTECTED_KEYWORD);
+ return this;
+ }
+
+ /*
+ * Type modifiers
+ */
+
+ public boolean isAbstract()
+ {
+ return ma.hasModifier(getTypeDeclaration(), ModifierKeyword.ABSTRACT_KEYWORD);
+ }
+
+ public JavaClass setAbstract(boolean abstrct)
+ {
+ if (abstrct)
+ {
+ ma.addModifier(getTypeDeclaration(), ModifierKeyword.ABSTRACT_KEYWORD);
+ }
+ else
+ {
+ ma.removeModifier(getTypeDeclaration(), ModifierKeyword.ABSTRACT_KEYWORD);
+ }
+ return this;
+ }
+
+ /*
+ * Non-manipulation methods.
+ */
+
@Override
public String toString()
{
Deleted: 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-27 19:11:53 UTC (rev 13519)
+++ sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/Method.java 2010-07-27 20:04:10 UTC (rev 13520)
@@ -1,327 +0,0 @@
-/*
- * 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.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.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 implements Internal, Mutable
-{
- private JavaClass parent = null;
- private AST ast = null;
- private CompilationUnit cu = null;
- private final MethodDeclaration method;
-
- private void init(final JavaClass parent)
- {
- 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 parent, final Object internal)
- {
- init(parent);
- method = (MethodDeclaration) internal;
- }
-
- public Method(final JavaClass parent, final String method)
- {
- 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 setConstructor(final boolean isConstructor)
- {
- method.setConstructor(isConstructor);
- if (isConstructor())
- {
- method.setName(ast.newSimpleName(parent.getName()));
- }
- return this;
- }
-
- public boolean isConstructor()
- {
- return method.isConstructor();
- }
-
- @SuppressWarnings("unchecked")
- public Method setAbstract()
- {
- method.modifiers().add(ast.newModifier(ModifierKeyword.ABSTRACT_KEYWORD));
- return this;
- }
-
- @SuppressWarnings("unchecked")
- public Method setFinal()
- {
- method.modifiers().add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD));
- return this;
- }
-
- public String getName()
- {
- 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 Method setPackagePrivate()
- {
- List<Modifier> modifiers = method.modifiers();
-
- List<Modifier> toBeRemoved = new ArrayList<Modifier>();
- for (Modifier modifier : modifiers)
- {
- if (modifier.isPrivate() || modifier.isProtected() || modifier.isPublic())
- {
- toBeRemoved.add(modifier);
- }
- }
-
- modifiers.removeAll(toBeRemoved);
-
- 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();
- }
- 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)
- {
- if (m.getKeyword() == modifier)
- {
- result = true;
- }
- }
- return result;
- }
-
- @Override
- public Object getInternal()
- {
- return method;
- }
-
- public void applyChanges()
- {
- parent.applyChanges();
- }
-
- @Override
- public int hashCode()
- {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((method == null) ? 0 : method.hashCode());
- result = prime * result + ((parent == null) ? 0 : parent.hashCode());
- return result;
- }
-
- @Override
- public boolean equals(final Object obj)
- {
- if (this == obj)
- {
- return true;
- }
- if (obj == null)
- {
- return false;
- }
- if (getClass() != obj.getClass())
- {
- return false;
- }
- Method other = (Method) obj;
- if (method == null)
- {
- if (other.method != null)
- {
- return false;
- }
- }
- else if (!method.equals(other.method))
- {
- return false;
- }
- if (parent == null)
- {
- if (other.parent != null)
- {
- return false;
- }
- }
- else if (!parent.equals(other.parent))
- {
- return false;
- }
- return true;
- }
-}
Copied: sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/Method.java (from rev 13512, 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 (rev 0)
+++ sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/Method.java 2010-07-27 20:04:10 UTC (rev 13520)
@@ -0,0 +1,314 @@
+/*
+ * 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.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.jdt.core.dom.Modifier.ModifierKeyword;
+import org.eclipse.jdt.core.dom.Statement;
+import org.jboss.encore.grammar.Internal;
+import org.jboss.encore.grammar.Mutable;
+import org.jboss.encore.grammar.java.ast.ModifierAccessor;
+
+/**
+ * @author <a href="mailto:lincolnbaxter at gmail.com">Lincoln Baxter, III</a>
+ *
+ */
+public class Method implements Internal, Mutable
+{
+ private JavaClass parent = null;
+ private AST ast = null;
+ private CompilationUnit cu = null;
+ private final ModifierAccessor ma = new ModifierAccessor();
+ private final MethodDeclaration method;
+
+ private void init(final JavaClass parent)
+ {
+ 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 parent, final Object internal)
+ {
+ init(parent);
+ method = (MethodDeclaration) internal;
+ }
+
+ public Method(final JavaClass parent, final String method)
+ {
+ 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 setConstructor(final boolean isConstructor)
+ {
+ method.setConstructor(isConstructor);
+ if (isConstructor())
+ {
+ method.setName(ast.newSimpleName(parent.getName()));
+ }
+ return this;
+ }
+
+ public boolean isConstructor()
+ {
+ return method.isConstructor();
+ }
+
+ public boolean isAbstract()
+ {
+ return ma.hasModifier(method, ModifierKeyword.ABSTRACT_KEYWORD);
+ }
+
+ public Method setAbstract(boolean abstrct)
+ {
+ if (abstrct)
+ {
+ ma.addModifier(method, ModifierKeyword.ABSTRACT_KEYWORD);
+ }
+ else
+ {
+ ma.removeModifier(method, ModifierKeyword.ABSTRACT_KEYWORD);
+ }
+ return this;
+ }
+
+ public Method setFinal()
+ {
+ ma.addModifier(method, ModifierKeyword.FINAL_KEYWORD);
+ return this;
+ }
+
+ public String getName()
+ {
+ return method.getName().getFullyQualifiedName();
+ }
+
+ 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());
+ }
+
+ public Method setPackagePrivate()
+ {
+ ma.clearVisibility(method);
+ return this;
+ }
+
+ public boolean isPublic()
+ {
+ return ma.hasModifier(method, ModifierKeyword.PUBLIC_KEYWORD);
+ }
+
+ public Method setPublic()
+ {
+ ma.clearVisibility(method);
+ ma.addModifier(method, ModifierKeyword.PUBLIC_KEYWORD);
+ return this;
+ }
+
+ public boolean isPrivate()
+ {
+ return ma.hasModifier(method, ModifierKeyword.PRIVATE_KEYWORD);
+ }
+
+ public Method setPrivate()
+ {
+ ma.clearVisibility(method);
+ ma.addModifier(method, ModifierKeyword.PRIVATE_KEYWORD);
+ return this;
+ }
+
+ public boolean isProtected()
+ {
+ return ma.hasModifier(method, ModifierKeyword.PROTECTED_KEYWORD);
+ }
+
+ public Method setProtected()
+ {
+ ma.clearVisibility(method);
+ ma.addModifier(method, ModifierKeyword.PROTECTED_KEYWORD);
+ return this;
+ }
+
+ public String getReturnType()
+ {
+ String result = null;
+ if (!isConstructor() && (method.getReturnType2() != null))
+ {
+ result = method.getReturnType2().toString();
+ }
+ 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 boolean isReturnTypeVoid()
+ {
+ return getReturnType() == null;
+ }
+
+ public Method setReturnTypeVoid()
+ {
+ method.setReturnType2(null);
+ return this;
+ }
+
+ private MethodDeclaration getMethodDeclaration()
+ {
+ return method;
+ }
+
+ @Override
+ public String toString()
+ {
+ return method.toString();
+ }
+
+ @Override
+ public Object getInternal()
+ {
+ return method;
+ }
+
+ @Override
+ public void applyChanges()
+ {
+ parent.applyChanges();
+ }
+
+ @Override
+ public int hashCode()
+ {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((method == null) ? 0 : method.hashCode());
+ result = prime * result + ((parent == null) ? 0 : parent.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(final Object obj)
+ {
+ if (this == obj)
+ {
+ return true;
+ }
+ if (obj == null)
+ {
+ return false;
+ }
+ if (getClass() != obj.getClass())
+ {
+ return false;
+ }
+ Method other = (Method) obj;
+ if (method == null)
+ {
+ if (other.method != null)
+ {
+ return false;
+ }
+ }
+ else if (!method.equals(other.method))
+ {
+ return false;
+ }
+ if (parent == null)
+ {
+ if (other.parent != null)
+ {
+ return false;
+ }
+ }
+ else if (!parent.equals(other.parent))
+ {
+ return false;
+ }
+ return true;
+ }
+}
Added: sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/ast/ModifierAccessor.java
===================================================================
--- sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/ast/ModifierAccessor.java (rev 0)
+++ sandbox/encore/core/src/main/java/org/jboss/encore/grammar/java/ast/ModifierAccessor.java 2010-07-27 20:04:10 UTC (rev 13520)
@@ -0,0 +1,67 @@
+package org.jboss.encore.grammar.java.ast;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.jdt.core.dom.BodyDeclaration;
+import org.eclipse.jdt.core.dom.Modifier;
+import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
+
+public class ModifierAccessor
+{
+ @SuppressWarnings("unchecked")
+ public boolean hasModifier(BodyDeclaration body, final ModifierKeyword modifier)
+ {
+ boolean result = false;
+ List<Modifier> modifiers = body.modifiers();
+ for (Modifier m : modifiers)
+ {
+ if (m.getKeyword() == modifier)
+ {
+ result = true;
+ }
+ }
+ return result;
+ }
+
+ @SuppressWarnings("unchecked")
+ public List<Modifier> clearVisibility(BodyDeclaration body)
+ {
+ List<Modifier> modifiers = body.modifiers();
+
+ List<Modifier> toBeRemoved = new ArrayList<Modifier>();
+ for (Modifier modifier : modifiers)
+ {
+ if (modifier.isPrivate() || modifier.isProtected() || modifier.isPublic())
+ {
+ toBeRemoved.add(modifier);
+ }
+ }
+
+ modifiers.removeAll(toBeRemoved);
+ return modifiers;
+ }
+
+ @SuppressWarnings("unchecked")
+ public void addModifier(BodyDeclaration body, ModifierKeyword keyword)
+ {
+ body.modifiers().add(body.getAST().newModifier(keyword));
+ }
+
+ @SuppressWarnings("unchecked")
+ public void removeModifier(BodyDeclaration body, ModifierKeyword keyword)
+ {
+ List<Modifier> modifiers = body.modifiers();
+
+ List<Modifier> toBeRemoved = new ArrayList<Modifier>();
+ for (Modifier modifier : modifiers)
+ {
+ if (modifier.getKeyword().equals(keyword))
+ {
+ toBeRemoved.add(modifier);
+ }
+ }
+
+ modifiers.removeAll(toBeRemoved);
+ }
+}
Deleted: 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-27 19:11:53 UTC (rev 13519)
+++ sandbox/encore/core/src/main/java/org/jboss/encore/model/Mutable.java 2010-07-27 20:04:10 UTC (rev 13520)
@@ -1,37 +0,0 @@
-/*
- * 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.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();
-}
Deleted: sandbox/encore/core/src/main/java/org/jboss/encore/model/Project.java
===================================================================
--- sandbox/encore/core/src/main/java/org/jboss/encore/model/Project.java 2010-07-27 19:11:53 UTC (rev 13519)
+++ sandbox/encore/core/src/main/java/org/jboss/encore/model/Project.java 2010-07-27 20:04:10 UTC (rev 13520)
@@ -1,33 +0,0 @@
-/*
- * 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.model;
-
-/**
- * The root node of the Project Object Graph (POG)
- *
- * @author <a href="mailto:lincolnbaxter at gmail.com">Lincoln Baxter, III</a>
- *
- */
-public interface Project extends Mutable, Stateful
-{
-
-}
Deleted: sandbox/encore/core/src/main/java/org/jboss/encore/model/Stateful.java
===================================================================
--- sandbox/encore/core/src/main/java/org/jboss/encore/model/Stateful.java 2010-07-27 19:11:53 UTC (rev 13519)
+++ sandbox/encore/core/src/main/java/org/jboss/encore/model/Stateful.java 2010-07-27 20:04:10 UTC (rev 13520)
@@ -1,31 +0,0 @@
-/*
- * 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.model;
-
-/**
- * @author <a href="mailto:lincolnbaxter at gmail.com">Lincoln Baxter, III</a>
- *
- */
-public interface Stateful
-{
-
-}
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-27 19:11:53 UTC (rev 13519)
+++ sandbox/encore/core/src/test/java/org/jboss/encore/grammar/java/JavaClassTest.java 2010-07-27 20:04:10 UTC (rev 13520)
@@ -22,6 +22,7 @@
package org.jboss.encore.grammar.java;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
@@ -44,42 +45,153 @@
@Before
public void reset()
{
- stream = JavaClassTest.class
- .getResourceAsStream("/org/jboss/encore/grammar/java/MockClassFile.java");
+ stream = JavaClassTest.class.getResourceAsStream("/org/jboss/encore/grammar/java/MockClassFile.java");
javaClass = new JavaClass(stream);
}
@Test
public void testParse() throws Exception
{
- List<Import> imports = javaClass.getImports();
- assertEquals(URL.class.getName(), imports.get(0).getName());
+ assertEquals(URL.class.getName(), javaClass.getImports().get(0).getName());
+ assertEquals(2, javaClass.getMethods().size());
+ assertEquals("MockClassFile", javaClass.getName());
+ assertTrue(javaClass.isPublic());
+ assertFalse(javaClass.isAbstract());
}
@Test
+ public void testSetName() throws Exception
+ {
+ assertEquals("MockClassFile", javaClass.getName());
+ javaClass.setName("Telephone");
+ assertEquals("Telephone", javaClass.getName());
+ }
+
+ @Test
+ public void testSetPackage() throws Exception
+ {
+ javaClass.setPackage("org.lincoln");
+ assertEquals("org.lincoln", javaClass.getPackage());
+ assertFalse(javaClass.isDefaultPackage());
+ }
+
+ @Test
+ public void testSetAbstract() throws Exception
+ {
+ javaClass.setAbstract(true);
+ assertTrue(javaClass.isAbstract());
+ }
+
+ @Test
+ public void testSetVisibilityPublic() throws Exception
+ {
+ javaClass.setPublic();
+ assertFalse(javaClass.isPrivate());
+ assertFalse(javaClass.isPackagePrivate());
+ assertTrue(javaClass.isPublic());
+ assertFalse(javaClass.isProtected());
+ }
+
+ @Test
+ public void testSetVisibilityPackagePrivate() throws Exception
+ {
+ javaClass.setPackagePrivate();
+ assertTrue(javaClass.isPackagePrivate());
+ assertFalse(javaClass.isPublic());
+ assertFalse(javaClass.isPrivate());
+ assertFalse(javaClass.isProtected());
+ }
+
+ @Test
+ public void testSetVisibilityPrivate() throws Exception
+ {
+ javaClass.setPrivate();
+ assertTrue(javaClass.isPrivate());
+ assertFalse(javaClass.isPackagePrivate());
+ assertFalse(javaClass.isPublic());
+ assertFalse(javaClass.isProtected());
+ }
+
+ @Test
+ public void testSetVisibilityProtected() throws Exception
+ {
+ javaClass.setProtected();
+ assertFalse(javaClass.isPrivate());
+ assertFalse(javaClass.isPackagePrivate());
+ assertFalse(javaClass.isPublic());
+ assertTrue(javaClass.isProtected());
+ }
+
+ @Test
+ public void testSetPackageDefault() throws Exception
+ {
+ javaClass.setDefaultPackage();
+ assertNull(javaClass.getPackage());
+ assertTrue(javaClass.isDefaultPackage());
+ }
+
+ @Test
public void testAddImport() throws Exception
{
javaClass.addImport(List.class.getName());
- List<Import> imports = javaClass.getImports();
- assertEquals(2, imports.size());
- assertEquals(URL.class.getName(), imports.get(0).getName());
- assertEquals(List.class.getName(), imports.get(1).getName());
+ assertEquals(2, javaClass.getImports().size());
+ assertEquals(URL.class.getName(), javaClass.getImports().get(0).getName());
+ assertEquals(List.class.getName(), javaClass.getImports().get(1).getName());
}
@Test
public void testAddImportsClasses() throws Exception
{
+ assertEquals(1, javaClass.getImports().size());
+
+ javaClass.addImports(List.class, Map.class);
+
+ assertEquals(3, javaClass.getImports().size());
+ assertEquals(Map.class.getName(), javaClass.getImports().get(2).getName());
+ }
+
+ @Test
+ public void testAddImportStatic() throws Exception
+ {
+ assertEquals(1, javaClass.getImports().size());
+ javaClass.addImport(List.class).setStatic(true).applyChanges();
+ assertEquals(2, javaClass.getImports().size());
+ assertTrue(javaClass.getImports().get(1).isStatic());
+ }
+
+ @Test
+ public void testRemoveImportByClass() throws Exception
+ {
List<Import> imports = javaClass.getImports();
assertEquals(1, imports.size());
+ assertEquals(URL.class.getName(), imports.get(0).getName());
- javaClass.addImports(List.class, Map.class);
-
+ javaClass.removeImport(URL.class);
imports = javaClass.getImports();
- assertEquals(3, imports.size());
- assertEquals(Map.class.getName(), imports.get(2).getName());
+ assertEquals(0, imports.size());
}
@Test
+ public void testRemoveImportByName() throws Exception
+ {
+ assertEquals(1, javaClass.getImports().size());
+ assertEquals(URL.class.getName(), javaClass.getImports().get(0).getName());
+
+ javaClass.removeImport(URL.class.getName());
+ assertEquals(0, javaClass.getImports().size());
+ }
+
+ @Test
+ public void testRemoveImportByReference() throws Exception
+ {
+ assertEquals(1, javaClass.getImports().size());
+ assertEquals(URL.class.getName(), javaClass.getImports().get(0).getName());
+
+ javaClass.removeImport(javaClass.getImports().get(0));
+ assertEquals(0, javaClass.getImports().size());
+ }
+
+ @Test
public void testAddMethod() throws Exception
{
javaClass.addMethod().setName("testMethod").setReturnTypeVoid().setBody("").applyChanges();
@@ -91,22 +203,19 @@
@Test
public void testAddMethodFromString() throws Exception
{
- javaClass.addMethod("public URL rewriteURL(String pattern, String replacement) { return null; }")
- .setPackagePrivate().applyChanges();
+ 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+", ""));
+ assertEquals("return null;".replaceAll("\\s+", ""), body.replaceAll("\\s+", ""));
}
@Test
public void testRemoveMethod() throws Exception
{
- // TODO Removing methods needs to work
List<Method> methods = javaClass.getMethods();
javaClass.removeMethod(methods.get(0)).applyChanges();
methods = javaClass.getMethods();
@@ -116,31 +225,29 @@
@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+", ""));
+ javaClass.addMethod().setName("testMethod").setConstructor(true).setProtected().setReturnType(String.class).setBody("System.out.println(\"I am a constructor!\");").applyChanges();
+ Method method = javaClass.getMethods().get(javaClass.getMethods().size() - 1);
+ assertEquals(3, javaClass.getMethods().size());
+ assertEquals(javaClass.getName(), method.getName());
+ assertTrue(method.isProtected());
+ assertTrue(method.isConstructor());
+ assertNull(method.getReturnType());
+ String body = method.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+", ""));
+ javaClass.addMethod().setName("testMethod").setConstructor(true).setPrivate().setReturnType(String.class).setBody("System.out.println(\"I am a constructor!\");").applyChanges();
+ Method method = javaClass.getMethods().get(javaClass.getMethods().size() - 1);
+ assertEquals(3, javaClass.getMethods().size());
+ assertTrue(method.isPrivate());
+ assertTrue(method.isConstructor());
+ assertNull(method.getReturnType());
+ assertEquals(javaClass.getName(), method.getName());
+ String body = method.getBody();
+ assertEquals("System.out.println(\"I am a constructor!\");".replaceAll("\\s+", ""), body.replaceAll("\\s+", ""));
}
}
Added: sandbox/encore/core/src/test/java/org/jboss/encore/grammar/java/MethodTest.java
===================================================================
--- sandbox/encore/core/src/test/java/org/jboss/encore/grammar/java/MethodTest.java (rev 0)
+++ sandbox/encore/core/src/test/java/org/jboss/encore/grammar/java/MethodTest.java 2010-07-27 20:04:10 UTC (rev 13520)
@@ -0,0 +1,132 @@
+/*
+ * 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.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.io.InputStream;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * @author <a href="mailto:lincolnbaxter at gmail.com">Lincoln Baxter, III</a>
+ */
+public class MethodTest
+{
+ private InputStream stream;
+ private JavaClass javaClass;
+ private Method method;
+
+ @Before
+ public void reset()
+ {
+ stream = MethodTest.class.getResourceAsStream("/org/jboss/encore/grammar/java/MockClassFile.java");
+ javaClass = new JavaClass(stream);
+ javaClass.addMethod("public URL rewriteURL(String pattern, String replacement) { return null; }");
+ method = javaClass.getMethods().get(javaClass.getMethods().size() - 1);
+ }
+
+ @Test
+ public void testSetName() throws Exception
+ {
+ assertEquals("rewriteURL", method.getName());
+ method.setName("doSomething");
+ assertEquals("doSomething", method.getName());
+ }
+
+ @Test
+ public void testSetReturnType() throws Exception
+ {
+ assertEquals("URL", method.getReturnType());
+ method.setReturnType(Class.class);
+ assertEquals("Class", method.getReturnType());
+ assertFalse(method.isReturnTypeVoid());
+ }
+
+ @Test
+ public void testSetReturnTypeVoid() throws Exception
+ {
+ assertEquals("URL", method.getReturnType());
+ method.setReturnTypeVoid();
+ assertEquals(null, method.getReturnType());
+ assertTrue(method.isReturnTypeVoid());
+ }
+
+ @Test
+ public void testSetConstructor() throws Exception
+ {
+ assertFalse(method.isConstructor());
+ method.setConstructor(true);
+ assertTrue(method.isConstructor());
+ assertEquals(javaClass.getName(), method.getName());
+ }
+
+ @Test
+ public void testSetAbstract() throws Exception
+ {
+ method.setAbstract(true);
+ assertTrue(method.isAbstract());
+ }
+
+ @Test
+ public void testSetPublic() throws Exception
+ {
+ method.setPublic();
+ assertTrue(method.isPublic());
+ assertFalse(method.isPackagePrivate());
+ assertFalse(method.isPrivate());
+ assertFalse(method.isProtected());
+ }
+
+ @Test
+ public void testSetPrivate() throws Exception
+ {
+ method.setPrivate();
+ assertFalse(method.isPublic());
+ assertFalse(method.isPackagePrivate());
+ assertTrue(method.isPrivate());
+ assertFalse(method.isProtected());
+ }
+
+ @Test
+ public void testSetProtected() throws Exception
+ {
+ method.setProtected();
+ assertFalse(method.isPublic());
+ assertFalse(method.isPackagePrivate());
+ assertFalse(method.isPrivate());
+ assertTrue(method.isProtected());
+ }
+
+ @Test
+ public void testSetPackagePrivate() throws Exception
+ {
+ method.setPackagePrivate();
+ assertFalse(method.isPublic());
+ assertTrue(method.isPackagePrivate());
+ assertFalse(method.isPrivate());
+ assertFalse(method.isProtected());
+ }
+}
Modified: sandbox/encore/model/pom.xml
===================================================================
--- sandbox/encore/model/pom.xml 2010-07-27 19:11:53 UTC (rev 13519)
+++ sandbox/encore/model/pom.xml 2010-07-27 20:04:10 UTC (rev 13520)
@@ -19,6 +19,11 @@
<dependencies>
<!-- Resource manipulation dependencies -->
<dependency>
+ <groupId>org.jboss.encore</groupId>
+ <artifactId>encore-core</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
<groupId>org.jboss.shrinkwrap</groupId>
<artifactId>shrinkwrap-api</artifactId>
<version>1.0.0-alpha-9</version>
Modified: sandbox/encore/model/src/main/java/org/jboss/encore/model/AbstractProject.java
===================================================================
--- sandbox/encore/model/src/main/java/org/jboss/encore/model/AbstractProject.java 2010-07-27 19:11:53 UTC (rev 13519)
+++ sandbox/encore/model/src/main/java/org/jboss/encore/model/AbstractProject.java 2010-07-27 20:04:10 UTC (rev 13520)
@@ -22,15 +22,14 @@
package org.jboss.encore.model;
import java.io.BufferedWriter;
-import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
-import java.io.InputStream;
import javax.enterprise.event.Event;
import javax.inject.Inject;
+import org.jboss.encore.grammar.java.JavaClass;
import org.jboss.encore.model.events.JavaFileCreated;
/**
@@ -42,21 +41,23 @@
@Inject
Event<JavaFileCreated> event;
- public boolean createJavaFile(final String path, final byte[] data)
+ public File createJavaFile(final String path, final char[] data)
{
- return createJavaFile(path, new ByteArrayInputStream(data));
+ return createJavaFile(path, new JavaClass(data));
}
- public boolean createJavaFile(final String path, final InputStream data)
+ public File createJavaFile(final String path, JavaClass clazz)
{
BufferedWriter writer = null;
try
{
File file = new File(path);
writer = new BufferedWriter(new FileWriter(file));
+ writer.write(clazz.toString());
+ writer.close();
+
event.fire(new JavaFileCreated(file));
- writer.close();
- return true;
+ return file;
}
catch (IOException e)
{
Modified: sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/builtin/HelpPlugin.java
===================================================================
--- sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/builtin/HelpPlugin.java 2010-07-27 19:11:53 UTC (rev 13519)
+++ sandbox/encore/shell/src/main/java/org/jboss/encore/shell/cli/builtin/HelpPlugin.java 2010-07-27 20:04:10 UTC (rev 13520)
@@ -49,7 +49,7 @@
@Command(help = "Get help about specific commands")
public void help(@Option("test") final String test, @Option final String... commands)
{
- if (commands == null)
+ if ((commands == null) || (commands.length == 0))
{
shell.write("Welcome to Encore!");
}
More information about the seam-commits
mailing list