예제 #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)));
  }
예제 #2
0
  @Override
  public Result execute(UIExecutionContext context) throws Exception {
    UIContext uiContext = context.getUIContext();
    JavaResource resource = (JavaResource) uiContext.getInitialSelection().get();
    String name = named.getValue();
    String fieldName = conversationFieldName.getValue();
    String beginName = beginMethodName.getValue();
    String endName = endMethodName.getValue();
    Boolean overwriteValue = overwrite.getValue();
    UIOutput output = uiContext.getProvider().getOutput();
    if (resource.exists()) {
      if (resource.getJavaSource().isClass()) {
        JavaClass javaClass = (JavaClass) resource.getJavaSource();

        if (javaClass.hasField(fieldName)
            && !javaClass.getField(fieldName).isType(Conversation.class)) {
          if (overwriteValue) {
            javaClass.removeField(javaClass.getField(fieldName));
          } else {
            return Results.fail("Field [" + fieldName + "] already exists.");
          }
        }
        if (javaClass.hasMethodSignature(beginName)
            && (javaClass.getMethod(beginName).getParameters().size() == 0)) {
          if (overwriteValue) {
            javaClass.removeMethod(javaClass.getMethod(beginName));
          } else {
            return Results.fail("Method [" + beginName + "] exists.");
          }
        }
        if (javaClass.hasMethodSignature(endName)
            && (javaClass.getMethod(endName).getParameters().size() == 0)) {
          if (overwriteValue) {
            javaClass.removeMethod(javaClass.getMethod(endName));
          } else {
            return Results.fail("Method [" + endName + "] exists.");
          }
        }

        javaClass
            .addField()
            .setPrivate()
            .setName(fieldName)
            .setType(Conversation.class)
            .addAnnotation(Inject.class);

        Method<JavaClass> beginMethod =
            javaClass.addMethod().setName(beginName).setReturnTypeVoid().setPublic();
        if (Strings.isNullOrEmpty(name)) {
          beginMethod.setBody(fieldName + ".begin();");
        } else {
          beginMethod.setBody(fieldName + ".begin(\"" + name + "\");");
        }

        if (timeout.getValue() != null) {
          beginMethod.setBody(
              beginMethod.getBody() + "\n" + fieldName + ".setTimeout(" + timeout + ");");
        }

        javaClass
            .addMethod()
            .setName(endName)
            .setReturnTypeVoid()
            .setPublic()
            .setBody(fieldName + ".end();");

        if (javaClass.hasSyntaxErrors()) {
          output.err().println("Modified Java class contains syntax errors:");
          for (SyntaxError error : javaClass.getSyntaxErrors()) {
            output.err().print(error.getDescription());
          }
        }

        resource.setContents(javaClass);
      } else {
        return Results.fail(
            "Must operate on a Java Class file, not an ["
                + resource.getJavaSource().getSourceType()
                + "]");
      }
    }
    return Results.success("Conversation block created");
  }