Classworking Toolbox: Annotations (Annotation) and ASM

Source: Internet
Author: User
Tags definition final modifiers

To J2SE 5.0,sun has added many new features to the Java platform. One of the most important new features is supporting annotations. Annotations will be useful in associating multiple types of metadata with Java code, and it has been widely used in lieu of custom profiles in the new and updated JSR that extends the Java platform. In this article, I'll show you how to combine the ASM bytecode action framework with the new features of the J2SE 5.0--instrumentation package-to convert classes by annotation control when the class is loaded into the JVM.

Annotation Basics

There's been a lot of discussion about the J2SE 5.0 annotation, so I'll just make a short summary here. A comment is a metadata for Java code. Functionally similar to the increasingly popular XDoclet-style metadata for dealing with complex framework configurations, and its implementation has more in common with C # attributes.

The Java implementation of this language feature uses an interface-like structure and some special extensions of the Java language syntax. I have found that in most cases this kind of interface-like structure is ignored and the hashmap of annotations as name-value pairs is clearer. Each annotation type defines a set of fixed names associated with it. Each name may be assigned a default value, or the name will be defined each time the annotation is used. Annotations can be specified to be applied to a particular type of Java component (such as classes, fields, methods, and so on), and can even be applied to other annotations. (In fact, you limit the components that the annotation applies to by using a special predefined annotation on the definition of the annotation you want to restrict.) )

Unlike the regular interface, annotations must use the keyword @interface in the definition. Also different from the regular interface is that annotations can only define methods that have no parameters and return only simple values (base type, String, Class, enum type, annotation, and arrays of any of these types). These "methods" are the names of the values associated with the annotation.

Annotations are used as modifiers at the time of declaration, like public, final, and other keyword modifiers defined by the Java language earlier than the J2SE 5.0 version. The use of annotations is indicated by the @ symbol followed by the annotation name. If you want to assign a value to an annotation, it is given as a name-value pair in parentheses after the annotation name.

Listing 1 shows an example annotation declaration followed by the definition of a class that uses the annotation for some methods. The Logme annotation is used to mark the method that should be included in the application's log record. I've already assigned two values to this note: A level that represents the logging that the call was included in, and another that represents the name used for the method call (the default is an empty string, assuming that the code that handles the annotation will be replaced with the actual method name when there is no name). I then used the annotation for the two methods in the Stringarray class, using only the default values for the merge () method, and the IndexOf () method providing an explicit value.

Listing 1. Reflection substitution interface and its implementation

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
/**
  * Annotation for method to be included in logging.
  */
@Target({ElementType.METHOD})
public @interface LogMe {
   int level() default 0;
   String name() default "";
}
public class StringArray
{
   private final String[] m_list;

   public StringArray(String[] list) {
     ...
   }

   public StringArray(StringArray base, String[] adds) {
     ...
   }

   @LogMe private String[] merge(String[] list1, String[]list2) {
     ...
   }

   public String get(int index) {
     return m_list[index];
   }

   @LogMe(level=1, name="lookup") public int indexOf(String value) {
     ...
   }

   public int size() {
     return m_list.length;
   }
}

In the next section I'll introduce a different (I think it's more interesting) application.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.