<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Radek's Tech Blog &#187; bcel</title>
	<atom:link href="http://urbas.tk/index.php/tag/bcel/feed/" rel="self" type="application/rss+xml" />
	<link>http://urbas.tk</link>
	<description>Radoslaw H. Urbas homepage / blog</description>
	<lastBuildDate>Tue, 01 Jun 2010 20:31:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Jakarta BCEL</title>
		<link>http://urbas.tk/index.php/2008/02/28/jakarta-bcel/</link>
		<comments>http://urbas.tk/index.php/2008/02/28/jakarta-bcel/#comments</comments>
		<pubDate>Thu, 28 Feb 2008 09:45:09 +0000</pubDate>
		<dc:creator>Radoslaw Urbas</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[bcel]]></category>
		<category><![CDATA[bytecode]]></category>

		<guid isPermaLink="false">http://urbas.tk/?p=24</guid>
		<description><![CDATA[BCEL (Byte Code Engineering Library) gives a possibility to analyze, create and modify Java class files. It gives very nice API for manipulating inside bytecode. Here is an example of using BCEL. It adds instructions relevant to System.out.println(&#8220;About to call: METHOD_NAME METHOD_SIGNATURE&#8221;) before invocation of any method in the class. &#160; import java.io.IOException; import java.util.Iterator; [...]]]></description>
			<content:encoded><![CDATA[<p>BCEL (Byte Code Engineering Library) gives a possibility to analyze, create and modify Java class files. It gives very nice API for manipulating inside bytecode. Here is an example of using BCEL. It adds instructions relevant to System.out.println(&#8220;About to call: METHOD_NAME METHOD_SIGNATURE&#8221;) before invocation of any method in the class.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Iterator</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.bcel.Repository</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.bcel.classfile.ClassFormatException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.bcel.classfile.Method</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.bcel.generic.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Instruments byte-code by adding: &lt;code&gt;
 * System.out.println(&quot;About to call: METHOD_NAME METHOD_SIGNATURE&quot;);
 * &lt;/code&gt;
 * before method invocation.
 *
 * @author Radoslaw Urbas
 * @since Oct 27, 2007
 *
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Transform
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> ClassGen classGen <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     *
     * @param className name of class to instrument
     * @throws ClassNotFoundException
     * @throws ClassFormatException
     * @throws IOException
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> Transform<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> className<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">ClassNotFoundException</span>,
            ClassFormatException, <span style="color: #003399;">IOException</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">classGen</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ClassGen<span style="color: #009900;">&#40;</span><span style="color: #003399;">Repository</span>.<span style="color: #006633;">lookupClass</span><span style="color: #009900;">&#40;</span>className<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Instruments the class by displaying method information before invocation.
     *
     * @return instrumented ClassGen object
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> ClassGen instrument<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        ConstantPoolGen pgen <span style="color: #339933;">=</span> classGen.<span style="color: #006633;">getConstantPool</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> cname <span style="color: #339933;">=</span> classGen.<span style="color: #006633;">getClassName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Method</span> method <span style="color: #339933;">:</span> classGen.<span style="color: #006633;">getMethods</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            MethodGen methgen <span style="color: #339933;">=</span> instrumentMethod<span style="color: #009900;">&#40;</span>pgen, cname, method<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            classGen.<span style="color: #006633;">replaceMethod</span><span style="color: #009900;">&#40;</span>method, methgen.<span style="color: #006633;">getMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> classGen<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Instruments method by displaying information before method invocation.
     *
     * @param constantPoolGen constant pool for class containing method to
     *            instrument
     * @param className name of the class containing method to instrument
     * @param method method to instrument
     * @return instrumented MethodGen object
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> MethodGen instrumentMethod<span style="color: #009900;">&#40;</span>ConstantPoolGen constantPoolGen,
            <span style="color: #003399;">String</span> className, <span style="color: #003399;">Method</span> method<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        InstructionFactory instructionFactory <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> InstructionFactory<span style="color: #009900;">&#40;</span>classGen<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        MethodGen methgen <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MethodGen<span style="color: #009900;">&#40;</span>method, className, constantPoolGen<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        InstructionList originalInstructionList <span style="color: #339933;">=</span> methgen.<span style="color: #006633;">getInstructionList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">Iterator</span> instructionIterator <span style="color: #339933;">=</span> originalInstructionList.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>instructionIterator.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            InstructionHandle ih <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>InstructionHandle<span style="color: #009900;">&#41;</span> instructionIterator
                    .<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ih.<span style="color: #006633;">getInstruction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> InvokeInstruction<span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                originalInstructionList.<span style="color: #006633;">insert</span><span style="color: #009900;">&#40;</span>ih, instructionFactory
                        .<span style="color: #006633;">createPrintln</span><span style="color: #009900;">&#40;</span>getMessage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>InvokeInstruction<span style="color: #009900;">&#41;</span> ih
                                .<span style="color: #006633;">getInstruction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, constantPoolGen<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        methgen.<span style="color: #006633;">setMaxStack</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> methgen<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     *
     * @param invokeInstruction object representing invoke instruction
     * @param constantPoolGen
     * @return message based on method name and signature
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> getMessage<span style="color: #009900;">&#40;</span>InvokeInstruction invokeInstruction,
            ConstantPoolGen constantPoolGen<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">String</span> text <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;About to call: &quot;</span>
                <span style="color: #339933;">+</span> invokeInstruction.<span style="color: #006633;">getMethodName</span><span style="color: #009900;">&#40;</span>constantPoolGen<span style="color: #009900;">&#41;</span>
                <span style="color: #339933;">+</span> invokeInstruction.<span style="color: #006633;">getSignature</span><span style="color: #009900;">&#40;</span>constantPoolGen<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> text<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     *
     * @param args requires one parameter: class name. Class should be available
     *            in directory ./classes
     * @throws ClassNotFoundException
     * @throws IOException
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">ClassNotFoundException</span>,
            <span style="color: #003399;">IOException</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">String</span> className <span style="color: #339933;">=</span> args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        Transform transform <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Transform<span style="color: #009900;">&#40;</span>className<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        ClassGen classGen <span style="color: #339933;">=</span> transform.<span style="color: #006633;">instrument</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        classGen.<span style="color: #006633;">getJavaClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">dump</span><span style="color: #009900;">&#40;</span>className <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;.class&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Homepage: <a href="http://jakarta.apache.org/bcel/" target="_blank">http://jakarta.apache.org/bcel/ </a></p>
]]></content:encoded>
			<wfw:commentRss>http://urbas.tk/index.php/2008/02/28/jakarta-bcel/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
