# This patch file was generated by NetBeans IDE # Following Index: paths are relative to: D:\ws\main-silver # This patch can be applied using context Tools: Patch action on respective folder. # It uses platform neutral UTF-8 encoding and \n newlines. # Above lines and this line are ignored by the patching process. Index: java.hints/src/org/netbeans/modules/java/hints/Bundle.properties --- java.hints/src/org/netbeans/modules/java/hints/Bundle.properties +++ java.hints/src/org/netbeans/modules/java/hints/Bundle.properties @@ -303,9 +303,9 @@ FieldForUnusedParamCustomizer.finalFields.text=Fields are final ACSD_Final_Fields=Make fields created by this hint final. -DSC_StaticImport=Convert method to static import. Feedback to http://www.netbeans.org/issues/show_bug.cgi?id=89258 +DSC_StaticImport=Convert a static method/field/enum-field reference to use a static import. Feedback to http://www.netbeans.org/issues/show_bug.cgi?id=89258 DN_StaticImport=Static imports -ERR_StaticImport=Convert method to static import \ No newline at end of file +ERR_StaticImport=Convert to static import HINT_StaticImport=Convert {0} to static import HINT_StaticImport2=Add static import for {0} Index: java.hints/src/org/netbeans/modules/java/hints/StaticImport.java --- java.hints/src/org/netbeans/modules/java/hints/StaticImport.java +++ java.hints/src/org/netbeans/modules/java/hints/StaticImport.java @@ -27,6 +27,7 @@ * Contributor(s): * * Portions Copyrighted 2009 Sun Microsystems, Inc. + * Portions Copyrighted 2014 markiewb */ package org.netbeans.modules.java.hints; @@ -38,6 +39,7 @@ import com.sun.source.tree.Tree.Kind; import com.sun.source.util.TreePath; import java.util.Collections; +import java.util.EnumSet; import java.util.List; import java.util.Set; import javax.lang.model.SourceVersion; @@ -67,14 +69,30 @@ import org.netbeans.spi.java.hints.TriggerTreeKind; /** - * Hint offering to convert a qualified static method into a static import. e.g. - * Math.abs(-1) -> abs(-1). + * Hint offering to convert code to use static imports. *

+ * Supported are + *

+ *

* Future versions might support other member types. * * @author Sam Halliday - * @see RFE 89258 - * @see RFE + * 89258 + * @see run(HintContext ctx) { CompilationInfo info = ctx.getInfo(); + if (!supportsStaticImports(info)) { + return null; + } TreePath treePath = ctx.getPath(); - TreePath mitp = treePath.getParentPath(); - if (mitp == null || mitp.getLeaf().getKind() != Kind.METHOD_INVOCATION || ((MethodInvocationTree)mitp.getLeaf()).getMethodSelect() != treePath.getLeaf()) { + + Element e = info.getTrees().getElement(treePath); + EnumSet supportedTypes = EnumSet.of(ElementKind.METHOD, ElementKind.ENUM_CONSTANT, ElementKind.FIELD); + if (e == null || !e.getModifiers().contains(Modifier.STATIC) || !supportedTypes.contains(e.getKind())) { return null; } - List typeArgs = ((MethodInvocationTree) mitp.getLeaf()).getTypeArguments(); - if (typeArgs != null && !typeArgs.isEmpty()) { + + if (ElementKind.METHOD.equals(e.getKind())) { + TreePath mitp = treePath.getParentPath(); + if (mitp == null || mitp.getLeaf().getKind() != Kind.METHOD_INVOCATION) { return null; } - Element e = info.getTrees().getElement(treePath); - if (e == null || !e.getModifiers().contains(Modifier.STATIC) || e.getKind() != ElementKind.METHOD) { + if (((MethodInvocationTree) mitp.getLeaf()).getMethodSelect() != treePath.getLeaf()) { return null; } - if (!supportsStaticImports(info)) { + List typeArgs = ((MethodInvocationTree) mitp.getLeaf()).getTypeArguments(); + if (typeArgs != null && !typeArgs.isEmpty()) { return null; } + } Element enclosingEl = e.getEnclosingElement(); if (enclosingEl == null) { return null; @@ -112,7 +138,7 @@ return null; } String fqn = null; - String fqn1 = getMethodFqn(e); + String fqn1 = getFqn(e); if (!isSubTypeOrInnerOfSubType(info, klass, enclosingEl) && !isStaticallyImported(info, fqn1)) { if (hasMethodNameClash(info, klass, sn) || hasStaticImportSimpleNameClash(info, sn)) { return null; @@ -161,7 +187,7 @@ WorkingCopy copy = ctx.getWorkingCopy(); TreePath treePath = ctx.getPath(); TreePath mitp = treePath.getParentPath(); - if (mitp == null || mitp.getLeaf().getKind() != Kind.METHOD_INVOCATION) { + if (mitp == null) { return; } Element e = copy.getTrees().getElement(treePath); @@ -280,9 +306,8 @@ * @param e * @return the FQN for a METHOD Element */ - private static String getMethodFqn(Element e) { + private static String getFqn(Element e) { // XXX or alternatively, upgrade getElementName to handle METHOD - assert e.getKind() == ElementKind.METHOD; return getElementName(e.getEnclosingElement(), true) + "." + e.getSimpleName(); } Index: java.hints/test/unit/src/org/netbeans/modules/java/hints/StaticImportTest.java --- java.hints/test/unit/src/org/netbeans/modules/java/hints/StaticImportTest.java +++ java.hints/test/unit/src/org/netbeans/modules/java/hints/StaticImportTest.java @@ -23,6 +23,7 @@ * License Header, with the fields enclosed by brackets [] replaced by * your own identifying information: * "Portions Copyrighted [year] [name of copyright owner]" + * Portions Copyrighted 2014 markiewb */ package org.netbeans.modules.java.hints; @@ -47,6 +48,58 @@ super(name); } + public void testStaticImportHint_ForEnumFields() throws Exception { + String test = "package test; import java.util.concurrent.TimeUnit; public class Test { public Test() { System.out.println(TimeUnit.D|AYS); } }"; + String golden = "package test; import java.util.concurrent.TimeUnit; import static java.util.concurrent.TimeUnit.DAYS; public class Test { public Test() { System.out.println(DAYS); } }"; + HintTest.create() + .setCaretMarker('|') + .input(test) + .run(StaticImport.class) + .findWarning("0:107-0:120:hint:" + NbBundle.getMessage(StaticImport.class, "ERR_StaticImport")) + .applyFix() + .assertCompilable() + .assertOutput(golden); + } + + public void testStaticImportHint_ForEnumFields_InAssignment() throws Exception { + String test = "package test; import java.util.concurrent.TimeUnit; public class Test { public Test() { TimeUnit foo = TimeUnit.D|AYS; } }"; + String golden = "package test; import java.util.concurrent.TimeUnit; import static java.util.concurrent.TimeUnit.DAYS; public class Test { public Test() { TimeUnit foo = DAYS; } }"; + HintTest.create() + .setCaretMarker('|') + .input(test) + .run(StaticImport.class) + .findWarning("0:103-0:116:hint:" + NbBundle.getMessage(StaticImport.class, "ERR_StaticImport")) + .applyFix() + .assertCompilable() + .assertOutput(golden); + } + + public void testStaticImportHint_ForFields() throws Exception { + String test = "package test; import java.util.Calendar; public class Test { public Test() { System.out.println(Calendar.JAN|UARY); } }"; + String golden = "package test; import java.util.Calendar; import static java.util.Calendar.JANUARY; public class Test { public Test() { System.out.println(JANUARY); } }"; + HintTest.create() + .setCaretMarker('|') + .input(test) + .run(StaticImport.class) + .findWarning("0:96-0:112:hint:" + NbBundle.getMessage(StaticImport.class, "ERR_StaticImport")) + .applyFix() + .assertCompilable() + .assertOutput(golden); + } + + public void testStaticImportHint_ForFields_InAssignment() throws Exception { + String test = "package test; import java.util.Calendar; public class Test { public Test() { int foo = Calendar.JAN|UARY; } }"; + String golden = "package test; import java.util.Calendar; import static java.util.Calendar.JANUARY; public class Test { public Test() { int foo = JANUARY; } }"; + HintTest.create() + .setCaretMarker('|') + .input(test) + .run(StaticImport.class) + .findWarning("0:87-0:103:hint:" + NbBundle.getMessage(StaticImport.class, "ERR_StaticImport")) + .applyFix() + .assertCompilable() + .assertOutput(golden); + } + \ No newline at end of file public void testStaticImportHint1() throws Exception { String test = "package test; public class Test { public Test() { Math.|abs(1); } }"; String golden = "package test; import static java.lang.Math.abs; public class Test { public Test() { abs(1); } }";