DOM: optimize JavaMethod annotation handling

This commit is contained in:
Yann Cébron
2014-05-05 14:55:12 +02:00
parent dcdbc00207
commit 24bdcbf325
2 changed files with 43 additions and 28 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -27,7 +27,7 @@ import java.util.List;
/**
* @author peter
*/
public final class JavaMethod implements AnnotatedElement{
public final class JavaMethod implements AnnotatedElement {
public static final JavaMethod[] EMPTY_ARRAY = new JavaMethod[0];
private static final Object NONE = new Object();
@@ -101,14 +101,10 @@ public final class JavaMethod implements AnnotatedElement{
return annotation == NONE ? null : (T)annotation;
}
@NotNull private Object findAnnotation(Class<? extends Annotation> annotationClass) {
for (Method method : mySignature.getAllMethods(myDeclaringClass)) {
final Annotation annotation = method.getAnnotation(annotationClass);
if (annotation != null) {
return annotation;
}
}
return NONE;
@NotNull
private Object findAnnotation(Class<? extends Annotation> annotationClass) {
final Annotation annotation = mySignature.findAnnotation(annotationClass, myDeclaringClass);
return annotation == null ? NONE : annotation;
}
@Override
@@ -16,6 +16,8 @@
package com.intellij.util.xml;
import com.intellij.util.ArrayUtil;
import com.intellij.util.CommonProcessors;
import com.intellij.util.Processor;
import com.intellij.util.ReflectionUtil;
import org.jetbrains.annotations.Nullable;
@@ -54,8 +56,8 @@ public class JavaMethodSignature {
return method;
}
private void collectMethods(final Class aClass, List<Method> methods) {
addMethodWithSupers(aClass, findMethod(aClass), methods);
private boolean processMethods(final Class aClass, Processor<Method> processor) {
return processMethodWithSupers(aClass, findMethod(aClass), processor);
}
@Nullable
@@ -64,38 +66,56 @@ public class JavaMethodSignature {
return method == null ? ReflectionUtil.getDeclaredMethod(aClass, myMethodName, myMethodParameters) : method;
}
private void addMethodWithSupers(final Class aClass, final Method method, List<Method> methods) {
private boolean processMethodWithSupers(final Class aClass, final Method method, final Processor<Method> processor) {
if (method != null) {
methods.add(method);
if (!processor.process(method)) return false;
}
final Class superClass = aClass.getSuperclass();
if (superClass != null) {
collectMethods(superClass, methods);
} else {
if (!processMethods(superClass, processor)) return false;
}
else {
if (aClass.isInterface()) {
collectMethods(Object.class, methods);
if (!processMethods(Object.class, processor)) return false;
}
}
for (final Class anInterface : aClass.getInterfaces()) {
collectMethods(anInterface, methods);
if (!processMethods(anInterface, processor)) return false;
}
return true;
}
public final List<Method> getAllMethods(final Class startFrom) {
final ArrayList<Method> methods = new ArrayList<Method>();
collectMethods(startFrom, methods);
return methods;
final List<Method> result = new ArrayList<Method>();
processMethods(startFrom, new CommonProcessors.CollectProcessor<Method>(result));
return result;
}
@Nullable
public final <T extends Annotation> Method findAnnotatedMethod(final Class<T> annotationClass, final Class startFrom) {
for (Method method : getAllMethods(startFrom)) {
final T annotation = method.getAnnotation(annotationClass);
if (annotation != null && ReflectionUtil.isAssignable(method.getDeclaringClass(), startFrom)) {
return method;
CommonProcessors.FindFirstProcessor<Method> processor = new CommonProcessors.FindFirstProcessor<Method>() {
@Override
protected boolean accept(Method method) {
final T annotation = method.getAnnotation(annotationClass);
return annotation != null && ReflectionUtil.isAssignable(method.getDeclaringClass(), startFrom);
}
}
return null;
};
processMethods(startFrom, processor);
return processor.getFoundValue();
}
@Nullable
public final <T extends Annotation> T findAnnotation(final Class<T> annotationClass, final Class startFrom) {
CommonProcessors.FindFirstProcessor<Method> processor = new CommonProcessors.FindFirstProcessor<Method>() {
@Override
protected boolean accept(Method method) {
final T annotation = method.getAnnotation(annotationClass);
return annotation != null;
}
};
processMethods(startFrom, processor);
final Method foundMethod = processor.getFoundValue();
return foundMethod == null ? null : foundMethod.getAnnotation(annotationClass);
}
public String toString() {
@@ -121,5 +141,4 @@ public class JavaMethodSignature {
result = 31 * result + Arrays.hashCode(myMethodParameters);
return result;
}
}