GithubHelp home page GithubHelp logo

Comments (2)

raphw avatar raphw commented on August 22, 2024

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.

rage5474 avatar rage5474 commented on August 22, 2024

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)

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.