<!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;">
    Re: Grabbing an existing class's constructors?
</h3>
<span style="margin-bottom: 10px;">
    created by <a href="https://community.jboss.org/people/mdale">Melissa Dale</a> in <i>Javassist</i> - <a href="https://community.jboss.org/message/818976#818976">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p>Thank you for replying, You are right, I do understand the difference between class initialization and object initialization in theory, and what I had posted was an extremely sloppy attempt made in desperation (although I acknowledge it is entirely possible I am failing to keep them separate in practice</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Instead of using ".isEmpty()" I have used a try/catch, where it tries to create the coupling, and if it fails I assume it's because a ctclass's constructor is not empty. In the catch block I attempt to insert an empty class constructor on the ctclass object (so to overload the constructor) So it's like this: </p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><span class="n">String</span> <span class="n">from_class</span> <span class="o">=</span> <span class="n">from</span><span class="o">.</span><span class="na" style="color: #008080;">getName</span><span class="o">();</span></p><p>&#160;&#160;&#160; <span class="c1" style="color: #999988; font-style: italic;">//Persistent Coupling</span></p><p>&#160;&#160;&#160; <span class="k" style="color: #004080;">if</span><span class="o">(</span><span class="n">strength</span> <span class="o">==</span> <span class="sc" style="color: #bb8844;">'P'</span> <span class="o">||</span> <span class="n">strength</span> <span class="o">==</span> <span class="sc" style="color: #bb8844;">'p'</span><span class="o">){</span></p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="k" style="color: #004080;">try</span><span class="o">{</span> </p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="n">String</span> <span class="n">toBeAdded</span> <span class="o">=</span> <span class="n">from_class</span> <span class="o">+</span> <span class="n">varname</span><span class="o">+</span><span class="s" style="color: #bb8844;">" = new "</span> <span class="o">+</span> <span class="n">from_class</span><span class="o">+</span><span class="s" style="color: #bb8844;">"();"</span><span class="o">;</span></p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="n">CtField</span> <span class="n">f</span> <span class="o">=</span> <span class="n">CtField</span><span class="o">.</span><span class="na" style="color: #008080;">make</span><span class="o">(</span><span class="n">toBeAdded</span><span class="o">,</span> <span class="n">to</span><span class="o">);</span></p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="n">to</span><span class="o">.</span><span class="na" style="color: #008080;">addField</span><span class="o">(</span><span class="n">f</span><span class="o">);</span></p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="o">}</span></p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="c1" style="color: #999988; font-style: italic;">//Hopefully this will catch cases where constructors are not empty and insert a dummy instructor </span></p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="k" style="color: #004080;">catch</span><span class="o">(</span><span class="n">Exception</span> <span class="n">e</span><span class="o">){</span></p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="n">from</span><span class="o">.</span><span class="na" style="color: #008080;">makeClassInitializer</span><span class="o">();</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="n">String</span> <span class="n">toBeAdded</span> <span class="o">=</span> <span class="n">from_class</span> <span class="o">+</span> <span class="n">varname</span><span class="o">+</span><span class="s" style="color: #bb8844;">" = new "</span> <span class="o">+</span> <span class="n">from_class</span><span class="o">+</span><span class="s" style="color: #bb8844;">"();"</span><span class="o">;</span></p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="n">CtField</span> <span class="n">f</span> <span class="o">=</span> <span class="n">CtField</span><span class="o">.</span><span class="na" style="color: #008080;">make</span><span class="o">(</span><span class="n">toBeAdded</span><span class="o">,</span> <span class="n">to</span><span class="o">);</span></p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="n">to</span><span class="o">.</span><span class="na" style="color: #008080;">addField</span><span class="o">(</span><span class="n">f</span><span class="o">);</span></p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="o">}</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>I still get a 'Exception in thread "main" javassist.CannotCompileException: [source error] no such constructor'. And as I mentioned, ideally I would like to use the getConstructors() on a ctclass to get the array of constructor objects, but this is much fuzzier in my mind. Once I have the array of constructor objects, is there a way to determine the object types? If I can loop through the array I can make a coupling with dummy variables, becasue I don't really care about the program's execution after it it coupled, only that it will compile. Something <strong>roughly </strong>like:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>CtConstructor [] consts = new CtConstructor[];</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>ArrayList &lt;?&gt; results = new ArrayList&lt;?&gt;();</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>for (int i=0; i&lt;consts.length(); i++){</p><p>&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;if (consts[i].type == int){</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;results[i]=13;</p><p>&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;else if (consts[i].type == double){</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;results[i] = 13.5;</p><p>&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;}</p><p>&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;else if (consts[i].type == char){</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;results[i] = 'M';</p><p>&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;}</p><p>&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;else if (consts[i].type == float){</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;results[i] = 13.5;</p><p>&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;}</p><p>&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;else if (consts[i].type == double){</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;results[i] = 13.5;</p><p>&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;}</p><p>&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;else if (consts[i].type == boolean){</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;results[i] = 13.5;</p><p>&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;}</p><p>&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;else{</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;results[i]=null;</p><p>&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;}</p><p>&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;}//for</p><p>}//method</p></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/818976#818976">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>