Uploaded image for project: 'Forge'
  1. Forge
  2. FORGE-674

JavaPlugin: add new command 'new-interface'

XMLWordPrintable

      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)

      { 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)));
      }

      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);
      }

              fiorenzino@gmail.com fiorenzo pizza (Inactive)
              fiorenzino@gmail.com fiorenzo pizza (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved: