<!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="http://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 3.15.0-GA, StackOverflowError with $sig in call to static method, can anyone confirm?
</h3>
<span style="margin-bottom: 10px;">
    created by <a href="http://community.jboss.org/people/wgayk">Wanja Gayk</a> in <i>Javassist</i> - <a href="http://community.jboss.org/message/643454#643454">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p>Cheers,</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>here's an Example of a possible bug I've found.</p><p>Could anyone confirm this or prove me an idiot, so I can either file a bug report or go kill myself in shame?</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Using $sig or $class as method parameter only seems to work on nonstatic methods, on static methods I get a StackOverflow Error.</p><p>Have I missed something in the dicumentation or is that a serious bug?</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>I could reproduce this with the latest JRE6 and the current JRE7:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Using $sig:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>instrumenting class: de/plush/brix/instrumentation/Main (995 bytes)</p><p>instrumenting class: javassist/runtime/Desc (3280 bytes)</p><p>Exception in thread "main" java.lang.StackOverflowError</p><p> at javassist.runtime.Desc.getParams(Desc.java)</p><p> at javassist.runtime.Desc.getParams(Desc.java)</p><p> at javassist.runtime.Desc.getParams(Desc.java)</p><p>[..]</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Using $class:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>instrumenting class: de/plush/brix/instrumentation/Main (995 bytes)</p><p>instrumenting class: javassist/runtime/Desc (3280 bytes)</p><p>Exception in thread "main" java.lang.StackOverflowError</p><p> at javassist.runtime.Desc.getClazz(Desc.java)</p><p> at javassist.runtime.Desc.getClazz(Desc.java)</p><p>[...]</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Compile the following code, create JAR-File, have a manifest like:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Manifest-Version: 1.0</p><p>Ant-Version: Apache Ant 1.8.2</p><p>Created-By: 1.7.0_02-b13 (Oracle Corporation)</p><p>Premain-Class: de.plush.brix.instrumentation.Agent</p><p>Class-Path: javassist.jar</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Start some Main.class with the jvm-arg:</p><p>-javaagent:${project_loc}\Agent.jar</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>And watch it happen.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>package de.plush.brix.instrumentation;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>public class Main {</p><p> void foo() {</p><p>&#160; System.out.println("Hello world");</p><p>&#160; }</p><p> public static void main(final String[] args) {</p><p>&#160; new Main().foo();</p><p> }</p><p>}</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>package de.plush.brix.instrumentation;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>import java.io.IOException;</p><p>import java.lang.instrument.ClassFileTransformer;</p><p>import java.lang.instrument.IllegalClassFormatException;</p><p>import java.lang.instrument.Instrumentation;</p><p>import java.lang.reflect.Modifier;</p><p>import java.security.ProtectionDomain;</p><p>import java.util.Arrays;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>import javassist.ByteArrayClassPath;</p><p>import javassist.CannotCompileException;</p><p>import javassist.ClassPool;</p><p>import javassist.CtClass;</p><p>import javassist.CtMethod;</p><p>import javassist.NotFoundException;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>public class Agent implements ClassFileTransformer {</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p> public Agent(final String agentArgs, final Instrumentation instrumentation) {</p><p>&#160; instrumentation.addTransformer(this);</p><p> }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p> public static void premain(final String agentArgs, final Instrumentation inst) {</p><p>&#160; new Agent(agentArgs, inst);</p><p> }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>public static void before(Class[] paramTypes) { //works</p><p> System.out.println("test passed");</p><p> }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>public static void before(Class paramTypes) { //works</p><p> System.out.println("test passed");</p><p> }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>public static void beforeStatic(Class[] paramTypes) { //stackOverflow</p><p>&#160; System.out.println("test passed");</p><p> }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>public static void beforeStatic2(Class paramTypes) { //stackOverflow</p><p> System.out.println("test passed");</p><p> }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p> @Override</p><p> public byte[] transform(final ClassLoader loader, final String className, final Class&lt;?&gt; classBeingRedefined, final ProtectionDomain protectionDomain,</p><p> final byte[] classfileBuffer) throws IllegalClassFormatException {</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160; System.out.println("instrumenting class: " + className + " (" + classfileBuffer.length + " bytes)");</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160; try {</p><p>&#160; final String javaClassName = className.replace('/', '.');</p><p>&#160; // turn bytecodes into a Javassist CtClass</p><p>&#160; final CtClass clazz = createClass(classfileBuffer, javaClassName);</p><p>&#160; // add output at the beginning and end of each method</p><p>&#160; for (final CtMethod method : clazz.getMethods()) {</p><p>&#160;&#160; if (!isInherited(javaClassName, method)) {</p><p>&#160;&#160;&#160;&#160; if (Modifier.isStatic(method.getModifiers())) {</p><p>&#160;&#160;&#160;&#160;&#160; method.insertBefore("{"+Agent.class.getName() + ".beforeStatic($sig);}"); // -- $sig kills it!, also $class</p><p>&#160;&#160;&#160;&#160;&#160; // method.insertBefore("{"+Agent.class.getName() + ".beforeStatic2($sig);}"); // -- kills it too!</p><p>&#160;&#160;&#160;&#160; } else {</p><p>&#160;&#160;&#160;&#160;&#160; method.insertBefore("{"+Agent.class.getName() + ".before($sig);}"); // -- $sig kills it!, also $class</p><p>&#160;&#160;&#160;&#160;&#160; // method.insertBefore("{"+Agent.class.getName() + ".before2($class);}"); // -- kills it too!</p><p>&#160;&#160;&#160; }</p><p>&#160;&#160; }</p><p>&#160; }</p><p>&#160; return clazz.toBytecode();</p><p> } catch (final NotFoundException e) {</p><p>&#160;&#160; throw new RuntimeException(e);</p><p> } catch (final CannotCompileException e) {</p><p>&#160;&#160; if (!e.getMessage().contains("no method body")) {</p><p>&#160;&#160;&#160; throw new RuntimeException(e);</p><p>&#160;&#160; }</p><p>&#160; // don't care</p><p> } catch (final IOException e) {</p><p>&#160; throw new RuntimeException(e);</p><p> }</p><p> return classfileBuffer;</p><p> }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>private boolean isInherited(final String javaClassName, final CtMethod method) {</p><p>&#160; return !method.getLongName().startsWith(javaClassName);</p><p> }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p> private CtClass createClass(final byte[] classfileBuffer, final String javassistClassName) throws NotFoundException {</p><p> final ClassPool cp = ClassPool.getDefault();</p><p> cp.insertClassPath(new ByteArrayClassPath(javassistClassName, classfileBuffer));</p><p> final CtClass cc = cp.get(javassistClassName);</p><p> return cc;</p><p> }</p><p>}</p></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Reply to this message by <a href="http://community.jboss.org/message/643454#643454">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in Javassist at <a href="http://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>