4 Replies Latest reply on Dec 15, 2014 7:28 PM by enielsen

    $proceed($$) for private methods

    aarontdixon

      I get a compile error when Javassist is trying to compile a method replacement when the method is private.

       

      method.instrument(new ExprEditor() {

           public void edit(MethodCall m) throws CannotCompileException {

                m.replace("$_ = $proceed($$);");

           }

      }

       

      Is there a way I can get Javassist to compile invocations of private methods?

        • 1. Re: $proceed($$) for private methods
          aarontdixon

          Clarification:

           

          Javassist fails when the private method that is invoked is *static private*.

           

          Is this a defect?

          • 2. Re: $proceed($$) for private methods
            settimer

            I encoutered the same issue. Have you figured it out?

             

            Below is a mored detailed description:

             

            I try to instrument the call in the method:

             

                public static Currency getInstance(String currencyCode) {

                    return getInstance(currencyCode, Integer.MIN_VALUE);

                }

             

            , which is inside java/util/Currency.class. The call invokes "private static Currency getInstance(String, int)", which is enclosed in the same class as the caller.

             

            I need to add some statements around the call "getInstance(currencyCode, Integer.MIN_VALUE);". So I used MethodCall.replace("{some statements; $_ = $proceed($$); other statements;}"), then I got the exception like below:

             

            Caused by: compile error: Method getInstance is private

                at javassist.compiler.

            MemberCodeGen.getAccessiblePrivate(MemberCodeGen.java:671)
                at javassist.compiler.MemberCodeGen.atMethodCallCore2(MemberCodeGen.java:612)
                 at javassist.compiler.MemberCodeGen.atMethodCallCore(MemberCodeGen.java:575)
                at javassist.compiler.MemberCodeGen.atCallExpr(MemberCodeGen.java:523)
                at javassist.compiler.JvstCodeGen.atCallExpr(JvstCodeGen.java:244)
                 at javassist.compiler.ast.CallExpr.accept(CallExpr.java:46)
                at javassist.compiler.CodeGen.compileExpr(CodeGen.java:230)
                at javassist.compiler.Javac$2.doit(Javac.java:490)
                at javassist.compiler.JvstCodeGen.atCallExpr(JvstCodeGen.java:235)
                 at javassist.compiler.ast.CallExpr.accept(CallExpr.java:46)
                at javassist.compiler.CodeGen.atAssignCore(CodeGen.java:860)
                at javassist.compiler.CodeGen.atVariableAssign(CodeGen.java:793)
                at javassist.compiler.CodeGen.atAssignExpr(CodeGen.java:747)
                 at javassist.compiler.CodeGen.atStmnt(CodeGen.java:332)
                at javassist.compiler.ast.Stmnt.accept(Stmnt.java:50)
                at javassist.compiler.CodeGen.atStmnt(CodeGen.java:351)
                at javassist.compiler.ast.Stmnt.accept(Stmnt.java:50)
                 at javassist.compiler.Javac.compileStmnt(Javac.java:569)
                at javassist.expr.MethodCall.replace(MethodCall.java:235)
                ... 18 more

             

            The bug can be reproduced. For example, in java.lang.securityManager, the method checkPackageAccess contains a call
            "packageAccess = getPackages(tmpPropertyStr);", The call invokes "private static String[] getPackages(String p)".

             

            In both cases, it involves invocations to "private static" methods.

            • 3. Re: $proceed($$) for private methods

              I had the same issue, please post the code fragment where you call makeClass on the ClassPool so I can check if it's the same case.

              • 4. Re: $proceed($$) for private methods
                enielsen

                I was having the same problem. In my case I was trying to call a private method in a superclass, which is of couse impossible to do!