【问题标题】:Java code - how to remove only annotated methods with scriptJava代码 - 如何使用脚本仅删除带注释的方法
【发布时间】:2020-05-06 03:57:07
【问题描述】:

我想知道是否有办法(gradle 脚本或任何脚本或任何其他没有 IDE 的方式)来删除带有某些注释的方法。示例:

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

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

现在当我运行脚本或 gradle 目标时,它应该删除 getValue() 方法,输出代码应该变成:

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

是否有现有的脚本或方法来实现这一点?它可能可以通过 grep 和字符串解析等来实现,但我正在寻找一种更简洁的解决方案,它能够通过注释 id 获取所有方法,并通过格式化删除它们。我尝试在 Google、Stack Overflow 等上搜索,但找不到解决方案。

【问题讨论】:

  • 您希望在 IDE 中完成此操作,还是通过读取源代码的程序来完成?
  • 嗨@MauricePerry - 最好是一个程序或一个gradle目标......可以独立调用的东西。谢谢。

标签: java code-generation automated-refactoring transpiler


【解决方案1】:

我写了一个模块来处理类似的任务。

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

}

你的情况

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

  int getValue() {
    return 20;
  }
}

会变成:

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

你会得到一个编译错误,因为现在我的模块只是删除任何包含你指定的键的方法或方法体语句。

【讨论】:

    猜你喜欢
    • 2010-10-24
    • 2015-01-09
    • 1970-01-01
    • 1970-01-01
    • 2010-10-04
    • 1970-01-01
    • 1970-01-01
    • 2017-10-14
    • 1970-01-01
    相关资源
    最近更新 更多