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

Acquired Connections grows abruptly leading to server crash
Getting an error when testing for an expected exception, even though it throws the right exception
ModelDriven stopped working in Struts2
How to find a memory leak in javac task
Automatically receive BLE Data in Android app when sent by device
Separate project in two part with Apache tomcat
DB Connection are not released while running the Spring+Eclipse Link jpa application
Make Zip task run after dependencies are resolved
Angularjs to spring controller
Creating a static method that meshes two strings together
LongSerializer vs ByteArraySerializer in kafka
How can I have a group of JSpinner objects with a maximum total value?
Optimizing memory leakage in JavaFX
runtime.getRuntime.exec Freezes executing
read formatted BLE adverting data in android logcat but cant invoke it
How to assign database row id to a button

Categories

HOME
urbancode
activiti
isabelle
azure-data-factory
fluentd
google-play
xmpp
setup-deployment
maven-3
sharepoint-designer
c#-2.0
cross-browser
ebean
wamp
flyway4
azure-media-services
add
session-variables
ssl-client-authentication
circular-dependency
vb.net-2010
iron-router
django-simple-history
libtiff
ef-migrations
propel
textfield
plunker
tibco-mdm
qhull
user-interaction
flink-streaming
nat
media-queries
http-status-code-503
twilio-api
siesta-swift
google-sites-2016
http-get
winrt-xaml-toolkit
uiswipegesturerecognizer
io-redirection
disassembling
vapor
broadcastreceiver
arena-simulation
mime
outlook-api
websphere-mq-fte
executenonquery
react-scripts
paxos
gabor-filter
worker-thread
snmptrapd
memory-alignment
iso8601
intrusion-detection
fuzzy-search
connect-by
hsv
execute
lib.web.mvc
rgeo
ionic
service-accounts
generic-programming
census
remobjects
pretty-print
fscommand
oxwall
nsbutton
googlemock
xojo
project-planning
responsive-slides
hyprlinkr
imdbpy
odata4j
dotnetnuke-5
interface-orientation
enterprisedb
http-unit
wsdl-2.0
simba
chuck
libstdc++
venn-diagram
propertyeditor
dsn
wse3.0
php-parser
cinema-4d
appendto
android-sdk-2.1
iphone-web-app
imac
gamma
modelstate
asp.net-profiles
exchange-server-2003
simpletest
project-hosting
defensive-programming
ubuntu-9.04
grid-system
signal-handling
lzh





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