org.objectweb.asm.commons.GeneratorAdapter Java Examples
The following examples show how to use
org.objectweb.asm.commons.GeneratorAdapter.
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: AsmDeltaSpikeProxyClassGenerator.java From deltaspike with Apache License 2.0 | 6 votes |
/** * Generates: * <pre> * Method method = * method.getDeclaringClass().getMethod("methodName", new Class[] { args... }); * </pre> * @param mg * @param method * @param methodType */ private static void loadCurrentMethod(GeneratorAdapter mg, java.lang.reflect.Method method, Type methodType) { mg.push(Type.getType(method.getDeclaringClass())); mg.push(method.getName()); // create the Class[] mg.push(methodType.getArgumentTypes().length); mg.newArray(TYPE_CLASS); // push parameters into array for (int i = 0; i < methodType.getArgumentTypes().length; i++) { // keep copy of array on stack mg.dup(); // push index onto stack mg.push(i); mg.push(methodType.getArgumentTypes()[i]); mg.arrayStore(TYPE_CLASS); } // invoke getMethod() with the method name and the array of types mg.invokeVirtual(TYPE_CLASS, Method.getMethod("java.lang.reflect.Method getDeclaredMethod(String, Class[])")); }
Example #3
Source File: IntSwitch.java From Aceso with Apache License 2.0 | 6 votes |
private void visitx(GeneratorAdapter mv, List<String> strings) { if (strings.size() == 1) { visitCase(strings.get(0)); return; } for (String string : strings) { Label label = new Label(); visitString(); mv.visitLdcInsn(string); mv.invokeVirtual(STRING_TYPE, Method.getMethod("boolean equals(Object)")); mv.visitJumpInsn(Opcodes.IFEQ, label); visitCase(string); mv.visitLabel(label); } visitDefault(); }
Example #4
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 #5
Source File: MeasureHyperLogLog.java From datakernel with Apache License 2.0 | 6 votes |
@Override public Type load(Context ctx) { GeneratorAdapter g = ctx.getGeneratorAdapter(); Type accumulatorType = accumulator.load(ctx); Type valueType = value.load(ctx); String methodName; Type methodParameterType; if (valueType == LONG_TYPE || valueType.getClassName().equals(Long.class.getName())) { methodName = "addLong"; methodParameterType = LONG_TYPE; } else if (valueType == INT_TYPE || valueType.getClassName().equals(Integer.class.getName())) { methodName = "addInt"; methodParameterType = INT_TYPE; } else { methodName = "addObject"; methodParameterType = getType(Object.class); } if (isWrapperType(valueType)) { g.unbox(methodParameterType); } ctx.invoke(accumulatorType, methodName, methodParameterType); return VOID_TYPE; }
Example #6
Source File: RobustAsmUtils.java From Robust with Apache License 2.0 | 6 votes |
private static void createClassArray(GeneratorAdapter mv, List<Type> args) { // create an array of objects capable of containing all the parameters and optionally the "this" createLocals(mv, args); // we need to maintain the stack index when loading parameters from, as for long and double // values, it uses 2 stack elements, all others use only 1 stack element. int stackIndex = 0; for (int arrayIndex = 0; arrayIndex < args.size(); arrayIndex++) { Type arg = args.get(arrayIndex); // duplicate the array of objects reference, it will be used to store the value in. mv.dup(); // index in the array of objects to store the boxed parameter. mv.push(arrayIndex); // Pushes the appropriate local variable on the stack redirectLocal(mv, arg); // mv.visitLdcInsn(Type.getType(arg.getDescriptor())); // potentially box up intrinsic types. // mv.box(arg); mv.arrayStore(Type.getType(Class.class)); // stack index must progress according to the parameter type we just processed. // stackIndex += arg.getSize(); } }
Example #7
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 #8
Source File: ExpressionIsNotNull.java From datakernel with Apache License 2.0 | 6 votes |
@Override public Type load(Context ctx) { GeneratorAdapter g = ctx.getGeneratorAdapter(); Label labelNotNull = new Label(); Label labelExit = new Label(); expression.load(ctx); g.ifNonNull(labelNotNull); g.push(false); g.goTo(labelExit); g.mark(labelNotNull); g.push(true); g.mark(labelExit); return Type.BOOLEAN_TYPE; }
Example #9
Source File: StringSwitch.java From AnoleFix with MIT License | 6 votes |
/** * Emit code for a string if-else block. * * if (s.equals("collided_method1")) { * visit(s); * } else if (s.equals("collided_method2")) { * visit(s); * } * * In the most common case of just one string, this degenerates to: * * visit(s) * */ private void visitx(GeneratorAdapter mv, List<String> strings) { if (strings.size() == 1) { visitCase(strings.get(0)); return; } for (int i = 0; i < strings.size(); ++i) { String string = strings.get(i); Label label = new Label(); visitString(); mv.visitLdcInsn(string); mv.invokeVirtual(STRING_TYPE, Method.getMethod("boolean equals(Object)")); mv.visitJumpInsn(Opcodes.IFEQ, label); visitCase(string); mv.visitLabel(label); } visitDefault(); }
Example #10
Source File: ByteCodeUtils.java From Aceso with Apache License 2.0 | 6 votes |
/** * Given an array on the stack, it loads it with the values of the given variables stating at * offset. */ static void loadVariableArray( GeneratorAdapter mv, List<LocalVariable> variables, int offset) { // we need to maintain the stack index when loading parameters from, as for long and double // values, it uses 2 stack elements, all others use only 1 stack element. for (int i = offset; i < variables.size(); i++) { LocalVariable variable = variables.get(i); // duplicate the array of objects reference, it will be used to store the value in. mv.dup(); // index in the array of objects to store the boxed parameter. mv.push(i); // Pushes the appropriate local variable on the stack mv.visitVarInsn(variable.type.getOpcode(Opcodes.ILOAD), variable.var); // potentially box up intrinsic types. mv.box(variable.type); // store it in the array mv.arrayStore(Type.getType(Object.class)); } }
Example #11
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 #12
Source File: ExpressionBooleanOr.java From datakernel with Apache License 2.0 | 6 votes |
@Override public Type load(Context ctx) { GeneratorAdapter g = ctx.getGeneratorAdapter(); Label exit = new Label(); Label labelTrue = new Label(); for (Expression predicate : expressions) { Type type = predicate.load(ctx); checkArgument(type == BOOLEAN_TYPE); g.ifZCmp(GeneratorAdapter.NE, labelTrue); } g.push(false); g.goTo(exit); g.mark(labelTrue); g.push(true); g.mark(exit); return BOOLEAN_TYPE; }
Example #13
Source File: StringSwitch.java From Stark with Apache License 2.0 | 6 votes |
/** * Emit code for a string if-else block. * * if (s.equals("collided_method1")) { * visit(s); * } else if (s.equals("collided_method2")) { * visit(s); * } * * In the most common case of just one string, this degenerates to: * * visit(s) * */ private void visitx(GeneratorAdapter mv, List<String> strings) { if (strings.size() == 1) { visitCase(strings.get(0)); return; } for (String string : strings) { Label label = new Label(); visitString(); mv.visitLdcInsn(string); mv.invokeVirtual(STRING_TYPE, Method.getMethod("boolean equals(Object)")); mv.visitJumpInsn(Opcodes.IFEQ, label); visitCase(string); mv.visitLabel(label); } visitDefault(); }
Example #14
Source File: Context.java From datakernel with Apache License 2.0 | 6 votes |
public Context(DefiningClassLoader classLoader, GeneratorAdapter g, Type selfType, Class<?> superclass, List<Class<?>> interfaces, Map<String, Class<?>> fields, Map<Method, Expression> methods, Map<Method, Expression> staticMethods, Method method, Map<String, Object> staticConstants) { this.classLoader = classLoader; this.g = g; this.selfType = selfType; this.superclass = superclass; this.interfaces = interfaces; this.fields = fields; this.methods = methods; this.staticMethods = staticMethods; this.method = method; this.staticConstants = staticConstants; }
Example #15
Source File: ExpressionIf.java From datakernel with Apache License 2.0 | 6 votes |
@Override public Type load(Context ctx) { Label labelTrue = new Label(); Label labelExit = new Label(); GeneratorAdapter g = ctx.getGeneratorAdapter(); Type conditionType = condition.load(ctx); g.push(true); g.ifCmp(conditionType, GeneratorAdapter.EQ, labelTrue); right.load(ctx); g.goTo(labelExit); g.mark(labelTrue); Type leftType = left.load(ctx); g.mark(labelExit); return leftType; }
Example #16
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 #17
Source File: ExpressionBooleanAnd.java From datakernel with Apache License 2.0 | 6 votes |
@Override public Type load(Context ctx) { GeneratorAdapter g = ctx.getGeneratorAdapter(); Label exit = new Label(); Label labelFalse = new Label(); for (Expression predicate : expressions) { Type type = predicate.load(ctx); checkArgument(type == BOOLEAN_TYPE); g.ifZCmp(GeneratorAdapter.EQ, labelFalse); } g.push(true); g.goTo(exit); g.mark(labelFalse); g.push(false); g.mark(exit); return BOOLEAN_TYPE; }
Example #18
Source File: ByteCodeUtils.java From Stark with Apache License 2.0 | 6 votes |
/** * Given an array with values at the top of the stack, the values are unboxed and stored * on the given variables. The array is popped from the stack. */ static void restoreVariables( @NonNull GeneratorAdapter mv, @NonNull List<LocalVariable> variables) { for (int i = 0; i < variables.size(); i++) { LocalVariable variable = variables.get(i); // Duplicates the array on the stack; mv.dup(); // Sets up the index mv.push(i); // Gets the Object value mv.arrayLoad(Type.getType(Object.class)); // Unboxes to the type of the local variable mv.unbox(variable.type); // Restores the local variable mv.visitVarInsn(variable.type.getOpcode(Opcodes.ISTORE), variable.var); } // Pops the array from the stack. mv.pop(); }
Example #19
Source File: ExpressionStaticField.java From datakernel with Apache License 2.0 | 6 votes |
@Override public void store(Context ctx, Object storeContext, Type type) { GeneratorAdapter g = ctx.getGeneratorAdapter(); try { Field javaField = owner.getField(name); if (Modifier.isPublic(javaField.getModifiers()) && Modifier.isStatic(javaField.getModifiers())) { Type fieldType = getType(javaField.getType()); g.putStatic((Type) storeContext, name, fieldType); return; } } catch (NoSuchFieldException ignored) { } throw new RuntimeException(format("No static field or setter for class %s for field \"%s\". %s ", ((Type) storeContext).getClassName(), name, exceptionInGeneratedClass(ctx)) ); }
Example #20
Source File: ExpressionThrow.java From datakernel with Apache License 2.0 | 5 votes |
@Override public Type load(Context ctx) { GeneratorAdapter g = ctx.getGeneratorAdapter(); g.newInstance(getType(exceptionClass)); g.dup(); if (message == null) { g.invokeConstructor(getType(exceptionClass), new Method("<init>", VOID_TYPE, new Type[]{})); } else { message.load(ctx); g.invokeConstructor(getType(exceptionClass), new Method("<init>", VOID_TYPE, new Type[]{getType(String.class)})); } g.throwException(); return VOID_TYPE; }
Example #21
Source File: AbstractExpressionMapForEach.java From datakernel with Apache License 2.0 | 5 votes |
@Override public final Type load(Context ctx) { GeneratorAdapter g = ctx.getGeneratorAdapter(); Label labelLoop = new Label(); Label labelExit = new Label(); ctx.invoke(getEntries(), "iterator"); VarLocal iterator = ctx.newLocal(getType(Iterator.class)); iterator.store(ctx); g.mark(labelLoop); ctx.invoke(iterator, "hasNext"); g.push(false); g.ifCmp(BOOLEAN_TYPE, GeneratorAdapter.EQ, labelExit); Type entryType = getType(entryClazz); ctx.cast(ctx.invoke(iterator, "next"), entryType); VarLocal entry = ctx.newLocal(entryType); entry.store(ctx); Type forKeyType = forKey.apply(getKey(entry)).load(ctx); if (forKeyType.getSize() == 1) g.pop(); if (forKeyType.getSize() == 2) g.pop2(); Type forValueType = forValue.apply(getValue(entry)).load(ctx); if (forValueType.getSize() == 1) g.pop(); if (forValueType.getSize() == 2) g.pop2(); g.goTo(labelLoop); g.mark(labelExit); return Type.VOID_TYPE; }
Example #22
Source File: TestThreadExecutionGenerator.java From lin-check with GNU Lesser General Public License v3.0 | 5 votes |
private static void pushArgumentOnStack(GeneratorAdapter mv, List<Object> objArgs, Object arg, Class<?> argClass) { if (argClass == boolean.class) { mv.push((boolean) arg); } else if (argClass == byte.class) { mv.push((byte) arg); } else if (argClass == char.class) { mv.push((char) arg); } else if (argClass == short.class) { mv.push((short) arg); } else if (argClass == int.class) { mv.push((int) arg); } else if (argClass == long.class) { mv.push((long) arg); } else if (argClass == float.class) { mv.push((float) arg); } else if (argClass == double.class) { mv.push((double) arg); } else if (argClass == String.class) { mv.push((String) arg); } else { // Object type mv.loadThis(); // -> this mv.getField(TEST_THREAD_EXECUTION_TYPE, "objArgs", OBJECT_ARRAY_TYPE); // this -> objArgs mv.push(objArgs.size()); // objArgs -> objArgs, index mv.arrayLoad(OBJECT_TYPE); // objArgs, index -> arg mv.checkCast(Type.getType(argClass)); // cast object to argument type objArgs.add(arg); } }
Example #23
Source File: AbstractExpressionIteratorForEach.java From datakernel with Apache License 2.0 | 5 votes |
public Type arrayForEach(Context ctx, GeneratorAdapter g, Label labelLoop, Label labelExit) { VarLocal len = ctx.newLocal(INT_TYPE); g.arrayLength(); len.store(ctx); g.push(0); VarLocal varPosition = ctx.newLocal(INT_TYPE); varPosition.store(ctx); g.mark(labelLoop); varPosition.load(ctx); len.load(ctx); g.ifCmp(INT_TYPE, GeneratorAdapter.GE, labelExit); collection.load(ctx); varPosition.load(ctx); g.arrayLoad(getType(type)); VarLocal it = ctx.newLocal(getType(type)); it.store(ctx); forEach.apply(getValue(it)).load(ctx); varPosition.load(ctx); g.push(1); g.math(GeneratorAdapter.ADD, INT_TYPE); varPosition.store(ctx); g.goTo(labelLoop); g.mark(labelExit); return VOID_TYPE; }
Example #24
Source File: ExpressionArrayGet.java From datakernel with Apache License 2.0 | 5 votes |
@Override public Type load(Context ctx) { GeneratorAdapter g = ctx.getGeneratorAdapter(); Type arrayType = array.load(ctx); Type type = Type.getType(arrayType.getDescriptor().substring(1)); index.load(ctx); g.arrayLoad(type); return type; }
Example #25
Source File: ExpressionFor.java From datakernel with Apache License 2.0 | 5 votes |
@Override public Type load(Context ctx) { GeneratorAdapter g = ctx.getGeneratorAdapter(); Label labelLoop = new Label(); Label labelExit = new Label(); VarLocal to = ctx.newLocal(INT_TYPE); this.to.load(ctx); to.store(ctx); from.load(ctx); VarLocal it = ctx.newLocal(INT_TYPE); it.store(ctx); g.mark(labelLoop); it.load(ctx); to.load(ctx); g.ifCmp(INT_TYPE, GeneratorAdapter.GE, labelExit); Type forType = forVar.apply(it).load(ctx); if (forType.getSize() == 1) g.pop(); if (forType.getSize() == 2) g.pop2(); it.load(ctx); g.push(1); g.math(GeneratorAdapter.ADD, INT_TYPE); it.store(ctx); g.goTo(labelLoop); g.mark(labelExit); return VOID_TYPE; }
Example #26
Source File: IncrementalVisitor.java From AnoleFix with MIT License | 5 votes |
protected static void trace(GeneratorAdapter mv, String s1, String s2) { mv.push(s1); mv.push(s2); mv.invokeStatic(Type.getType(PACKAGE + ".AndroidInstantRuntime"), Method.getMethod("void trace(String, String)")); }
Example #27
Source File: StringSwitch.java From AnoleFix with MIT License | 5 votes |
/** * Generates a standard error exception with message similar to: * * String switch could not find 'equals.(Ljava/lang/Object;)Z' with hashcode 0 * in com/example/basic/GrandChild * * @param mv The generator adaptor used to emit the lookup switch code. * @param visitedClassName The abstract string trie structure. */ void writeMissingMessageWithHash(GeneratorAdapter mv, String visitedClassName) { mv.newInstance(INSTANT_RELOAD_EXCEPTION_TYPE); mv.dup(); mv.push("String switch could not find '%s' with hashcode %s in %s"); mv.push(3); mv.newArray(OBJECT_TYPE); mv.dup(); mv.push(0); visitString(); mv.arrayStore(OBJECT_TYPE); mv.dup(); mv.push(1); visitString(); visitHashMethod(mv); mv.visitMethodInsn( Opcodes.INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;", false); mv.arrayStore(OBJECT_TYPE); mv.dup(); mv.push(2); mv.push(visitedClassName); mv.arrayStore(OBJECT_TYPE); mv.visitMethodInsn( Opcodes.INVOKESTATIC, "java/lang/String", "format", "(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;", false); mv.invokeConstructor(INSTANT_RELOAD_EXCEPTION_TYPE, Method.getMethod("void <init> (String)")); mv.throwException(); }
Example #28
Source File: ConstructorArgsRedirection.java From AnoleFix with MIT License | 5 votes |
@Override protected void createLocals(GeneratorAdapter mv, List<Type> args) { super.createLocals(mv, args); // Override the locals creation to keep a reference to it. We keep a reference to this // array because we use it to receive the values of the local variables after the // redirection is done. locals = mv.newLocal(Type.getType("[Ljava/lang/Object;")); mv.dup(); mv.storeLocal(locals); }
Example #29
Source File: ExpressionLength.java From datakernel with Apache License 2.0 | 5 votes |
@Override public Type load(Context ctx) { GeneratorAdapter g = ctx.getGeneratorAdapter(); Type valueType = value.load(ctx); if (valueType.getSort() == Type.ARRAY) { g.arrayLength(); } else if (valueType.getSort() == Type.OBJECT) { ctx.invoke(valueType, "size"); } return Type.INT_TYPE; }
Example #30
Source File: ByteCodeUtils.java From Stark with Apache License 2.0 | 5 votes |
/** * Pushes an array on the stack that contains the value of all the given variables. */ static void newVariableArray( @NonNull GeneratorAdapter mv, @NonNull List<LocalVariable> variables) { mv.push(variables.size()); mv.newArray(Type.getType(Object.class)); loadVariableArray(mv, variables, 0); }