Last active
August 29, 2015 13:57
-
-
Save dha-lo-jd/9669108 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.annotation.Annotation; | |
import java.util.EnumSet; | |
import com.google.common.collect.Lists; | |
public class AnnotationRecursiveDigger { | |
public interface Function<A extends Annotation> { | |
void work(Class<?> cls, Class<?> superCls, A anno); | |
} | |
public enum Target { | |
SuperClass, Interface, | |
} | |
public static <A extends Annotation> void processAnnotationDeep(Class<?> cls, Class<A> annotation, Class<?> targetCls, Function<A> function, | |
EnumSet<Target> targets) { | |
if (targets.contains(Target.SuperClass)) { | |
Class<?> superCls = targetCls.getSuperclass(); | |
if (superCls != null) { | |
processAnnotationDeep(cls, annotation, superCls, function, targets); | |
} | |
} | |
if (targets.contains(Target.Interface)) { | |
for (Class<?> superCls : targetCls.getInterfaces()) { | |
processAnnotationDeep(cls, annotation, superCls, function, targets); | |
} | |
} | |
A anno = targetCls.getAnnotation(annotation); | |
if (anno != null) { | |
function.work(cls, targetCls, anno); | |
} | |
} | |
public static <A extends Annotation> void processAnnotationDeep(Class<?> cls, Class<A> annotation, Function<A> function, Target... targetArgs) { | |
if (targetArgs == null || targetArgs.length == 0) { | |
targetArgs = Target.values(); | |
} | |
EnumSet<Target> targets = EnumSet.copyOf(Lists.newArrayList(targetArgs)); | |
processAnnotationDeep(cls, annotation, cls, function, targets); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment