Home > Articles

This chapter is from the book

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:

  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 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:

  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:

    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

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.

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