'java static method for abstract class

I have a java abstract class that declares the extended class as "JsonSerializable"

public abstract class JsonSerializable {
    private final String path;

    public JsonSerializable(String path) {
        this.path = path;
    }
} 

Now, if I try to "load" a class that extends JsonSerializable I need to get the "filepath" for the class I want to load. The way I am currently doing this is as follows:

private static <T extends JsonSerializable> T getNew(Class<T> tClass) throws [...] {
        return tClass.getDeclaredConstructor().newInstance();
}

But what if the constructing of a new class (of the type I am trying to load) has some side effects (e.g. upon initialization, the class "registers" itself inside another one) or if there IS no constructor with no arguments?

Is there any better way to do this? I don't want to create a "FileRegistry" class that just holds the file paths for each class.



Solution 1:[1]

But what if the constructing of a new class (of the type I am trying to load) has some side effects (e.g. upon initialization, the class "registers" itself inside another one)

Constructor#newInstance invokes the constructor. If it has side effects, those things will happen. But note that ideally, constructors should only initialize the object and not have side effects (especially nothing that leak the object/publosh the this-reference before the constructor finishes.

or if there IS no constructor with no arguments?

In that case, getDeclaredConstructor() throws an exception. However, you can get all constructors using Class#getDeclaredConstructors.

However, you can specify that classes extending JsonSerializable must have a no-args-constructor with no side effects in the Javadoc of JsonSerializable.

While you can create new instances without invoking the constructor using Unsafe, this should be avoided as it would leave variables uninitialized likely resulting in future exceptions.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dan1st