[JBoss-user] [Javassist user questions] - Re: Generating source code in adition to class files
I will keep that in mind, that a decompiler might struggle with a manipulated bytecode. Thanks for the advice. For your interest Dennis Sosnoski, who as written about Javassist - I think you reference his articles, is talking about writting a library that can manipulate bytecodes and generate source. His application is the same as mine, i.e. he wants to use apt and has also had trouble debugging code for which no source exists. I think like me he is mainly interested in generating source from classes written from scratch, not from existing classes that have had their bytecode manipulated. Also people are developing plug-ins for IDEs that allow source manipulation, e.g. JackPot for Netbeans. I think people are working on ones for Eclipse and IdeaJ also. I assume all this interest is like myself, because of apt. View the original post : http://www.jboss.org/index.html?module=bbop=viewtopicp=3886020#3886020 Reply to the post : http://www.jboss.org/index.html?module=bbop=postingmode=replyp=3886020 --- SF.Net email is sponsored by: Discover Easy Linux Migration Strategies from IBM. Find simple to follow Roadmaps, straightforward articles, informative Webcasts and more! Get everything you need to get up to speed, fast. http://ads.osdn.com/?ad_id=7477alloc_id=16492op=click ___ JBoss-user mailing list JBoss-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jboss-user
[JBoss-user] [Javassist user questions] - Re: Generating source code in adition to class files
@genman, Thanks for the tip - I will give jode a try View the original post : http://www.jboss.org/index.html?module=bbop=viewtopicp=3885692#3885692 Reply to the post : http://www.jboss.org/index.html?module=bbop=postingmode=replyp=3885692 --- SF.Net email is sponsored by: Discover Easy Linux Migration Strategies from IBM. Find simple to follow Roadmaps, straightforward articles, informative Webcasts and more! Get everything you need to get up to speed, fast. http://ads.osdn.com/?ad_id=7477alloc_id=16492op=click ___ JBoss-user mailing list JBoss-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jboss-user
[JBoss-user] [Javassist user questions] - Generating source code in adition to class files
Hi, Have you ever thought of adding the ability for Javassist to spit out source as well as class files? In particular to have in CtClass methods in addition to toClass and writeFile that output source. EG toSource and writeSource. I am looking at using the annotation processing tool (apt) and this would be a useful feature since you can write source files with apt. I could then easily generate either a class file or a source file depending on what is most appropriate. The reason for wanting to produce a source file is that it is easier to debug programs when source is available and it is generally easier for someone to understand what is going on if they can see the generated source. However it is not always appropriate to generate source and therefore it would be nice to have the option of generating either. In the extreme case full decompilation from class to source would be nice, but even lesser facilities like only source generation if original source were available or only source if it is a new class would still be useful. Any thoughts of expansion into this area? Keep up the good work, Howard. View the original post : http://www.jboss.org/index.html?module=bbop=viewtopicp=3884947#3884947 Reply to the post : http://www.jboss.org/index.html?module=bbop=postingmode=replyp=3884947 --- SF.Net email is sponsored by: Discover Easy Linux Migration Strategies from IBM. Find simple to follow Roadmaps, straightforward articles, informative Webcasts and more! Get everything you need to get up to speed, fast. http://ads.osdn.com/?ad_id=7477alloc_id=16492op=click ___ JBoss-user mailing list JBoss-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jboss-user
[JBoss-user] [Javassist user questions] - Re: Setting a field in a constructor before super is called
You can by the way use Javassist to do what I want, but it is a bit of a pain! You can write a program that modifies Derived above so that it calls super after initializing the field: | package examples.vmts.initializationorder.chibaexample; | | | import javassist.*; | import javassist.bytecode.*; | | | public class ModifyDerived { | public static void main( final String[] notUsed ) throws NotFoundException, | BadBytecode, | CannotCompileException, | java.io.IOException { | final CtClass ctClass = ClassPool.getDefault() | .getAndRename( examples.vmts.initializationorder.chibaexample.Derived, | examples.vmts.initializationorder.chibaexample.ModifiedDerived ); | ctClass.replaceClassName( | examples.vmts.initializationorder.chibaexample.ExternalTest, | examples.vmts.initializationorder.chibaexample.ModifiedTest ); | | final ClassFile clazz = ctClass.getClassFile(); | final MethodInfo constructor = clazz.getMethod( MethodInfo.nameInit ); | final CodeAttribute code = constructor.getCodeAttribute(); | final byte[] bytes = code.getCode(); | final CodeIterator codeIter = code.iterator(); | codeIter.skipSuperConstructor(); // move to super call, this is what skipSuperConstructor does! | | if ( codeIter.hasNext() ) { // need to modify clazz, i.e. super not last opcode | | final int superSize = codeIter.next(); // index of opcode after super call | final int rest = bytes.length - superSize - 1; // number of bytes up to return opcode | final byte[] superCall = new byte[ superSize ]; | System.arraycopy( bytes, 0, superCall, 0, superSize ); | System.arraycopy( bytes, superSize, bytes, 0, rest ); | System.arraycopy( superCall, 0, bytes, rest, superSize ); | } | | ctClass.writeFile( C:\\Personal\\Java\\ ); | } | } | The above takes class Derived and modifies it, the new class is similar except that: 1. Its name is ModifiedDerived 2. Its field and constructor arguments are of type ModifiedTest 3. And this is the important one, it calls super after it has initialized the field You can test the modified class with: | package examples.vmts.initializationorder.chibaexample; | | | public class ModifiedTest { | int field = 1; | | Base base() { | return new ModifiedDerived( this ); | } | | public static void main( final String[] notUsed ) { | new ModifiedTest().base(); | } | } | Which gives the expected result of 1. As I said at the start all this is a bit of a pain, any chance that the Javassist compiler will relax the restriction that super must be first? Keep up the good work, Howard. View the original post : http://www.jboss.org/index.html?module=bbop=viewtopicp=3882447#3882447 Reply to the post : http://www.jboss.org/index.html?module=bbop=postingmode=replyp=3882447 --- SF.Net email is sponsored by: Discover Easy Linux Migration Strategies from IBM. Find simple to follow Roadmaps, straightforward articles, informative Webcasts and more! Get everything you need to get up to speed, fast. http://ads.osdn.com/?ad_id=7477alloc_id=16492op=click ___ JBoss-user mailing list JBoss-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jboss-user
[JBoss-user] [Javassist user questions] - Re: Setting a field in a constructor before super is called
My Sun 1.4.2 javac initializes the pointer first. Also see the end of the first sub-section in the how it works section of the 1997 specification: http://www.flex-compiler.csail.mit.edu/jdk/guide/innerclasses/spec/innerclasses.doc2.html This implies that it is erronous not to initialize this$0 before calling super. View the original post : http://www.jboss.org/index.html?module=bbop=viewtopicp=3882568#3882568 Reply to the post : http://www.jboss.org/index.html?module=bbop=postingmode=replyp=3882568 --- SF.Net email is sponsored by: Discover Easy Linux Migration Strategies from IBM. Find simple to follow Roadmaps, straightforward articles, informative Webcasts and more! Get everything you need to get up to speed, fast. http://ads.osdn.com/?ad_id=7477alloc_id=16492op=click ___ JBoss-user mailing list JBoss-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jboss-user
[JBoss-user] [Javassist user questions] - Re: Setting a field in a constructor before super is called
I am pretty certain I am right, inner classes initialize their pointer to the outer field before calling their own super. EG: | abstract class Base { | Base() { | System.out.println( getField() ); | } | | abstract int getField(); | } | The constructor calls the abstract method getField and if this abstract method is implemented with an instance inner class then all is OK, e.g.: | public class InnerTest { | int field = 1; | | Base base() { | return new Base() { | int getField() { | return field; | } | }; | } | | public static void main( final String[] notUsed ) { | new InnerTest().base(); | } | } | This prints 1 as you would expect. But if you hand code the inner class then it won't work, e.g.: | class Derived extends Base { | final ExternalTest outer; | | Derived( final ExternalTest outer ) { | this.outer = outer; | } | | int getField() { | return outer.field; | } | } | | public class ExternalTest { | int field = 1; | | Base base() { | return new Derived( this ); | } | | public static void main( final String[] notUsed ) { | new ExternalTest().base(); | } | } | This gives a NullPointerException since the field outer is not initialized when constructor Derived calls its super constructor, Base. Also: you can see if you dissassemble the code for InternalTest$1 that it initializes its pointer to the outer class, this$0, before calling its super. What the inner class does is equivalent to: | Derived( final ExternalTest outer ) { | this.outer = outer; | super(); | } | } | Which is illegal in Java because super is always called first. My question is how can I do this in Javassist which also requires the call to super to be the first line. View the original post : http://www.jboss.org/index.html?module=bbop=viewtopicp=3882299#3882299 Reply to the post : http://www.jboss.org/index.html?module=bbop=postingmode=replyp=3882299 --- SF.Net email is sponsored by: Discover Easy Linux Migration Strategies from IBM. Find simple to follow Roadmaps, straightforward articles, informative Webcasts and more! Get everything you need to get up to speed, fast. http://ads.osdn.com/?ad_id=7477alloc_id=16492op=click ___ JBoss-user mailing list JBoss-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jboss-user
[JBoss-user] [Javassist user questions] - Setting a field in a constructor before super is called
I want to set a field in a constructor before super is called so that the super constructor can use the field, e.g.: | abstract class Base { | Base() { System.out.println( getField() ); } | abstract int getField(); | } | | class Derived extends Base { | int field; | Derived() { | field = 1; | super(); | } | int getField() { return field; } | } | The above isn't valid Java (the call to super must be the first line of constructor Derived) and it is also not valid in Javassist. Is there a way round this in Javassist? Note: this setting of a field before calling super is similar to how instance inner (nested) classes work, they set the field that points to their outer class before calling their own super constructor. View the original post : http://www.jboss.org/index.html?module=bbop=viewtopicp=3882208#3882208 Reply to the post : http://www.jboss.org/index.html?module=bbop=postingmode=replyp=3882208 --- SF.Net email is sponsored by: Discover Easy Linux Migration Strategies from IBM. Find simple to follow Roadmaps, straightforward articles, informative Webcasts and more! Get everything you need to get up to speed, fast. http://ads.osdn.com/?ad_id=7477alloc_id=16492op=click ___ JBoss-user mailing list JBoss-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jboss-user
[JBoss-user] [Javassist user questions] - get finds classes it shouldn't
If a class is compiled that does not have a package statement but is mistakenly loaded in Javassist as though it did have a package statement then Javassist will not detect this error and find the class and some methods will give incorrect results but not thow an exception whilst others will throw an exceptoion. EG: | // NoPackage.java - note no package statement | public class NoPackage {} | | // NoPackageTest.java - note has package statement | package examples.javassisttests; | | import javassist.*; | | | public class NoPackageTest { | public static void main( final String[] notUsed ) throws NotFoundException { | final CtClass noPackage = ClassPool.getDefault().get( examples.javassisttests.NoPackage ); | System.out.println( package = + noPackage.getPackageName() ); | System.out.println( modifiers = + noPackage.getModifiers() ); | } | } | When run, the above produces: C:\Personal\Javajava -classpath javassist.jar;. examples.javassisttests.NoPackageTest package = examples.javassisttests Exception in thread main java.lang.RuntimeException: NoPackage in examples/javassisttests/NoPackage.java at javassist.CtClassType.getClassFile2(CtClassType.java:191) at javassist.CtClassType.getModifiers(CtClassType.java:343) at examples.javassisttests.NoPackageTest.main(NoPackageTest.java:10) Showing that get found the class even though it shouldn't, getPackageName ran but gave the incorrect package, and getModifiers threw an exception with a less then ideal error message. May I ask: 1. Is a known bug? 2. Is there a fix? 3. Any suggestions on how to code round the problem? Keep up the good work, Howard. View the original post : http://www.jboss.org/index.html?module=bbop=viewtopicp=3872252#3872252 Reply to the post : http://www.jboss.org/index.html?module=bbop=postingmode=replyp=3872252 --- This SF.net email is sponsored by Demarc: A global provider of Threat Management Solutions. Download our HomeAdmin security software for free today! http://www.demarc.com/info/Sentarus/hamr30 ___ JBoss-user mailing list JBoss-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jboss-user
[JBoss-user] [Javassist user questions] - Re: Array Class Literals
First two appologies: I don't seem to be able to watch this thread so I missed your post and secondly my test class is a little long :( . Take a look at lines 75 to 90 which demonstrate the problem I am having with array class literals and also with testing for null on primitive arrays (see seperate post to this forum for null problems). | package examples.javassisttests.poolget; | | import javassist.*; | import pec.compile.*; | import java.io.*; | import java.lang.reflect.*; | | public class Test { | static class Y {} | static class X extends Y {} | public static void main( final String[] notUsed ) throws Exception { | // Test of assignment compatability | final X[] xs = new X[ 1 ]; | final Y[] ys = xs; | ys[ 0 ] = new X(); // OK | // ys[ 0 ] = new Y(); // ArrayStoreException | final ClassPool pool = ClassPool.getDefault(); | final CtClass intClass = pool.get( int ); | final CtClass longClass = pool.get( long ); | System.out.println( Class: + long.class.isAssignableFrom( int.class ) ); | System.out.println( CtClass: + longClass.subtypeOf( intClass ) ); | | // get classes | System.out.println( int = + intClass ); | final CtClass intArrayClass = pool.get( int[] ); | System.out.println( int[] = + intArrayClass ); | final CtClass objectArrayClass = pool.get( java.lang.Object[] ); | System.out.println( Object[] = + objectArrayClass ); | | // class definition (AbstractTest) | final CtClass clazz = pool.makeClass( examples.javassisttests.poolget.AbstractTest ); | clazz.setModifiers( javassist.Modifier.ABSTRACT ); | clazz.setModifiers( javassist.Modifier.PUBLIC ); | | // protected constructor definition | final CtConstructor constructor = CtNewConstructor.defaultConstructor( | clazz ); | constructor.setModifiers( javassist.Modifier.PROTECTED ); | clazz.addConstructor( constructor ); | | // abstract method definition | final CtClass[] arguments = new CtClass[] { intClass, intArrayClass, objectArrayClass }; | final CtMethod method = CtNewMethod.abstractMethod( | intClass, | f, | arguments, | null, | clazz ); | clazz.addMethod( method ); | | // write the abstract class | clazz.writeFile( C:\\Personal\\Java ); | | // print the abstract class | final Class classOfClazz = Class.forName( examples.javassisttests.poolget.AbstractTest ); | System.out.println(); | Utilities.printSignature( classOfClazz, null ); | | // class definition (DerivedTest) | final CtClass derivedClass = pool.makeClass( | examples.javassisttests.poolget.DerivedTest, clazz ); | derivedClass.setModifiers( javassist.Modifier.PUBLIC ); | | // public constructor definition | final CtConstructor derivedConstructor = CtNewConstructor.defaultConstructor( | derivedClass ); | derivedClass.addConstructor( derivedConstructor ); | | // method definition | final CtMethod derivedMethod = CtNewMethod.make( | intClass, | f, | arguments, | null, | /*{\n + | if ( $2 != null )\n + | return $2.length;\n + | if ( $3 instanceof Object[].class )\n + | return $3.length;\n + | return $1;\n + | },*/ | {\n + | if ( System.identityHashCode( $2 ) != 0 )\n + | return $2.length;\n + | Class array = java.lang.reflect.Array.newInstance( Object.class, 0 ).getClass();\n + | if ( array.isInstance( $3 ) )\n + | return $3.length;\n + | return $1;\n + | },
[JBoss-user] [Javassist user questions] - Re: Array Class Literals
3 couldn't when I tried the above example. Howard. View the original post : http://www.jboss.org/index.html?module=bbop=viewtopicp=3869965#3869965 Reply to the post : http://www.jboss.org/index.html?module=bbop=postingmode=replyp=3869965 --- SF email is sponsored by - The IT Product Guide Read honest candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now. http://ads.osdn.com/?ad_id=6595alloc_id=14396op=click ___ JBoss-user mailing list JBoss-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jboss-user
[JBoss-user] [Javassist user questions] - Array Class Literals
Hi, The following code fails in the Javassist compiler: if ( $3 instanceof Object[].class ) | return $3.length; Instead I have to use: Class array = java.lang.reflect.Array.newInstance( Object.class, 0 ).getClass(); | if ( array.isInstance( $3 ) ) | return $3.length; Are there plans to add array class literals? Is there a better work around? Keep up the good work - Howard. View the original post : http://www.jboss.org/index.html?module=bbop=viewtopicp=3868130#3868130 Reply to the post : http://www.jboss.org/index.html?module=bbop=postingmode=replyp=3868130 --- SF email is sponsored by - The IT Product Guide Read honest candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now. http://ads.osdn.com/?ad_id=6595alloc_id=14396op=click ___ JBoss-user mailing list JBoss-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jboss-user
[JBoss-user] [Javassist user questions] - Primitive arrays compared to null
Hi, The following code fails in the Javassist compiler when $2 is a primitive array: if ( $2 != null ) | return $2.length; Instead I have to use: if ( System.identityHashCode( $2 ) != 0 ) | return $2.length; Are there plans to add support for comparing primitive arrays to null? Is there a better work around? Keep up the good work - Howard. View the original post : http://www.jboss.org/index.html?module=bbop=viewtopicp=3868132#3868132 Reply to the post : http://www.jboss.org/index.html?module=bbop=postingmode=replyp=3868132 --- SF email is sponsored by - The IT Product Guide Read honest candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now. http://ads.osdn.com/?ad_id=6595alloc_id=14396op=click ___ JBoss-user mailing list JBoss-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jboss-user
[JBoss-user] [Javassist user questions] - Re: Array Class Literals
Ignore - I forgot to watch the topic :) View the original post : http://www.jboss.org/index.html?module=bbop=viewtopicp=3868135#3868135 Reply to the post : http://www.jboss.org/index.html?module=bbop=postingmode=replyp=3868135 --- SF email is sponsored by - The IT Product Guide Read honest candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now. http://ads.osdn.com/?ad_id=6595alloc_id=14396op=click ___ JBoss-user mailing list JBoss-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jboss-user
[JBoss-user] [Javassist user questions] - Re: Primitive arrays compared to null
Ignore - I forgot to watch the topic :) View the original post : http://www.jboss.org/index.html?module=bbop=viewtopicp=3868136#3868136 Reply to the post : http://www.jboss.org/index.html?module=bbop=postingmode=replyp=3868136 --- SF email is sponsored by - The IT Product Guide Read honest candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now. http://ads.osdn.com/?ad_id=6595alloc_id=14396op=click ___ JBoss-user mailing list JBoss-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jboss-user
[JBoss-user] [Javassist user questions] - Re: Bug in getDeclaringClass in 3.0RC1
chiba wrote : I have implemented getEnclosingClass(). | The source is in CVS HEAD. Thanks - I look forward to the next release Keep up the good work View the original post : http://www.jboss.org/index.html?module=bbop=viewtopicp=3863341#3863341 Reply to the post : http://www.jboss.org/index.html?module=bbop=postingmode=replyp=3863341 --- This SF.Net email is sponsored by: IntelliVIEW -- Interactive Reporting Tool for open source databases. Create drag--drop reports. Save time by over 75%! Publish reports on the web. Export to DOC, XLS, RTF, etc. Download a FREE copy at http://www.intelliview.com/go/osdn_nl ___ JBoss-user mailing list JBoss-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jboss-user
[JBoss-user] [Javassist user questions] - Re: Bug in getDeclaringClass in 3.0RC1
I think I can answer my own question :) In java.lang.Class the method getDeclaringClass returns null for an anonymous class because the Java Language Specification doesn't list anonymous classes as members of a class (but strangely does list named classes as members). I suspect therefore that CtClass's getDeclaringClass is mimicing this behaviour when it throws a NotFoundException for an anonymous class. Sun have introduced in J5 a new method, getEnclosingClass, which works with anonymous and named classes and gives the outer class. Therefore, are there plans to add getEnclosingClass to Javassist? View the original post : http://www.jboss.org/index.html?module=bbop=viewtopicp=3860984#3860984 Reply to the post : http://www.jboss.org/index.html?module=bbop=postingmode=replyp=3860984 --- The SF.Net email is sponsored by: Beat the post-holiday blues Get a FREE limited edition SourceForge.net t-shirt from ThinkGeek. It's fun and FREE -- well, almosthttp://www.thinkgeek.com/sfshirt ___ JBoss-user mailing list JBoss-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jboss-user
[JBoss-user] [Javassist user questions] - Bug in getDeclaringClass in 3.0RC1
There is a bug in the getDeclaringClass method of CtClass when you try and get the declaring class of an anonymous inner class it throws FileNotFoundException. EG: package examples.javassisttests.declaringclasses; import javassist.*; public class Test { public static void main( final String[] notUsed ) throws NotFoundException { new Object() {}; // annonymous inner class final CtClass inner = ClassPool.getDefault().get( examples.javassisttests.declaringclasses.Test$1 ); System.out.println( Inner class = + inner.getName() ); System.out.println( Outer class (direct) = + ClassPool.getDefault().get( examples.javassisttests.declaringclasses.Test ).getName() ); System.out.println( Outer class (from inner) = + inner.getDeclaringClass().getName() ); } } Produces: Inner class = examples.javassisttests.declaringclasses.Test$1 Outer class (direct) = examples.javassisttests.declaringclasses.Test javassist.NotFoundException at javassist.ClassPool.get(ClassPool.java:295) at javassist.CtClassType.getDeclaringClass(CtClassType.java:412) at examples.javassisttests.declaringclasses.Test.main(Test.java:9) Exception in thread main View the original post : http://www.jboss.org/index.html?module=bbop=viewtopicp=3860260#3860260 Reply to the post : http://www.jboss.org/index.html?module=bbop=postingmode=replyp=3860260 --- The SF.Net email is sponsored by: Beat the post-holiday blues Get a FREE limited edition SourceForge.net t-shirt from ThinkGeek. It's fun and FREE -- well, almosthttp://www.thinkgeek.com/sfshirt ___ JBoss-user mailing list JBoss-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jboss-user