2828import com .sun .source .tree .Tree ;
2929import com .sun .source .tree .Tree .Kind ;
3030import com .sun .source .util .TreePath ;
31- import java .util .Collections ;
3231import java .util .EnumSet ;
3332import java .util .List ;
3433import java .util .Set ;
4241import javax .lang .model .type .TypeMirror ;
4342import javax .lang .model .util .Types ;
4443import org .netbeans .api .java .source .CompilationInfo ;
45- import org .netbeans .api .java .source .ElementUtilities ;
4644import org .netbeans .api .java .source .GeneratorUtilities ;
4745import org .netbeans .api .java .source .TreeMaker ;
4846import org .netbeans .api .java .source .TreePathHandle ;
7775 *
7876 * @author Sam Halliday
7977 * @author markiewb
80- * @see <a href="http://www.netbeans.org/issues/show_bug.cgi?id=89258">RFE 89258</a>
81- * @see <a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/static-import.html>Static Imports</a>
78+ * @see <a href="https://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html>Static Imports</a>
8279 */
8380@ Hint (category ="rules15" , displayName ="#DN_StaticImport" , description ="#DSC_StaticImport" , severity =Severity .HINT , enabled =false , suppressWarnings ={"" , "StaticImport" },
8481 minSourceVersion = "5" )
8582public class StaticImport {
83+
84+ private static final Set <ElementKind > SUPPORTED_TYPES = EnumSet .of (ElementKind .METHOD , ElementKind .ENUM_CONSTANT , ElementKind .FIELD );
8685
8786 @ TriggerTreeKind (Kind .MEMBER_SELECT )
8887 public static List <ErrorDescription > run (HintContext ctx ) {
8988 CompilationInfo info = ctx .getInfo ();
9089 TreePath treePath = ctx .getPath ();
9190
9291 Element e = info .getTrees ().getElement (treePath );
93- EnumSet <ElementKind > supportedTypes = EnumSet .of (ElementKind .METHOD , ElementKind .ENUM_CONSTANT , ElementKind .FIELD );
94- if (e == null || !e .getModifiers ().contains (Modifier .STATIC ) || !supportedTypes .contains (e .getKind ())) {
92+ if (e == null || !e .getModifiers ().contains (Modifier .STATIC ) || !SUPPORTED_TYPES .contains (e .getKind ())) {
9593 return null ;
9694 }
9795
9896 if (ElementKind .METHOD == e .getKind ()) {
9997 TreePath mitp = treePath .getParentPath ();
10098 if (mitp == null || mitp .getLeaf ().getKind () != Kind .METHOD_INVOCATION ) {
101- return null ;
102- }
99+ return null ;
100+ }
103101 if (((MethodInvocationTree ) mitp .getLeaf ()).getMethodSelect () != treePath .getLeaf ()) {
104- return null ;
105- }
102+ return null ;
103+ }
106104 List <? extends Tree > typeArgs = ((MethodInvocationTree ) mitp .getLeaf ()).getTypeArguments ();
107105 if (typeArgs != null && !typeArgs .isEmpty ()) {
108- return null ;
109- }
106+ return null ;
107+ }
110108 }
111109 Element enclosingEl = e .getEnclosingElement ();
112110 if (enclosingEl == null ) {
@@ -122,7 +120,7 @@ public static List<ErrorDescription> run(HintContext ctx) {
122120 return null ;
123121 }
124122 Element klass = info .getTrees ().getElement (cc );
125- if (klass == null || klass .getKind () != ElementKind . CLASS ) {
123+ if (klass == null || ! klass .getKind (). isDeclaredType () ) {
126124 return null ;
127125 }
128126 String fqn = null ;
@@ -143,7 +141,7 @@ public static List<ErrorDescription> run(HintContext ctx) {
143141 if (ctx .isCanceled ()) {
144142 return null ;
145143 }
146- return Collections . singletonList (ed );
144+ return List . of (ed );
147145 }
148146
149147 public static final class FixImpl extends JavaFix {
@@ -189,31 +187,18 @@ protected void performRewrite(TransformationContext ctx) throws Exception {
189187 return ;
190188 }
191189 CompilationUnitTree cut = (CompilationUnitTree ) copy .resolveRewriteTarget (copy .getCompilationUnit ());
192- CompilationUnitTree nue = GeneratorUtilities .get (copy ).addImports (cut , Collections . singleton (e ));
190+ CompilationUnitTree nue = GeneratorUtilities .get (copy ).addImports (cut , Set . of (e ));
193191 copy .rewrite (cut , nue );
194192 }
195193
196194 }
197195
198- /**
199- * @param info
200- * @return true if the source level supports the static import language feature
201- */
202- private static boolean supportsStaticImports (CompilationInfo info ) {
203- return info .getSourceVersion ().compareTo (SourceVersion .RELEASE_5 ) >= 0 ;
204- }
205-
206196 // returns true if a METHOD is enclosed in element with simple name sn
207197 private static boolean hasMethodWithSimpleName (CompilationInfo info , Element element , final String sn ) {
208- Iterable <? extends Element > members =
209- info .getElementUtilities ().getMembers (element .asType (), new ElementUtilities .ElementAcceptor () {
210-
211- @ Override
212- public boolean accept (Element e , TypeMirror type ) {
213- return e .getKind () == ElementKind .METHOD && e .getSimpleName ().toString ().equals (sn );
214- }
215- });
216- return members .iterator ().hasNext ();
198+ return info .getElementUtilities ().getMembers (
199+ element .asType (),
200+ (elem , type ) -> elem .getKind () == ElementKind .METHOD && elem .getSimpleName ().toString ().equals (sn )
201+ ).iterator ().hasNext ();
217202 }
218203
219204 /**
@@ -266,7 +251,7 @@ private static boolean hasStaticImportSimpleNameClash(CompilationInfo info, Stri
266251 */
267252 private static boolean isSubTypeOrInnerOfSubType (CompilationInfo info , Element t1 , Element t2 ) {
268253 boolean isSubtype = info .getTypes ().isSubtype (t1 .asType (), t2 .asType ());
269- boolean isInnerClass = t1 .getEnclosingElement ().getKind () == ElementKind . CLASS ;
254+ boolean isInnerClass = t1 .getEnclosingElement ().getKind (). isDeclaredType () ;
270255 return isSubtype || (isInnerClass && info .getTypes ().isSubtype (t1 .getEnclosingElement ().asType (), t2 .asType ()));
271256 }
272257
@@ -278,15 +263,12 @@ private static boolean isSubTypeOrInnerOfSubType(CompilationInfo info, Element t
278263 * methods in klass (which may be an inner or static class).
279264 */
280265 private static boolean hasMethodNameClash (CompilationInfo info , Element klass , String simpleName ) {
281- assert klass != null ;
282- assert klass .getKind () == ElementKind .CLASS ;
283-
284266 // check the members and inherited members of the klass
285267 if (hasMethodWithSimpleName (info , klass , simpleName )) {
286268 return true ;
287269 }
288270 Element klassEnclosing = klass .getEnclosingElement ();
289- return ( klassEnclosing != null && klassEnclosing .getKind () == ElementKind . CLASS && hasMethodWithSimpleName (info , klassEnclosing , simpleName ) );
271+ return klassEnclosing != null && klassEnclosing .getKind (). isDeclaredType () && hasMethodWithSimpleName (info , klassEnclosing , simpleName );
290272 }
291273
292274 /**
0 commit comments