diff --git a/src/main/java/de/markiewb/netbeans/plugins/hints/structure/AddThisToMember.java b/src/main/java/de/markiewb/netbeans/plugins/hints/structure/AddThisToMember.java index d32220b..0a20cfa 100644 --- a/src/main/java/de/markiewb/netbeans/plugins/hints/structure/AddThisToMember.java +++ b/src/main/java/de/markiewb/netbeans/plugins/hints/structure/AddThisToMember.java @@ -67,7 +67,7 @@ "DESC_AddThisToMember=Adds \"this.\" as prefix for non-static member variables/methods.

The hint is disabled by default, because there are only a few specific usecases for this hint.

Provided by nb-additional-hints plugin

",}) public class AddThisToMember { - private static EnumSet supportedKinds = EnumSet.of(ElementKind.FIELD, ElementKind.METHOD); + private static final EnumSet supportedKinds = EnumSet.of(ElementKind.FIELD, ElementKind.METHOD); @Hint(displayName = "#DN_AddThisToMember", description = "#DESC_AddThisToMember", category = "suggestions", hintKind = Hint.Kind.INSPECTION, severity = Severity.WARNING, enabled = false) @NbBundle.Messages("ERR_AddThisToMember=Add \"this.\" prefix") @@ -82,8 +82,13 @@ public static ErrorDescription toTernary(HintContext ctx) { if (!supportedKinds.contains(element.getKind())) { return null; } + //do not transform patterns like "this.dialog" to "this.this.dialog" + final String memberName = ((IdentifierTree) path.getLeaf()).getName().toString(); + if ("this".equals(memberName)) { + return null; + } - String result = "this." + ((IdentifierTree) path.getLeaf()).getName().toString(); + String result = "this." + memberName; if (result != null) { Fix fix = org.netbeans.spi.java.hints.JavaFixUtilities.rewriteFix(ctx, Bundle.ERR_AddThisToMember(), path, result); return ErrorDescriptionFactory.forName(ctx, path, Bundle.ERR_AddThisToMember(), fix); diff --git a/src/test/java/de/markiewb/netbeans/plugins/hints/structure/AddThisToMemberTest.java b/src/test/java/de/markiewb/netbeans/plugins/hints/structure/AddThisToMemberTest.java index c97d883..1436541 100644 --- a/src/test/java/de/markiewb/netbeans/plugins/hints/structure/AddThisToMemberTest.java +++ b/src/test/java/de/markiewb/netbeans/plugins/hints/structure/AddThisToMemberTest.java @@ -16,12 +16,11 @@ public void testMemberMethod() throws Exception { + " void getMember(){\n" + " staticMember();\n" + " getMember();\n" - + " variable = 43;\n" + " };\n" + "}" ) .run(AddThisToMember.class) - .assertWarnings("6:9-6:18:warning:" + Bundle.ERR_AddThisToMember(), "7:9-7:17:warning:" + Bundle.ERR_AddThisToMember()) + .assertWarnings("6:9-6:18:warning:" + Bundle.ERR_AddThisToMember()) .findWarning("6:9-6:18:warning:" + Bundle.ERR_AddThisToMember()) .applyFix() .assertCompilable() @@ -33,12 +32,27 @@ public void testMemberMethod() throws Exception { + " void getMember(){\n" + " staticMember();\n" + " this.getMember();\n" - + " variable = 43;\n" + " };\n" + "}"); } + @Test + public void testMemberMethod_AlreadyThis() throws Exception { + HintTest.create() + .input( + "package example;\n" + + "public class Test {\n" + + " void getMember(){\n" + + " this.getMember();\n" + + " };\n" + + "}" + ) + .run(AddThisToMember.class) + .assertWarnings(); + + } + @Test public void testMemberVariable() throws Exception { HintTest.create() @@ -49,14 +63,13 @@ public void testMemberVariable() throws Exception { + " static void staticMember() {};\n" + " void getMember(){\n" + " staticMember();\n" - + " getMember();\n" + " variable = 43;\n" + " };\n" + "}" ) .run(AddThisToMember.class) - .assertWarnings("6:9-6:18:warning:" + Bundle.ERR_AddThisToMember(), "7:9-7:17:warning:" + Bundle.ERR_AddThisToMember()) - .findWarning("7:9-7:17:warning:" + Bundle.ERR_AddThisToMember()) + .assertWarnings("6:9-6:17:warning:" + Bundle.ERR_AddThisToMember()) + .findWarning("6:9-6:17:warning:" + Bundle.ERR_AddThisToMember()) .applyFix() .assertCompilable() .assertOutput( @@ -66,11 +79,29 @@ public void testMemberVariable() throws Exception { + " static void staticMember() {};\n" + " void getMember(){\n" + " staticMember();\n" - + " getMember();\n" + " this.variable = 43;\n" + " };\n" + "}"); } + @Test + public void testMemberVariable_AlreadyThis() throws Exception { + HintTest.create() + .input( + "package example;\n" + + "public class Test {\n" + + " int variable=42;\n" + + " static void staticMember() {};\n" + + " void getMember(){\n" + + " staticMember();\n" + + " this.variable = 43;\n" + + " };\n" + + "}" + ) + .run(AddThisToMember.class) + .assertWarnings(); + + } + }