10.7. Bytecode Engineering
You have seen how annotations can be processed at runtime or at the source code level. There is a third possibility: processing at the bytecode level. Unless annotations are removed at the source level, they are present in the class files. The class file format is documented (see http://docs.oracle.com/javase/specs/jvms/se7/html). The format is rather complex, and it would be challenging to process class files without special libraries. One such library is the Bytecode Engineering Library (BCEL), available at http://jakarta.apache.org/bcel.
In this section, we use BCEL to add logging messages to annotated methods. If a method is annotated with
@LogEntry(logger=loggerName)
then we add the bytecodes for the following statement at the beginning of the method:
Logger.getLogger(loggerName).entering(className, methodName);
For example, if you annotate the hashCode method of the Item class as
@LogEntry(logger="global") public int hashCode()
then a message similar to the following is printed whenever the method is called:
Aug 17, 2004 9:32:59 PM Item hashCode FINER: ENTRY
To achieve this, we do the following:
- Load the bytecodes in the class file.
- Locate all methods.
- For each method, check whether it has a LogEntry annotation.
- If it does, add the bytecodes for the following instructions at the beginning of the method:
ldc loggerName invokestatic java/util/logging/Logger.getLogger:(Ljava/lang/String;)Ljava/util/logging/Logger; ldc className ldc methodName invokevirtual java/util/logging/Logger.entering:(Ljava/lang/String;Ljava/lang/String;)V
Inserting these bytecodes sounds tricky, but BCEL makes it fairly straightforward. We don’t describe the process of analyzing and inserting bytecodes in detail. The important point is that the program in Listing 10.14 edits a class file and inserts a logging call at the beginning of the methods annotated with the LogEntry annotation.
You’ll need version 6.0 or later of the BCEL library to compile and run the EntryLogger program. (As this chapter was written, that version was still a work in progress. If it isn’t finished when you read this, check out the trunk from the Subversion repository.)
For example, here is how you add the logging instructions to Item.java in Listing 10.15:
javac set/Item.java javac -classpath .:bcel-version.jar bytecodeAnnotations/EntryLogger.java java -classpath .:bcel-version.jar bytecodeAnnotations.EntryLogger set.Item
Try running
javap -c set.Item
before and after modifying the Item class file. You can see the inserted instructions at the beginning of the hashCode, equals, and compareTo methods.
public int hashCode(); Code: 0: ldc #85; //String global 2: invokestatic #80; //Method java/util/logging/Logger.getLogger:(Ljava/lang/String;)Ljava/util/logging/Logger; 5: ldc #86; //String Item 7: ldc #88; //String hashCode 9: invokevirtual #84; //Method java/util/logging/Logger.entering:(Ljava/lang/String;Ljava/lang/String;)V 12: bipush 13 14: aload_0 15: getfield #2; //Field description:Ljava/lang/String; 18: invokevirtual #15; //Method java/lang/String.hashCode:()I 21: imul 22: bipush 17 24: aload_0 25: getfield #3; //Field partNumber:I 28: imul 29: iadd 30: ireturn
The SetTest program in Listing 10.16 inserts Item objects into a hash set. When you run it with the modified class file, you will see the logging messages.
Aug 18, 2004 10:57:59 AM Item hashCode FINER: ENTRY Aug 18, 2004 10:57:59 AM Item hashCode FINER: ENTRY Aug 18, 2004 10:57:59 AM Item hashCode FINER: ENTRY Aug 18, 2004 10:57:59 AM Item equals FINER: ENTRY [[description=Toaster, partNumber=1729], [description=Microwave, partNumber=4104]]
Note the call to equals when we insert the same item twice.
This example shows the power of bytecode engineering. Annotations are used to add directives to a program, and a bytecode editing tool picks up the directives and modifies the virtual machine instructions.
Listing 10.14. bytecodeAnnotations/EntryLogger.java
1 package bytecodeAnnotations; 2 3 import java.io.*; 4 import org.apache.bcel.*; 5 import org.apache.bcel.classfile.*; 6 import org.apache.bcel.generic.*; 7 8 /** 9 * Adds "entering" logs to all methods of a class that have the LogEntry annotation. 10 * @version 1.10 2007-10-27 11 * @author Cay Horstmann 12 */ 13 public class EntryLogger 14 { 15 private ClassGen cg; 16 private ConstantPoolGen cpg; 17 /** 18 * Adds entry logging code to the given class. 19 * @param args the name of the class file to patch 20 */ 21 public static void main(String[] args) 22 { 23 try 24 { 25 if (args.length == 0) 26 System.out.println("USAGE: java bytecodeAnnotations.EntryLogger classname"); 27 else 28 { 29 JavaClass jc = Repository.lookupClass(args[0]); 30 ClassGen cg = new ClassGen(jc); 31 EntryLogger el = new EntryLogger(cg); 32 el.convert(); 33 String f = Repository.lookupClassFile(cg.getClassName()).getPath(); 34 System.out.println("Dumping " + f); 35 cg.getJavaClass().dump(f); 36 } 37 } 38 catch (Exception e) 39 { 40 e.printStackTrace(); 41 } 42 } 43 44 /** 45 * Constructs an EntryLogger that inserts logging into annotated methods of a given class. 46 * @param cg the class 47 */ 48 public EntryLogger(ClassGen cg) 49 { 50 this.cg = cg; 51 cpg = cg.getConstantPool(); 52 } 53 54 /** 55 * converts the class by inserting the logging calls. 56 */ 57 public void convert() throws IOException 58 { 59 for (Method m : cg.getMethods()) 60 { 61 AnnotationEntry[] annotations = m.getAnnotationEntries(); 62 for (AnnotationEntry a : annotations) 63 { 64 if (a.getAnnotationType().equals("LbytecodeAnnotations/LogEntry;")) 65 { 66 for (ElementValuePair p : a.getElementValuePairs()) 67 { 68 if (p.getNameString().equals("logger")) 69 { 70 String loggerName = p.getValue().stringifyValue(); 71 cg.replaceMethod(m, insertLogEntry(m, loggerName)); 72 } 73 } 74 } 75 } 76 } 77 } 78 79 /** 80 * Adds an "entering" call to the beginning of a method. 81 * @param m the method 82 * @param loggerName the name of the logger to call 83 */ 84 private Method insertLogEntry(Method m, String loggerName) 85 { 86 MethodGen mg = new MethodGen(m, cg.getClassName(), cpg); 87 String className = cg.getClassName(); 88 String methodName = mg.getMethod().getName(); 89 System.out.printf("Adding logging instructions to %s.%s%n", className, methodName); 90 91 int getLoggerIndex = cpg.addMethodref("java.util.logging.Logger", "getLogger", 92 "(Ljava/lang/String;)Ljava/util/logging/Logger;"); 93 int enteringIndex = cpg.addMethodref("java.util.logging.Logger", "entering", 94 "(Ljava/lang/String;Ljava/lang/String;)V"); 95 96 InstructionList il = mg.getInstructionList(); 97 InstructionList patch = new InstructionList(); 98 patch.append(new PUSH(cpg, loggerName)); 99 patch.append(new INVOKESTATIC(getLoggerIndex)); 100 patch.append(new PUSH(cpg, className)); 101 patch.append(new PUSH(cpg, methodName)); 102 patch.append(new INVOKEVIRTUAL(enteringIndex)); 103 InstructionHandle[] ihs = il.getInstructionHandles(); 104 il.insert(ihs[0], patch); 105 106 mg.setMaxStack(); 107 return mg.getMethod(); 108 } 109 }
Listing 10.15. set/Item.java
1 package set; 2 3 import java.util.*; 4 import bytecodeAnnotations.*; 5 6 /** 7 * An item with a description and a part number. 8 * @version 1.01 2012-01-26 9 * @author Cay Horstmann 10 */ 11 public class Item 12 { 13 private String description; 14 private int partNumber; 15 16 /** 17 * Constructs an item. 18 * @param aDescription the item's description 19 * @param aPartNumber the item's part number 20 */ 21 public Item(String aDescription, int aPartNumber) 22 { 23 description = aDescription; 24 partNumber = aPartNumber; 25 } 26 27 /** 28 * Gets the description of this item. 29 * @return the description 30 */ 31 public String getDescription() 32 { 33 return description; 34 } 35 36 public String toString() 37 { 38 return "[description=" + description + ", partNumber=" + partNumber + "]"; 39 } 40 41 @LogEntry(logger = "global") 42 public boolean equals(Object otherObject) 43 { 44 if (this == otherObject) return true; 45 if (otherObject == null) return false; 46 if (getClass() != otherObject.getClass()) return false; 47 Item other = (Item) otherObject; 48 return Objects.equals(description, other.description) && partNumber == other.partNumber; 49 } 50 51 @LogEntry(logger = "global") 52 public int hashCode() 53 { 54 return Objects.hash(description, partNumber); 55 } 56 }
Listing 10.16. set/SetTest.java
1 package set; 2 3 import java.util.*; 4 import java.util.logging.*; 5 6 /** 7 * @version 1.02 2012-01-26 8 * @author Cay Horstmann 9 */ 10 public class SetTest 11 { 12 public static void main(String[] args) 13 { 14 Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).setLevel(Level.FINEST); 15 Handler handler = new ConsoleHandler(); 16 handler.setLevel(Level.FINEST); 17 Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).addHandler(handler); 18 19 Set<Item> parts = new HashSet<>(); 20 parts.add(new Item("Toaster", 1279)); 21 parts.add(new Item("Microwave", 4104)); 22 parts.add(new Item("Toaster", 1279)); 23 System.out.println(parts); 24 } 25 }
10.7.1. Modifying Bytecodes at Load Time
In the preceding section, you saw a tool that edits class files. However, it can be cumbersome to add yet another tool into the build process. An attractive alternative is to defer the bytecode engineering until load time, when the class loader loads the class.
Before Java SE 5.0, you had to write a custom class loader to achieve this task. Now, the instrumentation API has a hook for installing a bytecode transformer. The transformer must be installed before the main method of the program is called. You can meet this requirement by defining an agent, a library that is loaded to monitor a program in some way. The agent code can carry out initializations in a premain method.
Here are the steps required to build an agent:
- Implement a class with a method
public static void premain(String arg, Instrumentation instr)
This method is called when the agent is loaded. The agent can get a single command-line argument, which is passed in the arg parameter. The instr parameter can be used to install various hooks.
- Make a manifest file EntryLoggingAgent.mf that sets the Premain-Class attribute, for example:
Premain-Class: bytecodeAnnotations.EntryLoggingAgent
- Package the agent code and the manifest into a JAR file, for example:
javac -classpath .:bcel-version.jar bytecodeAnnotations.EntryLoggingAgent jar cvfm EntryLoggingAgent.jar EntryLoggingAgent.mf bytecodeAnnotations/Entry*.class
To launch a Java program together with the agent, use the following command-line options:
java -javaagent:AgentJARFile=agentArgument. . .
For example, to run the SetTest program with the entry logging agent, call
javac SetTest.java java -javaagent:EntryLoggingAgent.jar=set.Item -classpath .:bcel-version .jar set.SetTest
The Item argument is the name of the class that the agent should modify.
Listing 10.17 shows the agent code. The agent installs a class file transformer. The transformer first checks whether the class name matches the agent argument. If so, it uses the EntryLogger class from the preceding section to modify the byte-codes. However, the modified bytecodes are not saved to a file. Instead, the transformer returns them for loading into the virtual machine (see Figure 10.4). In other words, this technique carries out “just in time” modification of the bytecodes.
Figure 10.4. Modifying classes at load time
Listing 10.17. bytecodeAnnotations/EntryLoggingAgent.java
1 package bytecodeAnnotations; 2 3 import java.lang.instrument.*; 4 import java.io.*; 5 import java.security.*; 6 import org.apache.bcel.classfile.*; 7 import org.apache.bcel.generic.*; 8 9 /** 10 * @version 1.00 2004-08-17 11 * @author Cay Horstmann 12 */ 13 public class EntryLoggingAgent 14 { 15 public static void premain(final String arg, Instrumentation instr) 16 { 17 instr.addTransformer(new ClassFileTransformer() 18 { 19 public byte[] transform(ClassLoader loader, String className, Class<?> cl, 20 ProtectionDomain pd, byte[] data) 21 { 22 if (!className.equals(arg)) return null; 23 try 24 { 25 ClassParser parser = new ClassParser(new ByteArrayInputStream(data), className 26 + ".java"); 27 JavaClass jc = parser.parse(); 28 ClassGen cg = new ClassGen(jc); 29 EntryLogger el = new EntryLogger(cg); 30 el.convert(); 31 return cg.getJavaClass().getBytes(); 32 } 33 catch (Exception e) 34 { 35 e.printStackTrace(); 36 return null; 37 } 38 } 39 }); 40 } 41 }
In this chapter, you have learned how to
- Add annotations to Java programs
- Design your own annotation interfaces
- Implement tools that make use of the annotations
You have seen three technologies for processing code: scripting, compiling Java programs, and processing annotations. The first two were quite straightforward. On the other hand, building annotation tools is undeniably complex and not something that most developers will need to tackle. This chapter gave you the background for understanding the inner workings of the annotation tools you will encounter, and perhaps piqued your interest in developing your own tools.
In the next chapter, you will learn about the RMI mechanism, a distributed object model for Java programs.