Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

f:ix allow to set a classloader for PackageSpecificTypeAdapter #331

Merged
merged 1 commit into from
Jul 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,73 @@
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.google.gson.TypeAdapterFactory;
import org.apache.commons.lang3.Validate;

import java.util.LinkedList;
import java.util.List;

public class PackageSpecificTypeAdapter<T> extends AbstractTypeAdapterFactory<T> {
/**
* This {@link TypeAdapterFactory} allows to create an object from JSON definition
* based on a "type" attribute. According to the value of the value in the "type" attribute,
* a class is loaded and an object of this class is instantiated. Since this explicitly works
* with simple class names only, a search space of possible packages need to be defined, which
* are used to resolve the actual class.
*/
public final class PackageSpecificTypeAdapter<T> extends AbstractTypeAdapterFactory<T> {

/**
* Holds all package names which are used to search for suitable classes to instantiate based on the given type name.
*/
private final List<String> packageNames = new LinkedList<>();

/**
* The class loader to load the given class from.
*/
private ClassLoader classLoader = PackageSpecificTypeAdapter.class.getClassLoader();

public PackageSpecificTypeAdapter(TypeAdapterFactory parentFactory, Gson gson) {
super(parentFactory, gson);
}

/**
* Adds the package of the given class to the search space. All classes within this package
* are candidates to be loaded by a given type name.
*/
public PackageSpecificTypeAdapter<T> searchInPackageOfClass(Class<?> clazz) {
return searchInPackage(clazz.getPackage());
}

/**
* Adds the given package to the search space. All classes within this package
* are candidates to be loaded by a given type name.
*/
public PackageSpecificTypeAdapter<T> searchInPackage(Package searchPackage) {
return searchInPackage(searchPackage.getName());
}

/**
* Adds the given fully qualified package name to the search space. All classes
* within the given package are candidates to be loaded by a given type name.
*/
public PackageSpecificTypeAdapter<T> searchInPackage(String packageName) {
packageNames.add(packageName);
return this;
}

/**
* Defines a specific class loader from which the searched classes are loaded.
*/
public PackageSpecificTypeAdapter<T> withClassLoader(ClassLoader classLoader) {
this.classLoader = Validate.notNull(classLoader, "Given class loader must not be null");
return this;
}

@Override
protected Class<?> fromTypeName(String type) {
Class<?> returnClass = null;
for (String searchPackage : packageNames) {
try {
returnClass = Class.forName(searchPackage + "." + type);
returnClass = classLoader.loadClass(searchPackage + "." + type);
} catch (ClassNotFoundException ignored) {
// nop
}
Expand Down
Loading