org.mvel2.util.StaticFieldStub Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of virtdata-lib-realer Show documentation
Show all versions of virtdata-lib-realer Show documentation
With inspiration from other libraries
package org.mvel2.util;
import org.mvel2.integration.VariableResolverFactory;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
/**
* @author Mike Brock
*/
public class StaticFieldStub implements StaticStub {
private final Field field;
private final Object cachedValue;
public StaticFieldStub(Field field) {
this.field = field;
if (!field.isAccessible() || (field.getModifiers() & Modifier.STATIC) == 0) {
throw new RuntimeException("not an accessible static field: " + field.getDeclaringClass().getName()
+ "." + field.getName());
}
try {
cachedValue = field.get(null);
}
catch (IllegalAccessException e) {
throw new RuntimeException("error accessing static field", e);
}
}
public Object call(Object ctx, Object thisCtx, VariableResolverFactory factory, Object[] parameters) {
return cachedValue;
}
}