Note that there are some explanatory texts on larger screens.

plurals
  1. POjavassist not injecting annotation at existing field
    primarykey
    data
    text
    <p>I'm trying to inject JAXB annotation at runtime using Javassist. I have written following code:</p> <pre><code>public class AssistAnnotationInjector { public static void addAnnotationRunTime(String className, String fieldName) throws NotFoundException, CannotCompileException, IOException, ClassNotFoundException{ CtClass ctClass = ClassPool.getDefault().get(className); ClassFile ccFile = ctClass.getClassFile(); ConstPool constPool = ccFile.getConstPool(); AnnotationsAttribute attr = new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag); Annotation annot = new Annotation("javax.xml.bind.annotation.XmlTransient",constPool); attr.addAnnotation(annot); CtField field = ctClass.getDeclaredField(fieldName); field.getFieldInfo().addAttribute(attr); System.out.println(field.getAnnotation(XmlTransient.class)); ccFile.setVersionToJava5(); ctClass.writeFile(); } public static void main (String args[]) throws CannotCompileException, NotFoundException, IOException, SecurityException, NoSuchMethodException, ClassNotFoundException, JAXBException, NoSuchFieldException{ Person&lt;Student&gt; p = new Person&lt;Student&gt;(); p.setName("XYZ"); Student s = new Student(); s.setName("ABC"); s.setId("239423"); p.setPayload(s); addAnnotationRunTime("RuntimeAnnotation.Person", "name"); Field f = p.getClass().getDeclaredField("name"); System.out.println(f.getAnnotation(XmlTransient.class)); JAXBContext context = JAXBContext.newInstance(p.getClass()); Marshaller mr = context.createMarshaller(); mr.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); mr.marshal(p, System.out); } } </code></pre> <p>And Person.java class is:</p> <pre><code>@XmlRootElement(name="Person") @XmlAccessorType(XmlAccessType.FIELD) @XmlSeeAlso({Student.class}) public class Person &lt;T&gt;{ private T payload; private String name; public void setPayload(T payload){ this.payload = payload; } public T getPayload(){ return payload; } public void setName(String name){ this.name = name; } public String getName(){ return name; } } </code></pre> <p>In AssistAnnotationInjector.java, I am trying to add XmlTransient annotation to 'name' field. But the name field is still coming in marshalling output. Why is it so?</p> <p>PS: marshal output is :</p> <pre><code>@javax.xml.bind.annotation.XmlTransient null &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;Person&gt; &lt;payload xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="student"&gt; &lt;name&gt;ABC&lt;/name&gt; &lt;id&gt;239423&lt;/id&gt; &lt;/payload&gt; **&lt;name&gt;XYZ&lt;/name&gt;** &lt;/Person&gt; </code></pre> <p>name tag was not expected to present in output..</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload