Hello!

There are several small projects. To one of them screwed JMX. Happened. But now you need to make a separate module, where JMX will be implemented using reflection. The idea is that in the class that needs to be monitored, annotations are added to the necessary methods / fields (for example: @JMXTest) and all the necessary interfaces and classes for the work of JMX are already created.

The following point is not very clear to me: how to create a class with the help of reflection with the necessary set of fields and methods? As I understand it, reflection allows you to create instances of classes, but they must be described in advance.

Thanks in advance for any help and please do not throw rotten vegetables))

PS: The logic itself implemented in the classes does not seem to play a role, so I don’t attach the code.

  • As I understand it, this can be done using Annotation Processors (examples: 1 , 2 ) - zRrr

1 answer 1

The following point is not very clear to me: how to create a class with the help of reflection with the necessary set of fields and methods?

No Reflection does not allow this. Here code generation is most likely needed in runtime, it is programming on the fly.


In your case, a two-stage compilation will suit you:

  1. first compile the program;
  2. then (by a separate program) you generate the classes you need based on the attributes;
  3. finally compile these generated classes with the java compiler and add to the main program.
  • thanks for the help. The problem was solved much easier - using the DynamicMBean interface, thus eliminating the need for code generation. - bearbeard