You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Robin Helgelin <lo...@gmail.com> on 2008/02/19 18:31:33 UTC

Class transformation

Hi,

I'm trying to do a simple prefix and extend to a single method,
however, it seems that ClassTransformation.extendMethod(), doesn't
find variables declared with prefixMethod().

Simple example which throws a javassist.CannotCompileException:

        transformation.prefixMethod(method, "int i = 1;");
        transformation.extendMethod(method, "i += 1;");

I also tried expanding the method by wrapping it, but that seems to
break @Persist:et members. I attach my attempt. It seems the problem
is that Tapestry only scans the new method, not the old one copied, am
I correct?

    public void renameAndExtendMethod(TransformMethodSignature
methodSignature, String newMethodName, String methodBody)
    {
        failIfFrozen();

        CtMethod methodOld = findMethod(methodSignature);
        methodOld.setName(newMethodName);

        CtMethod methodNew;
        try
        {
             methodNew = CtNewMethod.copy(methodOld,
                     methodSignature.getMethodName(),
                     _ctClass,
                     null);
             methodNew.setBody(methodBody);
             _ctClass.addMethod(methodNew);
        }
        catch (CannotCompileException ex)
        {
            throw new
MethodCompileException(ServicesMessages.methodCompileError(
                    methodSignature,
                    methodBody,
                    ex), methodBody, ex);
        }

        addMethodToDescription("renameAndExtend", methodSignature, methodBody);
        _addedMethods.add(methodOld);
    }

-- 
        regards,
        Robin

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Class transformation

Posted by Robin Helgelin <lo...@gmail.com>.
On Wed, Feb 20, 2008 at 5:37 PM, Filip S. Adamsen <fs...@fsadev.com> wrote:
> Sorry, my bad. That is indeed not possible at the moment.

I did post and example in my first message in this thread where I was
playing with it, but I couldn't get Tapestry to process my renamed
method, so I gave it a rest.

-- 
 regards,
 Robin

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Class transformation

Posted by "Filip S. Adamsen" <fs...@fsadev.com>.
Sorry, my bad. That is indeed not possible at the moment.

-Filip

Robin Helgelin skrev:
> On Wed, Feb 20, 2008 at 3:35 PM, Filip S. Adamsen <fs...@fsadev.com> wrote:
>> Can't you just override the method instead and call the super method at
>>  the right time?
> 
> Sure, but currently Tapestry class transformation doesn't support
> this. Or maybe I'm just blind :)
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Class transformation

Posted by Robin Helgelin <lo...@gmail.com>.
On Wed, Feb 20, 2008 at 3:35 PM, Filip S. Adamsen <fs...@fsadev.com> wrote:
> Can't you just override the method instead and call the super method at
>  the right time?

Sure, but currently Tapestry class transformation doesn't support
this. Or maybe I'm just blind :)

-- 
 regards,
 Robin

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Class transformation

Posted by "Filip S. Adamsen" <fs...@fsadev.com>.
Can't you just override the method instead and call the super method at 
the right time?

-Filip

Robin Helgelin skrev:
> On Feb 19, 2008 8:47 PM, Howard Lewis Ship <hl...@gmail.com> wrote:
>> I think it's because the code you add is effectively inside a block,
>> i.e. in Java it would be:
>> {
>>   int i = 5;
>> }
>>
>> So the scope of the new variable is limited.  That means you need to
>> generate all the code that uses the variable as a single body passed
>> to ClassTransformation.
> 
> Ah, ok. That seems problematic, as I want to both prefix and expand. I
> guess it could be solved by adding a field to the class instead.
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Class transformation

Posted by Robin Helgelin <lo...@gmail.com>.
On Feb 19, 2008 8:47 PM, Howard Lewis Ship <hl...@gmail.com> wrote:
> I think it's because the code you add is effectively inside a block,
> i.e. in Java it would be:
> {
>   int i = 5;
> }
>
> So the scope of the new variable is limited.  That means you need to
> generate all the code that uses the variable as a single body passed
> to ClassTransformation.

Ah, ok. That seems problematic, as I want to both prefix and expand. I
guess it could be solved by adding a field to the class instead.

-- 
        regards,
        Robin

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Class transformation

Posted by Howard Lewis Ship <hl...@gmail.com>.
I think it's because the code you add is effectively inside a block,
i.e. in Java it would be:
{
  int i = 5;
}

So the scope of the new variable is limited.  That means you need to
generate all the code that uses the variable as a single body passed
to ClassTransformation.

On Feb 19, 2008 10:55 AM, Robin Helgelin <lo...@gmail.com> wrote:
> On Feb 19, 2008 7:44 PM, Howard Lewis Ship <hl...@gmail.com> wrote:
> > Could the extend method be failing because it does not return a value?
>
> I tried returning a value, no change. It barfs on my variable i,
> backtrace as this:
>
> Caused by: org.apache.tapestry.internal.services.MethodCompileException:
> Error compiling method public java.lang.String onActionFromCounter()
> (i += 1;): [source error] no such field: i
>         at org.apache.tapestry.internal.services.InternalClassTransformationImpl.extendMethod(InternalClassTransformationImpl.java:625)
>         at nu.localhost.tapestry.acegi.services.internal.AcegiWorker.transformMethod(AcegiWorker.java:112)
>         at nu.localhost.tapestry.acegi.services.internal.AcegiWorker.transform(AcegiWorker.java:46)
>         at $ComponentClassTransformWorker_118330aced0.transform($ComponentClassTransformWorker_118330aced0.java)
>         at $ComponentClassTransformWorker_118330aceca.transform($ComponentClassTransformWorker_118330aceca.java)
>         at org.apache.tapestry.internal.services.ComponentClassTransformerImpl.transformComponentClass(ComponentClassTransformerImpl.java:147)
>         ... 94 more
> Caused by: javassist.CannotCompileException: [source error] no such field: i
>         at javassist.CtBehavior.insertAfter(CtBehavior.java:703)
>         at javassist.CtBehavior.insertAfter(CtBehavior.java:627)
>         at org.apache.tapestry.internal.services.InternalClassTransformationImpl.extendMethod(InternalClassTransformationImpl.java:621)
>         ... 99 more
> Caused by: compile error: no such field: i
>         at javassist.compiler.TypeChecker.fieldAccess(TypeChecker.java:812)
>         at javassist.compiler.TypeChecker.atFieldAssign(TypeChecker.java:270)
>         at javassist.compiler.JvstTypeChecker.atFieldAssign(JvstTypeChecker.java:83)
>         at javassist.compiler.TypeChecker.atAssignExpr(TypeChecker.java:229)
>         at javassist.compiler.ast.AssignExpr.accept(AssignExpr.java:38)
>         at javassist.compiler.CodeGen.doTypeCheck(CodeGen.java:241)
>         at javassist.compiler.CodeGen.atStmnt(CodeGen.java:329)
>         at javassist.compiler.ast.Stmnt.accept(Stmnt.java:49)
>         at javassist.compiler.Javac.compileStmnt(Javac.java:568)
>         at javassist.CtBehavior.insertAfterAdvice(CtBehavior.java:724)
>         at javassist.CtBehavior.insertAfter(CtBehavior.java:669)
>         ... 101 more
>
> --
>
>         regards,
>         Robin
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>



-- 
Howard M. Lewis Ship

Creator Apache Tapestry and Apache HiveMind

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Class transformation

Posted by Robin Helgelin <lo...@gmail.com>.
On Feb 19, 2008 7:44 PM, Howard Lewis Ship <hl...@gmail.com> wrote:
> Could the extend method be failing because it does not return a value?

I tried returning a value, no change. It barfs on my variable i,
backtrace as this:

Caused by: org.apache.tapestry.internal.services.MethodCompileException:
Error compiling method public java.lang.String onActionFromCounter()
(i += 1;): [source error] no such field: i
        at org.apache.tapestry.internal.services.InternalClassTransformationImpl.extendMethod(InternalClassTransformationImpl.java:625)
        at nu.localhost.tapestry.acegi.services.internal.AcegiWorker.transformMethod(AcegiWorker.java:112)
        at nu.localhost.tapestry.acegi.services.internal.AcegiWorker.transform(AcegiWorker.java:46)
        at $ComponentClassTransformWorker_118330aced0.transform($ComponentClassTransformWorker_118330aced0.java)
        at $ComponentClassTransformWorker_118330aceca.transform($ComponentClassTransformWorker_118330aceca.java)
        at org.apache.tapestry.internal.services.ComponentClassTransformerImpl.transformComponentClass(ComponentClassTransformerImpl.java:147)
        ... 94 more
Caused by: javassist.CannotCompileException: [source error] no such field: i
        at javassist.CtBehavior.insertAfter(CtBehavior.java:703)
        at javassist.CtBehavior.insertAfter(CtBehavior.java:627)
        at org.apache.tapestry.internal.services.InternalClassTransformationImpl.extendMethod(InternalClassTransformationImpl.java:621)
        ... 99 more
Caused by: compile error: no such field: i
        at javassist.compiler.TypeChecker.fieldAccess(TypeChecker.java:812)
        at javassist.compiler.TypeChecker.atFieldAssign(TypeChecker.java:270)
        at javassist.compiler.JvstTypeChecker.atFieldAssign(JvstTypeChecker.java:83)
        at javassist.compiler.TypeChecker.atAssignExpr(TypeChecker.java:229)
        at javassist.compiler.ast.AssignExpr.accept(AssignExpr.java:38)
        at javassist.compiler.CodeGen.doTypeCheck(CodeGen.java:241)
        at javassist.compiler.CodeGen.atStmnt(CodeGen.java:329)
        at javassist.compiler.ast.Stmnt.accept(Stmnt.java:49)
        at javassist.compiler.Javac.compileStmnt(Javac.java:568)
        at javassist.CtBehavior.insertAfterAdvice(CtBehavior.java:724)
        at javassist.CtBehavior.insertAfter(CtBehavior.java:669)
        ... 101 more

-- 
        regards,
        Robin

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Class transformation

Posted by Howard Lewis Ship <hl...@gmail.com>.
Could the extend method be failing because it does not return a value?

On Feb 19, 2008 9:31 AM, Robin Helgelin <lo...@gmail.com> wrote:
> Hi,
>
> I'm trying to do a simple prefix and extend to a single method,
> however, it seems that ClassTransformation.extendMethod(), doesn't
> find variables declared with prefixMethod().
>
> Simple example which throws a javassist.CannotCompileException:
>
>         transformation.prefixMethod(method, "int i = 1;");
>         transformation.extendMethod(method, "i += 1;");
>
> I also tried expanding the method by wrapping it, but that seems to
> break @Persist:et members. I attach my attempt. It seems the problem
> is that Tapestry only scans the new method, not the old one copied, am
> I correct?
>
>     public void renameAndExtendMethod(TransformMethodSignature
> methodSignature, String newMethodName, String methodBody)
>     {
>         failIfFrozen();
>
>         CtMethod methodOld = findMethod(methodSignature);
>         methodOld.setName(newMethodName);
>
>         CtMethod methodNew;
>         try
>         {
>              methodNew = CtNewMethod.copy(methodOld,
>                      methodSignature.getMethodName(),
>                      _ctClass,
>                      null);
>              methodNew.setBody(methodBody);
>              _ctClass.addMethod(methodNew);
>         }
>         catch (CannotCompileException ex)
>         {
>             throw new
> MethodCompileException(ServicesMessages.methodCompileError(
>                     methodSignature,
>                     methodBody,
>                     ex), methodBody, ex);
>         }
>
>         addMethodToDescription("renameAndExtend", methodSignature, methodBody);
>         _addedMethods.add(methodOld);
>     }
>
> --
>         regards,
>         Robin
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>



-- 
Howard M. Lewis Ship

Creator Apache Tapestry and Apache HiveMind

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org