<?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>JavaBeat &#187; sraja</title>
	<atom:link href="http://www.javabeat.net/author/sraja/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javabeat.net</link>
	<description>Java Technology News</description>
	<lastBuildDate>Wed, 22 May 2013 01:42:58 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>New features in JDBC 3.0</title>
		<link>http://www.javabeat.net/2012/06/new-features-jdbc-3-0/</link>
		<comments>http://www.javabeat.net/2012/06/new-features-jdbc-3-0/#comments</comments>
		<pubDate>Mon, 18 Jun 2012 23:59:46 +0000</pubDate>
		<dc:creator>sraja</dc:creator>
				<category><![CDATA[JDBC]]></category>
		<category><![CDATA[JDBC 3.0]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/?p=4363</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>This article provides an introduction to the array of core new features available in JDBC 3.0. More specifically, the features &#8216;supporting save points&#8217;, &#8216;using parameter metadata&#8217;, &#8216;updating large objects&#8217; and &#8216;auto generated keys&#8217; are discussed. Wherever possible, to get a hang of it, relevant code samples have been provided in the respective sections. Download Source [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><a id="dd_start"></a><p>This article provides an introduction to the array of core new features available in <strong><em>JDBC 3.0</em></strong>. More specifically, the features &#8216;supporting save points&#8217;, &#8216;using parameter metadata&#8217;, &#8216;updating large objects&#8217; and &#8216;auto generated keys&#8217; are discussed. Wherever possible, to get a hang of it, relevant code samples have been provided in the respective sections.</p>
<blockquote><p><strong>Download Source Code:</strong> <a class="downloadlink" href="http://www.javabeat.net/downloads/Jdbc3-NewFeatures.zip" title=" downloaded 67 times" >New Features in JDBC 3.0 (67)</a></p></blockquote>
<h2>Defining savepoints</h2>
<p>It is possible to programmatically control the creation and releasing of <strong><em>save points</em></strong> through <strong>JDBC 3.0</strong>. But before doing it, one must check whether the underlying database supports the concepts of <strong>savepoints</strong>. Save points provide multiple-level-control of database commit or rollback operations within a single transaction. For example, if two unrelated database operations have to happen with a single transaction, these two save points can be created for the very purpose. Then based on various business conditions, commit/rollback can be done to any of these save points within the same transaction. To illustrate the usage, consider the following example,</p>
<pre class="brush: java; title: ; notranslate">
package net.javabeat.jdbc3.newfeatures.savepoint;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.Savepoint;

import net.javabeat.jdbc3.newfeatures.CommonUtils;

public class SavepointTest {
 public static void main(String[] args) throws Exception{
	Connection connection = CommonUtils.getConnection();
	DatabaseMetaData databaseMetadata = connection.getMetaData();
	if (databaseMetadata.supportsSavepoints()){
		System.out.println(&quot;Savepoints supported by
		       the target database&quot;);
		processTable(connection);
	}
	}

	private static void processTable(Connection connection)
		throws Exception{
		PreparedStatement pStatement = connection.prepareStatement(&quot;
		          INSERT INTO CUSTOMER VALUES (?, ?)&quot;);
		pStatement.setInt(1, 1);
		pStatement.setString(2, &quot;New Customer&quot;);
		pStatement.execute();
		Savepoint countrySavepoint =
			connection.setSavepoint(&quot;country&quot;);
		pStatement = connection.prepareStatement(&quot;
		          INSERT INTO COUNTRY VALUES (?, ?)&quot;);
		pStatement.setInt(1, 1);
		pStatement.setString(2, &quot;India&quot;);
		// Make this transaction to fail deliberately
		connection.rollback(countrySavepoint);
		connection.commit();
	}
}
</pre>
<p>As seen from the above example, the program checks whether save points are supported by the target database by querying methods available in DatabaseMetadata. Note for save points are created by calling the method setSavePoint() defined on the Connection object. A save point can be given a name so that at a later point of time, a transaction can be made to commit or rollback based on the name. In the example code, to illustrate the usage of save point, we deliberately rollback the operation related to customer table, whereas, the changes made to country table are committed.</p>
<h2>Parameter Metadata</h2>
<p>Construction of queries for statements such as prepared statements might involve specifying dynamic values through &#8216;?&#8217;. Previously, there was no support for the tools or for the applications to identify the parameter information embedded in the queries. Now, support has been added to retrieve the parameter information that is passed to prepared statements. Please refer the below code that illustrates the usage,</p>
<pre class="brush: java; title: ; notranslate">
package net.javabeat.jdbc3.newfeatures.pmd;

import java.sql.Connection;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;

import net.javabeat.jdbc3.newfeatures.CommonUtils;

public class PMDTest {
	public static void main(String[] args) throws Exception{
		Connection connection = CommonUtils.getConnection();
		PreparedStatement pStatement = connection.prepareStatement(
			&quot;INSERT INTO CUSTOMER VALUES (?, ?)&quot;);
		pStatement.setInt(1, 1);
		pStatement.setString(2, &quot;New Customer&quot;);
		ParameterMetaData parameterMetadata = pStatement.getParameterMetaData();
		int parameterCount = parameterMetadata.getParameterCount();
		for (int index = 1; index &lt;= parameterCount; index ++){
			String parameterClassName = parameterMetadata.getParameterClassName(index);
			int parameterMode = parameterMetadata.getParameterMode(index);
			String paramterTypeName = parameterMetadata.getParameterTypeName(index);
			System.out.println(parameterClassName + &quot;/&quot; + parameterMode + &quot;/&quot;
				+ &quot;/&quot; + paramterTypeName);
		}
	}
}
</pre>
<h2>Auto Generated Keys</h2>
<p>Execution of queries sometimes results in the automatic generation of fields or values for tables. The behavior is entirely dependent on the underlying database implementation. Previously, there was no standard mechanism for retrieving such information once the query is executed. Application developers are forced to use non-standard mechanisms for fetching information that happens outside the scope of query execution. For example, consider a table column containing a particular column, say &#8216;name&#8217;, and an equivalent upper column, let&#8217;s say &#8216;upper_name&#8217;. The value for the &#8216;upper_name&#8217; will be automatically populated as soon as the value is populated for the &#8216;name&#8217; column. This is one of the examples of <strong><em>auto generation feature</em></strong> and the newer specification supports this feature. Refer the code sample below,</p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Middle-Med-Rect */
google_ad_slot = "7805667846";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<pre class="brush: java; title: ; notranslate">
package net.javabeat.jdbc3.newfeatures.autogenkey;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.Statement;

import net.javabeat.jdbc3.newfeatures.CommonUtils;

public class AutoGeneratedKeyTest {
	public static void main(String[] args) throws Exception{
		Connection connection = CommonUtils.getConnection();
		DatabaseMetaData databaseMetadata = connection.getMetaData();
		if (databaseMetadata.supportsGetGeneratedKeys()){
			processAutoGeneratedKeys(connection);
		}
	}

	private static void processAutoGeneratedKeys(Connection connection)
		throws Exception{
		System.out.println(&quot;Auto generated keys can be retrived&quot;);
		Statement statement = connection.createStatement();
		statement.execute(&quot;INSERT INTO ADDRESSINFORMATION (ADDRESSDATA ) &quot; +
			&quot;VALUES ('Address Text')&quot;, Statement.RETURN_GENERATED_KEYS);
		ResultSet resultSet = statement.getGeneratedKeys();
		java.sql.ResultSetMetaData rsMetadata = resultSet.getMetaData();
		for (int index = 1; index &lt;= rsMetadata.getColumnCount(); index ++){
			while (resultSet.next()){
				String columnLabel = rsMetadata.getColumnName(index);
				System.out.println(resultSet.getObject(columnLabel));
			}
		}
	}

}
</pre>
<p>Note that, while executing the statement, the application has to provide indication to the underlying engine that, if any auto generation columns are applicable once the query is executed, those information have to be made available in the equivalent statement object. This is done through the method Statement.execute() where the second parameter specifies this option. Next, the method getGeneratedKeys() is added to the Statement object which returns a ResultSet containing the desired values. Because, this feature may or may not be supported by the database engine, the API supportsGeteGeneratedKeys() can be used to check the feature availability.</p>
<h2>Updating clob/blob objects</h2>
<p>The support to update clob objects is directly available on the Clob/Blob objects. The method <em>updateClob()</em> is added to the <em>ResultSet</em> object. Previously, there is no standard way to update large data objects. Note to ensure that this works and because the operation is defined on the ResultSet, the underlying ResultSet object should support updating the record. This is possible if the <em>ResultSet</em> is a flavor of <em>CONCUR_UPDATABLE</em>. Please refer the below code,</p>
<pre class="brush: java; title: ; notranslate">
package net.javabeat.jdbc3.newfeatures.clblupdate;

import java.io.StringReader;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import net.javabeat.jdbc3.newfeatures.CommonUtils;

public class ClobTest {

	public static void main(String[] args) throws Exception{
		Connection connection = CommonUtils.getConnection();
		int key = 1;
		insertClob(connection, key);
		readAndUpdateClob(connection, key);
	}

	private static void insertClob(Connection connection, int key)
		throws Exception{
		String sql = &quot;INSERT INTO CLOBTEST VALUES (?, ?)&quot;;
		PreparedStatement pStatement = connection.prepareStatement(sql);
		pStatement.setInt(1, key);
		StringReader reader = new StringReader(&quot;som big data&quot;);
		pStatement.setClob(2, reader);
		pStatement.execute();
	}

	private static void readAndUpdateClob(Connection connection, int key)
		throws Exception{
		String sql =  &quot;SELECT DATA FROM CLOBTEST WHERE ID = ?&quot;;
		PreparedStatement pStatement = connection.prepareStatement(
			sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
		pStatement.setInt(1, key);
		ResultSet resultSet = pStatement.executeQuery();
		if (resultSet.next()){
			Clob clobObject = resultSet.getClob(1);
			clobObject.setString(1, &quot;new big data&quot;);
			resultSet.updateClob(1, clobObject);
		}
	}
}
</pre>
<h2>Conclusion</h2>
<blockquote><p><strong>Download Source Code:</strong> <a class="downloadlink" href="http://www.javabeat.net/downloads/Jdbc3-NewFeatures.zip" title=" downloaded 67 times" >New Features in JDBC 3.0 (67)</a></p></blockquote>
<p>The new features in JDBC 3.0 have addressed most of the common problems that tools or developer community is encountering, thereby providing a unified way of solution. There are other minor updates done to JDBC 3.0, such as &#8216;configuring connection pools&#8217;, &#8216;adding of new data types as Boolean, Datalink, URL&#8217; etc.</p>
<div class='dd_outer'><div class='dd_inner'><div id='dd_ajax_float'><div class='dd_button_v'><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><fb:like href="http%3A%2F%2Fwww.javabeat.net%2Fauthor%2Fsraja%2Ffeed%2F" send="false" show_faces="false"  layout="box_count" width="50"  ></fb:like></div><div style='clear:left'></div><div class='dd_button_v'><script type='text/javascript' src='https://apis.google.com/js/plusone.js'></script><g:plusone size='tall' href='http://www.javabeat.net/author/sraja/feed/'></g:plusone></div><div style='clear:left'></div><div class='dd_button_v'><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.javabeat.net/author/sraja/feed/" data-count="vertical" data-text="" data-via="javabeat" ></a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div style='clear:left'></div><div class='dd_button_extra_v'><script type="text/javascript">jQuery(document).load(function(){ stLight.options({publisher:'bab47279-62c9-46af-addc-79fd1fe8fee0'}); });</script><div class="st_email_custom"><span id='dd_email_text'>email</span></div></div><div style='clear:left'></div><div class='dd_button_extra_v'><div id='dd_print_button'><span id='dd_print_text'><a href='javascript:window:print()'>print</a></span></div></div><div style='clear:left'></div></div></div></div><script type="text/javascript">var dd_offset_from_content = 44; var dd_top_offset_from_content = 0;</script><script type="text/javascript" src="http://www.javabeat.net/wp-content/plugins/digg-digg//js/diggdigg-floating-bar.js?ver=5.3.0"></script><div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2012/06/new-features-jdbc-3-0/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Introduction to Java Agents</title>
		<link>http://www.javabeat.net/2012/06/introduction-to-java-agents/</link>
		<comments>http://www.javabeat.net/2012/06/introduction-to-java-agents/#comments</comments>
		<pubDate>Sat, 16 Jun 2012 04:12:38 +0000</pubDate>
		<dc:creator>sraja</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Java 5.0]]></category>
		<category><![CDATA[Java Agents]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/?p=4168</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>In this article we will discuss about Java Agents. Java Agents are software components that provide instrumentation capabilities to an application. In the context of agents, instrumentation provides the capability of re-defining the content of class that is loaded at run-time. We will discuss this in more detail in the further sections. Download Source Code: [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><p>In this article we will discuss about <strong><em>Java Agents</em></strong>. <strong>Java Agents</strong> are software components that provide instrumentation capabilities to an application. In the context of agents, <strong><em>instrumentation</em></strong> provides the capability of re-defining the content of class that is loaded at run-time. We will discuss this in more detail in the further sections.</p>
<blockquote><p><strong>Download Source Code:</strong> <a class="downloadlink" href="http://www.javabeat.net/downloads/JavaAgent-Introduction.zip" title=" downloaded 360 times" >Java Agents (360)</a></p></blockquote>
<h2>Writing the first agent</h2>
<p>This section details on writing the first<strong> java agent</strong>. The purpose of the agent will be get as simple as possible as the aim of this section is just to get a bit introduced to the the usage of <strong>java agents</strong>. Coding a agent requires writing a java class that has the <em>premain()</em> method. Please refer the below code.</p>
<pre class="brush: java; title: ; notranslate">
package net.javabeat.articles.javaagent.test;
import java.lang.instrument.Instrumentation;
public class TestJavaAgent {
	public static void premain(String agentArgument,
                   Instrumentation instrumentation){
		System.out.println(&amp;quot;Test Java Agent&amp;quot;);
	}
}
</pre>
<p>Note that it is mandatory for agents to have the above method with the exact signature. Note that the above agent does nothing another that printing sample content to the console.</p>
<pre class="brush: java; title: ; notranslate">
Manifest-Version: 1.0
Premain-Class: net.javabeat.articles.javaagent.
                                test.TestJavaAgent
</pre>
<p>Agents are packaged as jar files and are made visible to the application using the option &#8216;javaagent&#8217;. Note that it is essential for the jar file to have the Premain-Class attribute that specifies the fully qualified name of the agent class containing the premain method. Now that we have written an agent, we have to make use of the agent. For making use of the agent, let us create a sample java class as follows. Note that nowhere in the code, we are invoking the agent. Instead while running the application, agents are specified as virtual machine arguments.</p>
<pre class="brush: java; title: ; notranslate">
package net.javabeat.articles.javaagent.test;

public class TestMain {

	public static void main(String[] args) {
		System.out.println(&amp;quot;Test Main Class&amp;quot;);
	}
}
</pre>
<ul>
<li>Create a jar file called &#8216;test-agent.jar&#8217; by packaging the class file &#8216;net.javabeat.articles.javaagent.test.TestJavaAgent&#8217; and the manifest file &#8216;MANIFEST.MF&#8217;</li>
<li>Make use of the command, to run the agent, java -javaagent:test-agent.jar net.javabeat.articles.javaagent.test.TestMain</li>
</ul>
<p>The above command will produce the below output,</p>
<pre class="brush: java; title: ; notranslate">
Test Java Agent
Test Main Class
</pre>
<p>Note that there are several things to be noted in the above section. First of all, the agent to be run is passed as virtual machine parameter to the application. This is done through the option &#8216;javaagent&#8217;, what follows after that is &#8216;:&#8217; followed by the path to the jar file. Note that agent classes are invoked first before the above application classes, this can be seen from the output produced by the sample program.</p>
<h2>Writing a simple Agent</h2>
<p>Now that we have familiarized with the several aspects involved in writing a simple agent, let us start adding some more functionality to the agent in this section. Consider the below code snippet representing the agent,</p>
<pre class="brush: java; title: ; notranslate">
package net.javabeat.articles.javaagent.simple;
import java.lang.instrument.Instrumentation;
public class SimpleAgent {
	public static void premain(String agentArguments,
                      Instrumentation instrumentation){
		System.out.println(&amp;quot;Simple Agent&amp;quot;);
		SimpleClassTransformer transformer =
                    new SimpleClassTransformer();
		instrumentation.addTransformer(transformer);
	}
}
</pre>
<p>Note that the very purpose of having agent is to provide instrumentation capabilities to the application – i.e. the capability to re-define the signature of the class files during run-time. The method premain() is passed with Instrumentation object that serves its very purpose. Using Instrumentation object it is possible to adding transformer objects. Transformer object does the real job of transforming (or re-defining) the content of class files at run-time. The above code defines a transformer object, whose declaration is given below,</p>
<pre class="brush: java; title: ; notranslate">
package net.javabeat.articles.javaagent.simple;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;
public class SimpleClassTransformer implements ClassFileTransformer{
	public byte[] transform(ClassLoader    loader,
            String              className,
            Class            classBeingRedefined,
            ProtectionDomain    protectionDomain,
            byte[]              classfileBuffer)
			throws IllegalClassFormatException {

		System.out.println(className);
		return classfileBuffer;
	}
}
</pre>
<p>Note that, a transformer object must implement the ClassFileTransformer interface and the abstract method transform needs to be overridden. This method will be called for every class that is loaded as part of the application. Note that the content of the class can be represented as byte array and this method can re-define the class content that returnss a different byte array (as denoted by the method signature). For simplicity, this method returns the original class byte array that is passed to it.</p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Middle-Med-Rect */
google_ad_slot = "7805667846";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<pre class="brush: java; title: ; notranslate">
Manifest-Version: 1.0
Premain-Class: net.javabeat.articles.javaagent.simple.SimpleAgent
</pre>
<p>Given above is the content of the manifest file for the agent. Note that for testing the agent, we need a sample application file which is given below. To illustrate the behavior of class loading, the application creates instances for Test classes.</p>
<pre class="brush: java; title: ; notranslate">
package net.javabeat.articles.javaagent.simple;
public class SimpleMain {
	public static void main(String[] args) {
		Test1 one = new Test1();
		Test2 two = new Test2();
		System.out.println(&amp;quot;Simple Main &amp;quot; + one + two);
	}
}
class Test1{}
class Test2{}
</pre>
<p>Running the above application along with the agent will produce the following output.</p>
<pre class="brush: java; title: ; notranslate">
Simple Agent
net/javabeat/articles/javaagent/simple/SimpleMain
net/javabeat/articles/javaagent/simple/Test1
net/javabeat/articles/javaagent/simple/Test2
Simple Main net.javabeat.articles.javaagent.simple.
Test1@61de33net.javabeat.articles.javaagent.simple.Test2@14318bb
</pre>
<h2>Agents for collecting statistical data</h2>
<p>In the last section, we have seen how to make use of Instrumentation object. We also saw how to manipulate Instrumentation object by adding Transformer objects. In this final section, we will see the real capability of agents for providing some statistical information about the methods being loaded for a particular class. For the purpose of illustration, let us expand the scope of the sample by provide a business class having empty business methods. Let us also assume that because the methods are for external usage, all the methods have to be public.</p>
<pre class="brush: java; title: ; notranslate">
package net.javabeat.articles.javaagent.statistics;
public class MyBusinessClass {
	public void bizMethod1(){
		System.out.println(&amp;quot;Biz Method 1&amp;quot;);
	}
	@SuppressWarnings(&amp;quot;unused&amp;quot;)
	private void bizMethod2(){
		System.out.println(&amp;quot;Biz Method 2&amp;quot;);
	}
}
</pre>
<p>The above code has the definition of the business class. Deliberately, the modifier for the method bizMethod2() is given as private. The agent class is given below. Note that, as illustrated in the last section, this class makes use of Instrumentation object to add transformer objects to it.</p>
<pre class="brush: java; title: ; notranslate">
package net.javabeat.articles.javaagent.statistics;
import java.lang.instrument.Instrumentation;
public class StatisticsAgent {
	public static void premain(String agentArguments,
                         Instrumentation instrumentation){
		StatisticsClassTransformer transformer =
                   new StatisticsClassTransformer();
		instrumentation.addTransformer(transformer);
	}
}
</pre>
<p>The flavor of the Transformer class that has the functionality of providing statistical information is given below. At this point, note that by the time when the agent is invoked, the class is not loaded. It is only during the class loading time, the agent is invoked. Hence reflection related APIs cannot be used at this point. For querying the method information for a particular class, the implementation makes use of ASM (byte code analysis library).</p>
<pre class="brush: java; title: ; notranslate">
package net.javabeat.articles.javaagent.statistics;

import java.io.IOException;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.security.ProtectionDomain;
import java.util.List;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodNode;

public class StatisticsClassTransformer implements ClassFileTransformer{

	public byte[] transform(ClassLoader    loader,
            String              className,
            Class            classBeingRedefined,
            ProtectionDomain    protectionDomain,
            byte[]              classfileBuffer)
			throws IllegalClassFormatException {

		System.out.println();
		System.out.println(&amp;quot;Processing class &amp;quot; + className);

		String normalizedClassName = className.replaceAll(&amp;quot;/&amp;quot;, &amp;quot;.&amp;quot;);

		ClassReader classReader = null;
		try {
			classReader = new ClassReader(normalizedClassName);
		} catch (IOException e1) {
			e1.printStackTrace();
		}

		ClassNode classNode = new ClassNode();
		classReader.accept(classNode, ClassReader.SKIP_DEBUG);

		@SuppressWarnings(&amp;quot;unchecked&amp;quot;)
		List allMethods = classNode.methods;
		for (MethodNode methodNode : allMethods){
			System.out.println(methodNode.name);
		}
		return classfileBuffer;
	}

	private static void processBizMethods(Class classObject) {
		if (MyBusinessClass.class.equals(classObject)){
			Method[] allMethods = classObject.getDeclaredMethods();
			for (Method aMethod : allMethods){
				System.out.println(aMethod.getName());
				int modifiers = aMethod.getModifiers();
				if (Modifier.isPrivate(modifiers)){
					System.out.println(&amp;quot;Method &amp;quot; +
                                        aMethod.getName() + &amp;quot; is private&amp;quot;);
				}
			}
		}
	}

	public static void main(String[] args) {
		processBizMethods(MyBusinessClass.class);
	}
}
</pre>
<p>The content of the manifest file for the above agent is given below.</p>
<pre class="brush: java; title: ; notranslate">
Manifest-Version: 1.0
Premain-Class: net.javabeat.articles.javaagent.statistics.StatisticsAgent
</pre>
<p>The content of the application that triggers the agent class is given below.</p>
<pre class="brush: java; title: ; notranslate">
package net.javabeat.articles.javaagent.statistics;
public class StatisticsMain {
	public static void main(String[] args) {
		MyBusinessClass object = new MyBusinessClass();
		System.out.println(&amp;quot;Biz Object &amp;quot; + object);
	}
}
</pre>
<p>The output of the application is given below,</p>
<pre class="brush: java; title: ; notranslate">
Processing class net/javabeat/articles/javaagent/statistics/StatisticsMain
&amp;lt;init&amp;gt;
main

Processing class net/javabeat/articles/javaagent/statistics/MyBusinessClass
&amp;lt;init&amp;gt;
bizMethod1
bizMethod2
Biz Object net.javabeat.articles.javaagent.statistics.MyBusinessClass@a59698
</pre>
<h2>Conclusion</h2>
<blockquote><p><strong>Download Source Code:</strong> <a class="downloadlink" href="http://www.javabeat.net/downloads/JavaAgent-Introduction.zip" title=" downloaded 360 times" >Java Agents (360)</a></p></blockquote>
<p>This article provides introductory concepts about <strong>Java agents</strong> which are introduced from 5.0. It provides a good starter about the usage of agents by providing lot of examples. Hope the readers will be benefited after reading this article.</p>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2012/06/introduction-to-java-agents/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
