You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Geoff Longman <gl...@intelligentworks.com> on 2004/03/26 17:44:56 UTC

[Hivemind] [PATCH] ClassFab/MethodFab patch for

Ran into a problem creating an interceptor method that calls another
interceptor method that has not been added yet.

Javassist's compiler doesn't like that. Fortunately, Javassist  provides a
workaround.
One needs to add both the methods as abstract, then set the bodies on them.
Lastly, because adding an abstract method to a CtClass make the class
abstract, one must be able to reset the class modifier to public (non
abstract).

Attached is are patches for the following classes:

ClassFab:

adds a method signature removeAbstractClassModifier() for removing an
abstract modifier from the class

MethodFab:

adds a setBody() method, so you can set the method body after creating the
method

ClassFabImpl:

calling addMethod with a null body will still add the method, but with an
abstract modifier.
has an impementation of removeAbstractClassModifier

MethodFabImpl

has an implementation of setBody()


example:

ClassFab classFab = fClassFactory.newClass(... blah .. blah ..);

MethodFab method1 = classFab.addMethod(
        Modifier.PUBLIC,
        "methodA",
        Void.TYPE,
        new Class[] { },
        new Class[] { },
        null);

MethodFab method2 = classFab.addMethod(
        Modifier.PUBLIC,
        "methodB",
        Void.TYPE,
        new Class[] { },
        new Class[] { },
        null);

method1.setBody( .. stuff that calls method2 ..);
method2.setBody( .. stuff ..);

ClassFab.removeAbstractClassModifier() ;

Class resultClass = classFab.createClass();



Geoff

offrey Longman
Intelligent Works Inc.

Re: [Hivemind] [PATCH] ClassFab/MethodFab patch for

Posted by Geoff Longman <gl...@intelligentworks.com>.
> One caveat: I don't think removeAbstractClassModifier() is needed. It can
just be assumed (ClassFab
> exists to create concrete classes, not abstract classes), with the logic
rolled into createClass().
>

Sure.  I forgot to add in my last that the patch represents a hackish
solution I made so I could get past the problem.

Geoff
----- Original Message -----
From: "Howard M. Lewis Ship" <hl...@comcast.net>
To: "'Jakarta Commons Developers List'" <co...@jakarta.apache.org>
Sent: Friday, March 26, 2004 1:17 PM
Subject: RE: [Hivemind] [PATCH] ClassFab/MethodFab patch for


> This is cool. I think this is a good change.
>
> --
> Howard M. Lewis Ship
> Independent J2EE / Open-Source Java Consultant
> Creator, Tapestry: Java Web Components
> http://howardlewisship.com
>
>
> > -----Original Message-----
> > From: Geoff Longman [mailto:glongman@intelligentworks.com]
> > Sent: Friday, March 26, 2004 11:45 AM
> > To: Jakarta Commons Developers List
> > Subject: [Hivemind] [PATCH] ClassFab/MethodFab patch for
> >
> >
> > Ran into a problem creating an interceptor method that calls another
> > interceptor method that has not been added yet.
> >
> > Javassist's compiler doesn't like that. Fortunately,
> > Javassist  provides a
> > workaround.
> > One needs to add both the methods as abstract, then set the
> > bodies on them.
> > Lastly, because adding an abstract method to a CtClass make the class
> > abstract, one must be able to reset the class modifier to public (non
> > abstract).
> >
> > Attached is are patches for the following classes:
> >
> > ClassFab:
> >
> > adds a method signature removeAbstractClassModifier() for removing an
> > abstract modifier from the class
> >
> > MethodFab:
> >
> > adds a setBody() method, so you can set the method body after
> > creating the
> > method
> >
> > ClassFabImpl:
> >
> > calling addMethod with a null body will still add the method,
> > but with an
> > abstract modifier.
> > has an impementation of removeAbstractClassModifier
> >
> > MethodFabImpl
> >
> > has an implementation of setBody()
> >
> >
> > example:
> >
> > ClassFab classFab = fClassFactory.newClass(... blah .. blah ..);
> >
> > MethodFab method1 = classFab.addMethod(
> >         Modifier.PUBLIC,
> >         "methodA",
> >         Void.TYPE,
> >         new Class[] { },
> >         new Class[] { },
> >         null);
> >
> > MethodFab method2 = classFab.addMethod(
> >         Modifier.PUBLIC,
> >         "methodB",
> >         Void.TYPE,
> >         new Class[] { },
> >         new Class[] { },
> >         null);
> >
> > method1.setBody( .. stuff that calls method2 ..);
> > method2.setBody( .. stuff ..);
> >
> > ClassFab.removeAbstractClassModifier() ;
> >
> > Class resultClass = classFab.createClass();
> >
> >
> >
> > Geoff
> >
> > offrey Longman
> > Intelligent Works Inc.
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


RE: [Hivemind] [PATCH] ClassFab/MethodFab patch for

Posted by "Howard M. Lewis Ship" <hl...@comcast.net>.
This is cool. I think this is a good change.

One caveat: I don't think removeAbstractClassModifier() is needed. It can just be assumed (ClassFab
exists to create concrete classes, not abstract classes), with the logic rolled into createClass().

--
Howard M. Lewis Ship
Independent J2EE / Open-Source Java Consultant
Creator, Tapestry: Java Web Components 
http://howardlewisship.com


> -----Original Message-----
> From: Geoff Longman [mailto:glongman@intelligentworks.com] 
> Sent: Friday, March 26, 2004 11:45 AM
> To: Jakarta Commons Developers List
> Subject: [Hivemind] [PATCH] ClassFab/MethodFab patch for 
> 
> 
> Ran into a problem creating an interceptor method that calls another
> interceptor method that has not been added yet.
> 
> Javassist's compiler doesn't like that. Fortunately, 
> Javassist  provides a
> workaround.
> One needs to add both the methods as abstract, then set the 
> bodies on them.
> Lastly, because adding an abstract method to a CtClass make the class
> abstract, one must be able to reset the class modifier to public (non
> abstract).
> 
> Attached is are patches for the following classes:
> 
> ClassFab:
> 
> adds a method signature removeAbstractClassModifier() for removing an
> abstract modifier from the class
> 
> MethodFab:
> 
> adds a setBody() method, so you can set the method body after 
> creating the
> method
> 
> ClassFabImpl:
> 
> calling addMethod with a null body will still add the method, 
> but with an
> abstract modifier.
> has an impementation of removeAbstractClassModifier
> 
> MethodFabImpl
> 
> has an implementation of setBody()
> 
> 
> example:
> 
> ClassFab classFab = fClassFactory.newClass(... blah .. blah ..);
> 
> MethodFab method1 = classFab.addMethod(
>         Modifier.PUBLIC,
>         "methodA",
>         Void.TYPE,
>         new Class[] { },
>         new Class[] { },
>         null);
> 
> MethodFab method2 = classFab.addMethod(
>         Modifier.PUBLIC,
>         "methodB",
>         Void.TYPE,
>         new Class[] { },
>         new Class[] { },
>         null);
> 
> method1.setBody( .. stuff that calls method2 ..);
> method2.setBody( .. stuff ..);
> 
> ClassFab.removeAbstractClassModifier() ;
> 
> Class resultClass = classFab.createClass();
> 
> 
> 
> Geoff
> 
> offrey Longman
> Intelligent Works Inc.
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org