Home > Articles > Programming > Java

This chapter is from the book

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:

  1. Load the bytecodes in the class file.
  2. Locate all methods.
  3. For each method, check whether it has a LogEntry annotation.
  4. 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:

  1. 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.

  2. Make a manifest file EntryLoggingAgent.mf that sets the Premain-Class attribute, for example:
    Premain-Class: bytecodeAnnotations.EntryLoggingAgent
  3. 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

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.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020