Core Java

Java Thread Synchronization – Print Even and Odd in Order

Java Thread Synchronization is one of the most important concepts that every Java Developer should know. Any developer could easily talk about Threads at a high level but synchronizing threads is a tricky part.In this article lets see the intrinsics of thread synchronization using an example.

The problem statement of the example is “There are two threads one which prints only Even Numbers and other that prints Odd Numbers until 10. Synchronize the threads such that the numbers are printed in sequence from 0 to 10.”

The solution is below. Let us break the code and learn the synchronization concepts of Java Threads.

Runnable Interface: The Thread Classes “EvenThread” and “OddThread” implement Runnable interface which tells JVM that these classes behave as Threads. The alternate way of creating a thread class is by extending Thread class, which is considered bad practice because Java does not allow “multiple inheritance” and hence limiting the custom Thread class from extending any other required class and moreover the developer is not going to extend the functionality of Thread class for which the inheritance is intended for.

Thread Creation: The thread class is instantiated by creating a new Thread and passing the Runnable object to it.

run method: The method from Runnable interface that needs to be implemented in the thread class. This method contains the logic of the core work that the thread does.

start method: This method moves the Thread from New state to Runnable state making is part of Runnable pool and based on the scheduling algorithm & CPU availability the thread starts running.

NOTE: The run method cannot be called directly expecting the Thread to start running or behave like the thread . Only invoking the start method will actually start the thread.

synchronized keyword:  This keyword is used to synchronize a method or a block of code. This synchronization ensures that the block of code is accessed only by one Thread at a time hence avoiding any inconsistent object state or wrong computations. The synchronization can be on the class level or object level. In our example, the synchronization is on an object.

lock object: An object is created to be used as  an explicit lock. The same instance of the lock object is passed to two threads( EvenThread & OddThread) that need to be synchronized. A lock is acquired by the Thread on the lock object using the synchronized keyword.

wait method:  The wait method ensures that the thread starts waiting, releasing the lock (monitor) and going to sleep until it is notified by another thread on the same lock to resume its activity. Here the even thread prints the even number and waits until notified by the Odd Thread that it has printed the odd number.

notify method: The notify thread wakes up a thread that is in sleeping state because it was put to wait on the lock (monitor).

volatile keyword: Volatile is access specifier that ensures the value of the variable is always updated to the main memory rather than the thread cache, guaranteeing that all the threads reads updated value. This volatile operations are atomic.

Click here for GitHub Source Code

package com.saranyapk.practice.corejava.thread;

public class ThreadPractice
{
	private static volatile Boolean isEvenStarted = false;

	public static void main( String args[] )
	{
		Object lock = new Object();

		Thread even = new Thread( new ThreadPractice().new EvenThread( lock ) );
		Thread odd = new Thread( new ThreadPractice().new OddThread( lock ) );

		even.start();
		odd.start();

	}

	class EvenThread implements Runnable
	{
		Object lock;

		public EvenThread( Object lock )
		{
			this.lock = lock;
		}

		@Override
		public void run()
		{
			synchronized (lock)
			{
				isEvenStarted = true;

				lock.notify();
			}

			for ( int i = 0; i < 10; i = i + 2 )
			{
				synchronized (lock)
				{
					try
					{
						lock.notify();

						System.out.println( i );

						if ( i < 10 )
						{
							lock.wait();
						}
					}
					catch ( InterruptedException e )
					{
						e.printStackTrace();
					}
				}
			}
		}

	}

	class OddThread implements Runnable
	{
		Object lock;

		public OddThread( Object lock )
		{
			this.lock = lock;

		}

		@Override
		public void run()
		{
			synchronized (lock)
			{
				while ( !isEvenStarted )
				{
					try
					{
						lock.wait();
					}
					catch ( InterruptedException e )
					{
						e.printStackTrace();
					}
				}
			}
			for ( int i = 1; i < 10; i = i + 2 )
			{
				synchronized (lock)
				{
					try
					{

						lock.notify();

						System.out.println( i );

						if ( i < 9 )
						{
							lock.wait();
						}
					}
					catch ( InterruptedException e )
					{
						e.printStackTrace();
					}
				}
			}
		}

	}
}

EFFECTIVE JAVA BY JOSHUA BLOCH – KEY POINTS -PART 4

This is the final article in this series of “Effective Java – Key Points”. Let’s jump into the points.

  1. Synchronization is required for reliable communication between threads as well as for mutual exclusion. Synchronization will not have effect unless both read and write are synchronized.
  2. If only inter-thread communication is required and not mutual exclusion, then volatile modifier can be used.
  3. Avoid excessive synchronization. Never call an alien method from a synchronized region, to avoid any deadlock or data corruption as an alien method are designed to be subclassed and the API will never know what the implementation of those methods is.
  4. Never use newCachedThreadPool in a highly loaded environment because it as good as creating as many threads as requested. It is ideal to use newFixedThreadPool which could be optimized based on the server capacity.
  5. Lazy initialization holder class idiom and Double check idiom should be used in case of lazy initialization of threaded environment.

 

Java Class Parser (javap)

All java programmers know that the java source file compiles into a class file which the JVM can interpret. This article concentrates on the class file. The class file contains bytecode and the bytecode is written in a fashion that the compiler and JVM are in agreement with. The fashion is the class file structure as defined by the JVM Specification as in the oracle documentation mentioned below
https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html

Caution: Please read through the contents in the link above before going forward.

I have written a program that will parse and disassemble the class file. The input to the program is the “.class” file. The output of the program will also contain method decoded into JVM instructions.

Program: Expand to see the Source Code. Also shared the source code on GitHub.

package classfileparser;

public class AccessFlag
{
public static int ACC_PUBLIC = 0x0001;
public static int ACC_FINAL = 0x0010;
public static int ACC_SUPER = 0x0020;
public static int ACC_INTERFACE = 0x0200;
public static int ACC_ABSTRACT = 0x0400;
public static int ACC_VOLATILE = 0x0040;
public static int ACC_TRANSIENT = 0x0080;
public static int ACC_PROTECTED = 0x0004;
public static int ACC_PRIVATE = 0x0002;
}
package classfileparser;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.Formatter;

public class AttributeReader
{
 private DataInputStream dis = null;
 private ConstantPoolLookUp constantPoolLookUp = null;
 private MethodDecoder methodDecoder = new MethodDecoder();

 public void setDis( DataInputStream dis )
 {
 this.dis = dis;
 methodDecoder.setDis( dis );
 }

 public void setConstantPoolLookUp( ConstantPoolLookUp constantPoolLookUp )
 {
 this.constantPoolLookUp = constantPoolLookUp;
 methodDecoder.setConstantPoolLookUp( constantPoolLookUp );
 }

 public void readAttributes() throws Exception
 {
 int attributeCount = ByteReader.read_u2( dis );
 for ( int i = 0; i < attributeCount; i++ )
 {
 String attributeName = constantPoolLookUp.lookUp( ByteReader.read_u2( dis ) );
 int length = ByteReader.read_u4( dis );
 System.out.println( "t" + attributeName + " " + length );
 read( attributeName, length );
 }
 }

 public void read( String attributeName, long length ) throws Exception
 {
 if ( Attributes.CODE.equalsIgnoreCase( attributeName ) )
 {
 readCodeAttribute();
 }
 else if ( Attributes.CONSTANT_VALUE.equalsIgnoreCase( attributeName ) )
 {
 readConstantValueAttribute();
 }
 else if ( Attributes.EXCEPTIONS.equalsIgnoreCase( attributeName ) )
 {
 readExceptionsAttribute();
 }
 else if ( Attributes.INNER_CLASS.equalsIgnoreCase( attributeName ) )
 {
 readInnerClassAttribute();
 }
 else if ( Attributes.LINE_NUMBER_TABLE.equalsIgnoreCase( attributeName ) )
 {
 readLineNumberTableAttribute();
 }
 else if ( Attributes.LOCAL_VARIABLE_TABLE.equalsIgnoreCase( attributeName ) )
 {
 readLocalVariableTableAttribute();
 }
 else if ( Attributes.LOCAL_VARIABLE_TYPE_TABLE.equalsIgnoreCase( attributeName ) )
 {
 readLocalVariableTypeTableAttribute();
 }
 else if ( Attributes.SOURCE_FILE.equalsIgnoreCase( attributeName ) )
 {
 readSourceFileAttribute();
 }
 else if ( Attributes.SYNTHETIC.equalsIgnoreCase( attributeName ) )
 {
 readSyntheticAttribute();
 }
 else if ( Attributes.SIGNATURE.equalsIgnoreCase( attributeName ) )
 {
 readSignatureAttribute();
 }
 else if ( Attributes.STACKMAPTABLE.equalsIgnoreCase( attributeName ) )
 {
 readStackMapTableAttribute();
 }
 else
 {
 throw new Exception( "Attribute Impl NOT found:" + attributeName );
 }

 }

 private void readLocalVariableTypeTableAttribute() throws Exception
 {
 int localVariableTypeTableLength = ByteReader.read_u2( dis );
 if ( localVariableTypeTableLength > 0 )
 {
 Formatter formatter = new Formatter();
 System.out.println( "tt" + formatter.format( "%-10s %-10s %-30s %-50s %-10s", "Start_PC", "Length", "Name", "Signature", "Index" ) );

 }
 for ( int i = 0; i < localVariableTypeTableLength; i++ )
 {
 Formatter formatter = new Formatter();
 System.out.println( "tt" + formatter.format( "%-10s %-10s %-30s %-50s %-10s", ByteReader.read_u2( dis ), ByteReader.read_u2( dis ), constantPoolLookUp.lookUp( ByteReader.read_u2( dis ) ), constantPoolLookUp.lookUp( ByteReader.read_u2( dis ) ), ByteReader.read_u2( dis ) ) );
 }

 }

 private void readStackMapTableAttribute() throws Exception
 {
 int numberOfEntries = ByteReader.read_u2( dis );
 for ( int i = 0; i < numberOfEntries; i++ )
 {
 int frame_type = ByteReader.read_u1( dis );
 if ( frame_type >= 0 && frame_type <= 63 )
 {
 System.out.println( "ttFrame Type:" + "SAME" );
 }
 else if ( frame_type >= 64 && frame_type <= 127 )
 {
 System.out.println( "ttFrame Type:" + "SAME_LOCALS_1_STACK_ITEM" );
 readVerificationInfoType();
 }
 else if ( frame_type == 247 )
 {
 System.out.println( "ttFrame Type:" + "SAME_LOCALS_1_STACK_ITEM_EXTENDED" );
 System.out.println( "ttOffset Delta: " + ByteReader.read_u2( dis ) );
 readVerificationInfoType();
 }
 else if ( frame_type >= 248 && frame_type <= 250 )
 {
 System.out.println( "ttFrame Type:" + "CHOP" );
 System.out.println( "ttOffset Delta: " + ByteReader.read_u2( dis ) );
 }
 else if ( frame_type == 251 )
 {
 System.out.println( "ttFrame Type:" + "SAME_FRAME_EXTENDED" );
 System.out.println( "ttOffset Delta: " + ByteReader.read_u2( dis ) );
 }
 else if ( frame_type >= 252 && frame_type <= 254 )
 {
 System.out.println( "ttFrame Type:" + "APPEND" );
 System.out.println( "ttOffset Delta: " + ByteReader.read_u2( dis ) );
 for ( int j = 0; j < ( frame_type - 251 ); j++ )
 {
 readVerificationInfoType();
 }
 }
 else if ( frame_type == 255 )
 {
 System.out.println( "ttFrame Type:" + "FULL_FRAME" );
 System.out.println( "ttOffset Delta: " + ByteReader.read_u2( dis ) );
 int numberOfLocals = ByteReader.read_u2( dis );
 System.out.println( "ttlocals[" );
 for ( int k = 0; k < numberOfLocals; k++ )
 {
 readVerificationInfoType();
 }
 System.out.println( "tt]" );
 System.out.println( "ttstacks[" );
 int numberOfStackItems = ByteReader.read_u2( dis );
 for ( int l = 0; l < numberOfStackItems; l++ )
 {
 readVerificationInfoType();
 }
 System.out.println( "tt]" );
 }

 }
 }

 private void readVerificationInfoType() throws Exception
 {
 int verification_type_info = ByteReader.read_u1( dis );
 switch ( verification_type_info )
 {
 case 0:
 System.out.println( "ttVerification_Type_Info:" + " ITEM_Top" );
 break;
 case 1:
 System.out.println( "ttVerification_Type_Info:" + " ITEM_Integer" );
 break;
 case 2:
 System.out.println( "ttVerification_Type_Info:" + "ITEM_Float" );
 break;
 case 3:
 System.out.println( "ttVerification_Type_Info:" + " ITEM_Double" );
 break;
 case 4:
 System.out.println( "ttVerification_Type_Info:" + " ITEM_Long" );
 break;
 case 5:
 System.out.println( "ttVerification_Type_Info:" + " ITEM_Null" );
 break;
 case 6:
 System.out.println( "ttVerification_Type_Info:" + " ITEM_UninitializedThis" );
 break;
 case 7:
 System.out.println( "ttVerification_Type_Info:" + " ITEM_Object:" + constantPoolLookUp.lookUp( ByteReader.read_u2( dis ) ) );
 break;
 case 8:
 System.out.println( "ttVerification_Type_Info:" + " ITEM_Uninitialized" );
 break;
 }
 }

 private void readSignatureAttribute() throws IOException, Exception
 {
 System.out.println( "t" + constantPoolLookUp.lookUp( ByteReader.read_u2( dis ) ) );

 }

 private void readSyntheticAttribute()
 {
 //Do nothing because the attritube name will be "synthetic"

 }

 private void readSourceFileAttribute() throws Exception
 {
 System.out.println( "ttSource File Index:" + constantPoolLookUp.lookUp( ByteReader.read_u2( dis ) ) );
 }

 private void readLocalVariableTableAttribute() throws Exception
 {
 int localVariableTableLength = ByteReader.read_u2( dis );
 if ( localVariableTableLength > 0 )
 {
 Formatter formatter = new Formatter();
 System.out.println( "tt" + formatter.format( "%-10s %-10s %-30s %-50s %-10s", "Start_PC", "Length", "Name", "Description", "Index" ) );

 }
 for ( int i = 0; i < localVariableTableLength; i++ )
 {
 Formatter formatter = new Formatter();
 System.out.println( "tt" + formatter.format( "%-10s %-10s %-30s %-50s %-10s", ByteReader.read_u2( dis ), ByteReader.read_u2( dis ), constantPoolLookUp.lookUp( ByteReader.read_u2( dis ) ), constantPoolLookUp.lookUp( ByteReader.read_u2( dis ) ), ByteReader.read_u2( dis ) ) );
 }
 }

 private void readLineNumberTableAttribute() throws IOException
 {
 int lineNumberTableLength = ByteReader.read_u2( dis );
 if ( lineNumberTableLength > 0 )
 {
 Formatter formatter = new Formatter();
 System.out.println( "tt" + formatter.format( "%-10s %-10s", "Start_PC", "LineNumber" ) );

 }
 for ( int i = 0; i < lineNumberTableLength; i++ )
 {
 Formatter formatter = new Formatter();
 System.out.println( "tt" + formatter.format( "%-10s %-10s", ByteReader.read_u2( dis ), ByteReader.read_u2( dis ) ) );
 }

 }

 private void readInnerClassAttribute() throws Exception
 {
 int noOfClasses = ByteReader.read_u2(dis);
 for (int i = 0; i < noOfClasses; i++)
 {
 int innerClassInfoIndex = ByteReader.read_u2(dis);
 if(innerClassInfoIndex != 0)
 {
 System.out.println("ttt" + constantPoolLookUp.lookUp(innerClassInfoIndex));
 }
 
 int outerClassInfoIndex = ByteReader.read_u2(dis);
 if(outerClassInfoIndex != 0)
 {
 System.out.println("ttt" + constantPoolLookUp.lookUp(outerClassInfoIndex));
 }
 
 int innernameindex = ByteReader.read_u2(dis);
 if(innernameindex != 0)
 {
 System.out.println("ttt" + constantPoolLookUp.lookUp(innernameindex));
 }
 
 ClassFileParser.readAccessFlag();
 }

 }

 private void readExceptionsAttribute() throws Exception
 {
 int noOfExceptions = ByteReader.read_u2( dis );
 System.out.println( "ttExceptions" );
 for ( int i = 0; i < noOfExceptions; i++ )
 {
 System.out.println( "tt" + constantPoolLookUp.lookUp( ByteReader.read_u2( dis ) ) );
 }

 }

 private void readConstantValueAttribute() throws IOException, Exception
 {
 System.out.println( "Constant Value:" + constantPoolLookUp.lookUp( ByteReader.read_u2( dis ) ) );
 }

 private void readCodeAttribute() throws Exception
 {
 System.out.println( "ttMax Stack:" + ByteReader.read_u2( dis ) );
 System.out.println( "ttMax Locals:" + ByteReader.read_u2( dis ) );
 methodDecoder.readMethodCode();
 readException();
 readAttributes();
 }

 private void readException() throws Exception
 {
 int exceptionTableLength = ByteReader.read_u2( dis );
 System.out.println( "ttException:" );
 if ( exceptionTableLength > 0 )
 {
 Formatter formatter = new Formatter();
 System.out.println( "tt" + formatter.format( "%-10s %-10s %-10s %-10s", "Start_PC", "End_PC", "Handler_PC", "Catch_Type" ) );

 }
 for ( int i = 0; i < exceptionTableLength; i++ )
 {
 int catchType = ByteReader.read_u2( dis );
 Formatter formatter = new Formatter();
 System.out.println( "tt" + formatter.format( "%-10s %-10s %-10s %-10s", ByteReader.read_u2( dis ), ByteReader.read_u2( dis ), ByteReader.read_u2( dis ), ( ( catchType > 0 ) ? constantPoolLookUp.lookUp( catchType ) : "any" ) ) );
 }
 }

}
package classfileparser;

public class Attributes
{
 public static final String CODE = "Code";
 public static final String CONSTANT_VALUE = "ConstantValue";
 public static final String EXCEPTIONS = "Exceptions";
 public static final String INNER_CLASS = "InnerClasses";
 public static final String LINE_NUMBER_TABLE = "LineNumberTable";
 public static final String LOCAL_VARIABLE_TABLE = "LocalVariableTable";
 public static final String LOCAL_VARIABLE_TYPE_TABLE = "LocalVariableTypeTable";
 public static final String SOURCE_FILE = "SourceFile";
 public static final String SYNTHETIC = "Synthetic";
 public static final String SIGNATURE = "Signature";
 public static final String STACKMAPTABLE = "StackMapTable";
}

package classfileparser;

import java.io.DataInputStream;
import java.io.IOException;

public class ByteReader
{
 public static int read_u1( DataInputStream dis ) throws IOException
 {
 return dis.readUnsignedByte();
 }

 public static int read_u2( DataInputStream dis ) throws IOException
 {
 return dis.readUnsignedShort();
 }

 public static int read_u4( DataInputStream dis ) throws IOException
 {
 return dis.readInt();
 }

 public static long read_u8( DataInputStream dis ) throws IOException
 {

 return dis.readLong();
 }
 
 public static int read_s1( DataInputStream dis ) throws IOException
 {
 return dis.readByte();
 }
 
 public static int read_s2( DataInputStream dis ) throws IOException
 {
 return dis.readShort();
 }
 
 public static int read_s4( DataInputStream dis ) throws IOException
 {
 return dis.readInt();
 }

}

package classfileparser;

import java.io.*;

public class ClassFileParser
{

 public static DataInputStream dis = null;
 public static final String TAG_SEPARATOR = "#";

 private static ConstantPoolLookUp constantPoolLookUp = new ConstantPoolLookUp();
 private static AttributeReader attributeReader = new AttributeReader();
 private static ConstantPoolReader constantPoolReader = new ConstantPoolReader();

 private static String filePath = "";

 public static void main( String[] args ) throws Exception
 {
 try
 {
 
 //readCompleteClass();

 filePath = args[0];
 
 if( !validateFile() )
 {
 return;
 }
 
 dis = new DataInputStream( new FileInputStream( new File( filePath ) ) );

 attributeReader.setDis( dis );
 attributeReader.setConstantPoolLookUp( constantPoolLookUp );

 constantPoolReader.setDis( dis );
 constantPoolReader.setConstantPoolLookUp( constantPoolLookUp );

 System.out.println( "Magic Number:" + Integer.toHexString( ByteReader.read_u4( dis ) ) );

 System.out.println( "Minor Version:" + ByteReader.read_u2( dis ) );

 System.out.println( "Major Version:" + ByteReader.read_u2( dis ) );

 constantPoolReader.readConstantPool();

 System.out.println( "Class Access Info:" + readAccessFlag() );

 readThisClass();

 readSuperClass();

 readInterfaces();

 readFields();

 readMethods();

 attributeReader.readAttributes();

 }
 catch ( FileNotFoundException e )
 {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 catch ( IOException e )
 {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }

 }
 
 private static boolean validateFile()
 {
 if(!filePath.toUpperCase().endsWith(".CLASS"))
 {
 System.out.println("Please input a .class file to this parser.");
 
 return false;
 }
 
 return true;
 }

 @SuppressWarnings("unused")
 private static void readCompleteClass() throws IOException
 {
 FileInputStream fis = new FileInputStream( new File( filePath ) );

 byte[] b = new byte[10];

 while ( fis.read( b ) != -1 )
 {
 for ( byte bt : b )
 {
 System.out.print( (char)bt );
 }
 }

 fis.close();
 }

 private static void readFields() throws Exception
 {
 int fieldCount = ByteReader.read_u2( dis );
 System.out.println( "Fields:" );
 for ( int i = 0; i < fieldCount; i++ )
 {
 System.out.println( readAccessFlag() + " " + constantPoolLookUp.lookUp( ByteReader.read_u2( dis ) ) + " " + constantPoolLookUp.lookUp( ByteReader.read_u2( dis ) ) );
 attributeReader.readAttributes();
 System.out.println( "n" );
 }

 }

 private static void readMethods() throws Exception
 {
 int methodCount = ByteReader.read_u2( dis );
 System.out.println( "Methods:" );
 for ( int i = 0; i < methodCount; i++ )
 {
 System.out.println( readAccessFlag() + " " + constantPoolLookUp.lookUp( ByteReader.read_u2( dis ) ) + " " + constantPoolLookUp.lookUp( ByteReader.read_u2( dis ) ) );
 attributeReader.readAttributes();
 System.out.println( "n" );
 }

 }

 private static void readInterfaces() throws Exception
 {
 int interfacesCount = ByteReader.read_u2( dis );
 String interfaces = "";
 for ( int i = 0; i < interfacesCount; i++ )
 {
 interfaces += " " + constantPoolLookUp.lookUp( ByteReader.read_u2( dis ) );
 }
 System.out.println( "Interfaces Name:" + interfaces );
 }

 private static void readSuperClass() throws Exception
 {
 int superClass = ByteReader.read_u2( dis );

 System.out.println( "Super Name: " + constantPoolLookUp.lookUp( superClass ) );
 }

 private static void readThisClass() throws Exception
 {
 int thisClass = ByteReader.read_u2( dis );

 System.out.println( "Class Name: " + constantPoolLookUp.lookUp( thisClass ) );
 }

 public static String readAccessFlag() throws IOException
 {
 int accessFlag = ByteReader.read_u2( dis );

 String accessInfo = "";

 if ( ( accessFlag & AccessFlag.ACC_PUBLIC ) != 0 )
 {
 accessInfo += " public ";
 }
 if ( ( accessFlag & AccessFlag.ACC_ABSTRACT ) != 0 )
 {
 accessInfo += " abstract ";
 }
 if ( ( accessFlag & AccessFlag.ACC_FINAL ) != 0 )
 {
 accessInfo += " final ";
 }
 if ( ( accessFlag & AccessFlag.ACC_INTERFACE ) != 0 )
 {
 accessInfo += " interface ";
 }
 if ( ( accessFlag & AccessFlag.ACC_SUPER ) != 0 )
 {
 accessInfo += " Invoke Special ";
 }
 if ( ( accessFlag & AccessFlag.ACC_PRIVATE ) != 0 )
 {
 accessInfo += " private ";
 }
 if ( ( accessFlag & AccessFlag.ACC_PROTECTED ) != 0 )
 {
 accessInfo += " protected ";
 }
 if ( ( accessFlag & AccessFlag.ACC_VOLATILE ) != 0 )
 {
 accessInfo += " volatile ";
 }
 if ( ( accessFlag & AccessFlag.ACC_TRANSIENT ) != 0 )
 {
 accessInfo += " transient ";
 }

 return accessInfo;
 }

}
package classfileparser;

import java.util.HashMap;
import java.util.Map;

public class ConstantPoolLookUp
{
 public static Map< Integer, ConstantPoolValue > constantPool = new HashMap< Integer, ConstantPoolValue >();

 public void put( int tag, ConstantPoolType type, Object value )
 {
 ConstantPoolValue constantPoolValue = new ConstantPoolValue();
 constantPoolValue.type = type;
 constantPoolValue.value = value;
 constantPool.put( tag, constantPoolValue );
 }

 public String lookUp( Integer tag ) throws Exception
 {
 ConstantPoolValue cpValue = constantPool.get( tag );

 if ( cpValue == null )
 {
 //throw new Exception( "There value is null for index:" + tag );
 System.out.println("unable to look up:" + tag);
 return "";
 }
 if ( cpValue.type.equals( ConstantPoolType.CONSTANT_Utf8 ) )
 {
 return (String)cpValue.value;
 }
 else if ( cpValue.type.equals( ConstantPoolType.CONSTANT_Integer ) )
 {
 return ( (Integer)cpValue.value ).toString();
 }
 else if ( cpValue.type.equals( ConstantPoolType.CONSTANT_Long ) )
 {
 return ( (Long)cpValue.value ).toString();
 }
 else if ( cpValue.type.equals( ConstantPoolType.CONSTANT_Double ) )
 {
 return ( (Double)cpValue.value ).toString();
 }
 else if ( cpValue.type.equals( ConstantPoolType.CONSTANT_String ) )
 {
 return lookUp( (Integer)cpValue.value );
 }
 else if ( cpValue.type.equals( ConstantPoolType.CONSTANT_Class ) )
 {
 return lookUp( (Integer)cpValue.value );
 }
 else if ( cpValue.type.equals( ConstantPoolType.CONSTANT_Methodref ) || cpValue.type.equals( ConstantPoolType.CONSTANT_InterfaceMethodref ) || cpValue.type.equals( ConstantPoolType.CONSTANT_NameAndType ) || cpValue.type.equals( ConstantPoolType.CONSTANT_Fieldref ) )

 {
 String value = ( (String)cpValue.value );
 String[] tokens = value.split( ClassFileParser.TAG_SEPARATOR );
 return lookUp( new Integer( tokens[0] ) ) + " " + lookUp( new Integer( tokens[1] ) );
 }

 return " ";
 }

 class ConstantPoolValue
 {
 public ConstantPoolType type;
 public Object value;
 }
}

package classfileparser;

import java.io.DataInputStream;
import java.io.IOException;

public class ConstantPoolReader
{
 public static int i;

 private DataInputStream dis = null;

 private ConstantPoolLookUp constantPoolLookUp = null;

 public void setDis( DataInputStream dis )
 {
 this.dis = dis;
 }

 public void setConstantPoolLookUp( ConstantPoolLookUp constantPoolLookUp )
 {
 this.constantPoolLookUp = constantPoolLookUp;
 }

 public void readConstantPool() throws Exception
 {
 int constantPoolCount = ByteReader.read_u2( dis );

 System.out.println( "Constant Pool Count:" + constantPoolCount );

 for ( i = 1; i < constantPoolCount; i++ )
 {
 int tag = dis.readUnsignedByte();

 switch ( tag )
 {
 case 1:
 //System.out.println( "CONSTANT_Utf8" );
 readConstantUTF8Info();
 break;
 case 3:
 //System.out.println( "CONSTANT_Integer" );
 readConstantIntegerInfo();
 break;
 case 4:
 //System.out.println( "CONSTANT_Float" );
 readConstantFloatInfo();
 break;
 case 5:
 //System.out.println( "CONSTANT_Long" );
 readConstantLongInfo();
 i++;
 break;
 case 6:
 //System.out.println( "CONSTANT_Double" );
 readConstantDoubleInfo();
 i++;
 break;
 case 7:
 //System.out.println( "CONSTANT_Class" );
 readConstantClassInfo();
 break;
 case 8:
 //System.out.println( "CONSTANT_String" );
 readConstantStringInfo();
 break;
 case 9:
 //System.out.println( "CONSTANT_Fieldref" );
 readConstantFieldRefInfo();
 break;
 case 10:
 //System.out.println( "CONSTANT_Methodref" );
 readConstantMethodRefInfo();
 break;
 case 11:
 //System.out.println( "CONSTANT_InterfaceMethodref" );
 readConstantIntfMethodRefInfo();
 break;
 case 12:
 //System.out.println( "CONSTANT_NameAndType" );
 readConstantNameandTypeInfo();
 break;

 default:
 throw new Exception( "Unknown Tag:" + tag );
 }
 }
 }

 private void readConstantUTF8Info() throws IOException
 {
 //Read UTF directly reads the length
 String utf = dis.readUTF();
 constantPoolLookUp.put( i, ConstantPoolType.CONSTANT_Utf8, utf );
 System.out.println( i + " " + utf );

 }

 private void readConstantIntegerInfo() throws IOException
 {
 Integer const_integer = dis.readInt();
 constantPoolLookUp.put( i, ConstantPoolType.CONSTANT_Integer, const_integer );
 System.out.println( i + " " + const_integer );

 }

 private void readConstantFloatInfo() throws IOException
 {
 Float const_float = dis.readFloat();
 constantPoolLookUp.put( i, ConstantPoolType.CONSTANT_Float, const_float );
 System.out.println( i + " " + const_float );

 }

 private void readConstantLongInfo() throws IOException
 {
 Long const_long = dis.readLong();
 constantPoolLookUp.put( i, ConstantPoolType.CONSTANT_Long, const_long );
 System.out.println( i + " " + const_long );
 //These values consume two entries in constant_pool table. Hence the index of the next item will be two more than the current entry

 }

 private void readConstantDoubleInfo() throws IOException
 {
 Double const_double = dis.readDouble();
 constantPoolLookUp.put( i, ConstantPoolType.CONSTANT_Long, const_double );
 System.out.println( i + " " + const_double );

 }

 private void readConstantClassInfo() throws IOException
 {
 int class_info = ByteReader.read_u2( dis );
 constantPoolLookUp.put( i, ConstantPoolType.CONSTANT_Class, class_info );
 System.out.println( i + " " + class_info );

 }

 private void readConstantStringInfo() throws IOException
 {
 Integer string_info = ByteReader.read_u2( dis );
 constantPoolLookUp.put( i, ConstantPoolType.CONSTANT_String, string_info );
 System.out.println( i + " " + string_info.toString() );

 }

 private void readConstantFieldRefInfo() throws IOException
 {
 String field_ref_info = ByteReader.read_u2( dis ) + ClassFileParser.TAG_SEPARATOR + ByteReader.read_u2( dis );
 constantPoolLookUp.put( i, ConstantPoolType.CONSTANT_Fieldref, field_ref_info );
 System.out.println( i + " " + field_ref_info );

 }

 private void readConstantMethodRefInfo() throws IOException
 {
 String method_ref_info = ByteReader.read_u2( dis ) + ClassFileParser.TAG_SEPARATOR + ByteReader.read_u2( dis );
 constantPoolLookUp.put( i, ConstantPoolType.CONSTANT_Methodref, method_ref_info );
 System.out.println( i + " " + method_ref_info );
 }

 private void readConstantIntfMethodRefInfo() throws IOException
 {
 String intf_method_ref_info = ByteReader.read_u2( dis ) + ClassFileParser.TAG_SEPARATOR + ByteReader.read_u2( dis );
 constantPoolLookUp.put( i, ConstantPoolType.CONSTANT_InterfaceMethodref, intf_method_ref_info );
 System.out.println( i + " " + intf_method_ref_info );

 }

 private void readConstantNameandTypeInfo() throws IOException
 {
 String name_type_info = ByteReader.read_u2( dis ) + ClassFileParser.TAG_SEPARATOR + ByteReader.read_u2( dis );
 constantPoolLookUp.put( i, ConstantPoolType.CONSTANT_NameAndType, name_type_info );
 System.out.println( i + " " + name_type_info );
 }

}
package classfileparser;

public enum ConstantPoolType
{

 CONSTANT_Utf8( 1 ), CONSTANT_Integer( 3 ), CONSTANT_Float( 4 ), CONSTANT_Long( 5 ), CONSTANT_Double( 6 ), CONSTANT_Class( 7 ), CONSTANT_String( 8 ), CONSTANT_Fieldref( 9 ), CONSTANT_Methodref( 10 ), CONSTANT_InterfaceMethodref( 11 ), CONSTANT_NameAndType( 12 );

 int tag;

 ConstantPoolType( int tag )
 {
 this.tag = tag;
 }

}
package classfileparser;

import java.io.DataInputStream;
import java.io.IOException;

public class MethodDecoder
{
 private DataInputStream dis = null;
 private ConstantPoolLookUp constantPoolLookUp = null;
 private int pc;

 public void setDis(DataInputStream dis)
 {
 this.dis = dis;
 }

 public void setConstantPoolLookUp(ConstantPoolLookUp constantPoolLookUp)
 {
 this.constantPoolLookUp = constantPoolLookUp;
 }

 public void readMethodCode() throws Exception
 {
 int codeLength = ByteReader.read_u4(dis);
 for (pc = 0; pc < codeLength; pc++)
 {
 lookUpOpcode(ByteReader.read_u1(dis));
 }
 }

 private void lookUpOpcode(Integer opcode) throws IOException, Exception
 {
 switch (opcode)
 {
 case Opcodes.NOP:

 System.out.println("tt" + pc + " " + "nop");
 break;

 case Opcodes.AALOAD:

 System.out.println("tt" + pc + " " + "aaload");
 break;

 case Opcodes.AASTORE:

 System.out.println("tt" + pc + " " + "aastore");
 break;

 case Opcodes.ACONST_NULL:

 System.out.println("tt" + pc + " " + "aconst_null");
 break;

 case Opcodes.ALOAD:

 System.out.println("tt" + pc + " " + "aload " + getSimpleIndex() + "in Variable Array");
 break;

 case Opcodes.aload_0:

 System.out.println("tt" + pc + " " + "aload_0");
 break;

 case Opcodes.aload_1:

 System.out.println("tt" + pc + " " + "aload_1");
 break;

 case Opcodes.aload_2:

 System.out.println("tt" + pc + " " + "aload_2");
 break;

 case Opcodes.aload_3:

 System.out.println("tt" + pc + " " + "aload_3");
 break;

 case Opcodes.ANEWARRAY:

 System.out.println("tt" + pc + " " + "anewarray " + constantPoolLookUp.lookUp(getDoubleIndex()));
 break;

 case Opcodes.ARETURN:

 System.out.println("tt" + pc + " " + "areturn");
 break;

 case Opcodes.ARRAYLENGTH:

 System.out.println("tt" + pc + " " + "arraylength");
 break;

 case Opcodes.ASTORE:

 System.out.println("tt" + pc + " " + "astore " + getSimpleIndex() + "in Variable Array");
 break;

 case Opcodes.astore_0:

 System.out.println("tt" + pc + " " + "astore_0");
 break;

 case Opcodes.astore_1:

 System.out.println("tt" + pc + " " + "astore_1");
 break;

 case Opcodes.astore_2:

 System.out.println("tt" + pc + " " + "astore_2");
 break;

 case Opcodes.astore_3:

 System.out.println("tt" + pc + " " + "astore_3");
 break;

 case Opcodes.ATHROW:

 System.out.println("tt" + pc + " " + "athrow");
 break;

 case Opcodes.BALOAD:

 System.out.println("tt" + pc + " " + "baload");
 break;

 case Opcodes.BASTORE:

 System.out.println("tt" + pc + " " + "bastore");
 break;

 case Opcodes.BIPUSH:

 System.out.println("tt" + pc + " " + "bipush " + ByteReader.read_s1(dis));
 pc++;
 break;

 case Opcodes.BREAKPOINT:

 System.out.println("tt" + pc + " " + "breakpoint ");
 break;

 case Opcodes.CALOAD:

 System.out.println("tt" + pc + " " + "caload");
 break;

 case Opcodes.CASTORE:

 System.out.println("tt" + pc + " " + "castore");
 break;

 case Opcodes.CHECKCAST:

 System.out.println("tt" + pc + " " + "checkcast " + constantPoolLookUp.lookUp(ByteReader.read_u2(dis)));
 pc++;
 pc++;
 break;

 case Opcodes.D2F:

 System.out.println("tt" + pc + " " + "d2f");
 break;

 case Opcodes.D2I:

 System.out.println("tt" + pc + " " + "d2i");
 break;

 case Opcodes.D2L:

 System.out.println("tt" + pc + " " + "d2l");
 break;

 case Opcodes.DADD:

 System.out.println("tt" + pc + " " + "dadd");
 break;

 case Opcodes.DALOAD:

 System.out.println("tt" + pc + " " + "daload");
 break;

 case Opcodes.DASTORE:

 System.out.println("tt" + pc + " " + "dastore");
 break;

 case Opcodes.DCMPG:

 System.out.println("tt" + pc + " " + "dcmpg");
 break;

 case Opcodes.DCMPL:

 System.out.println("tt" + pc + " " + "dcmpl");
 break;

 case Opcodes.DCONST_0:

 System.out.println("tt" + pc + " " + "dconst_0");
 break;

 case Opcodes.DCONST_1:

 System.out.println("tt" + pc + " " + "dconst_1");
 break;

 case Opcodes.DDIV:

 System.out.println("tt" + pc + " " + "ddiv");
 break;

 case Opcodes.DLOAD:

 System.out.println("tt" + pc + " " + "dload" + getSimpleIndex() + " in variable array.");
 break;

 case Opcodes.dload_0:

 System.out.println("tt" + pc + " " + "dload_0");
 break;

 case Opcodes.dload_1:

 System.out.println("tt" + pc + " " + "dload_1");
 break;

 case Opcodes.dload_2:

 System.out.println("tt" + pc + " " + "dload_2");
 break;

 case Opcodes.dload_3:

 System.out.println("tt" + pc + " " + "dload_3");
 break;

 case Opcodes.DMUL:

 System.out.println("tt" + pc + " " + "dmul");
 break;

 case Opcodes.DNEG:

 System.out.println("tt" + pc + " " + "dneg");
 break;

 case Opcodes.DREM:

 System.out.println("tt" + pc + " " + "drem");
 break;

 case Opcodes.DRETURN:

 System.out.println("tt" + pc + " " + "dreturn");
 break;

 case Opcodes.DSTORE:

 System.out.println("tt" + pc + " " + "dstore " + getSimpleIndex() + " in variable array");
 break;

 case Opcodes.dstore_0:

 System.out.println("tt" + pc + " " + "dstore_0");
 break;

 case Opcodes.dstore_1:

 System.out.println("tt" + pc + " " + "dstore_1");
 break;

 case Opcodes.dstore_2:

 System.out.println("tt" + pc + " " + "dstore_2");
 break;

 case Opcodes.dstore_3:

 System.out.println("tt" + pc + " " + "dstore_3");
 break;

 case Opcodes.DSUB:

 System.out.println("tt" + pc + " " + "dsub");
 break;

 case Opcodes.DUP:

 System.out.println("tt" + pc + " " + "dup");
 break;

 case Opcodes.DUP_X1:

 System.out.println("tt" + pc + " " + "dup_x1");
 break;

 case Opcodes.DUP_X2:

 System.out.println("tt" + pc + " " + "dup_x2");
 break;
 case Opcodes.DUP2:

 System.out.println("tt" + pc + " " + "dup2");
 break;
 case Opcodes.DUP2_X1:

 System.out.println("tt" + pc + " " + "dup2_x1");
 break;
 case Opcodes.DUP2_X2:

 System.out.println("tt" + pc + " " + "dup2_x2");
 break;
 case Opcodes.F2D:

 System.out.println("tt" + pc + " " + "f2d");
 break;
 case Opcodes.F2I:

 System.out.println("tt" + pc + " " + "f2i");
 break;
 case Opcodes.F2L:

 System.out.println("tt" + pc + " " + "f2l");
 break;
 case Opcodes.FADD:

 System.out.println("tt" + pc + " " + "fadd");
 break;
 case Opcodes.FALOAD:

 System.out.println("tt" + pc + " " + "faload");
 break;
 case Opcodes.FASTORE:

 System.out.println("tt" + pc + " " + "fastore");
 break;
 case Opcodes.FCMPG:

 System.out.println("tt" + pc + " " + "fcmpg");
 break;
 case Opcodes.FCMPL:

 System.out.println("tt" + pc + " " + "fcmpl");
 break;
 case Opcodes.FCONST_0:

 System.out.println("tt" + pc + " " + "fconst_0");
 break;
 case Opcodes.FCONST_1:

 System.out.println("tt" + pc + " " + "fconst_1");
 break;
 case Opcodes.FDIV:

 System.out.println("tt" + pc + " " + "fdiv");
 break;
 case Opcodes.FLOAD:

 System.out.println("tt" + pc + " " + "fload " + getSimpleIndex() + " in variable array");
 break;
 case Opcodes.fload_0:

 System.out.println("tt" + pc + " " + "fload_0");
 break;
 case Opcodes.fload_1:

 System.out.println("tt" + pc + " " + "fload_1");
 break;
 case Opcodes.fload_2:

 System.out.println("tt" + pc + " " + "fload_2");
 break;
 case Opcodes.fload_3:

 System.out.println("tt" + pc + " " + "fload_3");
 break;
 case Opcodes.FMUL:

 System.out.println("tt" + pc + " " + "fmul");
 break;
 case Opcodes.FNEG:

 System.out.println("tt" + pc + " " + "fneg");
 break;
 case Opcodes.FREM:

 System.out.println("tt" + pc + " " + "frem");
 break;
 case Opcodes.FRETURN:

 System.out.println("tt" + pc + " " + "freturn");
 break;

 case Opcodes.FSTORE:

 System.out.println("tt" + pc + " " + "fstore " + getSimpleIndex() + " in variable array");
 break;

 case Opcodes.fstore_0:

 System.out.println("tt" + pc + " " + "fstore_0");
 break;
 case Opcodes.fstore_1:

 System.out.println("tt" + pc + " " + "fstore_1");
 break;
 case Opcodes.fstore_2:

 System.out.println("tt" + pc + " " + "fstore_2");
 break;
 case Opcodes.fstore_3:

 System.out.println("tt" + pc + " " + "fstore_3");
 break;
 case Opcodes.FSUB:

 System.out.println("tt" + pc + " " + "fsub");
 break;
 case Opcodes.GETFIELD:

 System.out.println("tt" + pc + " " + "getfield " + constantPoolLookUp.lookUp(ByteReader.read_u2(dis)));
 pc++;
 pc++;
 break;
 case Opcodes.GETSTATIC:

 System.out.println("tt" + pc + " " + "getstatic " + constantPoolLookUp.lookUp(ByteReader.read_u2(dis)));
 pc++;
 pc++;
 break;
 case Opcodes.GOTO:

 int pcL = pc;

 System.out.println("tt" + pc + " " + "goto " + (pcL + ByteReader.read_u2(dis)));
 pc++;
 pc++;
 break;
 case Opcodes.goto_w:

 pcL = pc;

 System.out.println("tt" + pc + " " + "goto_w " + (pcL + ByteReader.read_s4(dis)));
 pc++;
 pc++;
 pc++;
 pc++;
 break;
 case Opcodes.I2B:

 System.out.println("tt" + pc + " " + "i2b");
 break;
 case Opcodes.I2C:

 System.out.println("tt" + pc + " " + "i2c");
 break;
 case Opcodes.I2D:

 System.out.println("tt" + pc + " " + "i2d");
 break;
 case Opcodes.I2F:

 System.out.println("tt" + pc + " " + "i2f");
 break;
 case Opcodes.I2L:

 System.out.println("tt" + pc + " " + "i2l");
 break;
 case Opcodes.I2S:

 System.out.println("tt" + pc + " " + "i2s");
 break;
 case Opcodes.IADD:

 System.out.println("tt" + pc + " " + "iadd");
 break;
 case Opcodes.IALOAD:

 System.out.println("tt" + pc + " " + "iaload");
 break;

 case Opcodes.IAND:

 System.out.println("tt" + pc + " " + "iand");
 break;
 case Opcodes.IASTORE:

 System.out.println("tt" + pc + " " + "iastore");
 break;
 case Opcodes.ICONST_M1:

 System.out.println("tt" + pc + " " + "iconst_m1");
 break;
 case Opcodes.ICONST_0:

 System.out.println("tt" + pc + " " + "iconst_0");
 break;
 case Opcodes.ICONST_1:

 System.out.println("tt" + pc + " " + "iconst_1");
 break;
 case Opcodes.ICONST_2:

 System.out.println("tt" + pc + " " + "iconst_2");
 break;
 case Opcodes.ICONST_3:

 System.out.println("tt" + pc + " " + "iconst_3");
 break;
 case Opcodes.ICONST_4:

 System.out.println("tt" + pc + " " + "iconst_4");
 break;
 case Opcodes.ICONST_5:

 System.out.println("tt" + pc + " " + "iconst_5");
 break;
 case Opcodes.IDIV:

 System.out.println("tt" + pc + " " + "idiv");
 break;
 case Opcodes.IF_ACMPEQ:

 System.out.println("tt" + pc + " " + "if_acmpeq " + getBranchInstPC());
 break;
 case Opcodes.IF_ACMPNE:

 System.out.println("tt" + pc + " " + "if_acmpne " + getBranchInstPC());
 break;
 case Opcodes.IF_ICMPEQ:

 System.out.println("tt" + pc + " " + "if_icmpeq " + getBranchInstPC());
 break;
 case Opcodes.IF_ICMPGE:

 System.out.println("tt" + pc + " " + "if_icmpge " + getBranchInstPC());
 break;
 case Opcodes.IF_ICMPGT:

 System.out.println("tt" + pc + " " + "if_icmpgt " + getBranchInstPC());
 break;
 case Opcodes.IF_ICMPLE:

 System.out.println("tt" + pc + " " + "if_icmple " + getBranchInstPC());
 break;
 case Opcodes.IF_ICMPLT:

 System.out.println("tt" + pc + " " + "if_icmplt " + getBranchInstPC());
 break;
 case Opcodes.IF_ICMPNE:

 System.out.println("tt" + pc + " " + "if_icmpne " + getBranchInstPC());
 break;
 case Opcodes.IFNE:

 System.out.println("tt" + pc + " " + "ifne " + getBranchInstPC());
 break;
 case Opcodes.IFEQ:

 System.out.println("tt" + pc + " " + "ifeq " + getBranchInstPC());
 break;
 case Opcodes.IFGE:

 System.out.println("tt" + pc + " " + "ifge " + getBranchInstPC());
 break;
 case Opcodes.IFGT:

 System.out.println("tt" + pc + " " + "ifgt " + getBranchInstPC());
 break;
 case Opcodes.IFLE:

 System.out.println("tt" + pc + " " + "ifle " + getBranchInstPC());
 break;
 case Opcodes.IFLT:

 System.out.println("tt" + pc + " " + "iflt " + getBranchInstPC());
 break;
 case Opcodes.IFNONNULL:

 System.out.println("tt" + pc + " " + "ifnotnull " + getBranchInstPC());
 break;
 case Opcodes.IFNULL:

 System.out.println("tt" + pc + " " + "ifnull " + getBranchInstPC());
 break;
 case Opcodes.IINC:

 System.out.println("tt" + pc + " " + "iinc " + getSimpleIndex() + " in variable array by " + ByteReader.read_s1(dis));
 pc++;
 break;
 case Opcodes.ILOAD:

 System.out.println("tt" + pc + " " + "iload " + getSimpleIndex() + " in variable array");
 break;
 case Opcodes.iload_0:

 System.out.println("tt" + pc + " " + "iload_0");
 break;
 case Opcodes.iload_1:

 System.out.println("tt" + pc + " " + "iload_1");
 break;
 case Opcodes.iload_2:

 System.out.println("tt" + pc + " " + "iload_2");
 break;
 case Opcodes.iload_3:

 System.out.println("tt" + pc + " " + "iload_3");
 break;
 case Opcodes.IMUL:

 System.out.println("tt" + pc + " " + "imul");
 break;
 case Opcodes.INEG:

 System.out.println("tt" + pc + " " + "ineg");
 break;
 case Opcodes.INSTANCEOF:

 System.out.println("tt" + pc + " " + "instanceof " + constantPoolLookUp.lookUp(ByteReader.read_u2(dis)));
 pc++;
 pc++;
 break;
 case Opcodes.INVOKEDYNAMIC:

 System.out.println("tt" + pc + " " + "invokedynamic " + constantPoolLookUp.lookUp(ByteReader.read_u2(dis)) + " Count:" + ByteReader.read_u1(dis) + " " + ByteReader.read_u1(dis));
 pc++;
 pc++;
 pc++;
 pc++;
 break;
 case Opcodes.INVOKEINTERFACE:

 System.out.println("tt" + pc + " " + "invokeinterface " + constantPoolLookUp.lookUp(ByteReader.read_u2(dis)) + " Count:" + ByteReader.read_u1(dis) + " " + ByteReader.read_u1(dis));
 pc++;
 pc++;
 pc++;
 pc++;
 break;

 case Opcodes.INVOKESPECIAL:

 System.out.println("tt" + pc + " " + "invokespecial " + constantPoolLookUp.lookUp(ByteReader.read_u2(dis)));
 pc++;
 pc++;
 break;
 case Opcodes.INVOKESTATIC:

 System.out.println("tt" + pc + " " + "invokestatic " + constantPoolLookUp.lookUp(ByteReader.read_u2(dis)));
 pc++;
 pc++;
 break;
 case Opcodes.INVOKEVIRTUAL:

 System.out.println("tt" + pc + " " + "invokevirtual " + constantPoolLookUp.lookUp(ByteReader.read_u2(dis)));
 pc++;
 pc++;
 break;
 case Opcodes.IOR:

 System.out.println("tt" + pc + " " + "ior");
 break;
 case Opcodes.IREM:

 System.out.println("tt" + pc + " " + "irem");
 break;
 case Opcodes.IRETURN:

 System.out.println("tt" + pc + " " + "ireturn");
 break;
 case Opcodes.ISHL:

 System.out.println("tt" + pc + " " + "ishl");
 break;
 case Opcodes.ISHR:

 System.out.println("tt" + pc + " " + "ishr");
 break;
 case Opcodes.ISTORE:

 System.out.println("tt" + pc + " " + "istore " + constantPoolLookUp.lookUp(getSimpleIndex()));
 break;

 case Opcodes.istore_0:

 System.out.println("tt" + pc + " " + "istore_0");
 break;
 case Opcodes.istore_1:

 System.out.println("tt" + pc + " " + "istore_1");
 break;
 case Opcodes.istore_2:

 System.out.println("tt" + pc + " " + "istore_2");
 break;
 case Opcodes.istore_3:

 System.out.println("tt" + pc + " " + "istore_3");
 break;
 case Opcodes.ISUB:

 System.out.println("tt" + pc + " " + "isub");
 break;
 case Opcodes.IUSHR:

 System.out.println("tt" + pc + " " + "iushr");
 break;
 case Opcodes.IXOR:

 System.out.println("tt" + pc + " " + "ixor");
 break;
 case Opcodes.JSR:

 System.out.println("tt" + pc + " " + "jsr " + getBranchInstPC());
 break;
 case Opcodes.JSR_W:

 pcL = pc;
 System.out.println("tt" + pc + " " + "jsr_w " + (pcL + ByteReader.read_s4(dis)));
 pc++;
 pc++;
 pc++;
 pc++;
 break;
 case Opcodes.L2D:

 System.out.println("tt" + pc + " " + "l2d");
 break;
 case Opcodes.L2F:

 System.out.println("tt" + pc + " " + "l2f");
 break;
 case Opcodes.L2I:

 System.out.println("tt" + pc + " " + "l2i");
 break;
 case Opcodes.LADD:

 System.out.println("tt" + pc + " " + "ladd");
 break;
 case Opcodes.LALOAD:

 System.out.println("tt" + pc + " " + "laload");
 break;
 case Opcodes.LAND:

 System.out.println("tt" + pc + " " + "land");
 break;
 case Opcodes.LASTORE:

 System.out.println("tt" + pc + " " + "lastore");
 break;
 case Opcodes.LCMP:

 System.out.println("tt" + pc + " " + "lcmp");
 break;
 case Opcodes.LCONST_0:

 System.out.println("tt" + pc + " " + "lconst_0");
 break;
 case Opcodes.LCONST_1:

 System.out.println("tt" + pc + " " + "lconst_1");
 break;
 case Opcodes.LDC:

 System.out.println("tt" + pc + " " + "ldc " + constantPoolLookUp.lookUp(ByteReader.read_u1(dis)));
 pc++;
 break;
 case Opcodes.LDC_W:

 System.out.println("tt" + pc + " " + "ldc_w " + constantPoolLookUp.lookUp(getConstantPoolIndex()));
 break;
 case Opcodes.LDC2_W:

 System.out.println("tt" + pc + " " + "ldc2_w " + constantPoolLookUp.lookUp(getConstantPoolIndex()));
 break;
 case Opcodes.LDIV:

 System.out.println("tt" + pc + " " + "ldiv");
 break;
 case Opcodes.LLOAD:

 System.out.println("tt" + pc + " " + "lload " + constantPoolLookUp.lookUp(ByteReader.read_u1(dis)));
 pc++;
 break;
 case Opcodes.lload_0:

 System.out.println("tt" + pc + " " + "lload_0");
 break;
 case Opcodes.lload_1:

 System.out.println("tt" + pc + " " + "lload_1");
 break;
 case Opcodes.lload_2:

 System.out.println("tt" + pc + " " + "lload_2");
 break;
 case Opcodes.lload_3:

 System.out.println("tt" + pc + " " + "lload_3");
 break;
 case Opcodes.LMUL:

 System.out.println("tt" + pc + " " + "lmul");
 break;
 case Opcodes.LNEG:

 System.out.println("tt" + pc + " " + "lneg");
 break;
 case Opcodes.LOOKUPSWITCH:

 System.out.println("tt" + pc + " " + "lookupswitch");

 pad();

 System.out.println("ttt" + "default:" + " " + getIndexWithFourBytes());

 int npairs = getIndexWithFourBytes();

 for (int i = 0; i < npairs; i++)
 {
 System.out.println("ttt" + ByteReader.read_u4(dis) + ":" + ByteReader.read_u4(dis));
 }

 break;
 case Opcodes.LOR:

 System.out.println("tt" + pc + " " + "lor");
 break;
 case Opcodes.LREM:

 System.out.println("tt" + pc + " " + "lrem");
 break;
 case Opcodes.LRETURN:

 System.out.println("tt" + pc + " " + "lreturn");
 break;
 case Opcodes.LSHL:

 System.out.println("tt" + pc + " " + "lshl");
 break;
 case Opcodes.LSHR:

 System.out.println("tt" + pc + " " + "lshr");
 break;
 case Opcodes.LSTORE:

 System.out.println("tt" + pc + " " + "lstore " + getSimpleIndex() + " in variable array");
 break;
 case Opcodes.lstore_0:

 System.out.println("tt" + pc + " " + "lstore_0");
 break;
 case Opcodes.lstore_1:

 System.out.println("tt" + pc + " " + "lstore_1");
 break;
 case Opcodes.lstore_2:

 System.out.println("tt" + pc + " " + "lstore_2");
 break;
 case Opcodes.lstore_3:

 System.out.println("tt" + pc + " " + "lstore_3");
 break;
 case Opcodes.LSUB:

 System.out.println("tt" + pc + " " + "lsub");
 break;
 case Opcodes.LUSHR:

 System.out.println("tt" + pc + " " + "lushr");
 break;
 case Opcodes.LXOR:

 System.out.println("tt" + pc + " " + "lxor");
 break;
 case Opcodes.MONITORENTER:

 System.out.println("tt" + pc + " " + "monitorenter ");
 break;
 case Opcodes.MONITOREXIT:

 System.out.println("tt" + pc + " " + "monitorexit ");
 break;
 case Opcodes.MULTIANEWARRAY:

 System.out.println("tt" + pc + " " + "multinewarray " + constantPoolLookUp.lookUp(ByteReader.read_u2(dis)));
 pc++;
 pc++;
 System.out.println("tttDimension: " + ByteReader.read_u1(dis));
 pc++;
 break;
 case Opcodes.NEW:

 System.out.println("tt" + pc + " " + "new " + constantPoolLookUp.lookUp(ByteReader.read_u2(dis)));
 pc++;
 pc++;
 break;
 case Opcodes.NEWARRAY:

 System.out.println("tt" + pc + " " + "newarray " + lookupAType());
 break;
 case Opcodes.POP:

 System.out.println("tt" + pc + " " + "pop");
 break;
 case Opcodes.POP2:

 System.out.println("tt" + pc + " " + "pop2");
 break;
 case Opcodes.PUTFIELD:

 System.out.println("tt" + pc + " " + "putfield " + constantPoolLookUp.lookUp(getDoubleIndex()));
 break;
 case Opcodes.PUTSTATIC:

 System.out.println("tt" + pc + " " + "putstatic " + constantPoolLookUp.lookUp(getDoubleIndex()));
 break;
 case Opcodes.RET:

 System.out.println("tt" + pc + " " + "ret " + getSimpleIndex());
 break;
 case Opcodes.RETURN:

 System.out.println("tt" + pc + " " + "return");
 break;
 case Opcodes.SALOAD:

 System.out.println("tt" + pc + " " + "saload");
 break;
 case Opcodes.SASTORE:

 System.out.println("tt" + pc + " " + "sastore");
 break;
 case Opcodes.SIPUSH:

 System.out.println("tt" + pc + " " + "sipush " + constantPoolLookUp.lookUp(getConstantPoolIndex()));
 break;
 case Opcodes.SWAP:

 System.out.println("tt" + pc + " " + "swap");
 break;
 case Opcodes.TABLESWITCH:

 System.out.println("tt" + pc + " " + "tableswitch");

 pcL = pc;

 pad();

 System.out.println("ttt" + "default:" + " " + (pcL + getIndexWithFourBytesSigned()));

 int low = getIndexWithFourBytesSigned();

 int high = getIndexWithFourBytesSigned();

 for (int i = 0; i < (high - low + 1); i++)
 {
 System.out.println("ttt" + i + ":" + (pcL + ByteReader.read_u4(dis)));
 pc++;
 pc++;
 pc++;
 pc++;
 }

 break;
 case Opcodes.WIDE:

 System.out.println("tt" + pc + " " + "wide");
 lookupWideOpCode(ByteReader.read_u1(dis));
 break;
 default:
 System.out.println("Unimplemented OP_CODE:" + opcode);
 }

 }

 public void lookupWideOpCode(int opcode) throws Exception
 {
 switch (opcode)
 {
 case Opcodes.ILOAD:

 System.out.println("tt" + pc + " " + "iload " + getDoubleIndex() + " in variable array");
 break;

 case Opcodes.ISTORE:

 System.out.println("tt" + pc + " " + "istore " + constantPoolLookUp.lookUp(getConstantPoolIndex()));
 break;

 case Opcodes.FLOAD:

 System.out.println("tt" + pc + " " + "fload " + getDoubleIndex() + " in variable array");
 break;

 case Opcodes.FSTORE:

 System.out.println("tt" + pc + " " + "fstore " + getDoubleIndex() + " in variable array");
 break;

 case Opcodes.ASTORE:

 System.out.println("tt" + pc + " " + "astore " + getDoubleIndex() + "in Variable Array");
 break;

 case Opcodes.ALOAD:

 System.out.println("tt" + pc + " " + "aload " + getDoubleIndex() + "in Variable Array");
 break;

 case Opcodes.LLOAD:

 System.out.println("tt" + pc + " " + "lload " + constantPoolLookUp.lookUp(getConstantPoolIndex()));
 break;

 case Opcodes.LSTORE:

 System.out.println("tt" + pc + " " + "lstore " + getDoubleIndex() + " in variable array");
 break;

 case Opcodes.DLOAD:

 System.out.println("tt" + pc + " " + "dload" + getDoubleIndex() + " in variable array.");
 break;

 case Opcodes.DSTORE:

 System.out.println("tt" + pc + " " + "dstore " + getDoubleIndex() + " in variable array");
 break;

 case Opcodes.RET:

 System.out.println("tt" + pc + " " + "ret " + getDoubleIndex());
 break;

 case Opcodes.IINC:

 System.out.println("tt" + pc + " " + "iinc " + getDoubleIndex() + " in variable array by " + ByteReader.read_s2(dis));
 pc++;
 pc++;
 break;

 default:
 System.out.println("Unimplemented OP_CODE:" + opcode);
 }
 }

 private Integer getIndexWithFourBytes() throws IOException
 {
 int indexbyte1 = ByteReader.read_u1(dis) & 0xFF;
 pc++;
 int indexbyte2 = ByteReader.read_u1(dis) & 0xFF;
 pc++;
 int indexbyte3 = ByteReader.read_u1(dis) & 0xFF;
 pc++;
 int indexbyte4 = ByteReader.read_u1(dis) & 0xFF;
 pc++;

 return (indexbyte1 << 24) | (indexbyte2 << 16) | (indexbyte3 << 8) | indexbyte4;
 }

 private Integer getIndexWithFourBytesSigned() throws IOException
 {
 int indexbyte1 = ByteReader.read_s1(dis);
 pc++;
 int indexbyte2 = ByteReader.read_s1(dis);
 pc++;
 int indexbyte3 = ByteReader.read_s1(dis);
 pc++;
 int indexbyte4 = ByteReader.read_s1(dis);
 pc++;

 return (indexbyte1 << 24) | (indexbyte2 << 16) | (indexbyte3 << 8) | indexbyte4;
 }

 private int getSimpleIndex() throws IOException
 {
 pc++;
 return ByteReader.read_u1(dis);
 }

 private int getConstantPoolIndex() throws IOException
 {
 pc++;
 pc++;
 return ByteReader.read_u2(dis);
 }

 private int getDoubleIndex() throws IOException
 {
 int indexbyte1 = ByteReader.read_u1(dis) & 0xFF;
 pc++;
 int indexbyte2 = ByteReader.read_u1(dis) & 0xFF;
 pc++;
 return indexbyte1 << 8 | indexbyte2;
 }

 private int getBranchInstPC() throws IOException
 {
 int pcL = pc;
 pc++;
 pc++;
 return pcL + ByteReader.read_s2(dis);
 }

 private void pad() throws IOException
 {
 switch (pc % 4)
 {
 case 0:
 for (int i = 1; i <= 3; i++)
 {
 ByteReader.read_u1(dis);
 pc++;
 }
 break;
 case 1:
 for (int i = 1; i <= 2; i++)
 {
 ByteReader.read_u1(dis);
 pc++;
 }
 break;

 case 2:
 ByteReader.read_u1(dis);
 pc++;
 break;

 case 3:
 //No nothing
 break;
 }
 }

 private String lookupAType() throws IOException //Array Type
 {
 int i = ByteReader.read_u1(dis);
 pc++;
 switch (i)
 {
 case 4:
 return "T_BOOLEAN";
 case 5:
 return "T_CHAR";
 case 6:
 return "T_FLOAT";
 case 7:
 return "T_DOUBLE";
 case 8:
 return "T_BYTE";
 case 9:
 return "T_SHORT";
 case 10:
 return "T_INT";
 case 11:
 return "T_LONG";
 default:
 return "";
 }
 }
}
package classfileparser;

public interface Opcodes
{

 public static final int ASM4 = 262144;
 public static final int V1_1 = 196653;
 public static final int V1_2 = 46;
 public static final int V1_3 = 47;
 public static final int V1_4 = 48;
 public static final int V1_5 = 49;
 public static final int V1_6 = 50;
 public static final int V1_7 = 51;
 public static final int ACC_PUBLIC = 1;
 public static final int ACC_PRIVATE = 2;
 public static final int ACC_PROTECTED = 4;
 public static final int ACC_STATIC = 8;
 public static final int ACC_FINAL = 16;
 public static final int ACC_SUPER = 32;
 public static final int ACC_SYNCHRONIZED = 32;
 public static final int ACC_VOLATILE = 64;
 public static final int ACC_BRIDGE = 64;
 public static final int ACC_VARARGS = 128;
 public static final int ACC_TRANSIENT = 128;
 public static final int ACC_NATIVE = 256;
 public static final int ACC_INTERFACE = 512;
 public static final int ACC_ABSTRACT = 1024;
 public static final int ACC_STRICT = 2048;
 public static final int ACC_SYNTHETIC = 4096;
 public static final int ACC_ANNOTATION = 8192;
 public static final int ACC_ENUM = 16384;
 public static final int ACC_DEPRECATED = 131072;
 public static final int T_BOOLEAN = 4;
 public static final int T_CHAR = 5;
 public static final int T_FLOAT = 6;
 public static final int T_DOUBLE = 7;
 public static final int T_BYTE = 8;
 public static final int T_SHORT = 9;
 public static final int T_INT = 10;
 public static final int T_LONG = 11;
 public static final int H_GETFIELD = 1;
 public static final int H_GETSTATIC = 2;
 public static final int H_PUTFIELD = 3;
 public static final int H_PUTSTATIC = 4;
 public static final int H_INVOKEVIRTUAL = 5;
 public static final int H_INVOKESTATIC = 6;
 public static final int H_INVOKESPECIAL = 7;
 public static final int H_NEWINVOKESPECIAL = 8;
 public static final int H_INVOKEINTERFACE = 9;
 public static final int F_NEW = -1;
 public static final int F_FULL = 0;
 public static final int F_APPEND = 1;
 public static final int F_CHOP = 2;
 public static final int F_SAME = 3;
 public static final int F_SAME1 = 4;
 public static final Integer TOP = new Integer( 0 );
 public static final Integer INTEGER = new Integer( 1 );
 public static final Integer FLOAT = new Integer( 2 );
 public static final Integer DOUBLE = new Integer( 3 );
 public static final Integer LONG = new Integer( 4 );
 public static final Integer NULL = new Integer( 5 );
 public static final Integer UNINITIALIZED_THIS = new Integer( 6 );
 public static final int NOP = 0;
 public static final int ACONST_NULL = 1;
 public static final int ICONST_M1 = 2;
 public static final int ICONST_0 = 3;
 public static final int ICONST_1 = 4;
 public static final int ICONST_2 = 5;
 public static final int ICONST_3 = 6;
 public static final int ICONST_4 = 7;
 public static final int ICONST_5 = 8;
 public static final int LCONST_0 = 9;
 public static final int LCONST_1 = 10;
 public static final int FCONST_0 = 11;
 public static final int FCONST_1 = 12;
 public static final int FCONST_2 = 13;
 public static final int DCONST_0 = 14;
 public static final int DCONST_1 = 15;
 public static final int BIPUSH = 16;
 
 public static final int BREAKPOINT = 202;
 public static final int SIPUSH = 17;
 public static final int LDC = 18;
 public static final int LDC_W = 19;
 public static final int LDC2_W = 20;
 public static final int ILOAD = 21;
 public static final int iload_0 = 26; //(0x1a)
 public static final int iload_1 = 27; //(0x1b)
 public static final int iload_2 = 28; //(0x1c)
 public static final int iload_3 = 29; // (0x1d) 
 public static final int lload_0 = 30; // (0x1d) 
 public static final int lload_1 = 31; // (0x1d) 
 public static final int lload_2 = 32; // (0x1d) 
 public static final int lload_3 = 33; // (0x1d) 
 public static final int LLOAD = 22;
 public static final int FLOAD = 23;
 public static final int fload_0 = 34; //(0x22)
 public static final int fload_1 = 35; //(0x23)
 public static final int fload_2 = 36; //(0x24)
 public static final int fload_3 = 37; //(0x25) 
 public static final int DLOAD = 24;
 public static final int dload_0 = 38;//(0x26)
 public static final int dload_1 = 39;//(0x27)
 public static final int dload_2 = 40;//(0x28)
 public static final int dload_3 = 41;//(0x29) 
 public static final int ALOAD = 25;
 public static final int aload_0 = 42; //(0x2a)
 public static final int aload_1 = 43;// (0x2b)
 public static final int aload_2 = 44; //(0x2c)
 public static final int aload_3 = 45; //(0x2d) 
 public static final int IALOAD = 46;
 public static final int LALOAD = 47;
 public static final int FALOAD = 48;
 public static final int DALOAD = 49;
 public static final int AALOAD = 50;
 public static final int BALOAD = 51;
 public static final int CALOAD = 52;
 public static final int SALOAD = 53;
 public static final int ISTORE = 54;
 public static final int istore_0 = 59;//(0x3b)
 public static final int istore_1 = 60;//(0x3c)
 public static final int istore_2 = 61;//(0x3d)
 public static final int istore_3 = 62;//(0x3e) 
 public static final int LSTORE = 55;
 public static final int lstore_0 = 63;//(0x3b)
 public static final int lstore_1 = 64;//(0x3c)
 public static final int lstore_2 = 65;//(0x3d)
 public static final int lstore_3 = 66;//(0x3e) 
 public static final int FSTORE = 56;
 public static final int fstore_0 = 67; //(0x43)
 public static final int fstore_1 = 68; //(0x44)
 public static final int fstore_2 = 69; //(0x45)
 public static final int fstore_3 = 70; //(0x46) 
 public static final int DSTORE = 57;
 public static final int dstore_0 = 71; //(0x47)
 public static final int dstore_1 = 72; //(0x48)
 public static final int dstore_2 = 73; //(0x49)
 public static final int dstore_3 = 74; //(0x4a) 
 public static final int ASTORE = 58;
 public static final int astore_0 = 75; //(0x4b)
 public static final int astore_1 = 76; //(0x4c)
 public static final int astore_2 = 77; //(0x4d)
 public static final int astore_3 = 78; //(0x4e) 
 public static final int IASTORE = 79;
 public static final int LASTORE = 80;
 public static final int FASTORE = 81;
 public static final int DASTORE = 82;
 public static final int AASTORE = 83;
 public static final int BASTORE = 84;
 public static final int CASTORE = 85;
 public static final int SASTORE = 86;
 public static final int POP = 87;
 public static final int POP2 = 88;
 public static final int DUP = 89;
 public static final int DUP_X1 = 90;
 public static final int DUP_X2 = 91;
 public static final int DUP2 = 92;
 public static final int DUP2_X1 = 93;
 public static final int DUP2_X2 = 94;
 public static final int SWAP = 95;
 public static final int IADD = 96;
 public static final int LADD = 97;
 public static final int FADD = 98;
 public static final int DADD = 99;
 public static final int ISUB = 100;
 public static final int LSUB = 101;
 public static final int FSUB = 102;
 public static final int DSUB = 103;
 public static final int IMUL = 104;
 public static final int LMUL = 105;
 public static final int FMUL = 106;
 public static final int DMUL = 107;
 public static final int IDIV = 108;
 public static final int LDIV = 109;
 public static final int FDIV = 110;
 public static final int DDIV = 111;
 public static final int IREM = 112;
 public static final int LREM = 113;
 public static final int FREM = 114;
 public static final int DREM = 115;
 public static final int INEG = 116;
 public static final int LNEG = 117;
 public static final int FNEG = 118;
 public static final int DNEG = 119;
 public static final int ISHL = 120;
 public static final int LSHL = 121;
 public static final int ISHR = 122;
 public static final int LSHR = 123;
 public static final int IUSHR = 124;
 public static final int LUSHR = 125;
 public static final int IAND = 126;
 public static final int LAND = 127;
 public static final int IOR = 128;
 public static final int LOR = 129;
 public static final int IXOR = 130;
 public static final int LXOR = 131;
 public static final int IINC = 132;
 public static final int I2L = 133;
 public static final int I2F = 134;
 public static final int I2D = 135;
 public static final int L2I = 136;
 public static final int L2F = 137;
 public static final int L2D = 138;
 public static final int F2I = 139;
 public static final int F2L = 140;
 public static final int F2D = 141;
 public static final int D2I = 142;
 public static final int D2L = 143;
 public static final int D2F = 144;
 public static final int I2B = 145;
 public static final int I2C = 146;
 public static final int I2S = 147;
 public static final int LCMP = 148;
 public static final int FCMPL = 149;
 public static final int FCMPG = 150;
 public static final int DCMPL = 151;
 public static final int DCMPG = 152;
 public static final int IFEQ = 153;
 public static final int IFNE = 154;
 public static final int IFLT = 155;
 public static final int IFGE = 156;
 public static final int IFGT = 157;
 public static final int IFLE = 158;
 public static final int IF_ICMPEQ = 159;
 public static final int IF_ICMPNE = 160;
 public static final int IF_ICMPLT = 161;
 public static final int IF_ICMPGE = 162;
 public static final int IF_ICMPGT = 163;
 public static final int IF_ICMPLE = 164;
 public static final int IF_ACMPEQ = 165;
 public static final int IF_ACMPNE = 166;
 public static final int GOTO = 167;
 public static final int goto_w = 200;// (0xc8) 
 public static final int JSR = 168;
 public static final int JSR_W = 201;
 public static final int RET = 169;
 public static final int TABLESWITCH = 170;
 public static final int LOOKUPSWITCH = 171;
 public static final int IRETURN = 172;
 public static final int LRETURN = 173;
 public static final int FRETURN = 174;
 public static final int DRETURN = 175;
 public static final int ARETURN = 176;
 public static final int RETURN = 177;
 public static final int GETSTATIC = 178;
 public static final int PUTSTATIC = 179;
 public static final int GETFIELD = 180;
 public static final int PUTFIELD = 181;
 public static final int INVOKEVIRTUAL = 182;
 public static final int INVOKESPECIAL = 183;
 public static final int INVOKESTATIC = 184;
 public static final int INVOKEINTERFACE = 185;
 public static final int INVOKEDYNAMIC = 186;
 public static final int NEW = 187;
 public static final int NEWARRAY = 188;
 public static final int ANEWARRAY = 189;
 public static final int ARRAYLENGTH = 190;
 public static final int ATHROW = 191;
 public static final int CHECKCAST = 192;
 public static final int INSTANCEOF = 193;
 public static final int MONITORENTER = 194;
 public static final int MONITOREXIT = 195;
 public static final int MULTIANEWARRAY = 197;
 public static final int IFNULL = 198;
 public static final int IFNONNULL = 199;
 public static final int WIDE = 196;

}

Note: The java command “javap” also is a class file disassembler provided by java.

This is the first step in developing a java class de-compiler. The same program can be improvised to implement a de-compiler. Try it!

Hello Java Experts!

Please review the above program and provide your comments on the design and coding style.

Thank You.

EFFECTIVE JAVA BY JOSHUA BLOCH – KEY POINTS -PART 3

The third article in the series of “Effective Java – Key Points” is here.

  1. Use Strategy Pattern when the algorithm/strategy is to be chosen at the run-time. E.g. Choosing the algorithm based on file format, file compression, etc. In this case, declare an Interface for the strategy and then implement the interface for a different algorithm.
  2. Never use raw types.
    E.g.

    List l = new ArrayList();

    Using raw types might end up in run-time exceptions because of adding incompatible objects to the list. Always use generics so that the compiler can identify the incompatible data types.
    E.g.

    List<String> l = new ArrayList<String>
  3. Never ignore the unchecked warning from the compiler. Fix these warning as much as possible to keep the code safe in terms of casting and conversion. At least provide a “Suppress warning” annotation in a minimum scope.
  4. Type Erasure: The generics concept introduced in Java 1.5 is only for the compatibility check at the compile time, thereby reducing the compatibility and casting issues at the run-time. Thus the concept of “type erasure” come into picture wherein the compiler erases the type information and make the references raw. Hence, the compiler will make the bytecode run in JVM as if there were no generics thereby reducing the changes on JVM.
  5. Always ensure to validate the parameters passed to constructor or method. This is a good practice because otherwise the program might fail with a wrong exception or silently compute wrong results.
  6. Prefer defensive copying while designing public API. The client using the code might mutate/change the value of the object.  In case the class is package scoped and the developer knows the usage then it is good to avoid defensive copying because it is costly in terms of memory.
  7. Prefer two element enum types instead of boolean parameters to improve the readability of the code.     E.g.
    public enum TemperatureScale { FAHRENHEIT , CELSIUS }
    
  8. Prefer BigDecimal for monetary calculation as float and double work on binary floating point arithmetic and cannot represent 0.1 accurately.
  9. Use checked exceptions in case of recoverable conditions where the application can recover from the exception and continue the execution without any flaws. Use unchecked exception to indicate programming errors. In case of programming errors, it is better to terminate the application instead of silently doing wrong stuff.
  10. The types of nested classes are

EFFECTIVE JAVA BY JOSHUA BLOCH – KEY POINTS -PART 2

This is the second article in the series of KEY POINTS FROM “EFFECTIVE JAVA” BY JOSHUA BLOCH. Let’s get to the key points.

  1. Encapsulation: A well-designed module should hide the implementation details cleanly separating the API details from its implementation.
  2. Designing a class with public mutable fields is not thread safe. Therefore, any public static final field in the class should be a primitive type or an immutable object.
  3. Immutable classes are easy to design, maintain and less error prone. String class in Java is immutable. Immutable objects are thread-safe and can be shared freely. In case of immutable objects, the frequently used instances can be cached using static factory methods to reduce the memory foot-print. The only disadvantage of the immutable class is separate object will be required for each state of the object.
  4. A Degenerate class is a class with no behavior meaning no methods.
  5. Favor composition over inheritance. When extended, subclass depends on the implementation details of the super class for proper functioning. Hence be judicious when choosing inheritance.
  6. In Decorator Pattern, the class uses composition and forwards the method calls to the composed class. Here a class decorates another class with more functionality.
  7. Mixin is a type that a class can implement in addition to its primary type. Interfaces are ideal for mixins.
  8. Simulation of multiple inheritance: Implement the interface (E.g Set) required. Create an inner class extending the skeletal implementation (E.g AbstractSet) of the interface and forward the method invocations using the inner class.
  9. Do not write a constant interface, an interface only with constants. Instead, use a class with the private constructor which will act as a utility class.
  10. Do not use tags (fields) in the class to indicate different types of object. Instead, use hierarchies to represent different type of objects.

                                                                                                             To be continued…

EFFECTIVE JAVA BY JOSHUA BLOCH – KEY POINTS

Target Audience: This article is for a Java techie who knows the basics of Java language and looking for best practices while using the language. This article throws light on the key points from the book “Effective Java” by Joshua Bloch.

  1. Consider a “builder pattern” when there are too many parameters to be passed to the constructor. The builder pattern also gives the option of naming the parameters and making it optional. In case of builder pattern, while building the object itself, the inconsistencies in the state of the object can be identified.
  2. A “singleton pattern” with a private constructor and static factory is not thread-safe ( unless all the conditions are taken care explicitly by synchronizing ). Serialization breaks singleton and the solution is to make the variables transient. Reflection can also create multiple instances of the singleton class. The best way of writing a singleton class is using Enum.
  3. A Class need not be always instantiated, it can be a kind of utility class which has only static methods. In case of utility classes, the constructor should be made private. Using abstract keyword to make a class non-instantiable is a bad practice. The Abstract  keyword should be used only if the class is meant for sub-classing.
  4. Do not create unnecessary objects. Use primitive types whenever possible, because auto-boxing creates a lot of objects. Moreover, since the wrapper types are immutable, the application can end up creating a lot of objects. Maintain your own “object pool” only if the object is really heavy weight like DB Connections, otherwise the modern GCs are efficient enough to handle lightweight objects.
  5. Do not write “Constant Interfaces”. Constant Interfaces are the interfaces with no behaviors and only constant values. Use a class with the private constructor to hold the list of constants.
  6. The Liskov Substitution Principle says that any important property of a type should also hold true for its subtypes so that any method written for the type should work equally well with its sub-types.
  7. A good hash function must distribute a collection of unequal instances uniformly across all possible hash values. In case of immutable objects, the hash code can be cached to improve the performance.
  8. Eliminate obsolete references. If a program manages its own memory, it is necessary that the object references are marked to null for the object to be garbage collected. Always keep the scope of the object very narrow. The best possible way to implement a cache is to use WeakHashMap.
  9. Do not override equals when each instance is always unique or there is no logical equality required or the super-classe’s “equals” method is sufficient. While overriding equals
    1. Always use @Override annotation
    2. Always check if the object to be compared, is the instance of this class
    3. Always compare the fields that are most likely to differ for better performance
    4. ALWAYS OVERRIDE HASH-CODE
  10. Avoid finalizers. There is no guarantee on when finalizers will execute. Never rely on the finalizers to release non-memory resources. The finalizer thread might be running at a lower priority than another application thread, so objects might not get finalized at the rate they become eligible for finalization. If an uncaught exception is thrown in finalization, then it is ignored and the finalization terminates. The finalizer chaining is not automatic and hence the super finalizer needs to be called explicitly. The best use case for finalizers are
    1. “Safety net” in case the explicit termination/finalization is not called
    2. Release the native resource.

 Note: This article might not be complete in itself. This might be a trigger for the reader to get into the details of writing a good code.