use of org.objectweb.asm.commons.Method in project deltaspike by apache.
the class AsmProxyClassGenerator method defineMethod.
private static void defineMethod(ClassWriter cw, java.lang.reflect.Method method, Class manualInvocationHandlerClass) {
Type methodType = Type.getType(method);
ArrayList<Type> exceptionsToCatch = new ArrayList<Type>();
for (Class<?> exception : method.getExceptionTypes()) {
if (!RuntimeException.class.isAssignableFrom(exception)) {
exceptionsToCatch.add(Type.getType(exception));
}
}
// push the method definition
int modifiers = (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED) & method.getModifiers();
Method asmMethod = Method.getMethod(method);
GeneratorAdapter mg = new GeneratorAdapter(modifiers, asmMethod, null, getTypes(method.getExceptionTypes()), cw);
// copy annotations
for (Annotation annotation : method.getDeclaredAnnotations()) {
mg.visitAnnotation(Type.getDescriptor(annotation.annotationType()), true).visitEnd();
}
mg.visitCode();
Label tryBlockStart = mg.mark();
mg.loadThis();
loadCurrentMethod(mg, method, methodType);
loadArguments(mg, method, methodType);
// invoke our ProxyInvocationHandler
mg.invokeStatic(Type.getType(manualInvocationHandlerClass), Method.getMethod("Object staticInvoke(Object, java.lang.reflect.Method, Object[])"));
// cast the result
mg.unbox(methodType.getReturnType());
// build try catch
Label tryBlockEnd = mg.mark();
// push return
mg.returnValue();
// catch runtime exceptions and rethrow it
Label rethrow = mg.mark();
mg.visitVarInsn(Opcodes.ASTORE, 1);
mg.visitVarInsn(Opcodes.ALOAD, 1);
mg.throwException();
mg.visitTryCatchBlock(tryBlockStart, tryBlockEnd, rethrow, Type.getInternalName(RuntimeException.class));
// catch checked exceptions and rethrow it
boolean throwableCatched = false;
if (!exceptionsToCatch.isEmpty()) {
rethrow = mg.mark();
mg.visitVarInsn(Opcodes.ASTORE, 1);
mg.visitVarInsn(Opcodes.ALOAD, 1);
mg.throwException();
// catch declared exceptions and rethrow it...
for (Type exceptionType : exceptionsToCatch) {
if (exceptionType.getClassName().equals(Throwable.class.getName())) {
throwableCatched = true;
}
mg.visitTryCatchBlock(tryBlockStart, tryBlockEnd, rethrow, exceptionType.getInternalName());
}
}
// if throwable isn't alreached cachted, catch it and wrap it with an UndeclaredThrowableException and throw it
if (!throwableCatched) {
Type uteType = Type.getType(UndeclaredThrowableException.class);
Label wrapAndRethrow = mg.mark();
mg.visitVarInsn(Opcodes.ASTORE, 1);
mg.newInstance(uteType);
mg.dup();
mg.visitVarInsn(Opcodes.ALOAD, 1);
mg.invokeConstructor(uteType, Method.getMethod("void <init>(java.lang.Throwable)"));
mg.throwException();
mg.visitTryCatchBlock(tryBlockStart, tryBlockEnd, wrapAndRethrow, Type.getInternalName(Throwable.class));
}
// finish the method
mg.endMethod();
mg.visitMaxs(10, 10);
mg.visitEnd();
}
use of org.objectweb.asm.commons.Method in project deltaspike by apache.
the class AsmProxyClassGenerator method defineDelegateInvocationHandlerConstructor.
private static void defineDelegateInvocationHandlerConstructor(ClassWriter cw, Type proxyType, Type superType, Type delegateInvocationHandlerType) {
GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, new Method("<init>", Type.VOID_TYPE, new Type[] { delegateInvocationHandlerType }), null, null, cw);
mg.visitCode();
// invoke super constructor
mg.loadThis();
mg.invokeConstructor(superType, Method.getMethod("void <init> ()"));
// set invocation handler
mg.loadThis();
mg.loadArg(0);
mg.putField(proxyType, FIELDNAME_DELEGATE_INVOCATION_HANDLER, delegateInvocationHandlerType);
mg.returnValue();
mg.endMethod();
mg.visitEnd();
}
use of org.objectweb.asm.commons.Method in project cdap by caskdata.
the class HttpHandlerGenerator method generateConstructor.
/**
* Generates the constructor. The constructor generated has signature {@code (DelegatorContext, MetricsContext)}.
*/
private void generateConstructor(TypeToken<? extends HttpServiceHandler> delegateType, ClassWriter classWriter) {
Method constructor = Methods.getMethod(void.class, "<init>", DelegatorContext.class, MetricsContext.class);
String signature = Signatures.getMethodSignature(constructor, TypeToken.of(void.class), getContextType(delegateType), TypeToken.of(MetricsContext.class));
// Constructor(DelegatorContext, MetricsContext)
GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, constructor, signature, null, classWriter);
// super(context, metricsContext);
mg.loadThis();
mg.loadArg(0);
mg.loadArg(1);
mg.invokeConstructor(Type.getType(AbstractHttpHandlerDelegator.class), Methods.getMethod(void.class, "<init>", DelegatorContext.class, MetricsContext.class));
mg.returnValue();
mg.endMethod();
}
use of org.objectweb.asm.commons.Method in project Lucee by lucee.
the class TagHelper method setAttributes.
private static void setAttributes(BytecodeContext bc, Tag tag, int currLocal, Type currType, boolean doDefault, boolean interf) throws TransformerException {
GeneratorAdapter adapter = bc.getAdapter();
Map<String, Attribute> attributes = tag.getAttributes();
String methodName;
Attribute attr;
Iterator<Attribute> it = attributes.values().iterator();
while (it.hasNext()) {
attr = it.next();
if (doDefault != attr.isDefaultAttribute())
continue;
if (attr.isDynamicType()) {
adapter.loadLocal(currLocal);
if (interf)
adapter.checkCast(Types.DYNAMIC_ATTRIBUTES);
adapter.visitInsn(Opcodes.ACONST_NULL);
// adapter.push(attr.getName());
bc.getFactory().registerKey(bc, bc.getFactory().createLitString(attr.getName()), false);
attr.getValue().writeOut(bc, Expression.MODE_REF);
ASMUtil.invoke(interf ? ASMUtil.INTERFACE : ASMUtil.VIRTUAL, adapter, interf ? Types.DYNAMIC_ATTRIBUTES : currType, SET_DYNAMIC_ATTRIBUTE);
// adapter.invokeVirtual(currType, SET_DYNAMIC_ATTRIBUTE);
} else {
// TagUtil.setAttribute(PageContext pc,boolean doDynamic,boolean silently,Tag tag, String name,Object value)
if (interf) {
// pc
adapter.loadArg(0);
// tag
adapter.loadLocal(currLocal);
// name
bc.getFactory().createLitString(attr.getName()).writeOut(bc, Expression.MODE_REF);
// value
attr.getValue().writeOut(bc, Expression.MODE_REF);
adapter.invokeStatic(TAG_UTIL, SET_ATTRIBUTE4);
} else {
Type type = CastOther.getType(attr.getType());
methodName = tag.getTagLibTag().getSetter(attr, type);
adapter.loadLocal(currLocal);
attr.getValue().writeOut(bc, Types.isPrimitiveType(type) ? Expression.MODE_VALUE : Expression.MODE_REF);
adapter.invokeVirtual(currType, new Method(methodName, Type.VOID_TYPE, new Type[] { type }));
}
}
}
}
use of org.objectweb.asm.commons.Method in project Lucee by lucee.
the class TagInclude method _writeOut.
/**
* @see lucee.transformer.bytecode.statement.tag.TagBase#_writeOut(org.objectweb.asm.commons.GeneratorAdapter)
*/
@Override
public void _writeOut(BytecodeContext bc) throws TransformerException {
Type type = Types.PAGE_CONTEXT;
Method func = DO_INCLUDE_RUN_ONCE2;
// cachedwithin
Expression cachedwithin = null;
Attribute attr = getAttribute("cachedwithin");
if (attr != null && attr.getValue() != null) {
cachedwithin = attr.getValue();
type = Types.PAGE_CONTEXT_IMPL;
func = DO_INCLUDE_RUN_ONCE3;
}
GeneratorAdapter adapter = bc.getAdapter();
adapter.loadArg(0);
if (cachedwithin != null)
adapter.checkCast(Types.PAGE_CONTEXT_IMPL);
// template
getAttribute("template").getValue().writeOut(bc, Expression.MODE_REF);
// run Once
attr = getAttribute("runonce");
ExprBoolean expr = (attr == null) ? bc.getFactory().FALSE() : bc.getFactory().toExprBoolean(attr.getValue());
expr.writeOut(bc, Expression.MODE_VALUE);
// cachedwithin
if (cachedwithin != null)
cachedwithin.writeOut(bc, Expression.MODE_REF);
adapter.invokeVirtual(type, func);
}
Aggregations