Coder Social home page Coder Social logo

Comments (3)

chibash avatar chibash commented on July 19, 2024

Hi,

Once you execute CtClass#writeFile(), then the class get frozen because you've already written it out and there is no way to modify that class if the class is loaded onto the JVM.

You can get a CtClass object back to be modifiable again by:

cc.stopPruning(true);    // don't delete the data if it is written out.
    :
cc.writeFile();
cc.defrost();             // now modifiable again

Another approach is to use debugWriteFile() instead of writeFile().
debugWriteFile() keeps a CtClass object modifiable after it is written out.
You don't have to call #stopPruning() or #defrost().

from javassist.

erdem-aslan avatar erdem-aslan commented on July 19, 2024

Hello,

Thanks for responding.

My very first attempt to modify the class is just after the isFrozen method call. So I am not calling writeFile if you check the sample I've provided.

Let me provide some comments on the steps, code is messy I know.

CtClass cc;
            try {
                cc = classPool.get (className); // just getting the class

                getLog().info("Class: " + cc.getSimpleName() + " is frozen ? : " + cc.isFrozen()); // checking if its frozen

            } catch (NotFoundException e) {
                e.printStackTrace ();
                continue;
            }

            if (cc.isAnnotation () // skipping interfaces and annotations
                    || cc.isInterface ())
                continue;

            CtMethod[] methods = cc.getDeclaredMethods (); // getting methods

            for (CtMethod ctMethod : methods) {

                try {

                    ctMethod.addLocalVariable ("executionStarted", CtClass.longType); // Class is frozen exception is thrown right away !
                    ctMethod.insertBefore ("executionStarted = System.currentTimeMillis ();");
                    ctMethod.insertAfter ("{final long executionFinished = System.currentTimeMillis ();"
                            + "System.out.println(\"Execution in ms: \" + (executionFinished-executionStarted));}");

                    cc.writeFile ("."); // The very first time of writeFile method in the whole project

                } catch (Throwable e) {
                    getLog ().error ("Error on class: "
                            + cc.getName ()
                            + " for method: "
                            + ctMethod.getName ());
                    getLog ().error (e);
                }
            }

I event tried calling defrost () before trying anything on the class but no good.

EDIT:

I think I overlooked one case and now I can make sense of the situation.

While iterating over methods I couldn't observe at least one successful iteration without exception thrown, according to your response there has to be at least one good iteration and consequents should all fail.

Maybe getDeclaredMethods returns also the default constructor and I missed it within debugs? I'll check and update here.

Thanks

from javassist.

erdem-aslan avatar erdem-aslan commented on July 19, 2024

Yes, I missed the successful first one while iterating over the methods of the class, simply moving out the cc.writeFile call to the outer loop did the trick.

Thanks again for your response

Cheers

from javassist.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.