Comments (9)
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.
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.
Can you post a running example that uses Byte Buddy to instrument your class?
from byte-buddy.
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.
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.
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.
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.
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.
Thanks!
from byte-buddy.
Related Issues (20)
- Can ByteBuddy add synchronized to intercepted setters? HOT 2
- Can ByteBuddy make a non-static field be final? HOT 1
- seems that namedOneOf isn't working as expected HOT 1
- Release missing of version 1.14.15 HOT 1
- java.lang.NoSuchMethodError when using the agent for method instrumentation HOT 2
- Cannot invoke public constructor HOT 5
- ByteBuddyAgent, Java 21 - A Java agent has been loaded dynamically HOT 2
- include method does not exist for Method Delegation approach HOT 6
- Having trouble delegating added instance method to static method HOT 2
- Setting value in constructor aliases collections HOT 5
- Is it possible to lazily instantiate AllArguments?
- install transformer takes too long time HOT 3
- Example that could be used in documentation HOT 6
- Issues attaching to Solaris Hotspot on Solaris 10 and 11. HOT 1
- How to generate multi-level generic code ? HOT 2
- java.lang.IllegalAccessError HOT 15
- InjectionStrategy.UsingUnsafe.OfFactory HOT 1
- java.lang.IncompatibleClassChangeError: Interface method reference: java.util.stream.Sink.begin(J)V HOT 1
- defineField not working well HOT 1
- JDK class enhancements HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from byte-buddy.