Problème avec des annotations
Hello,
Je voudras accéder à une annotation par introspéction mais je n'y arrive pas, il me retourne toujours null... :bug:
Qqch est il faux?
P.S. J'utilise déja l'introspéction sur le Field[] f et ça marche très bien, il n'y a que les annotations qui... :mur:
Merci d'avance!!
J'ai une annotation:
Code:
1 2 3 4 5
| public @interface range {
double min();
double max();
} |
Et un fichier Config01.java:
Code:
1 2 3 4 5 6 7 8 9 10
| public class Config01{
@range(min=1, max=9)
public byte byteS = 0;
public Config01() throws ConfigException {
String xmlFile = "xml/config.xml";
new Configuration(this, xmlFile);
}
} |
qui contient des annotations...
Configuration.java:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class Configuration {
public Configuration(Object o, String xmlFile) throws ConfigException {
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
factory.setValidating(true);
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(new File(xmlFile), new SaxParser(o) );
} catch (FileNotFoundException t){
// The specified file doesn't exist
throw new ConfigException(ConfigException.FILE_NOT_FOUND);
} catch (Throwable t) {
// Another error occured, for example, the XML isn't correct
try {
throw t;
} catch (Throwable e) {e.printStackTrace();}
}
}
} |
Et une partie de mon SaxParser:
Code:
1 2 3 4 5 6 7 8 9 10
| Class<?> c = null;
Field[] f = null;
...
c=this.o.getClass();
f=c.getDeclaredFields();
...
range ann = f[index].getClass().getAnnotation(range.class);
if(ann!=null){
... // ne passe jamais par là...
} |