1

I understand if I have a class file I can load it at run time and execute it's methods through classLoader. However, what if I only have bytecode or java code for a single method? Is it possible to dynamically create a class at run time and then invoke the method?

3
  • 1
    Possible duplicate of Interpret something, and run the generated bytecode in Java? Commented May 20, 2019 at 6:00
  • But in that case wouldn't I need to have the bytecode for entire class? I only have one method's bytecode. Commented May 20, 2019 at 6:41
  • You can declare the method only inside the Class. Using any byte code library you can create a class at run time and define your method (via code) in that class. Commented May 20, 2019 at 6:43

2 Answers 2

2

There is a planned feature, JEP 8158765: Isolated Methods, also on the bugtracking list, which would allow to load and execute such bytecode, without generating a fully materialized Class. It could look like

MethodHandle loadCode(String name, MethodType type, byte[] instructions, Object[] constants)

in the class MethodHandles.Lookup

However, this feature is in draft state, so it could take significant time before becoming an actual API and it might even happen that it gets dropped in favor of an entirely different feature covering the use cases the authors of the JEP have in mind.

Until then, there is no way around generating the necessary bytes before and after the method’s bytecode, to describe a complete class and loading that class. But of course, you can write your own method accepting a method’s byte code and some metadata, like the expected signature, generating such a class and reuse that method.

Note that there’s an alternative to creating a new ClassLoader, Class<?> defineClass​(byte[] bytes) in class MethodHandles.Lookup which allows to add a class to an existing class loading context, since Java 9.

Sign up to request clarification or add additional context in comments.

2 Comments

I was thinking how a lambda is created at runtime, while we only provide that "method" as users, right?
@Eugene at least for non-capturing lambdas, this could be an alternative. But the first use case would be the MethodHandle API itself, which has to create a lot of classes internally when optimizing the adapters, like creating an entire class, just to denote that one handle is dropping one of its arguments before calling into another. That’s the first overhead to eliminate with that feature.
1

The bytecode for a method refers to entries in the class's constant pool, so it doesn't make sense in isolation.

2 Comments

in general yes, but it seems that for trivial methods this is not always true; try to de-compile public static int add(int left, int right) { return left + right; } this will blindly load first two arguments from the stack and add them.
@Eugene you still need to provide the necessary context, like the expected method signature. On the other hand, then, the necessary API may even provide ways for defining the referenced constants, like in the proposal described in my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.