Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #232 from fiorenzino/FORGE-674
FORGE-674
  • Loading branch information
gastaldi committed Oct 23, 2012
2 parents b15d4a6 + 8bbf489 commit 1806a7e
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 14 deletions.
48 changes: 48 additions & 0 deletions dev-plugins/src/main/java/org/jboss/forge/dev/java/JavaPlugin.java
Expand Up @@ -21,6 +21,7 @@
import org.jboss.forge.parser.java.Import;
import org.jboss.forge.parser.java.JavaClass;
import org.jboss.forge.parser.java.JavaEnum;
import org.jboss.forge.parser.java.JavaInterface;
import org.jboss.forge.parser.java.JavaSource;
import org.jboss.forge.parser.java.Method;
import org.jboss.forge.parser.java.MethodHolder;
Expand Down Expand Up @@ -139,6 +140,53 @@ else if (in != null)
pickUp.fire(new PickupResource(java.getJavaResource(jc)));
}

@Command("new-interface")
public void newInterface(
@PipeIn final InputStream in,
@Option(required = false, help = "the package in which to build this Interface", description = "source package", type = PromptType.JAVA_PACKAGE, name = "package") final String pckg,
@Option(required = false, help = "the interface definition: surround with quotes", description = "interface definition") final String... def)
throws FileNotFoundException
{
JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);
JavaInterface jc = null;
if (def != null)
{
String classDef = Strings.join(Arrays.asList(def), " ");
jc = JavaParser.parse(JavaInterface.class, classDef);
}
else if (in != null)
{
jc = JavaParser.parse(JavaInterface.class, in);
}
else
{
throw new RuntimeException("arguments required");
}
if (pckg != null)
{
jc.setPackage(pckg);
}
if (!jc.hasSyntaxErrors())
{
java.saveJavaSource(jc);
}
else
{
writer.println(ShellColor.RED, "Syntax Errors:");
for (SyntaxError error : jc.getSyntaxErrors())
{
writer.println(error.toString());
}
writer.println();
if (prompt.promptBoolean(
"Your class has syntax errors, create anyway?", true))
{
java.saveJavaSource(jc);
}
}
pickUp.fire(new PickupResource(java.getJavaResource(jc)));
}

@Command("new-enum-type")
public void newEnumType(
@PipeIn final InputStream in,
Expand Down
Expand Up @@ -13,6 +13,7 @@
import org.jboss.forge.parser.java.Field;
import org.jboss.forge.parser.java.JavaClass;
import org.jboss.forge.parser.java.JavaEnum;
import org.jboss.forge.parser.java.JavaInterface;
import org.jboss.forge.parser.java.Method;
import org.jboss.forge.project.facets.JavaSourceFacet;
import org.jboss.forge.shell.util.Packages;
Expand Down Expand Up @@ -42,33 +43,51 @@ public void testCreateJavaClass() throws Exception
}

@Test
public void testCreateJavaField() throws Exception
public void testCreateJavaInterface() throws Exception
{
getShell().execute(
"java new-class --package org.jboss.forge.test.classes \"public class TestingFieldCreation {}\"");
getShell().execute("java new-field \"private int testing;\"");
getShell()
.execute(
"java new-interface --package org.jboss.forge.test.interfaces \"public interface TestingInterfaceCreation {}\"");
getShell().execute("build");
JavaInterface javaInterface = (JavaInterface) getProject()
.getFacet(JavaSourceFacet.class)
.getJavaResource(
Packages.toFileSyntax("org.jboss.forge.test.interfaces.TestingInterfaceCreation"))
.getJavaSource();
assertNotNull(javaInterface);
}

@Test
public void testCreateJavaMethodOnInterface() throws Exception
{
getShell()
.execute(
"java new-interface --package org.jboss.forge.test.interfaces \"public interface TestingInterfaceCreation {}\"");
getShell().execute("java new-method \"public void testing();\"");
getShell().execute("ls");
getShell().execute("build");
JavaClass javaClass = (JavaClass) getProject().getFacet(JavaSourceFacet.class)
.getJavaResource(Packages.toFileSyntax("org.jboss.forge.test.classes.TestingFieldCreation"))
JavaInterface javaInterface = (JavaInterface) getProject()
.getFacet(JavaSourceFacet.class)
.getJavaResource(
Packages.toFileSyntax("org.jboss.forge.test.interfaces.TestingInterfaceCreation"))
.getJavaSource();
Field<JavaClass> field = javaClass.getField("testing");
assertNotNull(field);
Method<JavaInterface> method = javaInterface.getMethod("testing");
assertNotNull(method);
}

@Test
public void testCreateJavaMethod() throws Exception
public void testCreateJavaField() throws Exception
{
getShell().execute(
"java new-class --package org.jboss.forge.test.classes \"public class TestingMethodCreation {}\"");
getShell().execute("java new-method \"public void testing(){}\"");
"java new-class --package org.jboss.forge.test.classes \"public class TestingFieldCreation {}\"");
getShell().execute("java new-field \"private int testing;\"");
getShell().execute("ls");
getShell().execute("build");
JavaClass javaClass = (JavaClass) getProject().getFacet(JavaSourceFacet.class)
.getJavaResource(Packages.toFileSyntax("org.jboss.forge.test.classes.TestingMethodCreation"))
.getJavaResource(Packages.toFileSyntax("org.jboss.forge.test.classes.TestingFieldCreation"))
.getJavaSource();
Method<JavaClass> method = javaClass.getMethod("testing");
assertNotNull(method);
Field<JavaClass> field = javaClass.getField("testing");
assertNotNull(field);
}

@Test
Expand Down

0 comments on commit 1806a7e

Please sign in to comment.