<!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>&#160; <a class="jive-link-external-small" href="http://stackoverflow.com/questions/15332399/javassist-not-injecting-annotation-at-existing-field#" rel="nofollow">favorite</a>&#160;&#160;&#160; </p></td><td class="postcell" style=";"><p class="post-text" style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</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{&#160; 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(); }&#160; 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);&#160; addAnnotationRunTime("RuntimeAnnotation.Person", "name");&#160; Field f = p.getClass().getDeclaredField("name"); System.out.println(f.getAnnotation(XmlTransient.class));&#160; JAXBContext context = JAXBContext.newInstance(p.getClass());&#160; Marshaller mr = context.createMarshaller(); mr.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);&#160; 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 &lt;T&gt;{&#160; private T payload;&#160; private String name;&#160; public void setPayload(T payload){ this.payload = payload; }&#160; public T getPayload(){ return payload; }&#160; public void setName(String name){ this.name = name; }&#160; 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 &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;Person&gt; &lt;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"&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; </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>