Skip to content

Commit

Permalink
issue #22: new hint "change modifiers" - Pull Request by https://gith…
Browse files Browse the repository at this point in the history
…ub.com/rasa-silva

* updated tests
* updated doc, license headers and version
  • Loading branch information
markiewb committed Nov 25, 2013
1 parent 6b58401 commit 485a7e0
Show file tree
Hide file tree
Showing 11 changed files with 457 additions and 127 deletions.
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>de.markiewb.netbeans.plugins</groupId>
<artifactId>AdditionalHints</artifactId>
<version>1.1.0</version>
<version>1.2.0-SNAPSHOT</version>
<packaging>nbm</packaging>

<name>Additional Java hints</name>
Expand Down Expand Up @@ -218,12 +218,18 @@
&lt;li&gt;" Convert to assertTrue/assertFalse" (since 1.1)&lt;/li&gt;
&lt;li&gt;"Support transformation to BigDecimal constants" (since 1.1)&lt;/li&gt;
&lt;li&gt;"Remove "public abstract" modifiers from method declarations within interfaces" (since 1.1)&lt;/li&gt;
&lt;li&gt;"Change modifiers" (since 1.2)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Example:&lt;/h2&gt;
&lt;img src="https://raw.github.com/markiewb/nb-additional-hints/master/doc/screenshot-1.1.0.png"/&gt;

&lt;h2&gt;Updates&lt;/h2&gt;
&lt;h3&gt;1.2.0:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;[&lt;a href="https://github.com/markiewb/nb-additional-hints/pull/22"&gt;New Hint&lt;/a&gt;]: Change the modifier of a class/method/field to public/package protected/protected/private (by &lt;a href="https://github.com/rasa-silva"&gt;rasa-silva&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;1.1.0:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;[&lt;a href="https://github.com/markiewb/nb-additional-hints/issues/11"&gt;New Hint&lt;/a&gt;]: Support transformation to BigDecimal constants&lt;/li&gt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.<p>Provided by <a href=\"https://github.com/markiewb/nb-additional-hints\">nb-additional-hints</a> plugin</p>"})
public class MakePackageProtected {

private static final EnumSet<Modifier> opositeModifiers = EnumSet.of(Modifier.PRIVATE, Modifier.PUBLIC, Modifier.PROTECTED);
private static final EnumSet<Modifier> 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)
Expand All @@ -88,8 +90,10 @@ public static ErrorDescription convert(HintContext ctx) {
&& !modifiers.getFlags().contains(Modifier.PUBLIC))) {
return null;
}
final EnumSet<Modifier> toAdd = EnumSet.noneOf(Modifier.class);
final EnumSet<Modifier> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.<p>Provided by <a href=\"https://github.com/markiewb/nb-additional-hints\">nb-additional-hints</a> plugin</p>"})
public class MakePrivate {

private static final EnumSet<Modifier> opositeModifiers = EnumSet.of(Modifier.PUBLIC, Modifier.PROTECTED);
private static final EnumSet<Modifier> oppositeModifiers = EnumSet.of(Modifier.PUBLIC, Modifier.PROTECTED);

@Hint(displayName = "#DN_MakePrivate", description = "#DESC_MakePrivate", category = "suggestions",
hintKind = Hint.Kind.INSPECTION, severity = Severity.HINT)
Expand All @@ -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;
}
Expand All @@ -87,8 +93,10 @@ public static ErrorDescription convert(HintContext ctx) {
if (modifiers == null || modifiers.getFlags().contains(Modifier.PRIVATE)) {
return null;
}
final EnumSet<Modifier> toAdd = EnumSet.of(Modifier.PRIVATE);
final EnumSet<Modifier> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.<p>Provided by <a href=\"https://github.com/markiewb/nb-additional-hints\">nb-additional-hints</a> plugin</p>"})
public class MakeProtected {

private static final EnumSet<Modifier> opositeModifiers = EnumSet.of(Modifier.PRIVATE, Modifier.PUBLIC);
private static final EnumSet<Modifier> oppositeModifiers = EnumSet.of(Modifier.PRIVATE, Modifier.PUBLIC);

@Hint(displayName = "#DN_MakeProtected", description = "#DESC_MakeProtected", category = "suggestions",
hintKind = Hint.Kind.INSPECTION, severity = Severity.HINT)
Expand All @@ -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;
}
Expand All @@ -90,8 +93,10 @@ public static ErrorDescription convert(HintContext ctx) {
if (modifiers == null || modifiers.getFlags().contains(Modifier.PROTECTED)) {
return null;
}
final EnumSet<Modifier> toAdd = EnumSet.of(Modifier.PROTECTED);
final EnumSet<Modifier> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.<p>Provided by <a href=\"https://github.com/markiewb/nb-additional-hints\">nb-additional-hints</a> plugin</p>"})
public class MakePublic {

private static final EnumSet<Modifier> opositeModifiers = EnumSet.of(Modifier.PRIVATE, Modifier.PROTECTED);
private static final EnumSet<Modifier> oppositeModifiers = EnumSet.of(Modifier.PRIVATE, Modifier.PROTECTED);

@Hint(displayName = "#DN_MakePublic", description = "#DESC_MakePublic", category = "suggestions",
hintKind = Hint.Kind.INSPECTION, severity = Severity.HINT)
Expand All @@ -86,8 +88,10 @@ public static ErrorDescription convert(HintContext ctx) {
if (modifiers == null || modifiers.getFlags().contains(Modifier.PUBLIC)) {
return null;
}
final EnumSet<Modifier> toAdd = EnumSet.of(Modifier.PUBLIC);
final EnumSet<Modifier> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand Down
Loading

0 comments on commit 485a7e0

Please sign in to comment.