0

Can you please tell me if this is possible scenario with byte-buddy:

  1. Java program is run.
  2. Standard java POJO classes with lots of properties are loaded.
  3. ByteBuddy - in runtime modify each class replacing property types e.g. int and Integer with MyInteger. MyInteger is there to intercept setting of this properties. I don't want to use setters, since it is not mandatory to have setters for all the properties.
  4. Each invocation to SomeClass.setSomeInt(1) will invoke some method defined on MyInteger.
  5. Remove all the properties back to original's MyInteger -> int and MyInteger -> Integer.

Basically run time proxies directly on properties.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
Marko Kraljevic
  • 401
  • 4
  • 19

2 Answers2

0

This is not possible using Byte Buddy (or any high-level code generation library I know of). Byte Buddy aims for binary-compatible changes. If you changed the type of a field you would need to change all code that accesses this field. This would require a global code change due to altering a class's API compared to changing a class's private implementation.

I would recommend you to add accessor methods to your class and intercept those accessors. An IDE can help you with that. Also, by intercepting these accessors, you can freely decide what to do during an interception.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
0

There should not be that many fields you would need to change at runtime, and if they are I would suggest a database storage with an updater exec. You could then change the database via whatever interface you like, and when the polling class repeats it will grab the stuff from the database and do all your setting for you.

This should be doable with any code injector or java agent, BUUUT, it would be doing it way wrong. Also cost you a whole lot of overhead for no better results.

Xype
  • 68
  • 11
  • Just to clarify to Rafael's point, I think ByteBuddy is faster and it does a whole lot of things better. However this is quite simple to achieve with javassist, you should look into its hookmanager – Xype Aug 05 '17 at 03:58