M
M
Marat2016-04-05 22:16:12
Java
Marat, 2016-04-05 22:16:12

How to serialize in Java 7, or rather save the byte code of functions with the possibility of further loading?

I apologize in advance for the long sections of code and I will be grateful to anyone who will review them.
Source

import java.io.*;
import java.util.ArrayList;

public class ListOfFunc implements Serializable{
    interface Executable extends Serializable {  void execute(); }
    ArrayList<Executable> alFunc = new ArrayList<>();
    private static Executable exec1 =new Executable() {
        @Override
        public void execute() { System.out.println("exec func1"); }
    };
    private static Executable exec2 =new Executable() {
        @Override
        public void execute() { System.out.println("exec func2"); }
    };

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ListOfFunc lf1 =new ListOfFunc();
        lf1.alFunc.add(exec1);
        lf1.alFunc.add(exec2);
        lf1.alFunc.get(0).execute();
        lf1.alFunc.get(1).execute();

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("data1.dat"));
        oos.writeObject(lf1);
        oos.close();

        System.out.println("after loading:");
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("data1.dat"));
        ListOfFunc ex2 = (ListOfFunc) ois.readObject();
        ois.close();
        ex2.alFunc.get(0).execute();
        ex2.alFunc.get(1).execute();
    }
}

Works with a bang, serializes and deserializes. Conclusion:
exec func1
exec func2
after loading:
exec func1
exec func2

But I want to make a version of serialization such that the source program does not describe the internals of the implementation of functions, so that they are unloaded (in the form of bytecode etc) and subsequently loaded without describing the implementations of the functions.
To ever form a bank of functions in the past, save it as a file. And somehow in the future, load it in such a simple way (without describing thousands of function bodies wrapped in interfaces):
import java.io.*;
import java.util.ArrayList;

public class ListOfFunc implements Serializable{
    interface Executable extends Serializable {  void execute(); }
    ArrayList<Executable> alFunc = new ArrayList<>();
   
    public static void main(String[] args) throws IOException, ClassNotFoundException {

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("data1.dat"));
        ListOfFunc ex2 = (ListOfFunc) ois.readObject();
        ois.close();
        // Запустить подряд все загруженные функции
        for (int i = 0; i < ex2.alFunc.size(); i++)  ex2.alFunc.get(i).execute();
    }
}

This example will naturally give an error local class incompatible: stream classdesc serialVersionUID = ... since there is no description of private static Executable. Maybe there are other mechanisms that are not related to serialization, but have the cherished Save and paired Load to implement the last example. Or maybe give me a direction in the right direction to implement something like this?
PS Ready even to grow up to Java8.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Denis Zagaevsky, 2016-04-05
@zagayevskiy

As far as I know, this cannot be done. Here it is described in detail what happens and what happens during serialization: https://habrahabr.ru/post/60317/ Briefly: the method bytecode is not written there.
But, it seems to me, your task can be solved without serialization. Create a jar with these classes, then load it dynamically. stackoverflow.com/questions/11016092/how-to-load-c...

A
aol-nnov, 2016-04-05
@aol-nnov

javassist or asm which thread is not worth considering?

D
DKT, 2016-04-06
@dkt11

You can compile the class and save the byte code (as a file, into a database, etc.). (Serialization has nothing to do with it). And then dynamically load and execute the byte code.
You can also read about rule sets, which seems to be what you need (but I can’t say more because I don’t know it personally).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question