示例#1
0
  @Command("new-class")
  public void newClass(
      @PipeIn final InputStream in,
      @Option(
              required = false,
              help = "the package in which to build this Class",
              description = "source package",
              type = PromptType.JAVA_PACKAGE,
              name = "package")
          final String pckg,
      @Option(
              required = false,
              help = "the class definition: surround with quotes",
              description = "class definition")
          final String... def)
      throws FileNotFoundException {

    JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);

    JavaClass jc = null;
    if (def != null) {
      String classDef = Strings.join(Arrays.asList(def), " ");
      jc = JavaParser.parse(JavaClass.class, classDef);
    } else if (in != null) {
      jc = JavaParser.parse(JavaClass.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)));
  }
  @Test
  public void testCreateEndpointPrimitiveNonStandardGetterId() throws Exception {
    Project project = getProject();
    JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);
    JavaClass entity =
        JavaParser.parse(JavaClass.class, RestPluginTest.class.getResourceAsStream("User3.java"));
    entity.setPackage(java.getBasePackage() + ".model");
    java.saveJavaSource(entity);

    getShell().setCurrentResource(java.getJavaResource(entity));

    setupRest();

    queueInputLines("");
    getShell().execute("rest endpoint-from-entity");

    JavaResource resource = java.getJavaResource(java.getBasePackage() + ".rest.User3Endpoint");
    JavaClass endpoint = (JavaClass) resource.getJavaSource();

    assertEquals("/user3", endpoint.getAnnotation(Path.class).getStringValue());
    assertTrue(endpoint.toString().contains("entity.setObjectId(id);"));
    getShell().execute("build");
  }