use of org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter in project powermock by powermock.
the class FixedValueGenerator method generate.
public void generate(ClassEmitter ce, Context context, List methods) {
for (Iterator it = methods.iterator(); it.hasNext(); ) {
MethodInfo method = (MethodInfo) it.next();
CodeEmitter e = context.beginMethod(ce, method);
context.emitCallback(e, context.getIndex(method));
e.invoke_interface(FIXED_VALUE, LOAD_OBJECT);
e.unbox_or_zero(e.getReturnType());
e.return_value();
e.end_method();
}
}
use of org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter in project powermock by powermock.
the class LazyLoaderGenerator method generate.
public void generate(ClassEmitter ce, Context context, List methods) {
Set indexes = new HashSet();
for (Iterator it = methods.iterator(); it.hasNext(); ) {
MethodInfo method = (MethodInfo) it.next();
if (TypeUtils.isProtected(method.getModifiers())) {
// ignore protected methods
} else {
int index = context.getIndex(method);
indexes.add(new Integer(index));
CodeEmitter e = context.beginMethod(ce, method);
e.load_this();
e.dup();
e.invoke_virtual_this(loadMethod(index));
e.checkcast(method.getClassInfo().getType());
e.load_args();
e.invoke(method);
e.return_value();
e.end_method();
}
}
for (Iterator it = indexes.iterator(); it.hasNext(); ) {
int index = ((Integer) it.next()).intValue();
String delegate = "CGLIB$LAZY_LOADER_" + index;
ce.declare_field(Constants.ACC_PRIVATE, delegate, Constants.TYPE_OBJECT, null);
CodeEmitter e = ce.begin_method(Constants.ACC_PRIVATE | Constants.ACC_SYNCHRONIZED | Constants.ACC_FINAL, loadMethod(index), null);
e.load_this();
e.getfield(delegate);
e.dup();
Label end = e.make_label();
e.ifnonnull(end);
e.pop();
e.load_this();
context.emitCallback(e, index);
e.invoke_interface(LAZY_LOADER, LOAD_OBJECT);
e.dup_x1();
e.putfield(delegate);
e.mark(end);
e.return_value();
e.end_method();
}
}
use of org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter in project powermock by powermock.
the class MethodInterceptorGenerator method generate.
public void generate(ClassEmitter ce, Context context, List methods) {
Map sigMap = new HashMap();
for (Iterator it = methods.iterator(); it.hasNext(); ) {
MethodInfo method = (MethodInfo) it.next();
Signature sig = method.getSignature();
Signature impl = context.getImplSignature(method);
String methodField = getMethodField(impl);
String methodProxyField = getMethodProxyField(impl);
sigMap.put(sig.toString(), methodProxyField);
ce.declare_field(Constants.PRIVATE_FINAL_STATIC, methodField, METHOD, null);
ce.declare_field(Constants.PRIVATE_FINAL_STATIC, methodProxyField, METHOD_PROXY, null);
ce.declare_field(Constants.PRIVATE_FINAL_STATIC, EMPTY_ARGS_NAME, Constants.TYPE_OBJECT_ARRAY, null);
CodeEmitter e;
// access method
e = ce.begin_method(Constants.ACC_FINAL, impl, method.getExceptionTypes());
superHelper(e, method);
e.return_value();
e.end_method();
// around method
e = context.beginMethod(ce, method);
Label nullInterceptor = e.make_label();
context.emitCallback(e, context.getIndex(method));
e.dup();
e.ifnull(nullInterceptor);
e.load_this();
e.getfield(methodField);
if (sig.getArgumentTypes().length == 0) {
e.getfield(EMPTY_ARGS_NAME);
} else {
e.create_arg_array();
}
e.getfield(methodProxyField);
e.invoke_interface(METHOD_INTERCEPTOR, INTERCEPT);
e.unbox_or_zero(sig.getReturnType());
e.return_value();
e.mark(nullInterceptor);
superHelper(e, method);
e.return_value();
e.end_method();
}
generateFindProxy(ce, sigMap);
}
use of org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter in project powermock by powermock.
the class Enhancer method emitGetCallbacks.
private void emitGetCallbacks(ClassEmitter ce) {
CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC, GET_CALLBACKS, null);
e.load_this();
e.invoke_static_this(BIND_CALLBACKS);
e.load_this();
e.push(callbackTypes.length);
e.newarray(CALLBACK);
for (int i = 0; i < callbackTypes.length; i++) {
e.dup();
e.push(i);
e.load_this();
e.getfield(getCallbackField(i));
e.aastore();
}
e.return_value();
e.end_method();
}
use of org.powermock.api.mockito.repackaged.cglib.core.CodeEmitter in project powermock by powermock.
the class Enhancer method emitMethods.
private void emitMethods(final ClassEmitter ce, List methods, List actualMethods) {
CallbackGenerator[] generators = CallbackInfo.getGenerators(callbackTypes);
Map groups = new HashMap();
final Map indexes = new HashMap();
final Map originalModifiers = new HashMap();
final Map positions = CollectionUtils.getIndexMap(methods);
Iterator it1 = methods.iterator();
Iterator it2 = (actualMethods != null) ? actualMethods.iterator() : null;
while (it1.hasNext()) {
MethodInfo method = (MethodInfo) it1.next();
Method actualMethod = (it2 != null) ? (Method) it2.next() : null;
int index = filter.accept(actualMethod, actualMethods);
if (index >= callbackTypes.length) {
throw new IllegalArgumentException("Callback filter returned an index that is too large: " + index);
}
originalModifiers.put(method, new Integer((actualMethod != null) ? actualMethod.getModifiers() : method.getModifiers()));
indexes.put(method, new Integer(index));
List group = (List) groups.get(generators[index]);
if (group == null) {
groups.put(generators[index], group = new ArrayList(methods.size()));
}
group.add(method);
}
Set seenGen = new HashSet();
CodeEmitter se = ce.getStaticHook();
se.new_instance(THREAD_LOCAL);
se.dup();
se.invoke_constructor(THREAD_LOCAL, CSTRUCT_NULL);
se.putfield(THREAD_CALLBACKS_FIELD);
final Object[] state = new Object[1];
CallbackGenerator.Context context = new CallbackGenerator.Context() {
public ClassLoader getClassLoader() {
return Enhancer.this.getClassLoader();
}
public int getOriginalModifiers(MethodInfo method) {
return ((Integer) originalModifiers.get(method)).intValue();
}
public int getIndex(MethodInfo method) {
return ((Integer) indexes.get(method)).intValue();
}
public void emitCallback(CodeEmitter e, int index) {
emitCurrentCallback(e, index);
}
public Signature getImplSignature(MethodInfo method) {
return rename(method.getSignature(), ((Integer) positions.get(method)).intValue());
}
public CodeEmitter beginMethod(ClassEmitter ce, MethodInfo method) {
CodeEmitter e = EmitUtils.begin_method(ce, method);
if (!interceptDuringConstruction && !TypeUtils.isAbstract(method.getModifiers())) {
Label constructed = e.make_label();
e.load_this();
e.getfield(CONSTRUCTED_FIELD);
e.if_jump(e.NE, constructed);
e.load_this();
e.load_args();
e.super_invoke();
e.return_value();
e.mark(constructed);
}
return e;
}
};
for (int i = 0; i < callbackTypes.length; i++) {
CallbackGenerator gen = generators[i];
if (!seenGen.contains(gen)) {
seenGen.add(gen);
final List fmethods = (List) groups.get(gen);
if (fmethods != null) {
try {
gen.generate(ce, context, fmethods);
gen.generateStatic(se, context, fmethods);
} catch (RuntimeException x) {
throw x;
} catch (Exception x) {
throw new CodeGenerationException(x);
}
}
}
}
se.return_value();
se.end_method();
}
Aggregations