Zbigniew Malinowski
Zbigniew Malinowski

Reputation: 1074

How expensive is call of Class.isAnnotationPresent(...)?

I'm wondering how expensive this operation can be on following runtimes:

The information about annotations of given class (target=TYPE) is available in compile time, so I'd guess that it can be cached somehow. But on the other hand, I've heard Dalvik had quite poor performance regarding annotations.

If I can check and cache the info during compile time (with some code generating plugin), should I do it, or would it be overoptimization?

Upvotes: 4

Views: 1237

Answers (1)

Andy Turner
Andy Turner

Reputation: 140514

Skimming the source code, it looks like isAnnotationPresent(...) calls getAnnotation(...), which calls initAnnotationsIfNecessary().

initAnnotationsIfNecessary() constructs a map of annotations the first time it is called; the previously-constructed map is reused on subsequent calls.

So, by the looks of it, the first call is expensive (-ish); the subsequent calls are pretty cheap.

Upvotes: 3

Related Questions