Skip to content

Commit 02ef997

Browse files
committed
Remove reflection and fallback as IDE validator reports errors
1 parent a61dd73 commit 02ef997

File tree

1 file changed

+3
-40
lines changed

1 file changed

+3
-40
lines changed
Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
package software.xdev.saveactions.processors.java.inspection;
22

3-
import com.intellij.codeInspection.ex.EntryPointsManagerBase;
4-
import com.intellij.codeInspection.visibility.EntryPointWithVisibilityLevel;
53
import com.intellij.codeInspection.visibility.VisibilityInspection;
64
import com.intellij.openapi.diagnostic.Logger;
75
import com.intellij.psi.PsiElement;
86
import com.intellij.psi.PsiJavaCodeReferenceElement;
97
import com.intellij.psi.PsiMember;
108
import com.intellij.psi.SyntaxTraverser;
11-
import one.util.streamex.StreamEx;
129
import org.jetbrains.annotations.NotNull;
13-
import software.xdev.saveactions.model.Action;
1410

15-
import java.lang.reflect.InvocationTargetException;
1611
import 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

Comments
 (0)