diff options
author | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2005-08-25 05:08:05 +0000 |
---|---|---|
committer | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2005-08-25 05:08:05 +0000 |
commit | 9366fe08625ceb75692b53cdb9be19dff2ad56c6 (patch) | |
tree | 2333f5caf415378b259986177e39078616fc12e0 /tutorial | |
parent | 2d60b1690e44dd33553e28618c7c0b25e4b34d9f (diff) | |
download | javassist-9366fe08625ceb75692b53cdb9be19dff2ad56c6.tar.gz javassist-9366fe08625ceb75692b53cdb9be19dff2ad56c6.zip |
Array initializer supports and better annotation supports.
git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@196 30ef5769-5b8d-40dd-aea6-55b5d6557bb3
Diffstat (limited to 'tutorial')
-rw-r--r-- | tutorial/tutorial2.html | 71 |
1 files changed, 67 insertions, 4 deletions
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 @@ <br><li><a href="#alter">Altering a method body</a> <br><li><a href="#add">Adding a new method or field</a> <br><li><a href="#runtime">Runtime support classes</a> +<br><li><a href="#annotation">Annotations</a> <br><li><a href="#import">Import</a> <br><li><a href="#limit">Limitations</a> </ul> @@ -1440,11 +1441,73 @@ or <code>removeMethod()</code> in <code>CtClass</code>. A <code>CtConstructor</code> can be removed by <code>removeConstructor()</code> in <code>CtClass</code>. +<p><br> + +<a name="annotation"> +<h3>4.4 Annotations</h3> + +<p><code>CtClass</code>, <code>CtMethod</code>, <code>CtField</code> +and <code>CtConstructor</code> provides a convenient method +<code>getAnnotations()</code> for reading annotations. +It returns an annotation-type object. + +<p>For example, suppose the following annotation: + +<ul><pre> +public @interface Author { + String name(); + int year(); +} +</pre></ul> + +<p>This annotation is used as the following: + +<ul><pre> +@Author(name="Chiba", year=2005) +public class Point { + int x, y; +} +</pre></ul> + +<p>Then, the value of the annotation can be obtained by +<code>getAnnotations()</code>. +It returns an array containing +annotation-type objects. + +<ul><pre> +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); +</pre></ul> + +<p>This code snippet should print: + +<ul><pre> +name: Chiba, year: 2005 +</pre></ul> + +<p> +Since the annoation of <code>Point</code> is only <code>@Author</code>, +the length of the array <code>all</code> is one +and <code>all[0]</code> is an <code>Author</code> object. +The member values of the annotation can be obtained +by calling <code>name()</code> and <code>year()</code> +on the <code>Author</code> object. + +<p>To use <code>getAnnotations()</code>, annotation types +such as <code>Author</code> must be included in the current +class path. <em>They must be also accessible from a +<code>ClassPool</code> object.</em> If the class file of an annotation +type is not found, Javassist cannot obtain the default values +of the members of that annotation type. <p><br> <a name="runtime"> -<h3>4.4 Runtime support classes</h3> +<h3>4.5 Runtime support classes</h3> <p>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. <p><br> <a name="import"> -<h3>4.5 Import</h3> +<h3>4.6 Import</h3> <p>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. <p><br> <a name="limit"> -<h3>4.6 Limitations</h3> +<h3>4.7 Limitations</h3> <p>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 <code>javassist.bytecode.annotation</code> package. <p><li>Array initializers, a comma-separated list of expressions enclosed by braces <code>{</code> and <code>}</code>, are not -supported. +available unless the array dimension is one. <p><li>Inner classes or anonymous classes are not supported. |