diff --git a/xml/dom-openapi/src/com/intellij/util/xml/JavaMethod.java b/xml/dom-openapi/src/com/intellij/util/xml/JavaMethod.java index 41113774d0d0..c4fe1c96596f 100644 --- a/xml/dom-openapi/src/com/intellij/util/xml/JavaMethod.java +++ b/xml/dom-openapi/src/com/intellij/util/xml/JavaMethod.java @@ -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 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 annotationClass) { + final Annotation annotation = mySignature.findAnnotation(annotationClass, myDeclaringClass); + return annotation == null ? NONE : annotation; } @Override diff --git a/xml/dom-openapi/src/com/intellij/util/xml/JavaMethodSignature.java b/xml/dom-openapi/src/com/intellij/util/xml/JavaMethodSignature.java index 54a2fe95a98c..3e82904ad3d7 100644 --- a/xml/dom-openapi/src/com/intellij/util/xml/JavaMethodSignature.java +++ b/xml/dom-openapi/src/com/intellij/util/xml/JavaMethodSignature.java @@ -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 methods) { - addMethodWithSupers(aClass, findMethod(aClass), methods); + private boolean processMethods(final Class aClass, Processor 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 methods) { + private boolean processMethodWithSupers(final Class aClass, final Method method, final Processor 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 getAllMethods(final Class startFrom) { - final ArrayList methods = new ArrayList(); - collectMethods(startFrom, methods); - return methods; + final List result = new ArrayList(); + processMethods(startFrom, new CommonProcessors.CollectProcessor(result)); + return result; } @Nullable public final Method findAnnotatedMethod(final Class 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 processor = new CommonProcessors.FindFirstProcessor() { + @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 findAnnotation(final Class annotationClass, final Class startFrom) { + CommonProcessors.FindFirstProcessor processor = new CommonProcessors.FindFirstProcessor() { + @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; } - }