Skip to content

Commit

Permalink
Removed redundant object cloning in usercode wrapper to fix class loa…
Browse files Browse the repository at this point in the history
…ding issue.
  • Loading branch information
StephanEwen committed Feb 26, 2014
1 parent 7b2cf3a commit 93ecfc6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,23 @@ public class UserCodeClassWrapper<T> implements UserCodeWrapper<T> {
private static final long serialVersionUID = 1L;

private Class<? extends T> userCodeClass;
private String className;

public UserCodeClassWrapper(Class<? extends T> userCodeClass) {
this.userCodeClass = userCodeClass;
this.className = userCodeClass.getName();
}

@SuppressWarnings("unchecked")
@Override
public T getUserCodeObject(Class<? super T> superClass, ClassLoader cl) {
try {
Class<T> clazz = (Class<T>) Class.forName(className, true, cl).asSubclass(superClass);
return InstantiationUtil.instantiate(clazz, superClass);
} catch (ClassNotFoundException e) {
throw new RuntimeException("User code class could not be instantiated: " + e);
}
return InstantiationUtil.instantiate(userCodeClass, superClass);
}

@Override
public T getUserCodeObject() {
try {
return userCodeClass.newInstance();
} catch (InstantiationException e) {
throw new RuntimeException("User code class could not be instantiated: " + e);
} catch (IllegalAccessException e) {
throw new RuntimeException("User code class could not be instantiated: " + e);
}
return InstantiationUtil.instantiate(userCodeClass, Object.class);
}

@Override
public <A extends Annotation> A getUserCodeAnnotation(
Class<A> annotationClass) {
public <A extends Annotation> A getUserCodeAnnotation(Class<A> annotationClass) {
return userCodeClass.getAnnotation(annotationClass);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import org.apache.commons.lang3.SerializationUtils;

import com.google.common.base.Preconditions;

/**
Expand Down Expand Up @@ -92,22 +90,17 @@ public UserCodeObjectWrapper(T userCodeObject) {

@Override
public T getUserCodeObject(Class<? super T> superClass, ClassLoader cl) {
return getUserCodeObject();
return userCodeObject;
}

@SuppressWarnings("unchecked")
@Override
public T getUserCodeObject() {
// return a clone because some code retrieves this and runs configure() on it before
// the job is actually run. This way we can always hand out a pristine copy.
Serializable ser = (Serializable) userCodeObject;
T cloned = (T) SerializationUtils.clone(ser);
return cloned;
return userCodeObject;

}

@Override
public <A extends Annotation> A getUserCodeAnnotation(
Class<A> annotationClass) {
public <A extends Annotation> A getUserCodeAnnotation(Class<A> annotationClass) {
return userCodeObject.getClass().getAnnotation(annotationClass);
}

Expand Down

0 comments on commit 93ecfc6

Please sign in to comment.