From 485a7e0981bb0decfca6ff968ea70c098978a25c Mon Sep 17 00:00:00 2001 From: markiewb Date: Tue, 26 Nov 2013 00:08:08 +0100 Subject: [PATCH] issue #22: new hint "change modifiers" - Pull Request by https://github.com/rasa-silva * updated tests * updated doc, license headers and version --- pom.xml | 8 +- .../hints/modifiers/MakePackageProtected.java | 10 +- .../plugins/hints/modifiers/MakePrivate.java | 14 +- .../hints/modifiers/MakeProtected.java | 11 +- .../plugins/hints/modifiers/MakePublic.java | 10 +- .../hints/modifiers/ModifierUtils.java | 47 +++++++ .../modifiers/MakePackageProtectedTest.java | 111 +++++++++------- .../hints/modifiers/MakePrivateTest.java | 123 +++++++++++++++--- .../hints/modifiers/MakeProtectedTest.java | 106 ++++++++++----- .../hints/modifiers/MakePublicTest.java | 123 +++++++++++++++--- src/test/java/example/ChangeModifier.java | 21 +++ 11 files changed, 457 insertions(+), 127 deletions(-) create mode 100644 src/test/java/example/ChangeModifier.java diff --git a/pom.xml b/pom.xml index 24b337f..3b890b7 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ de.markiewb.netbeans.plugins AdditionalHints - 1.1.0 + 1.2.0-SNAPSHOT nbm Additional Java hints @@ -218,12 +218,18 @@ <li>" Convert to assertTrue/assertFalse" (since 1.1)</li> <li>"Support transformation to BigDecimal constants" (since 1.1)</li> <li>"Remove "public abstract" modifiers from method declarations within interfaces" (since 1.1)</li> +<li>"Change modifiers" (since 1.2)</li> </ul> <h2>Example:</h2> <img src="https://raw.github.com/markiewb/nb-additional-hints/master/doc/screenshot-1.1.0.png"/> <h2>Updates</h2> +<h3>1.2.0:</h3> +<ul> +<li>[<a href="https://github.com/markiewb/nb-additional-hints/pull/22">New Hint</a>]: Change the modifier of a class/method/field to public/package protected/protected/private (by <a href="https://github.com/rasa-silva">rasa-silva</a>)</li> +</ul> + <h3>1.1.0:</h3> <ul> <li>[<a href="https://github.com/markiewb/nb-additional-hints/issues/11">New Hint</a>]: Support transformation to BigDecimal constants</li> diff --git a/src/main/java/de/markiewb/netbeans/plugins/hints/modifiers/MakePackageProtected.java b/src/main/java/de/markiewb/netbeans/plugins/hints/modifiers/MakePackageProtected.java index 8f8c788..24d0582 100644 --- a/src/main/java/de/markiewb/netbeans/plugins/hints/modifiers/MakePackageProtected.java +++ b/src/main/java/de/markiewb/netbeans/plugins/hints/modifiers/MakePackageProtected.java @@ -38,6 +38,7 @@ * Version 2 license, then the option applies only if the new code is * made subject to such option by the copyright holder. * + * "Portions Copyrighted 2013 rasa-silva" * "Portions Copyrighted 2013 Benno Markiewicz" */ package de.markiewb.netbeans.plugins.hints.modifiers; @@ -60,14 +61,15 @@ /** * Turns a class, method of field public. + * @author https://github.com/rasa-silva */ @NbBundle.Messages({ "ERR_MakePackageProtected=Make Package Protected", "DN_MakePackageProtected=Make Package Protected", - "DESC_MakePackageProtected=Makes a class, method or field package protected."}) + "DESC_MakePackageProtected=Makes a class, method or field package protected.

Provided by nb-additional-hints plugin

"}) public class MakePackageProtected { - private static final EnumSet opositeModifiers = EnumSet.of(Modifier.PRIVATE, Modifier.PUBLIC, Modifier.PROTECTED); + private static final EnumSet oppositeModifiers = EnumSet.of(Modifier.PRIVATE, Modifier.PUBLIC, Modifier.PROTECTED); @Hint(displayName = "#DN_MakePackageProtected", description = "#DESC_MakePackageProtected", category = "suggestions", hintKind = Hint.Kind.INSPECTION, severity = Severity.HINT) @@ -88,8 +90,10 @@ public static ErrorDescription convert(HintContext ctx) { && !modifiers.getFlags().contains(Modifier.PUBLIC))) { return null; } + final EnumSet toAdd = EnumSet.noneOf(Modifier.class); + final EnumSet toRemove = oppositeModifiers; - Fix fix = FixFactory.changeModifiersFix(ctx.getInfo(), new TreePath(path, modifiers), EnumSet.of(Modifier.PROTECTED), opositeModifiers, Bundle.ERR_MakePackageProtected()); + Fix fix = FixFactory.changeModifiersFix(ctx.getInfo(), new TreePath(path, modifiers), toAdd, toRemove, Bundle.ERR_MakePackageProtected()); return ErrorDescriptionFactory.forName(ctx, path, Bundle.ERR_MakePackageProtected(), fix); } } diff --git a/src/main/java/de/markiewb/netbeans/plugins/hints/modifiers/MakePrivate.java b/src/main/java/de/markiewb/netbeans/plugins/hints/modifiers/MakePrivate.java index 276e7e0..21d9fd3 100644 --- a/src/main/java/de/markiewb/netbeans/plugins/hints/modifiers/MakePrivate.java +++ b/src/main/java/de/markiewb/netbeans/plugins/hints/modifiers/MakePrivate.java @@ -38,6 +38,7 @@ * Version 2 license, then the option applies only if the new code is * made subject to such option by the copyright holder. * + * "Portions Copyrighted 2013 rasa-silva" * "Portions Copyrighted 2013 Benno Markiewicz" */ package de.markiewb.netbeans.plugins.hints.modifiers; @@ -58,13 +59,17 @@ import org.netbeans.spi.java.hints.support.FixFactory; import org.openide.util.NbBundle; +/** + * + * @author https://github.com/rasa-silva + */ @NbBundle.Messages({ "ERR_MakePrivate=Make Private", "DN_MakePrivate=Make Private", - "DESC_MakePrivate=Makes a class, method or field private."}) + "DESC_MakePrivate=Makes a class, method or field private.

Provided by nb-additional-hints plugin

"}) public class MakePrivate { - private static final EnumSet opositeModifiers = EnumSet.of(Modifier.PUBLIC, Modifier.PROTECTED); + private static final EnumSet oppositeModifiers = EnumSet.of(Modifier.PUBLIC, Modifier.PROTECTED); @Hint(displayName = "#DN_MakePrivate", description = "#DESC_MakePrivate", category = "suggestions", hintKind = Hint.Kind.INSPECTION, severity = Severity.HINT) @@ -78,6 +83,7 @@ public static ErrorDescription convert(HintContext ctx) { return null; } + //XXX top level classes cannot be made private if (ModifierUtils.isTopLevelClass(element)) { return null; } @@ -87,8 +93,10 @@ public static ErrorDescription convert(HintContext ctx) { if (modifiers == null || modifiers.getFlags().contains(Modifier.PRIVATE)) { return null; } + final EnumSet toAdd = EnumSet.of(Modifier.PRIVATE); + final EnumSet toRemove = oppositeModifiers; - Fix fix = FixFactory.changeModifiersFix(ctx.getInfo(), new TreePath(path, modifiers), EnumSet.of(Modifier.PRIVATE), opositeModifiers, Bundle.ERR_MakePrivate()); + Fix fix = FixFactory.changeModifiersFix(ctx.getInfo(), new TreePath(path, modifiers), toAdd, toRemove, Bundle.ERR_MakePrivate()); return ErrorDescriptionFactory.forName(ctx, path, Bundle.ERR_MakePrivate(), fix); } } diff --git a/src/main/java/de/markiewb/netbeans/plugins/hints/modifiers/MakeProtected.java b/src/main/java/de/markiewb/netbeans/plugins/hints/modifiers/MakeProtected.java index 0e4cbc1..b717add 100644 --- a/src/main/java/de/markiewb/netbeans/plugins/hints/modifiers/MakeProtected.java +++ b/src/main/java/de/markiewb/netbeans/plugins/hints/modifiers/MakeProtected.java @@ -38,6 +38,7 @@ * Version 2 license, then the option applies only if the new code is * made subject to such option by the copyright holder. * + * "Portions Copyrighted 2013 rasa-silva" * "Portions Copyrighted 2013 Benno Markiewicz" */ package de.markiewb.netbeans.plugins.hints.modifiers; @@ -60,14 +61,15 @@ /** * Turns a class, method of field public. + * @author https://github.com/rasa-silva */ @NbBundle.Messages({ "ERR_MakeProtected=Make Protected", "DN_MakeProtected=Make Protected", - "DESC_MakeProtected=Makes a class, method or field protected."}) + "DESC_MakeProtected=Makes a class, method or field protected.

Provided by nb-additional-hints plugin

"}) public class MakeProtected { - private static final EnumSet opositeModifiers = EnumSet.of(Modifier.PRIVATE, Modifier.PUBLIC); + private static final EnumSet oppositeModifiers = EnumSet.of(Modifier.PRIVATE, Modifier.PUBLIC); @Hint(displayName = "#DN_MakeProtected", description = "#DESC_MakeProtected", category = "suggestions", hintKind = Hint.Kind.INSPECTION, severity = Severity.HINT) @@ -81,6 +83,7 @@ public static ErrorDescription convert(HintContext ctx) { return null; } + //XXX top level classes cannot be made protected if (ModifierUtils.isTopLevelClass(element)) { return null; } @@ -90,8 +93,10 @@ public static ErrorDescription convert(HintContext ctx) { if (modifiers == null || modifiers.getFlags().contains(Modifier.PROTECTED)) { return null; } + final EnumSet toAdd = EnumSet.of(Modifier.PROTECTED); + final EnumSet toRemove = oppositeModifiers; - Fix fix = FixFactory.changeModifiersFix(ctx.getInfo(), new TreePath(path, modifiers), EnumSet.of(Modifier.PROTECTED), opositeModifiers, Bundle.ERR_MakeProtected()); + Fix fix = FixFactory.changeModifiersFix(ctx.getInfo(), new TreePath(path, modifiers), toAdd, toRemove, Bundle.ERR_MakeProtected()); return ErrorDescriptionFactory.forName(ctx, path, Bundle.ERR_MakeProtected(), fix); } } diff --git a/src/main/java/de/markiewb/netbeans/plugins/hints/modifiers/MakePublic.java b/src/main/java/de/markiewb/netbeans/plugins/hints/modifiers/MakePublic.java index 8bb57cb..8fb8a6e 100644 --- a/src/main/java/de/markiewb/netbeans/plugins/hints/modifiers/MakePublic.java +++ b/src/main/java/de/markiewb/netbeans/plugins/hints/modifiers/MakePublic.java @@ -38,6 +38,7 @@ * Version 2 license, then the option applies only if the new code is * made subject to such option by the copyright holder. * + * "Portions Copyrighted 2013 rasa-silva" * "Portions Copyrighted 2013 Benno Markiewicz" */ package de.markiewb.netbeans.plugins.hints.modifiers; @@ -60,14 +61,15 @@ /** * Turns a class, method of field public. + * @author https://github.com/rasa-silva */ @NbBundle.Messages({ "ERR_MakePublic=Make Public", "DN_MakePublic=Make Public", - "DESC_MakePublic=Makes a class, method or field public."}) + "DESC_MakePublic=Makes a class, method or field public.

Provided by nb-additional-hints plugin

"}) public class MakePublic { - private static final EnumSet opositeModifiers = EnumSet.of(Modifier.PRIVATE, Modifier.PROTECTED); + private static final EnumSet oppositeModifiers = EnumSet.of(Modifier.PRIVATE, Modifier.PROTECTED); @Hint(displayName = "#DN_MakePublic", description = "#DESC_MakePublic", category = "suggestions", hintKind = Hint.Kind.INSPECTION, severity = Severity.HINT) @@ -86,8 +88,10 @@ public static ErrorDescription convert(HintContext ctx) { if (modifiers == null || modifiers.getFlags().contains(Modifier.PUBLIC)) { return null; } + final EnumSet toAdd = EnumSet.of(Modifier.PUBLIC); + final EnumSet toRemove = oppositeModifiers; - Fix fix = FixFactory.changeModifiersFix(ctx.getInfo(), new TreePath(path, modifiers), EnumSet.of(Modifier.PUBLIC), opositeModifiers, Bundle.ERR_MakePublic()); + Fix fix = FixFactory.changeModifiersFix(ctx.getInfo(), new TreePath(path, modifiers), toAdd, toRemove, Bundle.ERR_MakePublic()); return ErrorDescriptionFactory.forName(ctx, path, Bundle.ERR_MakePublic(), fix); } } diff --git a/src/main/java/de/markiewb/netbeans/plugins/hints/modifiers/ModifierUtils.java b/src/main/java/de/markiewb/netbeans/plugins/hints/modifiers/ModifierUtils.java index 66199fe..b50dd91 100644 --- a/src/main/java/de/markiewb/netbeans/plugins/hints/modifiers/ModifierUtils.java +++ b/src/main/java/de/markiewb/netbeans/plugins/hints/modifiers/ModifierUtils.java @@ -1,3 +1,46 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun + * Microsystems, Inc. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * "Portions Copyrighted 2013 rasa-silva" + * "Portions Copyrighted 2013 Benno Markiewicz" + */ package de.markiewb.netbeans.plugins.hints.modifiers; import com.sun.source.tree.ClassTree; @@ -11,6 +54,10 @@ import javax.lang.model.element.NestingKind; import javax.lang.model.element.TypeElement; +/** + * + * @author https://github.com/rasa-silva + */ class ModifierUtils { static boolean isTopLevelClass(Element element) { diff --git a/src/test/java/de/markiewb/netbeans/plugins/hints/modifiers/MakePackageProtectedTest.java b/src/test/java/de/markiewb/netbeans/plugins/hints/modifiers/MakePackageProtectedTest.java index a735be1..5ecda43 100644 --- a/src/test/java/de/markiewb/netbeans/plugins/hints/modifiers/MakePackageProtectedTest.java +++ b/src/test/java/de/markiewb/netbeans/plugins/hints/modifiers/MakePackageProtectedTest.java @@ -6,34 +6,35 @@ public class MakePackageProtectedTest { @Test - public void testFixWorking_Public_TopLevel_Class() throws Exception { + public void testFixWorking_Private_Field() throws Exception { HintTest.create() .input("package test;\n" - + "public class Test {}\n") + + "public class Test {\n" + + " private String s = null;\n" + + "}\n") .run(MakePackageProtected.class) - .findWarning("1:13-1:17:hint:" + Bundle.ERR_MakePackageProtected()) - .applyFix() - .assertCompilable() + .findWarning("2:19-2:20:hint:" + Bundle.ERR_MakePackageProtected()) + .applyFix().assertCompilable() .assertOutput("package test;\n" - + "class Test {}\n"); + + "public class Test {\n" + + " String s = null;\n" + + "}\n"); } @Test - public void testFixWorking_Public_Method() throws Exception { + public void testFixWorking_Private_Inner_Class() throws Exception { HintTest.create() .input("package test;\n" + "public class Test {\n" - + " public static void main(String[] args) {\n" - + " }\n" + + " private class Inner {}\n" + "}\n") .run(MakePackageProtected.class) - .findWarning("2:23-2:27:hint:" + Bundle.ERR_MakePackageProtected()) + .findWarning("2:18-2:23:hint:" + Bundle.ERR_MakePackageProtected()) .applyFix() .assertCompilable() .assertOutput("package test;\n" + "public class Test {\n" - + " static void main(String[] args) {\n" - + " }\n" + + " class Inner {}\n" + "}\n"); } @@ -56,59 +57,81 @@ public void testFixWorking_Private_Method() throws Exception { + "}\n"); } + /** + * 1:23 modifier private not allowed here + * + * @throws Exception + */ + @Test(expected = AssertionError.class) + public void testFixWorking_Private_TopLevel_Class() throws Exception { + HintTest.create() + .input("package test; private class Test {}") + .run(MakePackageProtected.class); + } + @Test - public void testFixWorking_Protected_Method() throws Exception { + public void testFixWorking_Protected_Field() throws Exception { HintTest.create() .input("package test;\n" + "public class Test {\n" - + " protected static void main(String[] args) {\n" - + " }\n" + + " protected String s = null;\n" + "}\n") .run(MakePackageProtected.class) - .findWarning("2:26-2:30:hint:" + Bundle.ERR_MakePackageProtected()) - .applyFix() - .assertCompilable() + .findWarning("2:21-2:22:hint:" + Bundle.ERR_MakePackageProtected()) + .applyFix().assertCompilable() .assertOutput("package test;\n" + "public class Test {\n" - + " static void main(String[] args) {\n" - + " }\n" + + " String s = null;\n" + "}\n"); } @Test - public void testFixWorking_Protected_Field() throws Exception { + public void testFixWorking_Protected_Inner_Class() throws Exception { HintTest.create() .input("package test;\n" + "public class Test {\n" - + " protected String s = null;\n" + + " protected class Inner {}\n" + "}\n") .run(MakePackageProtected.class) - .findWarning("2:21-2:22:hint:" + Bundle.ERR_MakePackageProtected()) - .applyFix() - .assertCompilable() - .assertOutput("package test;\n" + .findWarning("2:20-2:25:hint:" + Bundle.ERR_MakePackageProtected()) + .applyFix().assertCompilable(). + assertOutput("package test;\n" + "public class Test {\n" - + " String s = null;\n" + + " class Inner {}\n" + "}\n"); } @Test - public void testFixWorking_Private_Field() throws Exception { + public void testFixWorking_Protected_Method() throws Exception { HintTest.create() .input("package test;\n" + "public class Test {\n" - + " private String s = null;\n" + + " protected static void main(String[] args) {\n" + + " }\n" + "}\n") .run(MakePackageProtected.class) - .findWarning("2:19-2:20:hint:" + Bundle.ERR_MakePackageProtected()) + .findWarning("2:26-2:30:hint:" + Bundle.ERR_MakePackageProtected()) .applyFix() .assertCompilable() .assertOutput("package test;\n" + "public class Test {\n" - + " String s = null;\n" + + " static void main(String[] args) {\n" + + " }\n" + "}\n"); } + /** + * 1:25 modifier protected not allowed here + * + * @throws Exception + */ + @Test(expected = AssertionError.class) + public void testFixWorking_Protected_TopLevel_Class() throws Exception { + HintTest.create() + .input("package test; protected class Test {}") + .run(MakePackageProtected.class); + } + @Test public void testFixWorking_Public_Field() throws Exception { HintTest.create() @@ -127,7 +150,7 @@ public void testFixWorking_Public_Field() throws Exception { } @Test - public void testFixWorking_Public_InnerClass() throws Exception { + public void testFixWorking_Public_Inner_Class() throws Exception { HintTest.create() .input("package test;\n" + "public class Test {\n" @@ -144,36 +167,32 @@ public void testFixWorking_Public_InnerClass() throws Exception { } @Test - public void testFixWorking_Private_InnerClass() throws Exception { + public void testFixWorking_Public_Method() throws Exception { HintTest.create() .input("package test;\n" + "public class Test {\n" - + " private class Inner {}\n" + + " public static void main(String[] args) {\n" + + " }\n" + "}\n") .run(MakePackageProtected.class) - .findWarning("2:18-2:23:hint:" + Bundle.ERR_MakePackageProtected()) + .findWarning("2:23-2:27:hint:" + Bundle.ERR_MakePackageProtected()) .applyFix() .assertCompilable() .assertOutput("package test;\n" + "public class Test {\n" - + " class Inner {}\n" + + " static void main(String[] args) {\n" + + " }\n" + "}\n"); } @Test - public void testFixWorking_Protected_InnerClass() throws Exception { + public void testFixWorking_Public_TopLevel_Class() throws Exception { HintTest.create() - .input("package test;\n" - + "public class Test {\n" - + " protected class Inner {}\n" - + "}\n") + .input("package test; public class Test {}") .run(MakePackageProtected.class) - .findWarning("2:20-2:25:hint:" + Bundle.ERR_MakePackageProtected()) + .findWarning("0:27-0:31:hint:" + Bundle.ERR_MakePackageProtected()) .applyFix() .assertCompilable() - .assertOutput("package test;\n" - + "public class Test {\n" - + " class Inner {}\n" - + "}\n"); + .assertOutput("package test; class Test {}"); } } diff --git a/src/test/java/de/markiewb/netbeans/plugins/hints/modifiers/MakePrivateTest.java b/src/test/java/de/markiewb/netbeans/plugins/hints/modifiers/MakePrivateTest.java index 4b6ad2c..5bf1658 100644 --- a/src/test/java/de/markiewb/netbeans/plugins/hints/modifiers/MakePrivateTest.java +++ b/src/test/java/de/markiewb/netbeans/plugins/hints/modifiers/MakePrivateTest.java @@ -6,34 +6,49 @@ public class MakePrivateTest { @Test - public void testFixWorking_Protected_Method() throws Exception { + public void testFixWorking_PackageProtected_Field() throws Exception { HintTest.create() .input("package test;\n" + "public class Test {\n" - + " protected static void main(String[] args) {\n" - + " }\n" + + " String s = null;\n" + "}\n") .run(MakePrivate.class) - .findWarning("2:26-2:30:hint:" + Bundle.ERR_MakePrivate()) + .findWarning("2:11-2:12:hint:" + Bundle.ERR_MakePrivate()) .applyFix() .assertCompilable() .assertOutput("package test;\n" + "public class Test {\n" - + " private static void main(String[] args) {\n" - + " }\n" + + " private String s = null;\n" + "}\n"); } @Test - public void testFixWorking_Public_Method() throws Exception { + public void testFixWorking_PackageProtected_Inner_Class() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " class Inner {}\n" + + "}\n") + .run(MakePrivate.class) + .findWarning("2:10-2:15:hint:" + Bundle.ERR_MakePrivate()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "public class Test {\n" + + " private class Inner {}\n" + + "}\n"); + } + + @Test + public void testFixWorking_PackageProtected_Method() throws Exception { HintTest.create() .input("package test;\n" + "public class Test {\n" - + " public static void main(String[] args) {\n" + + " static void main(String[] args) {\n" + " }\n" + "}\n") .run(MakePrivate.class) - .findWarning("2:23-2:27:hint:" + Bundle.ERR_MakePrivate()) + .findWarning("2:16-2:20:hint:" + Bundle.ERR_MakePrivate()) .applyFix() .assertCompilable() .assertOutput("package test;\n" @@ -43,6 +58,18 @@ public void testFixWorking_Public_Method() throws Exception { + "}\n"); } + /** + * top level classes cannot be made private + * @throws Exception + */ + @Test + public void testFixWorking_PackageProtected_TopLevel_Class() throws Exception { + HintTest.create() + .input("package test; class Test {}") + .run(MakePrivate.class) + .assertNotContainsWarnings("0:20-0:24:hint:" + Bundle.ERR_MakePrivate()); + } + @Test public void testFixWorking_Protected_Field() throws Exception { HintTest.create() @@ -51,13 +78,42 @@ public void testFixWorking_Protected_Field() throws Exception { + " protected String s = null;\n" + "}\n") .run(MakePrivate.class) - .findWarning("2:21-2:22:hint:" + Bundle.ERR_MakePrivate()) - .applyFix() - .assertCompilable() - .assertOutput("package test;\n" + .findWarning("2:21-2:22:hint:" + Bundle.ERR_MakePrivate()); + } + + @Test + public void testFixWorking_Protected_Inner_Class() throws Exception { + HintTest.create() + .input("package test;\n" + "public class Test {\n" - + " private String s = null;\n" - + "}\n"); + + " protected class Inner {}\n" + + "}\n") + .run(MakePrivate.class) + .findWarning("2:20-2:25:hint:" + Bundle.ERR_MakePrivate()); + } + + @Test + public void testFixWorking_Protected_Method() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " protected static void main(String[] args) {\n" + + " }\n" + + "}\n") + .run(MakePrivate.class) + .findWarning("2:26-2:30:hint:" + Bundle.ERR_MakePrivate()); + } + + /** + * 1:25 modifier protected not allowed here + * + * @throws Exception + */ + @Test(expected = AssertionError.class) + public void testFixWorking_Protected_TopLevel_Class() throws Exception { + HintTest.create() + .input("package test; protected class Test {}") + .run(MakePrivate.class); } @Test @@ -74,11 +130,12 @@ public void testFixWorking_Public_Field() throws Exception { .assertOutput("package test;\n" + "public class Test {\n" + " private String s = null;\n" - + "}\n"); + + "}\n") + ; } @Test - public void testFixWorking_Public_InnerClass() throws Exception { + public void testFixWorking_Public_Inner_Class() throws Exception { HintTest.create() .input("package test;\n" + "public class Test {\n" @@ -93,4 +150,36 @@ public void testFixWorking_Public_InnerClass() throws Exception { + " private class Inner {}\n" + "}\n"); } + + @Test + public void testFixWorking_Public_Method() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " static void main(String[] args) {\n" + + " }\n" + + "}\n") + .run(MakePrivate.class) + .findWarning("2:16-2:20:hint:" + Bundle.ERR_MakePrivate()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "public class Test {\n" + + " private static void main(String[] args) {\n" + + " }\n" + + "}\n") + ; + } + + /** + * top level classes cannot be made private + * @throws Exception + */ + @Test + public void testFixWorking_Public_TopLevel_Class() throws Exception { + HintTest.create() + .input("package test; public class Test {}") + .run(MakePrivate.class) + .assertNotContainsWarnings("0:27-1:31:hint:" + Bundle.ERR_MakePrivate()); + } } diff --git a/src/test/java/de/markiewb/netbeans/plugins/hints/modifiers/MakeProtectedTest.java b/src/test/java/de/markiewb/netbeans/plugins/hints/modifiers/MakeProtectedTest.java index 8b9a682..d088dd5 100644 --- a/src/test/java/de/markiewb/netbeans/plugins/hints/modifiers/MakeProtectedTest.java +++ b/src/test/java/de/markiewb/netbeans/plugins/hints/modifiers/MakeProtectedTest.java @@ -6,40 +6,36 @@ public class MakeProtectedTest { @Test - public void testFixWorking_Public_Method() throws Exception { + public void testFixWorking_PackageProtected_Field() throws Exception { HintTest.create() .input("package test;\n" + "public class Test {\n" - + " public static void main(String[] args) {\n" - + " }\n" + + " String s = null;\n" + "}\n") .run(MakeProtected.class) - .findWarning("2:23-2:27:hint:" + Bundle.ERR_MakeProtected()) + .findWarning("2:11-2:12:hint:" + Bundle.ERR_MakeProtected()) .applyFix() .assertCompilable() .assertOutput("package test;\n" + "public class Test {\n" - + " protected static void main(String[] args) {\n" - + " }\n" + + " protected String s = null;\n" + "}\n"); } @Test - public void testFixWorking_Private_Method() throws Exception { + public void testFixWorking_PackageProtected_Inner_Class() throws Exception { HintTest.create() .input("package test;\n" + "public class Test {\n" - + " private static void main(String[] args) {\n" - + " }\n" + + " class Inner {}\n" + "}\n") .run(MakeProtected.class) - .findWarning("2:24-2:28:hint:" + Bundle.ERR_MakeProtected()) + .findWarning("2:10-2:15:hint:" + Bundle.ERR_MakeProtected()) .applyFix() .assertCompilable() .assertOutput("package test;\n" + "public class Test {\n" - + " protected static void main(String[] args) {\n" - + " }\n" + + " protected class Inner {}\n" + "}\n"); } @@ -62,15 +58,27 @@ public void testFixWorking_PackageProtected_Method() throws Exception { + "}\n"); } + /** + * top level classes cannot be made protected + * @throws Exception + */ @Test - public void testFixWorking_PackageProtected_Field() throws Exception { + public void testFixWorking_PackageProtected_TopLevel_Class() throws Exception { + HintTest.create() + .input("package test; class Test {}") + .run(MakeProtected.class) + .assertNotContainsWarnings("0:20-0:24:hint:" + Bundle.ERR_MakeProtected()); + } + + @Test + public void testFixWorking_Private_Field() throws Exception { HintTest.create() .input("package test;\n" + "public class Test {\n" - + " String s = null;\n" + + " private String s = null;\n" + "}\n") .run(MakeProtected.class) - .findWarning("2:11-2:12:hint:" + Bundle.ERR_MakeProtected()) + .findWarning("2:19-2:20:hint:" + Bundle.ERR_MakeProtected()) .applyFix() .assertCompilable() .assertOutput("package test;\n" @@ -80,65 +88,80 @@ public void testFixWorking_PackageProtected_Field() throws Exception { } @Test - public void testFixWorking_Private_Field() throws Exception { + public void testFixWorking_Private_Inner_Class() throws Exception { HintTest.create() .input("package test;\n" + "public class Test {\n" - + " private String s = null;\n" + + " private class Inner {}\n" + "}\n") .run(MakeProtected.class) - .findWarning("2:19-2:20:hint:" + Bundle.ERR_MakeProtected()) + .findWarning("2:18-2:23:hint:" + Bundle.ERR_MakeProtected()) .applyFix() .assertCompilable() .assertOutput("package test;\n" + "public class Test {\n" - + " protected String s = null;\n" + + " protected class Inner {}\n" + "}\n"); } @Test - public void testFixWorking_Public_Field() throws Exception { + public void testFixWorking_Private_Method() throws Exception { HintTest.create() .input("package test;\n" + "public class Test {\n" - + " public String s = null;\n" + + " private static void main(String[] args) {\n" + + " }\n" + "}\n") .run(MakeProtected.class) - .findWarning("2:18-2:19:hint:" + Bundle.ERR_MakeProtected()) + .findWarning("2:24-2:28:hint:" + Bundle.ERR_MakeProtected()) .applyFix() .assertCompilable() .assertOutput("package test;\n" + "public class Test {\n" - + " protected String s = null;\n" + + " protected static void main(String[] args) {\n" + + " }\n" + "}\n"); } + /** + * 1:23 modifier private not allowed here + * + * @throws Exception + */ + @Test(expected = AssertionError.class) + public void testFixWorking_Private_TopLevel_Class() throws Exception { + HintTest.create() + .input("package test; private class Test {}") + .run(MakeProtected.class); + } + @Test - public void testFixWorking_Public_InnerClass() throws Exception { + public void testFixWorking_Public_Field() throws Exception { HintTest.create() .input("package test;\n" + "public class Test {\n" - + " public class Inner {}\n" + + " public String s = null;\n" + "}\n") .run(MakeProtected.class) - .findWarning("2:17-2:22:hint:" + Bundle.ERR_MakeProtected()) + .findWarning("2:18-2:19:hint:" + Bundle.ERR_MakeProtected()) .applyFix() .assertCompilable() .assertOutput("package test;\n" + "public class Test {\n" - + " protected class Inner {}\n" - + "}\n"); + + " protected String s = null;\n" + + "}\n") + ; } @Test - public void testFixWorking_Private_InnerClass() throws Exception { + public void testFixWorking_Public_Inner_Class() throws Exception { HintTest.create() .input("package test;\n" + "public class Test {\n" - + " private class Inner {}\n" + + " public class Inner {}\n" + "}\n") .run(MakeProtected.class) - .findWarning("2:18-2:23:hint:" + Bundle.ERR_MakeProtected()) + .findWarning("2:17-2:22:hint:" + Bundle.ERR_MakeProtected()) .applyFix() .assertCompilable() .assertOutput("package test;\n" @@ -148,19 +171,30 @@ public void testFixWorking_Private_InnerClass() throws Exception { } @Test - public void testFixWorking_PackageProtected_InnerClass() throws Exception { + public void testFixWorking_Public_Method() throws Exception { HintTest.create() .input("package test;\n" + "public class Test {\n" - + " class Inner {}\n" + + " static void main(String[] args) {\n" + + " }\n" + "}\n") .run(MakeProtected.class) - .findWarning("2:10-2:15:hint:" + Bundle.ERR_MakeProtected()) + .findWarning("2:16-2:20:hint:" + Bundle.ERR_MakeProtected()) .applyFix() .assertCompilable() .assertOutput("package test;\n" + "public class Test {\n" - + " protected class Inner {}\n" - + "}\n"); + + " protected static void main(String[] args) {\n" + + " }\n" + + "}\n") + ; + } + + @Test + public void testFixWorking_Public_TopLevel_Class() throws Exception { + HintTest.create() + .input("package test; public class Test {}") + .run(MakeProtected.class) + .assertNotContainsWarnings("0:20-1:24:hint:" + Bundle.ERR_MakeProtected()); } } diff --git a/src/test/java/de/markiewb/netbeans/plugins/hints/modifiers/MakePublicTest.java b/src/test/java/de/markiewb/netbeans/plugins/hints/modifiers/MakePublicTest.java index 8e96ee0..da39bdd 100644 --- a/src/test/java/de/markiewb/netbeans/plugins/hints/modifiers/MakePublicTest.java +++ b/src/test/java/de/markiewb/netbeans/plugins/hints/modifiers/MakePublicTest.java @@ -6,28 +6,49 @@ public class MakePublicTest { @Test - public void testFixWorking_PackageProtected_TopLevel_Class() throws Exception { + public void testFixWorking_PackageProtected_Field() throws Exception { HintTest.create() .input("package test;\n" - + "class Test {}\n") + + "public class Test {\n" + + " String s = null;\n" + + "}\n") .run(MakePublic.class) - .findWarning("1:6-1:10:hint:" + Bundle.ERR_MakePublic()) + .findWarning("2:11-2:12:hint:" + Bundle.ERR_MakePublic()) .applyFix() .assertCompilable() .assertOutput("package test;\n" - + "public class Test {}\n"); + + "public class Test {\n" + + " public String s = null;\n" + + "}\n"); } @Test - public void testFixWorking_Protected_Method() throws Exception { + public void testFixWorking_PackageProtected_Inner_Class() throws Exception { HintTest.create() .input("package test;\n" + "public class Test {\n" - + " protected static void main(String[] args) {\n" + + " class Inner {}\n" + + "}\n") + .run(MakePublic.class) + .findWarning("2:10-2:15:hint:" + Bundle.ERR_MakePublic()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "public class Test {\n" + + " public class Inner {}\n" + + "}\n"); + } + + @Test + public void testFixWorking_PackageProtected_Method() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " static void main(String[] args) {\n" + " }\n" + "}\n") .run(MakePublic.class) - .findWarning("2:26-2:30:hint:" + Bundle.ERR_MakePublic()) + .findWarning("2:16-2:20:hint:" + Bundle.ERR_MakePublic()) .applyFix() .assertCompilable() .assertOutput("package test;\n" @@ -37,6 +58,51 @@ public void testFixWorking_Protected_Method() throws Exception { + "}\n"); } + @Test + public void testFixWorking_PackageProtected_TopLevel_Class() throws Exception { + HintTest.create() + .input("package test; class Test {}") + .run(MakePublic.class) + .findWarning("0:20-0:24:hint:" + Bundle.ERR_MakePublic()) + .applyFix() + .assertCompilable() + .assertOutput("package test; public class Test {}"); + } + + @Test + public void testFixWorking_Private_Field() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " private String s = null;\n" + + "}\n") + .run(MakePublic.class) + .findWarning("2:19-2:20:hint:" + Bundle.ERR_MakePublic()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "public class Test {\n" + + " public String s = null;\n" + + "}\n"); + } + + @Test + public void testFixWorking_Private_Inner_Class() throws Exception { + HintTest.create() + .input("package test;\n" + + "public class Test {\n" + + " private class Inner {}\n" + + "}\n") + .run(MakePublic.class) + .findWarning("2:18-2:23:hint:" + Bundle.ERR_MakePublic()) + .applyFix() + .assertCompilable() + .assertOutput("package test;\n" + + "public class Test {\n" + + " public class Inner {}\n" + + "}\n"); + } + @Test public void testFixWorking_Private_Method() throws Exception { HintTest.create() @@ -56,6 +122,18 @@ public void testFixWorking_Private_Method() throws Exception { + "}\n"); } + /** + * 1:23 modifier private not allowed here + * + * @throws Exception + */ + @Test(expected = AssertionError.class) + public void testFixWorking_Private_TopLevel_Class() throws Exception { + HintTest.create() + .input("package test; private class Test {}") + .run(MakePublic.class); + } + @Test public void testFixWorking_Protected_Field() throws Exception { HintTest.create() @@ -74,36 +152,51 @@ public void testFixWorking_Protected_Field() throws Exception { } @Test - public void testFixWorking_Private_Field() throws Exception { + public void testFixWorking_Protected_Inner_Class() throws Exception { HintTest.create() .input("package test;\n" + "public class Test {\n" - + " private String s = null;\n" + + " protected class Inner {}\n" + "}\n") .run(MakePublic.class) - .findWarning("2:19-2:20:hint:" + Bundle.ERR_MakePublic()) + .findWarning("2:20-2:25:hint:" + Bundle.ERR_MakePublic()) .applyFix() .assertCompilable() .assertOutput("package test;\n" + "public class Test {\n" - + " public String s = null;\n" + + " public class Inner {}\n" + "}\n"); } @Test - public void testFixWorking_Public_InnerClass() throws Exception { + public void testFixWorking_Protected_Method() throws Exception { HintTest.create() .input("package test;\n" + "public class Test {\n" - + " private class Inner {}\n" + + " protected static void main(String[] args) {\n" + + " }\n" + "}\n") .run(MakePublic.class) - .findWarning("2:18-2:23:hint:" + Bundle.ERR_MakePublic()) + .findWarning("2:26-2:30:hint:" + Bundle.ERR_MakePublic()) .applyFix() .assertCompilable() .assertOutput("package test;\n" + "public class Test {\n" - + " public class Inner {}\n" + + " public static void main(String[] args) {\n" + + " }\n" + "}\n"); } + + /** + * 1:25 modifier protected not allowed here + * + * @throws Exception + */ + @Test(expected = AssertionError.class) + public void testFixWorking_Protected_TopLevel_Class() throws Exception { + HintTest.create() + .input("package test; protected class Test {}") + .run(MakePublic.class); + } + } diff --git a/src/test/java/example/ChangeModifier.java b/src/test/java/example/ChangeModifier.java new file mode 100644 index 0000000..9393023 --- /dev/null +++ b/src/test/java/example/ChangeModifier.java @@ -0,0 +1,21 @@ +package example; + +public class ChangeModifier { + + public String s = null; + + public void mainPublic() { + } + + void mainDefault() { + } + + protected void mainProtected() { + } + + private void mainPrivate() { + } + + public class InnerClass { + } +}