This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 89258
Collapse All | Expand All

(-)java.hints/src/org/netbeans/modules/java/hints/Bundle.properties (-2 / +2 lines)
Lines 303-311 Link Here
303
FieldForUnusedParamCustomizer.finalFields.text=<html>Fields are <code>final</code></html>
303
FieldForUnusedParamCustomizer.finalFields.text=<html>Fields are <code>final</code></html>
304
ACSD_Final_Fields=Make fields created by this hint final.
304
ACSD_Final_Fields=Make fields created by this hint final.
305
305
306
DSC_StaticImport=Convert method to static import. Feedback to http://www.netbeans.org/issues/show_bug.cgi?id=89258
306
DSC_StaticImport=Convert a static method/field/enum-field reference to use a static import. Feedback to <a href="http://www.netbeans.org/issues/show_bug.cgi?id=89258">http://www.netbeans.org/issues/show_bug.cgi?id=89258</a>
307
DN_StaticImport=Static imports
307
DN_StaticImport=Static imports
308
ERR_StaticImport=Convert method to static import
308
ERR_StaticImport=Convert to static import
309
HINT_StaticImport=Convert {0} to static import
309
HINT_StaticImport=Convert {0} to static import
310
HINT_StaticImport2=Add static import for {0}
310
HINT_StaticImport2=Add static import for {0}
311
311
(-)java.hints/src/org/netbeans/modules/java/hints/StaticImport.java (-15 / +40 lines)
Lines 27-32 Link Here
27
 * Contributor(s):
27
 * Contributor(s):
28
 *
28
 *
29
 * Portions Copyrighted 2009 Sun Microsystems, Inc.
29
 * Portions Copyrighted 2009 Sun Microsystems, Inc.
30
 * Portions Copyrighted 2014 markiewb
30
 */
31
 */
31
package org.netbeans.modules.java.hints;
32
package org.netbeans.modules.java.hints;
32
33
Lines 38-43 Link Here
38
import com.sun.source.tree.Tree.Kind;
39
import com.sun.source.tree.Tree.Kind;
39
import com.sun.source.util.TreePath;
40
import com.sun.source.util.TreePath;
40
import java.util.Collections;
41
import java.util.Collections;
42
import java.util.EnumSet;
41
import java.util.List;
43
import java.util.List;
42
import java.util.Set;
44
import java.util.Set;
43
import javax.lang.model.SourceVersion;
45
import javax.lang.model.SourceVersion;
Lines 67-80 Link Here
67
import org.netbeans.spi.java.hints.TriggerTreeKind;
69
import org.netbeans.spi.java.hints.TriggerTreeKind;
68
70
69
/**
71
/**
70
 * Hint offering to convert a qualified static method into a static import. e.g.
72
 * Hint offering to convert code to use static imports.
71
 * <code>Math.abs(-1)</code> -> <code>abs(-1)</code>.
72
 * <p>
73
 * <p>
74
 * Supported are
75
 * <ul>
76
 * <li>a qualified static method is tranformed into a static import. e.g.
77
 * <code>Math.abs(-1)</code> -> <code>abs(-1)</code>.
78
 * </li>
79
 * <li>a qualified static field is tranformed into a static import. e.g.
80
 * <code>java.util.Calendar.JANUARY</code> -> <code>JANUARY</code>.
81
 * </li>
82
 * <li>a qualified static enum field is tranformed into a static import. e.g.
83
 * <code>java.util.concurrent.TimeUnit.DAYS</code> -> <code>DAYS</code>.
84
 * </li>
85
 * </ul>
86
 * </p>
73
 * Future versions might support other member types.
87
 * Future versions might support other member types.
74
 *
88
 *
75
 * @author Sam Halliday
89
 * @author Sam Halliday
76
 * @see <a href="http://www.netbeans.org/issues/show_bug.cgi?id=89258">RFE 89258</a>
90
 * @author markiewb
77
 * @see <a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/static-import.html>Static Imports</a>
91
 * @see <a href="http://www.netbeans.org/issues/show_bug.cgi?id=89258">RFE
92
 * 89258</a>
93
 * @see <a
94
 * href="http://java.sun.com/j2se/1.5.0/docs/guide/language/static-import.html>Static
95
 * Imports</a>
78
 */
96
 */
79
@Hint(category="rules15", displayName="#DN_StaticImport", description="#DSC_StaticImport", severity=Severity.HINT, enabled=false, suppressWarnings={"", "StaticImport"})
97
@Hint(category="rules15", displayName="#DN_StaticImport", description="#DSC_StaticImport", severity=Severity.HINT, enabled=false, suppressWarnings={"", "StaticImport"})
80
public class StaticImport {
98
public class StaticImport {
Lines 82-103 Link Here
82
    @TriggerTreeKind(Kind.MEMBER_SELECT)
100
    @TriggerTreeKind(Kind.MEMBER_SELECT)
83
    public static List<ErrorDescription> run(HintContext ctx) {
101
    public static List<ErrorDescription> run(HintContext ctx) {
84
        CompilationInfo info = ctx.getInfo();
102
        CompilationInfo info = ctx.getInfo();
103
        if (!supportsStaticImports(info)) {
104
            return null;
105
        }
85
        TreePath treePath = ctx.getPath();
106
        TreePath treePath = ctx.getPath();
86
        TreePath mitp = treePath.getParentPath();
107
87
        if (mitp == null || mitp.getLeaf().getKind() != Kind.METHOD_INVOCATION || ((MethodInvocationTree)mitp.getLeaf()).getMethodSelect() != treePath.getLeaf()) {
108
        Element e = info.getTrees().getElement(treePath);
109
        EnumSet<ElementKind> supportedTypes = EnumSet.of(ElementKind.METHOD, ElementKind.ENUM_CONSTANT, ElementKind.FIELD);
110
        if (e == null || !e.getModifiers().contains(Modifier.STATIC) || !supportedTypes.contains(e.getKind())) {
88
            return null;
111
            return null;
89
        }
112
        }
90
        List<? extends Tree> typeArgs = ((MethodInvocationTree) mitp.getLeaf()).getTypeArguments();
113
91
        if (typeArgs != null && !typeArgs.isEmpty()) {
114
        if (ElementKind.METHOD.equals(e.getKind())) {
115
            TreePath mitp = treePath.getParentPath();
116
            if (mitp == null || mitp.getLeaf().getKind() != Kind.METHOD_INVOCATION) {
92
            return null;
117
            return null;
93
        }
118
        }
94
        Element e = info.getTrees().getElement(treePath);
119
            if (((MethodInvocationTree) mitp.getLeaf()).getMethodSelect() != treePath.getLeaf()) {
95
        if (e == null || !e.getModifiers().contains(Modifier.STATIC) || e.getKind() != ElementKind.METHOD) {
96
            return null;
120
            return null;
97
        }
121
        }
98
        if (!supportsStaticImports(info)) {
122
            List<? extends Tree> typeArgs = ((MethodInvocationTree) mitp.getLeaf()).getTypeArguments();
123
            if (typeArgs != null && !typeArgs.isEmpty()) {
99
            return null;
124
            return null;
100
        }
125
        }
126
        }
101
        Element enclosingEl = e.getEnclosingElement();
127
        Element enclosingEl = e.getEnclosingElement();
102
        if (enclosingEl == null) {
128
        if (enclosingEl == null) {
103
            return null;
129
            return null;
Lines 112-118 Link Here
112
            return null;
138
            return null;
113
        }
139
        }
114
        String fqn = null;
140
        String fqn = null;
115
        String fqn1 = getMethodFqn(e);
141
        String fqn1 = getFqn(e);
116
        if (!isSubTypeOrInnerOfSubType(info, klass, enclosingEl) && !isStaticallyImported(info, fqn1)) {
142
        if (!isSubTypeOrInnerOfSubType(info, klass, enclosingEl) && !isStaticallyImported(info, fqn1)) {
117
            if (hasMethodNameClash(info, klass, sn) || hasStaticImportSimpleNameClash(info, sn)) {
143
            if (hasMethodNameClash(info, klass, sn) || hasStaticImportSimpleNameClash(info, sn)) {
118
                return null;
144
                return null;
Lines 161-167 Link Here
161
            WorkingCopy copy = ctx.getWorkingCopy();
187
            WorkingCopy copy = ctx.getWorkingCopy();
162
            TreePath treePath = ctx.getPath();
188
            TreePath treePath = ctx.getPath();
163
            TreePath mitp = treePath.getParentPath();
189
            TreePath mitp = treePath.getParentPath();
164
            if (mitp == null || mitp.getLeaf().getKind() != Kind.METHOD_INVOCATION) {
190
            if (mitp == null) {
165
                return;
191
                return;
166
            }
192
            }
167
            Element e = copy.getTrees().getElement(treePath);
193
            Element e = copy.getTrees().getElement(treePath);
Lines 280-288 Link Here
280
     * @param e
306
     * @param e
281
     * @return the FQN for a METHOD Element
307
     * @return the FQN for a METHOD Element
282
     */
308
     */
283
    private static String getMethodFqn(Element e) {
309
    private static String getFqn(Element e) {
284
        // XXX or alternatively, upgrade getElementName to handle METHOD
310
        // XXX or alternatively, upgrade getElementName to handle METHOD
285
        assert e.getKind() == ElementKind.METHOD;
286
        return getElementName(e.getEnclosingElement(), true) + "." + e.getSimpleName();
311
        return getElementName(e.getEnclosingElement(), true) + "." + e.getSimpleName();
287
    }
312
    }
288
313
(-)java.hints/test/unit/src/org/netbeans/modules/java/hints/StaticImportTest.java (+53 lines)
Lines 23-28 Link Here
23
 * License Header, with the fields enclosed by brackets [] replaced by
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 * Portions Copyrighted 2014 markiewb
26
 */
27
 */
27
package org.netbeans.modules.java.hints;
28
package org.netbeans.modules.java.hints;
28
29
Lines 47-52 Link Here
47
        super(name);
48
        super(name);
48
    }
49
    }
49
50
51
    public void testStaticImportHint_ForEnumFields() throws Exception {
52
        String test = "package test; import java.util.concurrent.TimeUnit; public class Test { public Test() { System.out.println(TimeUnit.D|AYS); } }";
53
        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); } }";
54
        HintTest.create()
55
                .setCaretMarker('|')
56
                .input(test)
57
                .run(StaticImport.class)
58
                .findWarning("0:107-0:120:hint:" + NbBundle.getMessage(StaticImport.class, "ERR_StaticImport"))
59
                .applyFix()
60
                .assertCompilable()
61
                .assertOutput(golden);
62
    }
63
    
64
    public void testStaticImportHint_ForEnumFields_InAssignment() throws Exception {
65
        String test = "package test; import java.util.concurrent.TimeUnit; public class Test { public Test() { TimeUnit foo = TimeUnit.D|AYS; } }";
66
        String golden = "package test; import java.util.concurrent.TimeUnit; import static java.util.concurrent.TimeUnit.DAYS; public class Test { public Test() { TimeUnit foo = DAYS; } }";
67
        HintTest.create()
68
                .setCaretMarker('|')
69
                .input(test)
70
                .run(StaticImport.class)
71
                .findWarning("0:103-0:116:hint:" + NbBundle.getMessage(StaticImport.class, "ERR_StaticImport"))
72
                .applyFix()
73
                .assertCompilable()
74
                .assertOutput(golden);
75
    }
76
77
    public void testStaticImportHint_ForFields() throws Exception {
78
        String test = "package test; import java.util.Calendar; public class Test { public Test() { System.out.println(Calendar.JAN|UARY); } }";
79
        String golden = "package test; import java.util.Calendar; import static java.util.Calendar.JANUARY; public class Test { public Test() { System.out.println(JANUARY); } }";
80
        HintTest.create()
81
                .setCaretMarker('|')
82
                .input(test)
83
                .run(StaticImport.class)
84
                .findWarning("0:96-0:112:hint:" + NbBundle.getMessage(StaticImport.class, "ERR_StaticImport"))
85
                .applyFix()
86
                .assertCompilable()
87
                .assertOutput(golden);
88
    }
89
    
90
    public void testStaticImportHint_ForFields_InAssignment() throws Exception {
91
        String test = "package test; import java.util.Calendar; public class Test { public Test() { int foo = Calendar.JAN|UARY; } }";
92
        String golden = "package test; import java.util.Calendar; import static java.util.Calendar.JANUARY; public class Test { public Test() { int foo = JANUARY; } }";
93
        HintTest.create()
94
                .setCaretMarker('|')
95
                .input(test)
96
                .run(StaticImport.class)
97
                .findWarning("0:87-0:103:hint:" + NbBundle.getMessage(StaticImport.class, "ERR_StaticImport"))
98
                .applyFix()
99
                .assertCompilable()
100
                .assertOutput(golden);
101
    }
102
    
50
    public void testStaticImportHint1() throws Exception {
103
    public void testStaticImportHint1() throws Exception {
51
        String test = "package test; public class Test { public Test() { Math.|abs(1); } }";
104
        String test = "package test; public class Test { public Test() { Math.|abs(1); } }";
52
        String golden = "package test; import static java.lang.Math.abs; public class Test { public Test() { abs(1); } }";
105
        String golden = "package test; import static java.lang.Math.abs; public class Test { public Test() { abs(1); } }";

Return to bug 89258