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 |
|