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)
- Improve around advice to retransform loaded class HOT 4
- Consultation on implementing a similar functionality to javassist's instrument feature.
- com.oracle.svm.core.jdk.UnsupportedFeatureError: No classes have been predefined during the image build to load from bytecodes at runtime. HOT 1
- AndroidClassLoadingStrategy Injecting problem in Android SDK 33 HOT 3
- [maven plugin] transform-extended does not include dependency classes HOT 14
- latest guava cause [ unexpected type reference on method: 16 ] HOT 6
- `MemberSubstitution` can't replace instructions within synthetic methods. HOT 2
- IllegalArgumentException for Kotlin bytecode with erased type argument HOT 9
- Can byte-buddy modify bytecode in parallel? HOT 4
- Java 17 reflection changes
- A lazy `TypePool` will change with type resolution and thus deviate from its initial configuration. HOT 2
- JDK 23 Valhalla-EA: IllegalState Illegal modifiers 33 for class HOT 2
- Multiple transformation issue with JMH benchmarks HOT 1
- [question] How can replace field access with another class? HOT 2
- Eclipse project compilation error using mockito and byte-buddy HOT 5
- Runtime attachment causing VerifyError during class transformation HOT 13
- not able to intercept java.lang package HOT 9
- Questions about alternatives to the LocalVariablesSorter class obsoletization HOT 5
- How to create a class with updated private field using ByteBuddy? HOT 2
- Byte buddy access to package protected class HOT 5
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.