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

storing UUID in mysql database table
Java. Connect to server using FTPClient class
UnmappableCharacterException when running packaged program
Parsing text file in java - Error
Google Advertizer ID in Library
Spring taskexecutor probleme double execution
hibernate.cfg.xml error creation
Verify map with custom values
Alignment of text in JTextArea and JtextPane
HashMap / TreeSet combination inconsistency
Test Class for Android
Cannot find class in classpath: when running pom.xml within Eclipse + TestNg
Exception in hibernate
Grow Heap in android with Endless List
how to redirect stdin and stdout to a text file in java
Dynamically adding tomcat to a cluster - ehcache xml configurations files must be manually adjusted?

Categories

HOME
vim
google-chrome-extension
omnet++
image-processing
mediawiki
fme
cplex
getelementsbytagname
google-oauth
paw-app
microservices
rdf
icloud
disassembler
podio
pheatmap
cross-validation
google-translate
communication
vifm
fancybox-3
dax
highlight.js
facebook-page
reactcsstransitiongroup
interop
lldb
facebook-instant-articles
captiveportal
one-to-many
cas
status
publish
jaxb2
webkitspeechrecognition
unboundid
madlib
flink-streaming
fabric8
stormpath
nameservers
google-sites-2016
stringtemplate
textmate
y86
mmenu
mpmediaquery
bytecode-manipulation
ios5
serverside-rendering
android-browser
code-search-engine
sharefile
ruby-on-rails-3.1
reportingservices-2005
android-mediaprojection
nxlog
taffy
rails-routing
yii2-extension
tropo
aurelia-validation
python-cryptography
linode
flutterwave
django-scheduler
nested-sets
android-fonts
libpng
adxstudio-portals
plottable.js
autorest
ubuntu-10.04
packagist
wireshark-dissector
content-length
ionic
graph-drawing
inmobi
formatjs
file-copying
android-radiobutton
sgen
dml
wp-query
htmlcleaner
viadeo
industrial
farseer
jsctypes
ruby-datamapper
qt-faststart
sqlperformance
referrer
isnullorempty
first-responder
buildr
semantic-diff
pendrive
work-stealing
heartbeat
genshi
createwindow
digest-authentication
gin
preference
wsdl.exe
paul-graham

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