KOPI
PrevChapter 1. Classfile HandlingNext

KOPI Classfile API

License type: KOPI Classfile is distributed under the terms of the the GNU Lesser General Public License. Appendix A

This package contains the API to edit classfiles from within a Java program. If you want to read or write classfiles in a human readable form, you should use KSM and DIS which offer an interface to classfile.

Basically, this package contains three parts: one to read data from a file, one to create a consistant class from another program, and a third part to check the code of each function (compute stack and local variable usage, and optionnally optimize code) and then dump the class into a file.

Read a classfile

You just have to create a ClassInfo from a file:

	  ClassInfo clazz = new ClassInfo(new
	  RandomAccessFile(``AClass.class''), false);
	
or with its full name and a class path using the provided class ClassPath:
	
  ClassPath path = new ClassPath(``.''); 
  ClassInfo clazz = path.getClassFile(``pack.AClass'', true);
  // rmq: the last parameter allows you 
  //      to load only the interface (skip code)
	
Once you've read your class, you can access all the information you need from the provided API such as getName(), getSuperClass(), getMethods(), getSourceFile(). If you have loaded an entire class (not only the interface), you'll be able to change some information (the source file, the name, some modifiers...) and then to dump it to file again.

Create, edit, optimize and check a classfile

Create a class from scratch

You can create a class from scratch and then add all the attributes and members you want. See KSM or KJC for a real example of usage.

Edit a class

You can edit your class using the provided API:

	  
  ClassInfo clazz = new ClassInfo(new File(``AClass.class''); 
  clazz.setName(``BClass'');
	  

Optimize and check a classfile

After you have built or edited the instructions, you can check and optimize the code:

	  	  
  // obtained from any non empty method
  CodeInfo code = clazz.getMethods()[0]; 
  // 2 is the number of optimization loop, 0 for none
  code.optimizeAndCheck(2) 
	  
The main check is to ensure that the stack depth is correct for every branch in the program. We may add later some verifications made by the Java Verifier at runtime.

Write a classfile

Write a .class file

After you have built or edited a ClassInfo, you can dump it into a file with:

	  
	    ClassInfo clazz = new ClassInfo(new File(``AClass.class''); 
	    clazz.setName(``BClass'')
	    clazz.write(new DataOutputStream(new FileOutputStream(``BClass.class'');
	  

Example

This simple example sets all methods to be public in the class passed as argv[0] and writes this new class in a file argv[1]

  /** 
   * This simple example set all methods to 
   * be public in the class passed as first
   * parameters and write this new class in a file argv[1]
   */	    
  import at.dms.classfile.*; 
  import java.io.*;
  
  public class Editor implements Constants { 
    public static void main(String[] argv) 
    throws at.dms.classfile.ClassFileFormatException, 
           java.io.FileNotFoundException, 
           java.io.IOException 
    { 
      DataInputStream is = new DataInputStream(new FileInputStream(argv[0]));
    
      ClassInfo clazz = new ClassInfo(is, false); 
      MethodInfo[] method = clazz.getMethods(); 
      
      for (int i = 0; i < method.length; i++) 
        method[i].setModifiers(toPublic(method[i].getModifiers())); 
      
      clazz.write(new DataOutputStream(new FileOutputStream(argv[1])));
    } 
    private static short toPublic(short modifiers) { 
      return (short)((modifiers | ACC_PUBLIC) &
		       ~(ACC_PROTECTED | ACC_PRIVATE));
    } 
  }
	  

Where to use this package

This is a list of non restrictive possible tools to build with this package:

Pluggable Bytecode optimizer

This chapter explains which optimisations are provided by the bytecode optimizer. These optimizations are:

Unreachable code optimization

Unreachable code is simply removed from the CodeInfo. And nop instructions are removed if they are not targeted in a method.

Jump and peephole optimization

This is the current list of peephole optimization performed by KJC:


PrevHomeNext
Classfile HandlingUpThe KOPI Kopi Java Compiler