|
| 1 | +package software.xdev.saveactions.processors.java.inspection; |
| 2 | + |
| 3 | +import com.intellij.codeInspection.ex.EntryPointsManagerBase; |
| 4 | +import com.intellij.codeInspection.visibility.EntryPointWithVisibilityLevel; |
| 5 | +import com.intellij.codeInspection.visibility.VisibilityInspection; |
| 6 | +import com.intellij.openapi.diagnostic.Logger; |
| 7 | +import com.intellij.psi.PsiElement; |
| 8 | +import com.intellij.psi.PsiJavaCodeReferenceElement; |
| 9 | +import com.intellij.psi.PsiMember; |
| 10 | +import com.intellij.psi.SyntaxTraverser; |
| 11 | +import one.util.streamex.StreamEx; |
| 12 | +import org.jetbrains.annotations.NotNull; |
| 13 | +import software.xdev.saveactions.model.Action; |
| 14 | + |
| 15 | +import java.lang.reflect.InvocationTargetException; |
| 16 | +import java.lang.reflect.Method; |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.List; |
| 19 | + |
| 20 | +/** |
| 21 | + * Fork of {@link com.intellij.codeInspection.visibility.VisibilityInspection} but accessible for the plugin. |
| 22 | + */ |
| 23 | +public final class AccessibleVisibilityInspection { |
| 24 | + |
| 25 | + private static final Logger LOG = Logger.getInstance(AccessibleVisibilityInspection.class); |
| 26 | + |
| 27 | + private AccessibleVisibilityInspection() { |
| 28 | + } |
| 29 | + |
| 30 | + public static boolean containsReferenceTo(PsiElement source, PsiElement target) { |
| 31 | + return SyntaxTraverser.psiTraverser(source) |
| 32 | + .filter(PsiJavaCodeReferenceElement.class) |
| 33 | + .filter(ref -> ref.isReferenceTo(target)) |
| 34 | + .isNotEmpty(); |
| 35 | + } |
| 36 | + |
| 37 | + @SuppressWarnings("java:S3011") // reflection is needed because VisibilityInspection members are private |
| 38 | + public static int getMinVisibilityLevel(VisibilityInspection myVisibilityInspection, @NotNull PsiMember member) { |
| 39 | + List<Exception> exceptions = new ArrayList<>(); |
| 40 | + // 1. Try to access getMinVisibilityLevel directly |
| 41 | + try { |
| 42 | + Method getMinVisibilityLevel = myVisibilityInspection.getClass() |
| 43 | + .getDeclaredMethod("getMinVisibilityLevel", PsiMember.class); |
| 44 | + |
| 45 | + getMinVisibilityLevel.setAccessible(true); |
| 46 | + |
| 47 | + return (int) getMinVisibilityLevel.invoke(myVisibilityInspection, member); |
| 48 | + } catch (Exception e) { |
| 49 | + LOG.error("Failed to invoke getMinVisibilityLevel", e); |
| 50 | + exceptions.add(e); |
| 51 | + } |
| 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 | + } |
| 82 | +} |
0 commit comments