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

Add overloads with explicit class loader to FunctionUtils #198

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ target/

# IntelliJ Idea project files
.idea
*.iml
*.iml

# Eclipse
.project
.classpath
.settings/
53 changes: 51 additions & 2 deletions core/src/main/java/com/schibsted/spt/data/jslt/FunctionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,20 @@ static public Function wrapStaticMethod(String functionName,
String className,
String methodName)
throws LinkageError, ExceptionInInitializerError, ClassNotFoundException {
Class klass = Class.forName(className);
return wrapStaticMethod(functionName, className, methodName, FunctionUtils.class.getClassLoader(), Thread.currentThread().getContextClassLoader());
}

/**
* Create a JSLT function from a static Java method. This will fail
* if the method is overloaded. The given class loaders will be tried
* in order to load the named class.
*/
static public Function wrapStaticMethod(String functionName,
String className,
String methodName,
ClassLoader... classLoaders)
throws LinkageError, ExceptionInInitializerError, ClassNotFoundException {
Class klass = loadClass(className, classLoaders);
Method[] methods = klass.getMethods();
Method method = null;
for (int ix = 0; ix < methods.length; ix++) {
Expand All @@ -58,8 +71,44 @@ static public Function wrapStaticMethod(String functionName,
Class[] paramTypes)
throws LinkageError, ExceptionInInitializerError, ClassNotFoundException,
NoSuchMethodException {
Class klass = Class.forName(className);
return wrapStaticMethod(functionName, className, methodName, paramTypes, FunctionUtils.class.getClassLoader(), Thread.currentThread().getContextClassLoader());
}
/**
* Create a JSLT function from a static Java method. The given class
* loaders will be tried in order to load the named class.
* @param paramTypes Array of types used to match overloaded methods.
*/
static public Function wrapStaticMethod(String functionName,
String className,
String methodName,
Class[] paramTypes,
ClassLoader... classLoaders)
throws LinkageError, ExceptionInInitializerError, ClassNotFoundException,
NoSuchMethodException {
Class klass = loadClass(className, classLoaders);
Method method = klass.getMethod(methodName, paramTypes);
return new FunctionWrapper(functionName, method);
}

private static Class loadClass(String className, ClassLoader... classLoaders) throws LinkageError, ClassNotFoundException {
Class klass = null;
Throwable lastException = null;
for (ClassLoader classLoader : classLoaders) {
if (classLoader != null) {
try {
klass = Class.forName(className, true, classLoader);
lastException = null;
break;
} catch (LinkageError | ClassNotFoundException e) {
lastException = e;
}
}
}
if (lastException instanceof LinkageError) {
throw (LinkageError) lastException;
} else if (lastException instanceof ClassNotFoundException) {
throw (ClassNotFoundException) lastException;
}
return klass;
}
}