11package software .xdev .saveactions .processors .java .inspection ;
22
3- import com .intellij .codeInspection .ex .EntryPointsManagerBase ;
4- import com .intellij .codeInspection .visibility .EntryPointWithVisibilityLevel ;
53import com .intellij .codeInspection .visibility .VisibilityInspection ;
64import com .intellij .openapi .diagnostic .Logger ;
75import com .intellij .psi .PsiElement ;
86import com .intellij .psi .PsiJavaCodeReferenceElement ;
97import com .intellij .psi .PsiMember ;
108import com .intellij .psi .SyntaxTraverser ;
11- import one .util .streamex .StreamEx ;
129import org .jetbrains .annotations .NotNull ;
13- import software .xdev .saveactions .model .Action ;
1410
15- import java .lang .reflect .InvocationTargetException ;
1611import java .lang .reflect .Method ;
17- import java .util .ArrayList ;
18- import java .util .List ;
1912
2013/**
2114 * Fork of {@link com.intellij.codeInspection.visibility.VisibilityInspection} but accessible for the plugin.
@@ -34,10 +27,9 @@ public static boolean containsReferenceTo(PsiElement source, PsiElement target)
3427 .isNotEmpty ();
3528 }
3629
37- @ SuppressWarnings ("java:S3011" ) // reflection is needed because VisibilityInspection members are private
30+ // reflection is needed because VisibilityInspection members are private
31+ @ SuppressWarnings ({"java:S3011" })
3832 public static int getMinVisibilityLevel (VisibilityInspection myVisibilityInspection , @ NotNull PsiMember member ) {
39- List <Exception > exceptions = new ArrayList <>();
40- // 1. Try to access getMinVisibilityLevel directly
4133 try {
4234 Method getMinVisibilityLevel = myVisibilityInspection .getClass ()
4335 .getDeclaredMethod ("getMinVisibilityLevel" , PsiMember .class );
@@ -47,36 +39,7 @@ public static int getMinVisibilityLevel(VisibilityInspection myVisibilityInspect
4739 return (int ) getMinVisibilityLevel .invoke (myVisibilityInspection , member );
4840 } catch (Exception e ) {
4941 LOG .error ("Failed to invoke getMinVisibilityLevel" , e );
50- exceptions . add (e );
42+ throw new IllegalStateException (e );
5143 }
52-
53- // 2. Fallback to isEntryPointEnabled
54- LOG .warn ("getMinVisibilityLevel: Trying fallback method: isEntryPointEnabled" );
55- try {
56- Method isEntryPointEnabled = myVisibilityInspection .getClass ()
57- .getDeclaredMethod ("isEntryPointEnabled" , EntryPointWithVisibilityLevel .class );
58- isEntryPointEnabled .setAccessible (true );
59-
60- return StreamEx .of (EntryPointsManagerBase .DEAD_CODE_EP_NAME .getExtensions ())
61- .select (EntryPointWithVisibilityLevel .class )
62- .filter (point -> {
63- try {
64- return (boolean ) isEntryPointEnabled .invoke (myVisibilityInspection , point );
65- } catch (IllegalAccessException | InvocationTargetException e ) {
66- throw new IllegalStateException ("Reflection call failed" , e );
67- }
68- })
69- .mapToInt (point -> point .getMinVisibilityLevel (member ))
70- .max ().orElse (-1 );
71- } catch (Exception e ) {
72- LOG .error ("Failed to invoke isEntryPointEnabled" , e );
73- exceptions .add (e );
74- }
75-
76- LOG .error ("Execution of getMinVisibilityLevel with reflection failed; "
77- + "Please report the problem so that I can be fixed. In the meantime consider disabling '"
78- + Action .accessCanBeTightened .getText ()
79- + "' or downgrade your IDE" );
80- throw new IllegalStateException (exceptions .get (0 ));
8144 }
8245}
0 commit comments