Java Code Examples for org.objectweb.asm.commons.GeneratorAdapter#mark()

The following examples show how to use org.objectweb.asm.commons.GeneratorAdapter#mark() . 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 vote down vote up
@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: ExpressionIf.java    From datakernel with Apache License 2.0 6 votes vote down vote up
@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 3
Source File: ExpressionIsNotNull.java    From datakernel with Apache License 2.0 6 votes vote down vote up
@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 4
Source File: ExpressionIsNull.java    From datakernel with Apache License 2.0 6 votes vote down vote up
@Override
public Type load(Context ctx) {
	GeneratorAdapter g = ctx.getGeneratorAdapter();

	Label labelNull = new Label();
	Label labelExit = new Label();

	expression.load(ctx);
	g.ifNull(labelNull);
	g.push(false);
	g.goTo(labelExit);

	g.mark(labelNull);
	g.push(true);

	g.mark(labelExit);

	return Type.BOOLEAN_TYPE;
}
 
Example 5
Source File: ExpressionBooleanAnd.java    From datakernel with Apache License 2.0 6 votes vote down vote up
@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 6
Source File: SystemInstrument.java    From dacapobench with Apache License 2.0 6 votes vote down vote up
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 7
Source File: ExpressionBooleanOr.java    From datakernel with Apache License 2.0 6 votes vote down vote up
@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 8
Source File: ClinitInstrument.java    From dacapobench with Apache License 2.0 6 votes vote down vote up
@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 9
Source File: AbstractExpressionMapForEach.java    From datakernel with Apache License 2.0 5 votes vote down vote up
@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 10
Source File: AbstractExpressionIteratorForEach.java    From datakernel with Apache License 2.0 5 votes vote down vote up
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 11
Source File: ExpressionNeg.java    From datakernel with Apache License 2.0 5 votes vote down vote up
@Override
	public Type load(Context ctx) {
		GeneratorAdapter g = ctx.getGeneratorAdapter();
		Type argType = arg.load(ctx);
		int argSort = argType.getSort();

		if (argSort == Type.DOUBLE || argSort == Type.FLOAT || argSort == Type.LONG || argSort == Type.INT) {
			g.math(GeneratorAdapter.NEG, argType);
			return argType;
		}
		if (argSort == Type.BYTE || argSort == Type.SHORT || argSort == Type.CHAR) {
//			g.cast(argType, INT_TYPE);
			g.math(GeneratorAdapter.NEG, INT_TYPE);
			return argType;
		}

		if (argSort == Type.BOOLEAN) {
			Label labelTrue = new Label();
			Label labelExit = new Label();
			g.push(true);
			g.ifCmp(BOOLEAN_TYPE, GeneratorAdapter.EQ, labelTrue);
			g.push(true);
			g.goTo(labelExit);

			g.mark(labelTrue);
			g.push(false);

			g.mark(labelExit);
			return INT_TYPE;
		}

		throw new RuntimeException(format("%s is not primitive. %s",
				ctx.toJavaType(argType),
				exceptionInGeneratedClass(ctx))
		);
	}
 
Example 12
Source File: ExpressionFor.java    From datakernel with Apache License 2.0 5 votes vote down vote up
@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 13
Source File: ExpressionComparator.java    From datakernel with Apache License 2.0 4 votes vote down vote up
@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 14
Source File: ExpressionHash.java    From datakernel with Apache License 2.0 4 votes vote down vote up
@Override
public Type load(Context ctx) {
	GeneratorAdapter g = ctx.getGeneratorAdapter();

	int resultVar = g.newLocal(INT_TYPE);

	boolean firstIteration = true;

	for (Expression argument : arguments) {
		if (firstIteration) {
			g.push(0);
			firstIteration = false;
		} else {
			g.push(31);
			g.loadLocal(resultVar);
			g.math(IMUL, INT_TYPE);
		}

		Type fieldType = argument.load(ctx);

		if (isPrimitiveType(fieldType)) {
			if (fieldType.getSort() == Type.LONG) {
				g.dup2();
				g.push(32);
				g.visitInsn(LUSHR);
				g.visitInsn(LXOR);
				g.visitInsn(L2I);
			}
			if (fieldType.getSort() == Type.FLOAT) {
				g.invokeStatic(getType(Float.class), getMethod("int floatToRawIntBits (float)"));
			}
			if (fieldType.getSort() == Type.DOUBLE) {
				g.invokeStatic(getType(Double.class), getMethod("long doubleToRawLongBits (double)"));
				g.dup2();
				g.push(32);
				g.visitInsn(LUSHR);
				g.visitInsn(LXOR);
				g.visitInsn(L2I);
			}
			g.visitInsn(IADD);
		} else {
			int tmpVar = g.newLocal(fieldType);
			g.storeLocal(tmpVar);
			g.loadLocal(tmpVar);
			Label ifNullLabel = g.newLabel();
			g.ifNull(ifNullLabel);
			g.loadLocal(tmpVar);
			g.invokeVirtual(fieldType, getMethod("int hashCode()"));
			g.visitInsn(IADD);
			g.mark(ifNullLabel);
		}

		g.storeLocal(resultVar);
	}

	if (firstIteration) {
		g.push(0);
	} else {
		g.loadLocal(resultVar);
	}

	return INT_TYPE;
}
 
Example 15
Source File: ExpressionToString.java    From datakernel with Apache License 2.0 4 votes vote down vote up
@Override
public Type load(Context ctx) {
	GeneratorAdapter g = ctx.getGeneratorAdapter();

	g.newInstance(getType(StringBuilder.class));
	g.dup();
	g.invokeConstructor(getType(StringBuilder.class), getMethod("void <init> ()"));

	boolean first = true;

	for (Object key : arguments.keySet()) {
		String str = first ? begin : separator;
		first = false;
		if (key instanceof String) {
			str += key;
		}
		if (!str.isEmpty()) {
			g.dup();
			g.push(str);
			g.invokeVirtual(getType(StringBuilder.class), getMethod("StringBuilder append(String)"));
			g.pop();
		}

		g.dup();
		Expression expression = arguments.get(key);
		Type type = expression.load(ctx);
		if (isPrimitiveType(type)) {
			g.invokeStatic(wrap(type), new Method("toString", getType(String.class), new Type[]{type}));
		} else {
			Label nullLabel = new Label();
			Label afterToString = new Label();
			g.dup();
			g.ifNull(nullLabel);
			g.invokeVirtual(type, getMethod("String toString()"));
			g.goTo(afterToString);
			g.mark(nullLabel);
			g.pop();
			g.push("null");
			g.mark(afterToString);
		}
		g.invokeVirtual(getType(StringBuilder.class), getMethod("StringBuilder append(String)"));
		g.pop();
	}

	if (!end.isEmpty()) {
		g.dup();
		g.push(end);
		g.invokeVirtual(getType(StringBuilder.class), getMethod("StringBuilder append(String)"));
		g.pop();
	}

	g.invokeVirtual(getType(StringBuilder.class), getMethod("String toString()"));
	return getType(String.class);
}
 
Example 16
Source File: AbstractExpressionIteratorForEach.java    From datakernel with Apache License 2.0 4 votes vote down vote up
@Override
public final Type load(Context ctx) {
	GeneratorAdapter g = ctx.getGeneratorAdapter();
	Label labelLoop = new Label();
	Label labelExit = new Label();

	Type collectionType = collection.load(ctx);
	if (collectionType.getSort() == ARRAY) {
		return arrayForEach(ctx, g, labelLoop, labelExit);
	}

	VarLocal varIter = ctx.newLocal(getType(Iterator.class));

	Class<?> t = ctx.toJavaType(collectionType);
	if (t.isInstance(Iterator.class) || t == Iterator.class) {
		// do nothing
	} else {
		ctx.invoke(collectionType, "iterator");
	}
	varIter.store(ctx);

	g.mark(labelLoop);

	ctx.invoke(varIter, "hasNext");
	g.push(false);
	g.ifCmp(BOOLEAN_TYPE, GeneratorAdapter.EQ, labelExit);

	ctx.cast(ctx.invoke(varIter, "next"), getType(type));
	VarLocal it = ctx.newLocal(getType(type));
	it.store(ctx);

	Type forEachType = forEach.apply(getValue(it)).load(ctx);
	if (forEachType.getSize() == 1)
		g.pop();
	if (forEachType.getSize() == 2)
		g.pop2();

	g.goTo(labelLoop);
	g.mark(labelExit);
	return VOID_TYPE;

}
 
Example 17
Source File: ExpressionCmp.java    From datakernel with Apache License 2.0 4 votes vote down vote up
@Override
public Type load(Context ctx) {
	GeneratorAdapter g = ctx.getGeneratorAdapter();
	Label labelTrue = new Label();
	Label labelExit = new Label();

	Type leftType = left.load(ctx);
	Type rightType = right.load(ctx);
	checkArgument(Objects.equals(leftType, rightType), "Types of compared values should match");

	if (isPrimitiveType(leftType)) {
		g.ifCmp(leftType, operation.opCode, labelTrue);
	} else {
		if (operation == EQ || operation == NE) {
			g.invokeVirtual(leftType, new Method("equals", BOOLEAN_TYPE, new Type[]{Type.getType(Object.class)}));
			g.push(operation == EQ);
			g.ifCmp(BOOLEAN_TYPE, GeneratorAdapter.EQ, labelTrue);
		} else {
			g.invokeVirtual(leftType, new Method("compareTo", INT_TYPE, new Type[]{Type.getType(Object.class)}));
			if (operation == LT) {
				g.ifZCmp(GeneratorAdapter.LT, labelTrue);
			} else if (operation == GT) {
				g.ifZCmp(GeneratorAdapter.GT, labelTrue);
			} else if (operation == LE) {
				g.ifZCmp(GeneratorAdapter.LE, labelTrue);
			} else if (operation == GE) {
				g.ifZCmp(GeneratorAdapter.GE, labelTrue);
			}
		}
	}

	g.push(false);
	g.goTo(labelExit);

	g.mark(labelTrue);
	g.push(true);

	g.mark(labelExit);

	return BOOLEAN_TYPE;
}
 
Example 18
Source File: AsmDeltaSpikeProxyClassGenerator.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
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();
}