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

Eclipse Java Compiler Errors
JSPExpression Language reading value from bean
Switch case not returning string
Get selected cell object in datatable using different lists for each column [duplicate]
Trouble getting a Java Applet program to open in my browser
NoNodeAvailableException[None of the configured nodes are available] in Elasticsearch 5.1.2
SPRING #RestController: Passing Cookie to a Service from Every Method
Updating version numbers of modules in a multi-module Maven project where the aggregate root isn't a parent pom
How can I use dagger 2 so that I can use an abstract representation of shared preferences?
mock method is not getting called - java
Java 8 - how sum many fields into a dto?
How can I save the Open NLP parser output from Java, so that I can use it in Python?
Identifying time zones in ISO 8601
Javafx: Updating a phone number textfield with hashtags and numbers live
telephone links from Android WebView opening error URL instead of phone dialer
Replace <br/> with comma in HTML

Categories

HOME
hive
hook
magnific-popup
deezer
sqlite-net-extensions
elm
fingerprint
ojdbc
constraint-programming
node-notifier
esper
gitpitch
dacpac
user-input
rascal
size
quickfix
dbext
grails3
custom-wordpress-pages
mustache.php
php-openssl
url-scheme
neo4j-spatial
buck
objectanimator
tinymce-4
preg-match
protovis
android-kernel
html5-fullscreen
revolution-slider
dosbox
disassembling
adobe-premiere
swift3.0.2
abstract-class
document.write
ansible-playbook
elasticsearch-plugin
cookiecutter-django
sas-visual-analytics
mesos-chronos
modelmapper
taffy
dotnetzip
veracode
yii2-extension
svmlight
aurelia-validation
setter
iso8601
pycaffe
xcb
ableton-live
spring-android
grails-tomcat-plugin
radtreelist
probability-density
savon
xml-attribute
gstreamer-0.10
contact-list
markojs
separator
prerequisites
lib.web.mvc
cartesian-product
cannon.js
cyclomatic-complexity
p2
clipperlib
inmobi
truevault
directoryservices
geonetwork
clicktag
marmalade
oam
ms-project-server-2010
sitemesh
html4
qt-faststart
seed
xamlparseexception
first-responder
runas
viewswitcher
assembly-loading
infobox
actionview
lpeg
paster
getresponsestream
routedevent
asp.net-profiles
ext3
iweb
backcolor
avatar
sector
defensive-programming
zune
django-notification
.net-1.0

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