我到处都找过了,我能找到的唯一一个例子是标记 JOptionPane.show 并将其删除的例子。这对我没有多大帮助。
最佳答案
注意:这会生成一个匹配方法的提示,但完成后,您应该能够匹配更多内容(例如学习 RegEx)
这很难做到和弄清楚...我假设您已经阅读了 https://platform.netbeans.org/tutorials/nbm-java-hint.html 上的教程。 。通过那里的说明创建提示后,您现在有了一个基本的提示实现:
@Hint(displayName = "#DN_MainCallHint", description = "#DESC_MainCallHint",
category = "general")
@Messages({
"DN_MainCallHint=MainCall",
"DESC_MainCallHint=Warns the user when they are manually calling public "
+ "static void main"
})
public class MainCallHint {
@TriggerPattern(value = "$str.equals(\"\")", //Specify a pattern as needed
constraints = @ConstraintVariableType(variable = "$str", type
= "java.lang.String"))
@Messages("ERR_MainCallHint=Directly calling main! Did you mean to do that?")
public static ErrorDescription computeWarning(HintContext ctx) {
return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.
ERR_MainCallHint());
}
}
我遇到的主要问题是@TriggerPattern.value
。如果您想匹配方法调用,您可以将其设置为如下所示:
"$caller.method($arg1, $arg2)"
并且可以为每个变量设置类型约束。需要注意的一件重要事情是:如果您尝试匹配特定类型的静态方法,请使用该类的 FQN,否则它将无法使用参数。所以使用这样的东西:
"test.HintTest.main($args)"
导致 @TriggerPattern
位如下所示:
@TriggerPattern(value = "test.HintTest.main($args)", //Specify a pattern as needed
constraints = @ConstraintVariableType(variable = "$args", type
= "java.lang.String[]"))
你会得到这个:
现在,本教程展示了一种非常繁琐的修复方法。如果您需要做的只是简单的替换,那么可以像这样完成:
public static ErrorDescription computeWarning(HintContext ctx) {
Fix fix = JavaFixUtilities.rewriteFix(ctx, "Use notMain instead",
ctx.getPath(), "test.HintTest.notMain($args)");
return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.
ERR_MainCallHint(), fix);
}
注意:用于rewriteFix
的变量必须与您在@TriggerPattern
中使用的变量相同,否则它们将被解释为文字。
关于java - 如何为 Netbeans 编写提示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42983998/