Skip to content

Commit

Permalink
Merge branch 'rasa-silva-modifiers_hints'
Browse files Browse the repository at this point in the history
* 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
markiewb committed Nov 25, 2013
2 parents 8f3715e + 485a7e0 commit 83938be
Show file tree
Hide file tree
Showing 11 changed files with 1,311 additions and 5 deletions.
14 changes: 9 additions & 5 deletions 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 All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -220,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
@@ -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);
}
}
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);
}
}
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);
}
}
Loading

0 comments on commit 83938be

Please sign in to comment.