0

I wonder if there is a way (a gradle script or any script or any other way without an IDE) to remove methods annotated with certain annotations. Example:

class x {
  public static void main(String[] args) {
    int x = getValue();
    System.out.println(x);
  }

  @RemoveEnabled(id = "getValueMethod1", return = "10")
  int getValue() {
    return 20;
  }
}

Now when I run the script or gradle target, it should remove the getValue() method, and the output code should become:

class x {
  public static void main(String[] args) {
    int x = 10;
    System.out.println(x);
  }
}

Is there an existing script or way to achieve this? It might be achievable with grep and String parsing etc., but I'm looking for a cleaner solution which is able to get all methods by an annotation id, and remove them with formatting. I tried searching on Google, Stack Overflow etc., but couldn't find a solution.

2
  • Would you want this done from inside an IDE, or by a program reading the source code? Commented Jan 20, 2020 at 7:02
  • Hi @MauricePerry - Preferably a program or a gradle target.. something which can be independently invoked. Thanks. Commented Jan 20, 2020 at 7:06

1 Answer 1

1

I wrote a module to process similiar task.

https://github.com/KnIfER/Metaline

@StripMethods(keys={"ToRemove","AlsoRemove"})
public class YourCLZ{

void ToRemove(){} // will be removed

void AlsoRemove(){} // will be removed

@StripMethods(strip=true)
void AnotherTest(){} // will also be removed

}

in your case

@StripMethods(keys="getValue")
class yourx {
  public static void main(String[] args) {
    int x = getValue();
    System.out.println(x);
  }

  int getValue() {
    return 20;
  }
}

will become:

class yourx {
  public static void main(String[] args) {
    System.out.println(x);
  }
}

you will get a compilation error since for now my module simply remove any methods or method body statements that contain the key you specified.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.