Question

I have in an input stream the complete source code of a class. Is it possible to use Javassist to create a new CtClass object for this class ?.

I know that with the name of the class is possible to create a new CtClass object with something like:

ClassPool.getDefault().makeClass("name_of_the_new_class");

But in my case I do not know the name of the class in advance, but just its complete source code (of course I could get the name of the class parsing the source code, but please do not tell me that this is part of the solution :-) )

Was it helpful?

Solution

If you want to compile a source file on-the-fly, have a look at the tools API as well as ToolProvider.getSystemJavaCompiler(). From there you can compile .class files from source and load them using a classloader if you need to.

You could also then use Javassist to create a CtClass from the bytes of the created class file using ByteArrayClassPath with a new ClassPool.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top