Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance of calling Method/Field.getAnnotation(Class) several times vs. Pre-caching this data in a Map

I'd like to know if there are any comparison/studies about the performance of repeatidly calling (in Java) the Method.getAnnotation(Class) and Field.getAnnotation(Class) methods, versus storing (at program start up time) a precomputed Map with this metadata information of the classes and querying it repeatidly later. Which one would provide the best runtime performance?

And this performance would be the same under Java 5, 6 and 7?

like image 527
Gabriel Belingueres Avatar asked Nov 11 '11 19:11

Gabriel Belingueres


People also ask

How to check if Field has annotation Java?

The getAnnotation() method of java. lang. reflect. Field is used to return returns Field objects's for the specified type if such an annotation is present, else null.

How do you use getAnnotation?

This is important method to get annotation for Method object. Parameter: This method takes a mandatory parameter annotationClass which is the Class object of the annotation type. Return Value: This method returns the method's annotation for the specified annotation type if present on this element, else null.

What is @field annotation in Java?

Use @Field to define a structured data type's field name for an object mapped to NoSql data. Annotation Elements. Table 2-24 describes this annotation's elements.

How to get annotation attribute in Java?

There is no way to get the current method, e.g. there is no getMethod() such as getClass(). Therefore, the method accessing its own annotation would need to know its own name. The retention policy of the annotation must be set to RUNTIME , so you can access the annotation at runtime.


1 Answers

Map should be a more preferable approach. The main issue is not only about caching. But also improve the multi thread contention. In the Method.getAnnotation(), it calls a synchronized private method declaredAnnotations(). Synchronized method have bad impact on highly threaded application.

like image 130
grasssoil Avatar answered Sep 30 '22 12:09

grasssoil