Comments (2)
Byte Buddy implements an immutable builder, there should be no side effects. Maybe the weaver fails as you refer to the loaded MyPrinter class?
You can use a TypePool to describe s class from class bytes.
from byte-buddy.
Thanks for the hint. This code is working fine for me. With this service it is even possible to modify not exported classes without modifying anything in the manifest. This is really great, because I can modify cross-cutting things in OSGi without using java agents.
@Component(immediate = true)
public class WeavingHookService implements WeavingHook {
private static final String CLASS_TO_MODIFY = "my.waevinghook.example.internal.MyPrinter";
@Override
public void weave(WovenClass wovenClass) {
if (wovenClass.getClassName().equals(CLASS_TO_MODIFY)) {
try {
byte[] originalBytes = wovenClass.getBytes();
ClassFileLocator locator = ClassFileLocator.Simple
.of(wovenClass.getClassName(),
originalBytes);
Unloaded<Object> newPrinter = new ByteBuddy()
.rebase(TypePool.Default.of(wovenClass.getBundleWiring().getClassLoader())
.describe(
CLASS_TO_MODIFY).resolve(),
locator)
.method(ElementMatchers.named("getName"))
.intercept(FixedValue.value("Raphael"))
.make();
wovenClass.setBytes(newPrinter.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
from byte-buddy.
Related Issues (20)
- latest guava cause [ unexpected type reference on method: 16 ] HOT 5
- `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 4
- How to create a class with updated private field using ByteBuddy? HOT 2
- Byte buddy access to package protected class HOT 4
- mockito-android:5.12.0 cannot mock class HOT 1
- How to invoke the method of injecting objects into a class? HOT 2
- [Question] Why would a new field var2 be added after replace field access with method? HOT 3
- How to redefine dynamically created classes? HOT 2
- EntryPoint property breaks gradle build HOT 1
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.