@Override
 @SuppressWarnings({"unchecked", "rawtypes"})
 public boolean delete() throws UnsupportedOperationException {
   JavaSource<?> origin = method.getOrigin();
   if (origin instanceof MethodHolder) {
     ((MethodHolder) origin).removeMethod(method);
     if (!((MethodHolder) origin).hasMethodSignature(method)) {
       ((JavaResource) this.getParent()).setContents(origin.toString());
       return true;
     }
   }
   return false;
 }
Beispiel #2
0
  @Command("new-method")
  @RequiresResource(JavaResource.class)
  public void newMethod(
      @PipeIn final String in,
      final PipeOut out,
      @Option(
              required = false,
              help = "the method definition: surround with single quotes",
              description = "method definition")
          final String... def)
      throws FileNotFoundException {
    JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);

    String methodDef = null;
    if (def != null) {
      methodDef = Strings.join(Arrays.asList(def), " ");
    } else if (in != null) {
      methodDef = in;
    } else {
      throw new RuntimeException("arguments required");
    }

    JavaSource<?> source = resource.getJavaSource();
    if (source instanceof MethodHolder) {
      MethodHolder<?> clazz = ((MethodHolder<?>) source);

      Method<JavaClass> method =
          JavaParser.parse(JavaClass.class, "public class Temp{}").addMethod(methodDef);
      if (clazz.hasMethodSignature(method)) {
        throw new IllegalStateException(
            "Method with signature [" + method.toSignature() + "] already exists.");
      }

      clazz.addMethod(methodDef);
      java.saveJavaSource(source);
    }
  }
  /**
   * Lookup getter-based properties.
   *
   * <p>This method will be called after <code>lookupFields</code> but before <code>lookupSetters
   * </code>.
   */
  protected void lookupGetters(
      final Map<String, Property> properties, final MethodHolder<?> clazz) {
    // Hack until https://issues.jboss.org/browse/FORGE-368

    for (Method<?> method : clazz.getMethods()) {
      // Exclude static methods

      if (method.isStatic()) {
        continue;
      }

      // Get type

      if (!method.getParameters().isEmpty()) {
        continue;
      }

      String returnType = method.getQualifiedReturnType();

      if (returnType == null) {
        continue;
      }

      // Get name

      String propertyName = isGetter(method);

      if (propertyName == null) {
        continue;
      }

      Field<?> privateField = getPrivateField((FieldHolder<?>) clazz, propertyName);

      if (privateField != null && this.privateFieldConvention == null) {
        propertyName = privateField.getName();
      }

      properties.put(
          propertyName,
          new ForgeProperty(propertyName, returnType, method, null, privateField, this.project));
    }
  }
Beispiel #4
0
  /**
   * Lookup setter-based properties.
   *
   * <p>This method will be called after <code>lookupFields</code> and <code>lookupGetters</code>.
   */
  protected void lookupSetters(
      final Map<String, Property> properties, final MethodHolder<?> clazz) {
    for (Method<?> method : clazz.getMethods()) {
      // Exclude static methods

      if (method.isStatic()) {
        continue;
      }

      // Get type

      List<Parameter> parameters = method.getParameters();

      if (parameters.size() != 1) {
        continue;
      }

      // Get name

      String propertyName = isSetter(method);

      if (propertyName == null) {
        continue;
      }

      // Exclude based on other criteria
      //
      // (explicitly set to null in case we encountered an imbalanced field/getter)

      String type = parameters.get(0).getType();

      Field<?> privateField = getPrivateField((FieldHolder<?>) clazz, propertyName);

      if (privateField != null && this.privateFieldConvention == null) {
        propertyName = privateField.getName();
      }

      // Already found via its getter?

      Property existingProperty = properties.get(propertyName);

      if (existingProperty instanceof ForgeProperty) {
        ForgeProperty existingForgeProperty = (ForgeProperty) existingProperty;

        // Beware covariant return types: always prefer the getter's type

        properties.put(
            propertyName,
            new ForgeProperty(
                propertyName,
                existingForgeProperty.getType(),
                existingForgeProperty.getReadMethod(),
                method,
                getPrivateField((FieldHolder<?>) clazz, propertyName)));
        continue;
      }

      // Explicitly excluded based on getter already?

      if ((existingProperty == null) && properties.containsKey(propertyName)) {
        continue;
      }

      properties.put(
          propertyName, new ForgeProperty(propertyName, type, null, method, privateField));
    }
  }