Results 1 to 3 of 3
  1. #1
    lukiasz is offline Member
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Default 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:

    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;
    }
    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:
    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());
    		}
    	}
    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:
    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;
    }
    but Eclipse is telling me that this annotation is disallowed for this location. What should I do?

    Best regards!

    Lukasz

  2. #2
    Potato is offline Member
    Join Date
    Dec 2011
    Posts
    25
    Rep Power
    0

    Default Re: Java annotations question

    you should add

    Java Code:
    @Retention(RetentionPolicy.RUNTIME)
    right above your XmlElement annotation

    so like:

    Java Code:
    @Retention(RetentionPolicy.RUNTIME)
    @interface XmlElement {
    String name();
    boolean required() default false;
    
    // ...
    
    }
    that way, any XmlElement annotations are actually retained by the VM and you can read them.
    Last edited by Potato; 01-11-2013 at 01:43 AM.

  3. #3
    lukiasz is offline Member
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Default 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:
    Java Code:
    @Target(value={FIELD, METHOD, PARAMETER})
    @Retention(value=RUNTIME)
    Eventually, problem was in reading annotations, I should call:
    Java Code:
    Annotation[] annotations = field.getAnnotations();
    instead of
    Java Code:
    Class fieldCls = field.getClass();
        Annotation[] annotations = fieldCls.getAnnotations();
    Just for sake of curiosity - is there a way to override annotations in existing class or somehow put them without modification existing code?

Similar Threads

  1. java annotations
    By anbu.gn in forum New To Java
    Replies: 1
    Last Post: 05-04-2011, 07:42 AM
  2. java annotations - small prog
    By sebo in forum New To Java
    Replies: 5
    Last Post: 10-24-2008, 05:48 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •