0

So first I must point out that this is not a duplicate because I am aware a .class file can be serialized and sent over a network via a connection.

My question is, if I were to have a class sent over a network when a connection is established, and in that system (which originally does not have the class), I attempt to construct the object, it will not compile because the class is not found ahead of time.

How will I get the java code utilizing a class that does not exist (but is guaranteed to exist upon construction) to compile?

A generalized example:

I have Foo.class compiled on system A. I then serialize Foo.class and send it over a connection to system B during runtime. System B then constructs a Foo object. That makes sense right. However, compiling the code for System B will not work because it has not heard of Foo.class. It has not yet received it so it will say no such class exists during compile time.

And no, I cannot create a class Foo on System B ahead of time.

Basically, it is run-time construction and utilization of a class.

George Xavier
  • 191
  • 2
  • 12
  • maybe save the class as a file and then use ClassLoader? https://stackoverflow.com/questions/6219829/method-to-dynamically-load-java-class-files – JavaMan Jun 24 '19 at 14:27
  • @ThomasSallaberger and I can compile utilization of the methods even if they don't yet exist? – George Xavier Jun 24 '19 at 14:31
  • no :( maybe using an interface does work (if you already know the interface for "Foo") – JavaMan Jun 24 '19 at 14:36
  • Normally I would do this by requiring the generated class to implement an interface. Then code just calls known interface methods. Java's JDBC database connector / driver does this. You could also call methods by reflection, which will be a PITA. I think you could make a "fake" class on your side that has the same name and methods, and just use that to compile against. Maybe look at a mocking tool like SuperMock. – markspace Jun 24 '19 at 14:36
  • @markspace Yeh one of my ideas was to make a temporary empty Foo.class on System B but the goal is to not require previous installation of extra packages. So if I had Foo.class implement RowSet, then it would work? I do not understand how it would be instantiated without the class file being there. You can't instantiate an interface after all. And the rowset interface does not include a method returning a rowset. (Same with resultset). – George Xavier Jun 24 '19 at 14:54
  • These are things you should figure out before specifying that a class will be generated. Honestly I don't see how you're going to generate the byte codes of a class if you can't even work out basic stuff like this. Look at the JDBC tutorial pages, they use `Class.forName()` to load an "unknown" class. – markspace Jun 24 '19 at 16:11
  • @markspace Yeah I figured it out. I'll simply serialize the class over extending the interface, instantiate it via reflection, cast it to the interface, and execute the implemented methods. I was trying to create my own implementation of a RowSet because GWT does not offer RowSet implementation. Originally, my colleagues made a temporary DTO hack in half an hour, so I had to clean that up. And bossman wanted the implementation to be plugin-like, so he didn't want pre-installing of any packages. – George Xavier Jun 24 '19 at 16:19

0 Answers0