@NotNull
public static String toString(@NotNull Object object, @NotNull Object[] variables, Class<?>... parameters) throws NoSuchMethodException {
StringBuilder sb = new StringBuilder();
sb.append(object.getClass().getConstructor(parameters)).append('\n');
sb.append(object.getClass()).append(":");
for (Object variable : variables){
sb.append(" | ").append(variable.toString());
}
return sb.toString();
}
A brief explanation of the code, the toString() method uses an object, the variables of the constructor that the user wants to save, and the parameters of a constructor. This method would form the parameters in a way like this:
public User(java.lang.String,int)
class User: | username | 369172
I want to use this String format to convert the above into a new instance of the Object that was taken in using the method below:
public static Object toObject(@NotNull String formattedObject, @NotNull Object object, Class<?>... parameters){
// The code I need to figure out.
}
I want to figure out: how to get all the variables cast into a form where I could use them to make a new instance of the object if I didn't know how many variables/parameters there are, how to use the method in the event the constructor of the Object is private, and if one of the parameters is an Object that prints out the default Object toString() method. How could I do this?
Aucun commentaire:
Enregistrer un commentaire