<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">
<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>
                                <td>
                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="https://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>
                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px; -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
javassist not injecting annotation on an existing field
</h3>
<span style="margin-bottom: 10px;">
created by <a href="https://community.jboss.org/people/geekerebos">Ankur Gupta</a> in <i>Javassist</i> - <a href="https://community.jboss.org/message/801815#801815">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">
<div class="jive-rendered-content"><table><tbody><tr><td class="votecell" style=";"><p class="vote"> <span class="vote-count-post ">0</span> <a class="jive-link-anchor-small" rel="nofollow">down vote</a>  <a class="jive-link-external-small" href="http://stackoverflow.com/questions/15332399/javassist-not-injecting-annotation-at-existing-field#" rel="nofollow">favorite</a>    </p></td><td class="postcell" style=";"><p class="post-text" style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>I'm trying to inject JAXB annotation at runtime using Javassist. I have written following code:</p><pre class="jive-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<Student> p = new Person<Student>(); 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 class="jive-pre"><code>@XmlRootElement(name="Person") @XmlAccessorType(XmlAccessType.FIELD) @XmlSeeAlso({Student.class}) public class Person <T>{  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 class="jive-pre"><code><span>@javax.xml.bind.annotation.XmlTransient null <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Person> <payload xmlns:xsi="</span><a class="jive-link-external-small" href="http://www.w3.org/2001/XMLSchema-instance" rel="nofollow" target="_blank">http://www.w3.org/2001/XMLSchema-instance</a><span>" xsi:type="student"> <name>ABC</name> <id>239423</id> </payload> **<name>XYZ</name>** </Person> </span></code></pre><p>name tag was not expected to present in output..</p></td></tr></tbody></table></div>
<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
<p style="margin: 0;">Reply to this message by <a href="https://community.jboss.org/message/801815#801815">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in Javassist at <a href="https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2062">Community</a></p>
</div></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>