Results 1 to 3 of 3
Thread: Java annotations question
- 01-10-2013, 03:57 PM #1
Member
- Join Date
- Jan 2013
- Posts
- 2
- Rep Power
- 0
Java annotations question
Hello,
I'm trying to make insertions to a lot of different fields in database using jax-ws created implementation based on given wsdl. Here is sample of generated class I'm expecting at input of one of methods:
Everything is working properly, but I have to type fields name every time I want to store it in DB. DB fields names are shown in XmlElement annotations and they're coming from wsdl, so I thought it would be nice to reconstruct them at run-time:Java Code:public class InputType1{ @XmlElement(name = "FIELD_DB_1", required = true) protected NewType fielddb1; @XmlElement(name = "FIELD_DB_2", required = true) protected String fielddb2; }
Unfortunately, 'annotations' table is empty. I read somewhere, that it is necessary to have second annotation, namely @Retention(RetentionPolicy.RUNTIME) which tells that annotation should be persisted during run-time. So, I tried to add it into my generated class, so it looks like:Java Code:Class cls = InputType1.class; for (java.lang.reflect.Field field : cls.getDeclaredFields()) { Class fieldCls = field.getClass(); Annotation[] annotations = fieldCls.getAnnotations(); for(Annotation annotation : annotations){ if(annotation instanceof javax.xml.bind.annotation.XmlElement){ XmlElement myAnnotation = (XmlElement) annotation; System.out.println("name: " + myAnnotation.name()); } }
but Eclipse is telling me that this annotation is disallowed for this location. What should I do?Java Code:public class InputType1{ @Retention(RetentionPolicy.RUNTIME) @XmlElement(name = "FIELD_DB_1", required = true) protected NewType fielddb1; @Retention(RetentionPolicy.RUNTIME) @XmlElement(name = "FIELD_DB_2", required = true) protected String fielddb2; }
Best regards!
Lukasz
- 01-11-2013, 01:41 AM #2
Member
- Join Date
- Dec 2011
- Posts
- 25
- Rep Power
- 0
Re: Java annotations question
you should add
right above your XmlElement annotationJava Code:@Retention(RetentionPolicy.RUNTIME)
so like:
that way, any XmlElement annotations are actually retained by the VM and you can read them.Java Code:@Retention(RetentionPolicy.RUNTIME) @interface XmlElement { String name(); boolean required() default false; // ... }Last edited by Potato; 01-11-2013 at 01:43 AM.
- 01-11-2013, 09:13 AM #3
Member
- Join Date
- Jan 2013
- Posts
- 2
- Rep Power
- 0
Re: Java annotations question
Thanks for your response,
I've solved it right now. An XmlElement is definied in javax.xml.bind.annotation namespace, so I suppose I can't change it. Fortunately, when I put mouse cursor over it, Eclipse showed me, that this annotation already has a valid retention value:
Eventually, problem was in reading annotations, I should call:Java Code:@Target(value={FIELD, METHOD, PARAMETER}) @Retention(value=RUNTIME)
instead ofJava Code:Annotation[] annotations = field.getAnnotations();
Just for sake of curiosity - is there a way to override annotations in existing class or somehow put them without modification existing code?Java Code:Class fieldCls = field.getClass(); Annotation[] annotations = fieldCls.getAnnotations();
Similar Threads
-
java annotations
By anbu.gn in forum New To JavaReplies: 1Last Post: 05-04-2011, 07:42 AM -
java annotations - small prog
By sebo in forum New To JavaReplies: 5Last Post: 10-24-2008, 05:48 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks