-
Enhancement
-
Resolution: Done
-
Major
-
1.1.1.Final
javaplugin lacks an useful method to create java interfaces.
very similar to 'new-class', this is my implementation:
@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)
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();
if (prompt.promptBoolean(
"Your class has syntax errors, create anyway?", true))
}
pickUp.fire(new PickupResource(java.getJavaResource(jc)));
}
i tested this and also the command 'new-method' works nicely:
@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<JavaInterface> method = javaClass.getMethod("testing");
assertNotNull(method);
}