forge/core — pull request #215 by fiorenzino (2012-09-14 12:44:53+00:00)
Title: add new-interface command

Hi,

for https://issues.jboss.org/browse/FORGE-674

module: forge-developer-plugins

classes interested from this commit:
org.jboss.forge.dev.java.JavaPlugin
org.jboss.forge.dev.java.JavaPluginTest

below my edits...

In the class org.jboss.forge.dev.java.JavaPlugin a new command method:

```
@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)));
}
```
and in org.jboss.forge.dev.java.JavaPluginTest 2 new methods:

```
@Test
public void testCreateJavaInterface() throws Exception {
getShell()
.execute(
"java new-interface --package org.jboss.forge.test.interfaces \"public interface TestingInterfaceCreation {}\"");
getShell().execute("build");
JavaInterface javaClass = (JavaInterface) getProject()
.getFacet(JavaSourceFacet.class)
.getJavaResource(
Packages.toFileSyntax("org.jboss.forge.test.interfaces.TestingInterfaceCreation"))
.getJavaSource();
assertNotNull(javaClass);
}
```
and

```
@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");
JavaInterface javaClass = (JavaInterface) getProject()
.getFacet(JavaSourceFacet.class)
.getJavaResource(
Packages.toFileSyntax("org.jboss.forge.test.interfaces.TestingInterfaceCreation"))
.getJavaSource();
Method method = javaClass.getMethod("testing");
assertNotNull(method);
}
```