How to create and then instantiate POJO classes dynamically?
You can use reflection to instantiate the generated classses and access the provided methods.
Create Pojo at runtime and after saving pojo on to the disk throwing an error as class pruned?
When looking at the source code, it seems, that the problem is caused by the fact that both, wasChanged
and pruned
, are true
.
Actually, automatic pruning should be off by default, at least on recent versions. But if the class file has been pruned, it should not allow subsequent modifications. This leads to the conclusion that you can’t have modified the CtClass
object in-between, so the error is not on your side. Searching for occurrences of wasChanged
in the file reveals that it has been forgotten to ever set it to false
.
So what happens, is
- You set up the class under construction, which turns
wasChanged
totrue
. - You call
toClass()
, which in turn invokestoBytecode
, which will setpruned
andfrozen
totrue
, disallowing further modifications, but forgets to setwasChanged
tofalse
. - You call
writeFile
, which will also calltoBytecode
, which now detects that the file has been modified, according to the flag that it never resets, and throws an exception, as the class has been pruned.
If you swap toClass()
and writeFile
, the logic stays the same, as it is the fact that both call toBytecode
internally, which can’t be invoked twice, given the behavior described above.
You have several options.
You can call
debugWriteFile(path)
before callingtoClass()
, asdebugWriteFile
is documented to “not prune or freeze the class after writing the class file”.You can call
stopPruning(true)
before callingwriteFile
ortoClass
. As mentioned above, pruning should be even off by default.You may call
toBytecode()
directly yourself and call it only once. Then…Once you have the byte array, you can simply write it to a file (e.g.
Files.write(Paths.get(pathString), byteArray)
.To use this existing byte array to create the class, you may use your own class loader or use
MethodHandles.lookup().defineClass(array)
, if you are under Java 9 or newer and create a class in your own package.
This might be a bit more complicated, but since this constructs the class file bytes only once, it’s the most efficient solution.
Related Topics
Is There a Priorityqueue Implementation with Fixed Capacity and Custom Comparator
Why Is Files.Lines (And Similar Streams) Not Automatically Closed
How to Show Changes Between Commits with Jgit
Why in Java Enum Is Declared as Enum<E Extends Enum<E>>
Best Way to Create a Hashmap of Arraylist
Spring Boot Actuator Without Spring Boot
Java: Local Variable Mi Defined in an Enclosing Scope Must Be Final or Effectively Final
Where Are Generic Types Stored in Java Class Files
Case Insensitive JSON to Pojo Mapping Without Changing the Pojo
How to Cause a Child Process to Exit When the Parent Does
Java: How to Use Urlconnection to Post Request with Authorization
Java Byte Array Contains Negative Numbers