diff options
-rw-r--r-- | tutorial/tutorial.html | 2 | ||||
-rw-r--r-- | tutorial/tutorial2.html | 9 | ||||
-rw-r--r-- | tutorial/tutorial3.html | 31 |
3 files changed, 33 insertions, 9 deletions
diff --git a/tutorial/tutorial.html b/tutorial/tutorial.html index f7bb622a..95da0729 100644 --- a/tutorial/tutorial.html +++ b/tutorial/tutorial.html @@ -1100,6 +1100,6 @@ For more information, see the API documentation of <hr> Java(TM) is a trademark of Sun Microsystems, Inc.<br> -Copyright (C) 2000-2012 by Shigeru Chiba, All rights reserved. +Copyright (C) 2000-2014 by Shigeru Chiba, All rights reserved. </body> </html> diff --git a/tutorial/tutorial2.html b/tutorial/tutorial2.html index b3a61e7b..f2571c8b 100644 --- a/tutorial/tutorial2.html +++ b/tutorial/tutorial2.html @@ -1563,9 +1563,12 @@ has several limitations with respect to the language that the compiler can accept. Those limitations are: <p><li>The new syntax introduced by J2SE 5.0 (including enums and generics) -has not been supported. Annotations are supported only by the low level +has not been supported. Annotations are supported by the low level API of Javassist. -See the <code>javassist.bytecode.annotation</code> package. +See the <code>javassist.bytecode.annotation</code> package +(and also <code>getAnnotations()</code> +in <code>CtClass</code> and <code>CtBehavior</code>). +Generics are also only partly supported. See <a href="./tutorial3.html#generics">the latter section</a> for more details. <p><li>Array initializers, a comma-separated list of expressions enclosed by braces <code>{</code> and <code>}</code>, are not @@ -1625,6 +1628,6 @@ write: <hr> Java(TM) is a trademark of Sun Microsystems, Inc.<br> -Copyright (C) 2000-2010 by Shigeru Chiba, All rights reserved. +Copyright (C) 2000-2014 by Shigeru Chiba, All rights reserved. </body> </html> diff --git a/tutorial/tutorial3.html b/tutorial/tutorial3.html index 7c505e0b..e6964553 100644 --- a/tutorial/tutorial3.html +++ b/tutorial/tutorial3.html @@ -269,7 +269,15 @@ String s = (String)v.get(0); </pre></ul> <p>So when you write a bytecode transformer, you can just drop -off all type parameters. For example, if you have a class: +off all type parameters. Because the compiler embedded in Javassist +does not support generics, +you must insert an explicit type cast at the +caller site if the source code is compiled by Javassist, for example, +through <code>CtMethod.make()</code>. No type cast +is necessary if the source code is compiled by a normal Java compiler +such as <code>javac</code>. + +<p>For example, if you have a class: <ul><pre> public class Wrapper<T> { @@ -287,7 +295,7 @@ public interface Getter<T> { } </pre></ul> -<p>Then the interface you really have to add is <code>Getter</code> +<p>then the interface you really have to add is <code>Getter</code> (the type parameters <code><T></code> drops off) and the method you also have to add to the <code>Wrapper</code> class is this simple one: @@ -297,8 +305,20 @@ public Object get() { return value; } </pre></ul> <p>Note that no type parameters are necessary. +Since <code>get</code> returns an <code>Object</code>, an explicit type cast +is needed at the caller site if the source code is compiled by Javassist. +For example, if the type parameter <code>T</code> +is <code>String</code>, then <code>(String)</code> must be inserted as follows: + +<ul><pre> +Wrapper w = ... +String s = (String)w.get(); +</pre></ul> + +<p>The type cast is not needed if the source code is compiled by a normal Java +compiler because it will automatically insert a type cast. -<p>However, if you need to make type parameters accessible through reflection +<p>If you need to make type parameters accessible through reflection during runtime, you have to add generic signatures to the class file. For more details, see the API documentation (javadoc) of the <code>setGenericSignature</code> method in the <code>CtClass</code>. @@ -327,7 +347,8 @@ cc.addMethod(m); <p>The parameter type <code>int...</code> is changed into <code>int[]</code> and <code>Modifier.VARARGS</code> is added to the method modifiers. -<p>To call this method, you must write: +<p>To call this method in the source code compiled by the compiler embedded in Javassist, +you must write: <ul><pre> length(new int[] { 1, 2, 3 }); @@ -406,6 +427,6 @@ CtClass.debugDump = "./dump"; <hr> Java(TM) is a trademark of Sun Microsystems, Inc.<br> -Copyright (C) 2000-2012 by Shigeru Chiba, All rights reserved. +Copyright (C) 2000-2014 by Shigeru Chiba, All rights reserved. </body> </html> |