mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
annotations patterns support in DeadCodeInspection (IDEA-24043 Jersey request methods should not be inspected as "method is never used")
This commit is contained in:
@@ -67,6 +67,9 @@ public class DeadCodeInspection extends FilteringInspectionTool {
|
||||
public boolean ADD_NONJAVA_TO_ENTRIES = true;
|
||||
|
||||
public JDOMExternalizableStringList ADDITIONAL_ANNOTATIONS = new JDOMExternalizableStringList();
|
||||
private static final String[] ADDITIONAL_ANNOS = {
|
||||
"javax.ws.rs.*"
|
||||
};
|
||||
|
||||
private HashSet<RefElement> myProcessedSuspicious = null;
|
||||
private int myPhase;
|
||||
@@ -86,6 +89,7 @@ public class DeadCodeInspection extends FilteringInspectionTool {
|
||||
public final UnusedCodeExtension[] myExtensions;
|
||||
|
||||
public DeadCodeInspection() {
|
||||
ADDITIONAL_ANNOTATIONS.addAll(Arrays.asList(ADDITIONAL_ANNOS));
|
||||
myQuickFixActions = new QuickFixAction[]{new PermanentDeleteAction(), new CommentOutBin(), new MoveToEntries()};
|
||||
ExtensionPoint<UnusedCodeExtension> point = Extensions.getRootArea().getExtensionPoint(ExtensionPoints.DEAD_CODE_TOOL);
|
||||
final UnusedCodeExtension[] deadCodeAddins = point.getExtensions();
|
||||
@@ -430,7 +434,7 @@ public class DeadCodeInspection extends FilteringInspectionTool {
|
||||
if (isAddMainsEnabled() && PsiMethodUtil.hasMainMethod(aClass)) return true;
|
||||
}
|
||||
if (element instanceof PsiModifierListOwner
|
||||
&& AnnotationUtil.isAnnotated((PsiModifierListOwner)element, ADDITIONAL_ANNOTATIONS)) {
|
||||
&& AnnotationUtil.checkAnnotatedUsingPatterns((PsiModifierListOwner)element, ADDITIONAL_ANNOTATIONS)) {
|
||||
return true;
|
||||
}
|
||||
for (UnusedCodeExtension extension : myExtensions) {
|
||||
|
||||
@@ -222,4 +222,40 @@ public class AnnotationUtil {
|
||||
public static boolean isJetbrainsAnnotation(@NonNls final String simpleName) {
|
||||
return ArrayUtil.find(SIMPLE_NAMES, simpleName) != -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Works similar to #isAnnotated(PsiModifierListOwner, Collection<String>) but supports FQN patters
|
||||
* like "javax.ws.rs.*". Supports ending "*" only.
|
||||
*
|
||||
* @param owner modifier list
|
||||
* @param annotations annotations qualified names or patterns. Patterns can have '*' at the end
|
||||
* @return <code>true</code> if annotated of at least one annotation from the annotations list
|
||||
*/
|
||||
public static boolean checkAnnotatedUsingPatterns(PsiModifierListOwner owner, Collection<String> annotations) {
|
||||
List<String> fqns = null;
|
||||
final PsiModifierList modList;
|
||||
if (owner == null || (modList = owner.getModifierList()) == null) return false;
|
||||
|
||||
for (String fqn : annotations) {
|
||||
if (! fqn.endsWith("*") && isAnnotated(owner, fqn, false)) {
|
||||
return true;
|
||||
} else {
|
||||
if (fqns == null) {
|
||||
fqns = new ArrayList<String>();
|
||||
final PsiAnnotation[] annos = modList.getAnnotations();
|
||||
for (PsiAnnotation anno : annos) {
|
||||
fqns.add(anno.getQualifiedName());
|
||||
}
|
||||
if (fqns.isEmpty()) return false;
|
||||
}
|
||||
fqn = fqn.substring(0, fqn.length() - 2);
|
||||
for (String annoFQN : fqns) {
|
||||
if (annoFQN.startsWith(fqn)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user