0

For example, I have a JSON format like this:

{
  "Invoice_iDoc": {
    "name": "Invoice",
    "methods": [
      "customer",
      "project",
      "cost"
    ]
  },
  "PO": {
    "name": "PO",
    "methods": [
      "date",
      "amount"
    ]
  }
}

I would like to create classes dynamically as Invoice and its methods customer, project, cost with default parameter as String, and the same for class PO as well.

Here is the requirement:

1-I don't have class files, named Invoice, and have methods of class Invoice as customer, project, cost and 2nd class as PO and its methods as date & amount, can I create these classes ?

2-I DONT'T have the class files Invoice and PO , I would like to generate it on the runtime, is this possible to do so? Thanks.

  • 3
    This JSON does not tell you the return types, the parameter types, the method visibility (e.g., public vs private), whether the methods are final, if there are any fields, the method _implementations_, and so on. In short, you don't have enough information to create a useful class. And you indicate that these classes _do_ exist, you just don't have them available, so it's even worse as you definitely won't be able to guarantee the generated class exactly duplicates the existing class. However, note it is possible to write, compile, and load a class at run-time. – Slaw Jul 31 '22 at 05:29
  • Maybe you can use some of the ideas here https://stackoverflow.com/a/2320465/3587458 – AX2 Jul 31 '22 at 05:29
  • lets consider all are having public specifier, and String as return type and these methods will be used just for initialization purpose, and no need to make methods final, and no other fields methods would be used just for initialization purpose, and these classes does not exist dynamically we need to create these and may be later will be used for initialization purpose. – Java Learner Jul 31 '22 at 06:09
  • So, all you care about is having a sort of "skeleton" class that does nothing except contains signatures for the given method names? Why not do whatever "initialization" you're talking about with just the list of method names then? Why do you need an actual class? – Slaw Jul 31 '22 at 07:24

0 Answers0