4

I know this might sound kinda odd, but I was wondering if it was possible to manually create a java.lang.Method at runtime.

And what about Class<?>?

devoured elysium
  • 90,453
  • 117
  • 313
  • 521
  • 6
    Interested to see why would you do that. Generate code at runtime? – Marcelo Jan 30 '12 at 14:14
  • 3
    Use the [`JavaCompiler`](http://stackoverflow.com/a/8080955/418556) to compile code in memory. Then you can do as you wish with the resulting classes and their methods and attributes. Also curious to know the use-case for this functionality. Whenever a question starts with *"this might sound kinda odd"* it is a good hint to provide the use-case. ;) – Andrew Thompson Jan 30 '12 at 14:21

1 Answers1

4

It is possible to create a Class<?> at runtime using libraries such as:

These, can contain the method that you can retrieve as usually using reflection.

Take also a look at: https://stackoverflow.com/a/2532269/272388

Community
  • 1
  • 1
Kru
  • 4,002
  • 22
  • 29
  • I'm well aware of those libraries. I just wanted to know whether it would be possible to do it just with Java's reflection. – devoured elysium Jan 30 '12 at 14:24
  • No, it is not possible: `java.lang.reflect: Provides classes and interfaces for obtaining reflective information about classes and objects.` And you can use the `ClassLoader` to load classes, but you still have to create the bytecode. – Kru Jan 30 '12 at 14:40