Java Code Examples for org.objectweb.asm.commons.GeneratorAdapter#returnValue()
The following examples show how to use
org.objectweb.asm.commons.GeneratorAdapter#returnValue() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CallChainInstrument.java From dacapobench with Apache License 2.0 | 7 votes |
@SuppressWarnings("unchecked") public void visitEnd() { if (!done && found) { done = true; try { GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC, new Method(LOG_INTERNAL_METHOD, LOG_METHOD_SIGNATURE), LOG_METHOD_SIGNATURE, new Type[] {}, this); Label start = mg.mark(); mg.invokeStatic(LOG_INTERNAL_TYPE, Method.getMethod(LOG_INTERNAL_CLASS.getMethod(LOG_METHOD_NAME))); mg.returnValue(); Label end = mg.mark(); mg.catchException(start, end, JAVA_LANG_THROWABLE_TYPE); mg.returnValue(); mg.endMethod(); } catch (NoSuchMethodException nsme) { System.err.println("Unable to find Agent.rlogCallChain method"); System.err.println("M:"+nsme); nsme.printStackTrace(); } } super.visitEnd(); }
Example 2
Source File: Redirection.java From Stark with Apache License 2.0 | 6 votes |
/** * Adds the instructions to do a generic redirection. * <p> * Note that the generated bytecode does not have a direct translation to code, but as an * example, the following code block gets inserted. * <code> * if ($starkChange != null) { * $starkChange.access$dispatch($name, new object[] { arg0, ... argsN }) * $anyCodeInsertedbyRestore * } * $originalMethodBody *</code> * @param mv the method visitor to add the instructions to. * @param change the local variable containing the alternate implementation. */ void redirect(GeneratorAdapter mv, int change) { // code to check if a new implementation of the current class is available. Label l0 = new Label(); mv.loadLocal(change); mv.visitJumpInsn(Opcodes.IFNULL, l0); doRedirect(mv, change); // Return if (type == Type.VOID_TYPE) { mv.pop(); } else { ByteCodeUtils.unbox(mv, type); } mv.returnValue(); // jump label for classes without any new implementation, just invoke the original // method implementation. mv.visitLabel(l0); }
Example 3
Source File: SystemInstrument.java From dacapobench with Apache License 2.0 | 6 votes |
public void visitEnd() { if (! doneAddField) { doneAddField = true; super.visitField(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, CLASS_FIELD, Type.getDescriptor(Agent.class), null, null); } if (! doneAddMethod) { doneAddMethod = true; GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, new Method(LOG_CLASS_METHOD, LOG_CLASS_SIGNATURE), LOG_CLASS_SIGNATURE, new Type[] {}, this); Label target = mg.newLabel(); mg.getStatic(JAVA_LANG_SYSTEM_TYPE, CLASS_FIELD, JAVA_LANG_CLASS_TYPE); mg.ifNull(target); mg.push(LOG_INTERNAL_TYPE); mg.putStatic(JAVA_LANG_SYSTEM_TYPE, CLASS_FIELD, JAVA_LANG_CLASS_TYPE); mg.mark(target); mg.getStatic(JAVA_LANG_SYSTEM_TYPE, CLASS_FIELD, JAVA_LANG_CLASS_TYPE); mg.returnValue(); } super.visitEnd(); }
Example 4
Source File: ClinitInstrument.java From dacapobench with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") public void visitEnd() { if (!foundClinit && instrument()) { // didn't find <clinit> so lets make one try { GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, new Method(CLINIT_NAME, CLINIT_SIGNATURE), CLINIT_SIGNATURE, new Type[] {}, this); Label start = mg.mark(); mg.push(className); mg.invokeStatic(LOG_INTERNAL_TYPE, Method.getMethod(LOG_INTERNAL_CLASS.getMethod(LOG_METHOD_NAME, String.class))); Label end = mg.mark(); mg.returnValue(); mg.catchException(start, end, JAVA_LANG_THROWABLE_TYPE); mg.returnValue(); mg.endMethod(); } catch (NoSuchMethodException nsme) { System.out.println("Unable to find Agent.reportClass method"); } } super.visitEnd(); }
Example 5
Source File: Redirection.java From Aceso with Apache License 2.0 | 6 votes |
/** * Adds the instructions to do a generic redirection. */ protected void redirect(GeneratorAdapter mv, int change) { // code to check if a new implementation of the current class is available. Label l0 = new Label(); mv.loadLocal(change); mv.visitJumpInsn(Opcodes.IFNULL, l0); doRedirect(mv, change); // Return if (type == Type.VOID_TYPE) { mv.pop(); } else { ByteCodeUtils.unbox(mv, type); } mv.returnValue(); // jump label for classes without any new implementation, just invoke the original // method implementation. mv.visitLabel(l0); }
Example 6
Source File: TestCustomFunctions.java From lucene-solr with Apache License 2.0 | 6 votes |
public Class<?> createFakeClass() { String className = TestCustomFunctions.class.getName() + "$Foo"; ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); classWriter.visit(Opcodes.V1_5, ACC_PUBLIC | ACC_SUPER | ACC_FINAL | ACC_SYNTHETIC, className.replace('.', '/'), null, Type.getInternalName(Object.class), null); org.objectweb.asm.commons.Method m = org.objectweb.asm.commons.Method.getMethod("void <init>()"); GeneratorAdapter constructor = new GeneratorAdapter(ACC_PRIVATE | ACC_SYNTHETIC, m, null, null, classWriter); constructor.loadThis(); constructor.loadArgs(); constructor.invokeConstructor(Type.getType(Object.class), m); constructor.returnValue(); constructor.endMethod(); GeneratorAdapter gen = new GeneratorAdapter(ACC_STATIC | ACC_PUBLIC | ACC_SYNTHETIC, org.objectweb.asm.commons.Method.getMethod("double bar()"), null, null, classWriter); gen.push(2.0); gen.returnValue(); gen.endMethod(); byte[] bc = classWriter.toByteArray(); return defineClass(className, bc, 0, bc.length); }
Example 7
Source File: AsmDeltaSpikeProxyClassGenerator.java From deltaspike with Apache License 2.0 | 6 votes |
private static void defineDefaultConstructor(ClassWriter cw, Type proxyType, Type superType) { GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, new Method("<init>", Type.VOID_TYPE, new Type[]{ }), null, null, cw); mg.visitCode(); // invoke super constructor mg.loadThis(); mg.invokeConstructor(superType, Method.getMethod("void <init> ()")); mg.returnValue(); mg.endMethod(); mg.visitEnd(); }
Example 8
Source File: AsmDeltaSpikeProxyClassGenerator.java From deltaspike with Apache License 2.0 | 6 votes |
private static void defineSuperAccessorMethod(ClassWriter cw, java.lang.reflect.Method method, Type superType, String superAccessorMethodSuffix) { Method originalAsmMethod = Method.getMethod(method); Method newAsmMethod = new Method(method.getName() + superAccessorMethodSuffix, originalAsmMethod.getReturnType(), originalAsmMethod.getArgumentTypes()); GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, newAsmMethod, null, null, cw); mg.visitCode(); // call super method mg.loadThis(); mg.loadArgs(); mg.visitMethodInsn(Opcodes.INVOKESPECIAL, superType.getInternalName(), method.getName(), Type.getMethodDescriptor(method), false); mg.returnValue(); // finish the method mg.endMethod(); mg.visitMaxs(10, 10); mg.visitEnd(); }
Example 9
Source File: WeavingClassVisitor.java From glowroot with Apache License 2.0 | 5 votes |
@RequiresNonNull("type") private void overrideAndWeaveInheritedMethod(AnalyzedMethod inheritedMethod) { String superName = analyzedClass.superName(); // superName is null only for java.lang.Object which doesn't inherit anything // so safe to assume superName not null here checkNotNull(superName); String[] exceptions = new String[inheritedMethod.exceptions().size()]; for (int i = 0; i < inheritedMethod.exceptions().size(); i++) { exceptions[i] = ClassNames.toInternalName(inheritedMethod.exceptions().get(i)); } List<Advice> advisors = removeSuperseded(inheritedMethod.advisors()); MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, inheritedMethod.name(), inheritedMethod.getDesc(), inheritedMethod.signature(), exceptions); mv = visitMethodWithAdvice(mv, ACC_PUBLIC, inheritedMethod.name(), inheritedMethod.getDesc(), advisors); checkNotNull(mv); GeneratorAdapter mg = new GeneratorAdapter(mv, ACC_PUBLIC, inheritedMethod.name(), inheritedMethod.getDesc()); mg.visitCode(); mg.loadThis(); mg.loadArgs(); Type superType = Type.getObjectType(ClassNames.toInternalName(superName)); // method is called invokeConstructor, but should really be called invokeSpecial Method method = new Method(inheritedMethod.name(), inheritedMethod.getDesc()); mg.invokeConstructor(superType, method); mg.returnValue(); mg.endMethod(); }
Example 10
Source File: PointcutClassVisitor.java From glowroot with Apache License 2.0 | 5 votes |
@Override public void visitEnd() { if (onBeforeMethodVisitor != null) { int access = onBeforeMethodVisitor.access; String name = onBeforeMethodVisitor.name; String descriptor = "(Z" + onBeforeMethodVisitor.descriptor.substring(1); String signature = onBeforeMethodVisitor.signature; String[] exceptions = onBeforeMethodVisitor.exceptions; GeneratorAdapter mv = new GeneratorAdapter( cw.visitMethod(access, name, descriptor, signature, exceptions), access, name, descriptor); mv.visitCode(); mv.visitVarInsn(ILOAD, 0); Label endWithDefaultLabel = new Label(); mv.visitJumpInsn(IFEQ, endWithDefaultLabel); mv.loadArgs(1, mv.getArgumentTypes().length - 1); mv.visitMethodInsn(INVOKESTATIC, checkNotNull(className), name, onBeforeMethodVisitor.descriptor, false); mv.returnValue(); mv.visitLabel(endWithDefaultLabel); mv.visitFrame(F_NEW, 0, new Object[0], 0, new Object[0]); if (mv.getReturnType().getSort() != Type.VOID) { // return value will be ignored when !enabled, but need to return something WeavingMethodVisitor.pushDefault(mv, mv.getReturnType()); } mv.returnValue(); mv.endMethod(); } super.visitEnd(); }
Example 11
Source File: TestThreadExecutionGenerator.java From lin-check with GNU Lesser General Public License v3.0 | 4 votes |
private static void generateRun(ClassVisitor cv, Type testType, int iThread, List<Actor> actors, List<Object> objArgs, boolean waitsEnabled) { int access = ACC_PUBLIC; Method m = new Method("call", RESULT_ARRAY_TYPE, NO_ARGS); GeneratorAdapter mv = new GeneratorAdapter(access, m, // Try-catch blocks sorting is required new TryCatchBlockSorter(cv.visitMethod(access, m.getName(), m.getDescriptor(), null, null), access, m.getName(), m.getDescriptor(), null, null) ); mv.visitCode(); // Create Result[] array and store it to a local variable int resLocal = createResultArray(mv, actors.size()); // Call runner's onStart(iThread) method mv.loadThis(); mv.getField(TEST_THREAD_EXECUTION_TYPE, "runner", RUNNER_TYPE); mv.push(iThread); mv.invokeVirtual(RUNNER_TYPE, RUNNER_ON_START_METHOD); // Number of current operation (starts with 0) int iLocal = mv.newLocal(Type.INT_TYPE); mv.push(0); mv.storeLocal(iLocal); // Invoke actors for (int i = 0; i < actors.size(); i++) { Actor actor = actors.get(i); // Add busy-wait before operation execution (for non-first operations only) if (waitsEnabled && i > 0) { mv.loadThis(); mv.getField(TEST_THREAD_EXECUTION_TYPE, "waits", INT_ARRAY_TYPE); mv.push(i - 1); mv.arrayLoad(Type.INT_TYPE); mv.invokeStatic(UTILS_TYPE, UTILS_CONSUME_CPU); } // Start of try-catch block for exceptions which this actor should handle Label start, end = null, handler = null, handlerEnd = null; if (actor.handlesExceptions()) { start = mv.newLabel(); end = mv.newLabel(); handler = mv.newLabel(); handlerEnd = mv.newLabel(); for (Class<? extends Throwable> ec : actor.handledExceptions) mv.visitTryCatchBlock(start, end, handler, Type.getType(ec).getInternalName()); mv.visitLabel(start); } // Load result array and index to store the current result mv.loadLocal(resLocal); mv.push(i); // Load test instance mv.loadThis(); mv.getField(TEST_THREAD_EXECUTION_TYPE, "testInstance", OBJECT_TYPE); mv.checkCast(testType); // Load arguments for operation for (int j = 0; j < actor.arguments.length; j++) { pushArgumentOnStack(mv, objArgs, actor.arguments[j], actor.method.getParameterTypes()[j]); } // Invoke operation Method actorMethod = Method.getMethod(actor.method); mv.invokeVirtual(testType, actorMethod); // Create result mv.box(actorMethod.getReturnType()); // box if needed if (actor.method.getReturnType() == void.class) { mv.pop(); mv.invokeStatic(RESULT_TYPE, RESULT_CREATE_VOID_RESULT); } else { mv.invokeStatic(RESULT_TYPE, RESULT_CREATE_VALUE_RESULT); } // Store result to array mv.arrayStore(RESULT_TYPE); // End of try-catch block if (actor.handlesExceptions()) { mv.visitLabel(end); mv.goTo(handlerEnd); mv.visitLabel(handler); storeExceptionResultFromThrowable(mv, resLocal, iLocal); mv.visitLabel(handlerEnd); } // Increment number of current operation mv.iinc(iLocal, 1); } // Call runner's onFinish(iThread) method mv.loadThis(); mv.getField(TEST_THREAD_EXECUTION_TYPE, "runner", RUNNER_TYPE); mv.push(iThread); mv.invokeVirtual(RUNNER_TYPE, RUNNER_ON_FINISH_METHOD); // Return results mv.loadThis(); mv.loadLocal(resLocal); mv.returnValue(); mv.visitMaxs(1, 1); mv.visitEnd(); }
Example 12
Source File: ExpressionComparator.java From datakernel with Apache License 2.0 | 4 votes |
@Override public Type load(Context ctx) { GeneratorAdapter g = ctx.getGeneratorAdapter(); Label labelReturn = new Label(); for (ComparablePair pair : pairs) { Type leftPropertyType = pair.left.load(ctx); Type rightPropertyType = pair.right.load(ctx); checkArgument(leftPropertyType.equals(rightPropertyType), "Types of compared values should match"); if (isPrimitiveType(leftPropertyType)) { g.invokeStatic(wrap(leftPropertyType), new Method("compare", INT_TYPE, new Type[]{leftPropertyType, leftPropertyType})); g.dup(); g.ifZCmp(NE, labelReturn); g.pop(); } else if (!pair.nullable) { g.invokeVirtual(leftPropertyType, new Method("compareTo", INT_TYPE, new Type[]{Type.getType(Object.class)})); g.dup(); g.ifZCmp(NE, labelReturn); g.pop(); } else { VarLocal varRight = ctx.newLocal(rightPropertyType); varRight.store(ctx); VarLocal varLeft = ctx.newLocal(leftPropertyType); varLeft.store(ctx); Label continueLabel = new Label(); Label nonNulls = new Label(); Label leftNonNull = new Label(); varLeft.load(ctx); g.ifNonNull(leftNonNull); varRight.load(ctx); g.ifNull(continueLabel); g.push(-1); g.returnValue(); g.mark(leftNonNull); varRight.load(ctx); g.ifNonNull(nonNulls); g.push(1); g.returnValue(); g.mark(nonNulls); varLeft.load(ctx); varRight.load(ctx); g.invokeVirtual(leftPropertyType, new Method("compareTo", INT_TYPE, new Type[]{Type.getType(Object.class)})); g.dup(); g.ifZCmp(NE, labelReturn); g.pop(); g.mark(continueLabel); } } g.push(0); g.mark(labelReturn); return INT_TYPE; }
Example 13
Source File: AsmDeltaSpikeProxyClassGenerator.java From deltaspike with Apache License 2.0 | 4 votes |
private static void defineMethod(ClassWriter cw, java.lang.reflect.Method method, Type proxyType) { 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(); mg.getField(proxyType, FIELDNAME_INVOCATION_HANDLER, TYPE_DELTA_SPIKE_PROXY_INVOCATION_HANDLER); mg.loadThis(); loadCurrentMethod(mg, method, methodType); loadArguments(mg, method, methodType); mg.invokeVirtual(TYPE_DELTA_SPIKE_PROXY_INVOCATION_HANDLER, Method.getMethod("Object invoke(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(12, 12); mg.visitEnd(); }
Example 14
Source File: MethodRedirection.java From AnoleFix with MIT License | 3 votes |
/** * For methods, restore creates a return from the dispatch call, to exit the method * once the new implementation has been executed. for void methods, this is an empty return * and for all other methods, it returns the value from the dispatch call. * <p/> * Note that the generated bytecode does not have a direct translation to code, but as an * example, this restore implementation in combination with the base class generates the * following for methods with avoid return type: * <code> * if ($change != null) { * $change.access$dispatch($name, new object[] { arg0, ... argsN }) * return; * } * $originalMethodBody * </code> * and the following for methods with a non-void return type: * <code> * if ($change != null) { * return $change.access$dispatch($name, new object[] { arg0, ... argsN }) * } * $originalMethodBody * </code> */ @Override protected void restore(GeneratorAdapter mv, List<Type> args) { if (type == Type.VOID_TYPE) { mv.pop(); } else { ByteCodeUtils.unbox(mv, type); } mv.returnValue(); }