1

I'd like to implement an annotation for methods which will let me know where those annotated methods called, just like official @deprecated annotation.

How can I get the list of all calling methods for a given annotated method?

palindrom
  • 18,033
  • 1
  • 21
  • 37
  • At compile time / in an IDE? You would need write your own tool if you really want that. Something like http://tools.android.com/tips/lint or http://stackoverflow.com/questions/13563820/create-a-java-annotation-with-ide-contextual-behaviour – zapl Dec 16 '15 at 10:21
  • @zapl I want to track points where gcm.send is called, which must not be called from main thread. – palindrom Dec 16 '15 at 10:28
  • 1
    Possible duplicate of [Intellij/ Java - identify calls to annotated methods](http://stackoverflow.com/questions/26213727/intellij-java-identify-calls-to-annotated-methods) – palindrom Dec 16 '15 at 13:44

1 Answers1

0

I think this question may help you:

To find this annotated method (from Arthur Ronald's answer):

use org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider

API

A component provider that scans the classpath from a base package. It then applies exclude and include filters to the resulting classes to find candidates.

ClassPathScanningCandidateComponentProvider scanner =
new ClassPathScanningCandidateComponentProvider(<DO_YOU_WANT_TO_USE_DEFALT_FILTER>);

scanner.addIncludeFilter(new AnnotationTypeFilter(<TYPE_YOUR_ANNOTATION_HERE>.class));

for (BeanDefinition bd : scanner.findCandidateComponents(<TYPE_YOUR_BASE_PACKAGE_HERE>))
    System.out.println(bd.getBeanClassName());

Or (from Jonathan's answer):

Google reflections:

https://github.com/ronmamo/reflections

Quick review:

  • Spring solution is the way to go if you're using Spring. Otherwise it's a big dependency.
  • Using ASM directly is a bit cumbersome.
  • Using Java Assist directly is clunky too.
  • Annovention is super lightweight and convenient. No maven integration yet.
  • Google reflections pulls in Google collections. Indexes everything and then is super fast.

Update: If want to calling methods for a given annotated method, you should use AOP (Aspect Oriented Programming) and add @ Around or @ Before, for example something like this (I don't check this code):

public class Foo {
  @YourAnnotation
  public int power(int x, int p) {
    return Math.pow(x, p);
  }
}

@Aspect
public class MethodLogger {
  @Around("execution(* *(..)) && @annotation(YourAnnotation)")
  public Object around(ProceedingJoinPoint point) {
    Logger.info(
      "call by method" + MethodSignature.class.cast(point.getSignature()).getMethod().getName()
    );
    Object result = point.proceed();
    return result;
  }
}
Community
  • 1
  • 1
Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
  • I don't want to find annotated methods but methods calling annotated methods. – palindrom Dec 16 '15 at 10:34
  • I rellay appreciate your help but I want to do this at compile time like android lint does. Maybe my question wasn't clear enough. – palindrom Dec 16 '15 at 11:34
  • unfortunately finding method calls to an annotated method is not possible with structural search: https://devnet.jetbrains.com/message/5451478 – palindrom Dec 16 '15 at 13:42
  • Do you try to search a plugin in https://plugins.jetbrains.com/category/index?pr=idea&category_id=3 ? If its really need, you can try to create you own custom plugin unsing http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started.html – Slava Vedenin Dec 16 '15 at 14:13