1
1
package software .xdev .saveactions .processors .java .inspection ;
2
2
3
- import com .intellij .codeInspection .ex .EntryPointsManagerBase ;
4
- import com .intellij .codeInspection .visibility .EntryPointWithVisibilityLevel ;
5
3
import com .intellij .codeInspection .visibility .VisibilityInspection ;
6
4
import com .intellij .openapi .diagnostic .Logger ;
7
5
import com .intellij .psi .PsiElement ;
8
6
import com .intellij .psi .PsiJavaCodeReferenceElement ;
9
7
import com .intellij .psi .PsiMember ;
10
8
import com .intellij .psi .SyntaxTraverser ;
11
- import one .util .streamex .StreamEx ;
12
9
import org .jetbrains .annotations .NotNull ;
13
- import software .xdev .saveactions .model .Action ;
14
10
15
- import java .lang .reflect .InvocationTargetException ;
16
11
import java .lang .reflect .Method ;
17
- import java .util .ArrayList ;
18
- import java .util .List ;
19
12
20
13
/**
21
14
* 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)
34
27
.isNotEmpty ();
35
28
}
36
29
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" })
38
32
public static int getMinVisibilityLevel (VisibilityInspection myVisibilityInspection , @ NotNull PsiMember member ) {
39
- List <Exception > exceptions = new ArrayList <>();
40
- // 1. Try to access getMinVisibilityLevel directly
41
33
try {
42
34
Method getMinVisibilityLevel = myVisibilityInspection .getClass ()
43
35
.getDeclaredMethod ("getMinVisibilityLevel" , PsiMember .class );
@@ -47,36 +39,7 @@ public static int getMinVisibilityLevel(VisibilityInspection myVisibilityInspect
47
39
return (int ) getMinVisibilityLevel .invoke (myVisibilityInspection , member );
48
40
} catch (Exception e ) {
49
41
LOG .error ("Failed to invoke getMinVisibilityLevel" , e );
50
- exceptions . add (e );
42
+ throw new IllegalStateException (e );
51
43
}
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 ));
81
44
}
82
45
}
0 commit comments