-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'rasa-silva-modifiers_hints'
* rasa-silva-modifiers_hints: issue #22: new hint "change modifiers" - Pull Request by https://github.com/rasa-silva Add public, protected and package protected modifiers. Use builtin FixFactory. Added tests for protected method and field. Make private hint for classes, methods and fields.
- Loading branch information
Showing
11 changed files
with
1,311 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
|
@@ -13,14 +13,12 @@ | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir> | ||
<netbeans.version>RELEASE721</netbeans.version> | ||
<netbeans.run.params.ide /> | ||
<netbeans.run.params>${netbeans.run.params.ide}</netbeans.run.params> | ||
</properties> | ||
<organization> | ||
<name>Benno Markiewicz ([email protected])</name> | ||
</organization> | ||
<url>https://github.com/markiewb/nb-additional-hints</url> | ||
|
||
<repositories> | ||
<!-- | ||
Repository hosting NetBeans modules, especially APIs. | ||
|
@@ -120,7 +118,7 @@ | |
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>nbm-maven-plugin</artifactId> | ||
<version>3.11</version> | ||
<version>3.11.1</version> | ||
<extensions>true</extensions> | ||
<configuration> | ||
<!-- keep this id, else the update from existing versions will fail | ||
|
@@ -220,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> | ||
|
99 changes: 99 additions & 0 deletions
99
src/main/java/de/markiewb/netbeans/plugins/hints/modifiers/MakePackageProtected.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* 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.ModifiersTree; | ||
import com.sun.source.tree.Tree; | ||
import com.sun.source.util.TreePath; | ||
import java.util.EnumSet; | ||
import javax.lang.model.element.Element; | ||
import javax.lang.model.element.Modifier; | ||
import org.netbeans.spi.editor.hints.ErrorDescription; | ||
import org.netbeans.spi.editor.hints.Fix; | ||
import org.netbeans.spi.editor.hints.Severity; | ||
import org.netbeans.spi.java.hints.ErrorDescriptionFactory; | ||
import org.netbeans.spi.java.hints.Hint; | ||
import org.netbeans.spi.java.hints.HintContext; | ||
import org.netbeans.spi.java.hints.TriggerTreeKind; | ||
import org.netbeans.spi.java.hints.support.FixFactory; | ||
import org.openide.util.NbBundle; | ||
|
||
/** | ||
* 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.<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> 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) | ||
@TriggerTreeKind({Tree.Kind.CLASS, Tree.Kind.METHOD, Tree.Kind.VARIABLE}) | ||
public static ErrorDescription convert(HintContext ctx) { | ||
|
||
TreePath path = ctx.getPath(); | ||
Element element = ctx.getInfo().getTrees().getElement(path); | ||
|
||
if (element == null) { | ||
return null; | ||
} | ||
|
||
ModifiersTree modifiers = ModifierUtils.getModifiersTree(path, element); | ||
|
||
if (modifiers == null || (!modifiers.getFlags().contains(Modifier.PROTECTED) | ||
&& !modifiers.getFlags().contains(Modifier.PRIVATE) | ||
&& !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), toAdd, toRemove, Bundle.ERR_MakePackageProtected()); | ||
return ErrorDescriptionFactory.forName(ctx, path, Bundle.ERR_MakePackageProtected(), fix); | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
src/main/java/de/markiewb/netbeans/plugins/hints/modifiers/MakePrivate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* 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.ModifiersTree; | ||
import com.sun.source.tree.Tree; | ||
import com.sun.source.util.TreePath; | ||
import java.util.EnumSet; | ||
import javax.lang.model.element.Element; | ||
import javax.lang.model.element.Modifier; | ||
import org.netbeans.spi.editor.hints.ErrorDescription; | ||
import org.netbeans.spi.editor.hints.Fix; | ||
import org.netbeans.spi.editor.hints.Severity; | ||
import org.netbeans.spi.java.hints.ErrorDescriptionFactory; | ||
import org.netbeans.spi.java.hints.Hint; | ||
import org.netbeans.spi.java.hints.HintContext; | ||
import org.netbeans.spi.java.hints.TriggerTreeKind; | ||
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.<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> oppositeModifiers = EnumSet.of(Modifier.PUBLIC, Modifier.PROTECTED); | ||
|
||
@Hint(displayName = "#DN_MakePrivate", description = "#DESC_MakePrivate", category = "suggestions", | ||
hintKind = Hint.Kind.INSPECTION, severity = Severity.HINT) | ||
@TriggerTreeKind({Tree.Kind.CLASS, Tree.Kind.METHOD, Tree.Kind.VARIABLE}) | ||
public static ErrorDescription convert(HintContext ctx) { | ||
|
||
TreePath path = ctx.getPath(); | ||
Element element = ctx.getInfo().getTrees().getElement(path); | ||
|
||
if (element == null) { | ||
return null; | ||
} | ||
|
||
//XXX top level classes cannot be made private | ||
if (ModifierUtils.isTopLevelClass(element)) { | ||
return null; | ||
} | ||
|
||
ModifiersTree modifiers = ModifierUtils.getModifiersTree(path, element); | ||
|
||
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), toAdd, toRemove, Bundle.ERR_MakePrivate()); | ||
return ErrorDescriptionFactory.forName(ctx, path, Bundle.ERR_MakePrivate(), fix); | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
src/main/java/de/markiewb/netbeans/plugins/hints/modifiers/MakeProtected.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* 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.ModifiersTree; | ||
import com.sun.source.tree.Tree; | ||
import com.sun.source.util.TreePath; | ||
import java.util.EnumSet; | ||
import javax.lang.model.element.Element; | ||
import javax.lang.model.element.Modifier; | ||
import org.netbeans.spi.editor.hints.ErrorDescription; | ||
import org.netbeans.spi.editor.hints.Fix; | ||
import org.netbeans.spi.editor.hints.Severity; | ||
import org.netbeans.spi.java.hints.ErrorDescriptionFactory; | ||
import org.netbeans.spi.java.hints.Hint; | ||
import org.netbeans.spi.java.hints.HintContext; | ||
import org.netbeans.spi.java.hints.TriggerTreeKind; | ||
import org.netbeans.spi.java.hints.support.FixFactory; | ||
import org.openide.util.NbBundle; | ||
|
||
/** | ||
* 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.<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> oppositeModifiers = EnumSet.of(Modifier.PRIVATE, Modifier.PUBLIC); | ||
|
||
@Hint(displayName = "#DN_MakeProtected", description = "#DESC_MakeProtected", category = "suggestions", | ||
hintKind = Hint.Kind.INSPECTION, severity = Severity.HINT) | ||
@TriggerTreeKind({Tree.Kind.CLASS, Tree.Kind.METHOD, Tree.Kind.VARIABLE}) | ||
public static ErrorDescription convert(HintContext ctx) { | ||
|
||
TreePath path = ctx.getPath(); | ||
Element element = ctx.getInfo().getTrees().getElement(path); | ||
|
||
if (element == null) { | ||
return null; | ||
} | ||
|
||
//XXX top level classes cannot be made protected | ||
if (ModifierUtils.isTopLevelClass(element)) { | ||
return null; | ||
} | ||
|
||
ModifiersTree modifiers = ModifierUtils.getModifiersTree(path, element); | ||
|
||
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), toAdd, toRemove, Bundle.ERR_MakeProtected()); | ||
return ErrorDescriptionFactory.forName(ctx, path, Bundle.ERR_MakeProtected(), fix); | ||
} | ||
} |
Oops, something went wrong.