8.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/se8/html). The format is rather complex, and it would be challenging to process class files without special libraries. One such library is the ASM library, available at http://asm.ow2.org.
8.7.1 Modifying Class Files
In this section, we use ASM 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:
May 17, 2016 10:57:59 AM 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 ASM 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 8.12 edits a class file and inserts a logging call at the beginning of the methods annotated with the LogEntry annotation.
For example, here is how you add the logging instructions to Item.java in Listing 8.13, where asm is the directory into which you installed the ASM library:
javac set/Item.java javac -classpath .:asm/lib/\* bytecodeAnnotations/EntryLogger.java java -classpath .:asm/lib/\* 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 8.14 inserts Item objects into a hash set. When you run it with the modified class file, you will see the logging messages.
May 17, 2016 10:57:59 AM Item hashCode FINER: ENTRY May 17, 2016 10:57:59 AM Item hashCode FINER: ENTRY May 17, 2016 10:57:59 AM Item hashCode FINER: ENTRY May 17, 2016 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 8.12 bytecodeAnnotations/EntryLogger.java
1 package bytecodeAnnotations; 2 3 import java.io.*; 4 import java.nio.file.*; 5 6 import org.objectweb.asm.*; 7 import org.objectweb.asm.commons.*; 8 9 /** 10 * Adds "entering" logs to all methods of a class that have the LogEntry annotation. 11 * @version 1.20 2016-05-10 12 * @author Cay Horstmann 13 */ 14 public class EntryLogger extends ClassVisitor 15 { 16 private String className; 17 18 /** 19 * Constructs an EntryLogger that inserts logging into annotated methods of a given class. 20 * @param cg the class 21 */ 22 public EntryLogger(ClassWriter writer, String className) 23 { 24 super(Opcodes.ASM5, writer); 25 this.className = className; 26 } 27 28 @Override 29 public MethodVisitor visitMethod(int access, String methodName, String desc, 30 String signature, String[] exceptions) 31 { 32 MethodVisitor mv = cv.visitMethod(access, methodName, desc, signature, exceptions); 33 return new AdviceAdapter(Opcodes.ASM5, mv, access, methodName, desc) 34 { 35 private String loggerName; 36 37 public AnnotationVisitor visitAnnotation(String desc, boolean visible) 38 { 39 return new AnnotationVisitor(Opcodes.ASM5) 40 { 41 public void visit(String name, Object value) 42 { 43 if (desc.equals("LbytecodeAnnotations/LogEntry;") && name.equals("logger")) 44 loggerName = value.toString(); 45 } 46 }; 47 } 48 49 public void onMethodEnter() 50 { 51 if (loggerName != null) 52 { 53 visitLdcInsn(loggerName); 54 visitMethodInsn(INVOKESTATIC, "java/util/logging/Logger", "getLogger", 55 "(Ljava/lang/String;)Ljava/util/logging/Logger;", false); 56 visitLdcInsn(className); 57 visitLdcInsn(methodName); 58 visitMethodInsn(INVOKEVIRTUAL, "java/util/logging/Logger", "entering", 59 "(Ljava/lang/String;Ljava/lang/String;)V", false); 60 loggerName = null; 61 } 62 } 63 }; 64 } 65 66 /** 67 * Adds entry logging code to the given class. 68 * @param args the name of the class file to patch 69 */ 70 public static void main(String[] args) throws IOException 71 { 72 if (args.length == 0) 73 { 74 System.out.println("USAGE: java bytecodeAnnotations.EntryLogger classfile"); 75 System.exit(1); 76 } 77 Path path = Paths.get(args[0]); 78 ClassReader reader = new ClassReader(Files.newInputStream(path)); 79 ClassWriter writer = new ClassWriter( 80 ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); 81 EntryLogger entryLogger = new EntryLogger(writer, 82 path.toString().replace(".class", "").replaceAll("[/\\\\]", ".")); 83 reader.accept(entryLogger, ClassReader.EXPAND_FRAMES); 84 Files.write(Paths.get(args[0]), writer.toByteArray()); 85 } 86 }
Listing 8.13 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 = "com.horstmann") 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 = "com.horstmann") 52 public int hashCode() 53 { 54 return Objects.hash(description, partNumber); 55 } 56 }
Listing 8.14 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("com.horstmann").setLevel(Level.FINEST); 15 Handler handler = new ConsoleHandler(); 16 handler.setLevel(Level.FINEST); 17 Logger.getLogger("com.horstmann").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 }
8.7.2 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.
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:
javac -classpath .:asm/lib/\* bytecodeAnnotations/EntryLoggingAgent.java jar cvfm EntryLoggingAgent.jar bytecodeAnnotations/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 set/SetTest.java java -javaagent:EntryLoggingAgent.jar=set.Item -classpath .:asm/lib/\* set.SetTest
The Item argument is the name of the class that the agent should modify.
Listing 8.15 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 bytecodes. However, the modified bytecodes are not saved to a file. Instead, the transformer returns them for loading into the virtual machine (see Figure 8.3). In other words, this technique carries out “just in time” modification of the bytecodes.
Figure 8.3 Modifying classes at load time
Listing 8.15 bytecodeAnnotations/EntryLoggingAgent.java
1 package bytecodeAnnotations; 2 3 import java.lang.instrument.*; 4 5 import org.objectweb.asm.*; 6 7 /** 8 * @version 1.10 2016-05-10 9 * @author Cay Horstmann 10 */ 11 public class EntryLoggingAgent 12 { 13 public static void premain(final String arg, Instrumentation instr) 14 { 15 instr.addTransformer((loader, className, cl, pd, data) -> 16 { 17 if (!className.equals(arg)) return null; 18 ClassReader reader = new ClassReader(data); 19 ClassWriter writer = new ClassWriter( 20 ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); 21 EntryLogger el = new EntryLogger(writer, className); 22 reader.accept(el, ClassReader.EXPAND_FRAMES); 23 return writer.toByteArray(); 24 }); 25 } 26 }
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, we’ll move on to an entirely different topic: security. Security has always been a core feature of the Java platform. As the world in which we live and compute gets more dangerous, a thorough understanding of Java security will be of increasing importance for many developers.