GithubHelp home page GithubHelp logo

Comments (9)

raphw avatar raphw commented on July 19, 2024

Are you subclassing a class with a final method? It is not possible to override such a method, you would need to use a Java agent or some other sort of class redefinition. I once blogged about this: http://mydailyjava.blogspot.no/2015/01/make-agents-not-frameworks.html

from byte-buddy.

Pasupathi-Rajamanickam avatar Pasupathi-Rajamanickam commented on July 19, 2024

I don't do such. Have look at my code pls.

public class Hello {
  public final String sayHello() {
    return "hello";
  }
}

That's it. I cannot use byte-buddy to log this 'sayHello' function?

from byte-buddy.

raphw avatar raphw commented on July 19, 2024

Can you post a running example that uses Byte Buddy to instrument your class?

from byte-buddy.

Pasupathi-Rajamanickam avatar Pasupathi-Rajamanickam commented on July 19, 2024

Ya here is that. I have these 3 classes in 3 different files.

Main class - To create bytebuddy class and run it.

public class ByteEx {

  public static void main(String[] d) throws Exception{
    new ByteEx().loadclassDD();
  }

  public void loadclassDD() throws Exception{
    Hello loggingDatabase = new ByteBuddy() 
      .subclass(Hello.class)
      .method(named("sayHello")).intercept(MethodDelegation.to(LoggerInterceptor.class))
      .make()
      .load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
      .getLoaded()
      .newInstance();
    System.out.println(loggingDatabase.sayHello());
  }
}

Logger class. - This will return upper case of original string data if my method is called.

public class LoggerInterceptor {
    public static String log(@SuperCall Callable<String> zuper)
        throws Exception {
    System.out.println("Calling database");
    try {
      return  zuper.call().toUpperCase();
    } finally {
      System.out.println("Returned from database");
    }
  }
}

Target class which have a method will return lower case data

public class Hello {
  public final String sayHello() {
    return "hello";
  }
}

from byte-buddy.

raphw avatar raphw commented on July 19, 2024

Hi, as I said, it is not possible to *subclass and override a final method. Not with Byte Buddy and not on the Java platform in general. You would need to use a Java agent and redefine the class as I described in the linked blog posting.

from byte-buddy.

Pasupathi-Rajamanickam avatar Pasupathi-Rajamanickam commented on July 19, 2024

You mean to say I should remove the final from the class?
I've seen the blog, but still this cannot be achieved for final method right?

from byte-buddy.

raphw avatar raphw commented on July 19, 2024

No, it is simply not possible to override a final method, any (generated) class that would override such a method would be rejected with a verification error by the Java virtual machine. For this reason, Byte Buddy does not even consider overriding such a class.

from byte-buddy.

Pasupathi-Rajamanickam avatar Pasupathi-Rajamanickam commented on July 19, 2024

OK. Ya I think you are correct. I tried couple of interceptor, but no luck. OK. Thanks for the response. Good luck for your project. It's really awesome and simple.

from byte-buddy.

raphw avatar raphw commented on July 19, 2024

Thanks!

from byte-buddy.

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.