From 9366fe08625ceb75692b53cdb9be19dff2ad56c6 Mon Sep 17 00:00:00 2001 From: chiba Date: Thu, 25 Aug 2005 05:08:05 +0000 Subject: Array initializer supports and better annotation supports. git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@196 30ef5769-5b8d-40dd-aea6-55b5d6557bb3 --- tutorial/tutorial2.html | 71 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 4 deletions(-) (limited to 'tutorial') diff --git a/tutorial/tutorial2.html b/tutorial/tutorial2.html index 5befd301..31df6720 100644 --- a/tutorial/tutorial2.html +++ b/tutorial/tutorial2.html @@ -19,6 +19,7 @@
  • Altering a method body
  • Adding a new method or field
  • Runtime support classes +
  • Annotations
  • Import
  • Limitations @@ -1440,11 +1441,73 @@ or removeMethod() in CtClass. A CtConstructor can be removed by removeConstructor() in CtClass. +


    + + +

    4.4 Annotations

    + +

    CtClass, CtMethod, CtField +and CtConstructor provides a convenient method +getAnnotations() for reading annotations. +It returns an annotation-type object. + +

    For example, suppose the following annotation: + +

      +public @interface Author {
      +    String name();
      +    int year();
      +}
      +
    + +

    This annotation is used as the following: + +

      +@Author(name="Chiba", year=2005)
      +public class Point {
      +    int x, y;
      +}
      +
    + +

    Then, the value of the annotation can be obtained by +getAnnotations(). +It returns an array containing +annotation-type objects. + +

      +CtClass cc = ClassPool.getDefault().get("Point");
      +Object[] all = cc.getAnnotations();
      +Author a = (Author)all[0];
      +String name = a.name();
      +int year = a.year();
      +System.out.println("name: " + name + ", year: " + year);
      +
    + +

    This code snippet should print: + +

      +name: Chiba, year: 2005
      +
    + +

    +Since the annoation of Point is only @Author, +the length of the array all is one +and all[0] is an Author object. +The member values of the annotation can be obtained +by calling name() and year() +on the Author object. + +

    To use getAnnotations(), annotation types +such as Author must be included in the current +class path. They must be also accessible from a +ClassPool object. If the class file of an annotation +type is not found, Javassist cannot obtain the default values +of the members of that annotation type.


    -

    4.4 Runtime support classes

    +

    4.5 Runtime support classes

    In most cases, a class modified by Javassist does not require Javassist to run. However, some kinds of bytecode generated by the @@ -1458,7 +1521,7 @@ Javassist classes are never used at runtime of the modified classes.


    -

    4.5 Import

    +

    4.6 Import

    All the class names in source code must be fully qualified (they must include package names). @@ -1494,7 +1557,7 @@ must be always a fully qualified name.


    -

    4.6 Limitations

    +

    4.7 Limitations

    In the current implementation, the Java compiler included in Javassist has several limitations with respect to the language that the compiler can @@ -1507,7 +1570,7 @@ See the javassist.bytecode.annotation package.

  • Array initializers, a comma-separated list of expressions enclosed by braces { and }, are not -supported. +available unless the array dimension is one.

  • Inner classes or anonymous classes are not supported. -- cgit v1.2.3