java


Can't access annotation of field in Java


I have annotation
public #interface Equals {
String name() default "unknown";
}
and class whose field is annotated by Equals
public class TransportSearch {
#Equals(name="vin")
public String vin = null;
}
and I have one method in another class
public String (TransportSearch transportSearch){
Field[] fields = transportSearch.getClass().getDeclaredFields();
String vin = null;
for(Field field : fields){
try {
Equals annot = field.getAnnotation(Equals.class);
if(annot!=null && annot.name().equals("vin")){
try {
vin = (String) field.get(transportSearch);
} catch (IllegalAccessException e){
e.printStackTrace();
}
}
} catch(NullPointerException e) {
e.printStackTrace();
}
}
return vin;
}
unfortunately in this method field.getAnnotation(Equals.class) returns null and I can't understand why?
Can you help me?
You must set the RetentionPolicy of your annotation to RetentionPolicy.RUNTIME:
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
#Retention(RetentionPolicy.RUNTIME)
public #interface Equals {
String name() default "unknown";
}
The default RetentionPolicy is RetentionPolicy.CLASS, which means the annotation does not need to be available at runtime.

Related Links

Applet Stand alone issue
Partial compile time weaving third party jar with maven
how to store a dynamically typed text in a string in swings
How to fix this java.io.FileNotFoundException?
How to debug problems with GWT dev/hosted mode?
Converting array string to string and back in Java
Matching Uppercased Character with Lower Cased Character, Java
Parallel Boolean Expression Evaluation in Java [duplicate]
can we change the gps values of a mobile device?
I have a 3 comboboxes that represent dob. The 3 comboboxes need to be next to one another like this dd mm yyyy. Is it possible using gridbag layout?
Install different Java JDK versions on Mountain Lion
sort arraylist of complex objects alphabetically
Changing font type within strings.xml
python pickle.dumps and struct.pack in Java
Java Hibernate constant in sql-query
methods and call by reference

Categories

HOME
coq
react-virtualized
onedrive
office365api
tesseract
mean-stack
jsp-tags
v8
survey
dax
imacros
numeral.js
quickbooks
vb.net-2010
visual-studio-2005
dtrace
hapi
caml
workload-scheduler
libtiff
seaborn
windows-7-x64
ef-migrations
undefined
firefox-webextensions
nodatime
kryo
cx-freeze
functional-testing
cultureinfo
icloud-api
trading
p-value
angular2-aot
javacv
social-media
strncpy
msys2
windowbuilder
gitignore
srcset
binary-data
yadcf
data-manipulation
gtrendsr
mmenu
revolution-slider
serve
x11-forwarding
multilingual
objectlistview
webix-treetable
cookiecutter-django
businessworks
segment
youcompleteme
theming
sage-one
integrity
worker-thread
np-complete
sybase-asa
python-cryptography
watchconnectivity
time-and-attendance
abcpdf9
bonobo
etsy
qcustomplot
dlna
bluegiga
pagerank
itextpdf
system.management
vhd
reactive-banana
execute
collapse
comobject
libressl
content-length
angular-leaflet-directive
e10s
geonetwork
csquery
rake-task
tablelayout
winddk
castle
doskey
jmapviewer
jquery-mobile-dialog
propertyeditor
removeclass
heartbeat
infobox
inotifycollectionchanged
visitor-statistic
digest-authentication
gallio
libs
uimenucontroller
gacutil
w3wp

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App