org.objectweb.asm.commons.Method Java Examples

The following examples show how to use org.objectweb.asm.commons.Method. 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: Context.java    From datakernel with Apache License 2.0 6 votes vote down vote up
public Type invokeStatic(Type ownerType, String methodName, Type... argumentTypes) {
	Class<?>[] arguments = Stream.of(argumentTypes).map(this::toJavaType).toArray(Class[]::new);
	Method foundMethod;
	if (ownerType.equals(getSelfType())) {
		foundMethod = findMethod(
				getStaticMethods().keySet().stream(),
				methodName,
				arguments);
	} else {
		foundMethod = findMethod(
				Arrays.stream(toJavaType(ownerType).getMethods())
						.filter(m -> isStatic(m.getModifiers()))
						.map(Method::getMethod),
				methodName,
				arguments);
	}
	g.invokeStatic(ownerType, foundMethod);
	return foundMethod.getReturnType();
}
 
Example #3
Source File: PatchVisitor.java    From Stark with Apache License 2.0 6 votes vote down vote up
/**
 * For calls to constructors in the same package, calls are rewritten to use reflection
 * to create the instance (see above, the NEW and DUP instructions are also removed) using
 * the following pseudo code.
 * <p>
 * before:
 * <code>
 *   $value = new $type(arg1, arg2);
 * </code>
 * after:
 * <code>
 *   $value = ($type)$package/AndroidInstantRuntime.newForClass(new Object[] {arg1, arg2 },
 *       new Class[]{ String.class, Integer.class }, $type.class);
 * </code>
 *
 */
private boolean handleConstructor(String owner, String name, String desc) {

    if (isInSamePackage(owner)) {

        Type expectedType = Type.getType("L" + owner + ";");
        pushMethodRedirectArgumentsOnStack(name, desc);

        // pop the name, we don't need it.
        pop();
        visitLdcInsn(expectedType);

        invokeStatic(RUNTIME_TYPE, Method.getMethod(
                "Object newForClass(Object[], Class[], Class)"));

        checkCast(expectedType);
        ByteCodeUtils.unbox(this, expectedType);
        return true;
    } else {
        return false;
    }
}
 
Example #4
Source File: StackWatcherMethodAdapter.java    From AVM with MIT License 6 votes vote down vote up
@Override
public void visitCode(){
    super.visitCode();

    // Push the current stack size to operand stack and invoke AVMStackWatcher.enterMethod(int)
    Method m1 = Method.getMethod("void enterMethod(int)");
    visitLdcInsn(this.maxLocals + this.maxStack);
    invokeStatic(typeHelper, m1);

    // If current method has at least one try catch block, we need to generate a StackWacher stamp.
    if (this.tryCatchBlockCount > 0){
        //invoke AVMStackWatcher.getCurStackDepth() and put the result into local variable
        Method m2 = Method.getMethod("int getCurStackDepth()");
        invokeStatic(typeHelper, m2);
        this.stackDepthLocalVariableIndex = newLocal(typeInt);
        storeLocal(this.stackDepthLocalVariableIndex, typeInt);

        //invoke AVMStackWatcher.getCurStackSize() and put the result into local variable
        Method m3 = Method.getMethod("int getCurStackSize()");
        invokeStatic(typeHelper, m3);
        this.stackSizeLocalVariableIndex = newLocal(typeInt);
        storeLocal(this.stackSizeLocalVariableIndex, typeInt);
    }
}
 
Example #5
Source File: StringSwitch.java    From Stark with Apache License 2.0 6 votes vote down vote up
/**
 * 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 #6
Source File: AsmDeltaSpikeProxyClassGenerator.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
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 #7
Source File: ServletCallBackAdapter.java    From javametrics with Apache License 2.0 6 votes vote down vote up
/**
 * Inject a callback to our servlet handler.
 * 
 * @param method
 */
private void injectServletCallback(String method) {
    Label tryStart = new Label();
    Label tryEnd = new Label();
    Label catchStart = new Label();
    Label catchEnd = new Label();

    visitTryCatchBlock(tryStart, tryEnd, catchStart, "java/lang/NoClassDefFoundError");
    mark(tryStart); // try {
    loadArgs();
    invokeStatic(Type.getType(SERVLET_CALLBACK_TYPE), Method.getMethod(method));
    mark(tryEnd); // }
    visitJumpInsn(GOTO, catchEnd);
    mark(catchStart); // catch() {
    pop();
    mark(catchEnd); // }
}
 
Example #8
Source File: AdviceBuilder.java    From glowroot with Apache License 2.0 6 votes vote down vote up
private void initOnThrowAdvice(PointcutClass adviceClass, PointcutMethod adviceMethod)
        throws AdviceConstructionException {
    checkState(!hasOnThrowAdvice, "@Pointcut '" + adviceClass.type().getClassName()
            + "' has more than one @OnThrow method");
    Method asmMethod = adviceMethod.toAsmMethod();
    List<AdviceParameter> parameters =
            getAdviceParameters(adviceMethod.parameterAnnotationTypes(),
                    asmMethod.getArgumentTypes(), onThrowBindAnnotationTypes, OnThrowType);
    for (int i = 1; i < parameters.size(); i++) {
        checkState(parameters.get(i).kind() != ParameterKind.THROWABLE,
                "@BindThrowable must be the first argument to @OnThrow");
    }
    checkState(asmMethod.getReturnType().getSort() == Type.VOID,
            "@OnThrow method must return void (for now)");
    builder.onThrowAdvice(asmMethod);
    builder.addAllOnThrowParameters(parameters);
    checkForBindThreadContext(parameters);
    checkForBindOptionalThreadContext(parameters);
    hasOnThrowAdvice = true;
}
 
Example #9
Source File: AsmDeltaSpikeProxyClassGenerator.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
/**
 * Defines a new Object[] and push all method argmuments into the array.
 *
 * @param mg
 * @param method
 * @param methodType
 */
private static void loadArguments(GeneratorAdapter mg, java.lang.reflect.Method method, Type methodType)
{
    // create the Object[]
    mg.push(methodType.getArgumentTypes().length);
    mg.newArray(TYPE_OBJECT);

    // 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.loadArg(i);
        mg.valueOf(methodType.getArgumentTypes()[i]);
        mg.arrayStore(TYPE_OBJECT);
    }
}
 
Example #10
Source File: CommonMethodsBuilder.java    From OpenPeripheral with MIT License 6 votes vote down vote up
public void addExposedMethodBypass(Method method, Type sourceInterface) {
	MethodVisitor mv = writer.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, method.getName(), method.getDescriptor(), null, null);

	mv.visitCode();

	mv.visitVarInsn(Opcodes.ALOAD, 0);
	mv.visitFieldInsn(Opcodes.GETFIELD, clsName, TARGET_FIELD_NAME, targetType.getDescriptor());

	Type[] args = method.getArgumentTypes();
	for (int i = 0; i < args.length; i++)
		mv.visitVarInsn(args[i].getOpcode(Opcodes.ILOAD), i + 1);

	mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, sourceInterface.getInternalName(), method.getName(), method.getDescriptor(), true);
	Type returnType = method.getReturnType();
	mv.visitInsn(returnType.getOpcode(Opcodes.IRETURN));

	mv.visitMaxs(0, 0);
	mv.visitEnd();
}
 
Example #11
Source File: Utils.java    From datakernel with Apache License 2.0 6 votes vote down vote up
public static Method toPrimitive(Type type) {
	if (type.getSort() == BOOLEAN || type.equals(WRAPPED_BOOLEAN_TYPE))
		return getMethod("boolean booleanValue()");
	if (type.getSort() == CHAR || type.equals(WRAPPED_CHAR_TYPE))
		return getMethod("char charValue()");
	if (type.getSort() == BYTE || type.equals(WRAPPED_BYTE_TYPE))
		return getMethod("byte byteValue()");
	if (type.getSort() == SHORT || type.equals(WRAPPED_SHORT_TYPE))
		return getMethod("short shortValue()");
	if (type.getSort() == INT || type.equals(WRAPPED_INT_TYPE))
		return getMethod("int intValue()");
	if (type.getSort() == FLOAT || type.equals(WRAPPED_FLOAT_TYPE))
		return getMethod("float floatValue()");
	if (type.getSort() == LONG || type.equals(WRAPPED_LONG_TYPE))
		return getMethod("long longValue()");
	if (type.getSort() == DOUBLE || type.equals(WRAPPED_DOUBLE_TYPE))
		return getMethod("double doubleValue()");

	throw new IllegalArgumentException(format("No primitive value method for %s ", type.getClassName()));
}
 
Example #12
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 #13
Source File: AdviceBuilder.java    From glowroot with Apache License 2.0 6 votes vote down vote up
private void initOnBeforeAdvice(PointcutClass adviceClass, PointcutMethod adviceMethod)
        throws AdviceConstructionException {
    checkState(!hasOnBeforeAdvice, "@Pointcut '" + adviceClass.type().getClassName()
            + "' has more than one @OnBefore method");
    Method asmMethod = adviceMethod.toAsmMethod();
    builder.onBeforeAdvice(asmMethod);
    List<AdviceParameter> parameters =
            getAdviceParameters(adviceMethod.parameterAnnotationTypes(),
                    asmMethod.getArgumentTypes(), onBeforeBindAnnotationTypes, OnBeforeType);
    builder.addAllOnBeforeParameters(parameters);
    if (asmMethod.getReturnType().getSort() != Type.VOID) {
        builder.travelerType(asmMethod.getReturnType());
    }
    checkForBindThreadContext(parameters);
    checkForBindOptionalThreadContext(parameters);
    hasOnBeforeAdvice = true;
}
 
Example #14
Source File: IntSwitch.java    From Aceso with Apache License 2.0 6 votes vote down vote up
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 #15
Source File: IncrementalSupportVisitor.java    From Aceso with Apache License 2.0 6 votes vote down vote up
@Override
public void visitCode() {
    if (!disableRedirection) {
        // Labels cannot be used directly as they are volatile between different visits,
        // so we must use LabelNode and resolve before visiting for better performance.
        for (Redirection redirection : redirections) {
            resolvedRedirections.put(redirection.getPosition().getLabel(), redirection);
        }

        super.visitLabel(start);
        change = newLocal(MTD_MAP_TYPE);

        push(new Integer(AcesoProguardMap.instance().getClassIndex(visitedClassName)));
        push(new Integer(AcesoProguardMap.instance().getMtdIndex(visitedClassName, IncrementalTool.getMtdSig(name, desc))));
        invokeStatic(IncrementalVisitor.MTD_MAP_TYPE, Method.getMethod("com.android.tools.fd.runtime.IncrementalChange get(int,int)"));

        storeLocal(change);

        redirectAt(start);
    }
    super.visitCode();
}
 
Example #16
Source File: WeavingClassVisitor.java    From glowroot with Apache License 2.0 6 votes vote down vote up
@RequiresNonNull("type")
private void addShim(ShimType shimType) {
    for (java.lang.reflect.Method reflectMethod : shimType.shimMethods()) {
        Method method = Method.getMethod(reflectMethod);
        Shim shim = reflectMethod.getAnnotation(Shim.class);
        checkNotNull(shim);
        if (shim.value().length != 1) {
            throw new IllegalStateException(
                    "@Shim annotation must have exactly one value when used on methods");
        }
        Method targetMethod = Method.getMethod(shim.value()[0]);
        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, method.getName(), method.getDescriptor(),
                null, null);
        mv.visitCode();
        int i = 0;
        mv.visitVarInsn(ALOAD, i++);
        for (Type argumentType : method.getArgumentTypes()) {
            mv.visitVarInsn(argumentType.getOpcode(ILOAD), i++);
        }
        mv.visitMethodInsn(INVOKEVIRTUAL, type.getInternalName(), targetMethod.getName(),
                targetMethod.getDescriptor(), false);
        mv.visitInsn(method.getReturnType().getOpcode(IRETURN));
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
}
 
Example #17
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 #18
Source File: StarkMethodVerifier.java    From Stark with Apache License 2.0 6 votes vote down vote up
@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc,
        boolean itf) {

    Type receiver = Type.getObjectType(owner);
    if (!incompatibleChange.isPresent()) {
        if (opcode == Opcodes.INVOKEVIRTUAL && blackListedMethods.containsKey(receiver)) {
            for (Method method : blackListedMethods.get(receiver)) {
                if (method.getName().equals(name) && method.getDescriptor().equals(desc)) {
                    incompatibleChange = Optional.of(StarkVerifierStatus.REFLECTION_USED);
                }
            }
        }
    }

    super.visitMethodInsn(opcode, owner, name, desc, itf);
}
 
Example #19
Source File: AsmDeltaSpikeProxyClassGenerator.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
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 #20
Source File: Context.java    From datakernel with Apache License 2.0 6 votes vote down vote up
public Type invoke(Type ownerType, String methodName, Type... argumentTypes) {
	Class<?>[] arguments = Stream.of(argumentTypes).map(this::toJavaType).toArray(Class[]::new);
	Method foundMethod;
	if (ownerType.equals(getSelfType())) {
		foundMethod = findMethod(
				getMethods().keySet().stream(),
				methodName,
				arguments);
		g.invokeVirtual(ownerType, foundMethod);
	} else {
		Class<?> javaOwnerType = toJavaType(ownerType);
		foundMethod = findMethod(
				Arrays.stream(javaOwnerType.getMethods())
						.filter(m -> !isStatic(m.getModifiers()))
						.map(Method::getMethod),
				methodName,
				arguments);
		if (javaOwnerType.isInterface()) {
			g.invokeInterface(ownerType, foundMethod);
		} else {
			g.invokeVirtual(ownerType, foundMethod);
		}
	}
	return foundMethod.getReturnType();
}
 
Example #21
Source File: AdviceBuilder.java    From glowroot with Apache License 2.0 6 votes vote down vote up
private void initOnReturnAdvice(PointcutClass adviceClass, PointcutMethod adviceMethod)
        throws AdviceConstructionException {
    checkState(!hasOnReturnAdvice, "@Pointcut '" + adviceClass.type().getClassName()
            + "' has more than one @OnReturn method");
    Method asmMethod = adviceMethod.toAsmMethod();
    List<AdviceParameter> parameters =
            getAdviceParameters(adviceMethod.parameterAnnotationTypes(),
                    asmMethod.getArgumentTypes(), onReturnBindAnnotationTypes, OnReturnType);
    for (int i = 1; i < parameters.size(); i++) {
        checkState(parameters.get(i).kind() != ParameterKind.RETURN,
                "@BindReturn must be the first argument to @OnReturn");
        checkState(parameters.get(i).kind() != ParameterKind.OPTIONAL_RETURN,
                "@BindOptionalReturn must be the first argument to @OnReturn");
    }
    builder.onReturnAdvice(asmMethod);
    builder.addAllOnReturnParameters(parameters);
    checkForBindThreadContext(parameters);
    checkForBindOptionalThreadContext(parameters);
    hasOnReturnAdvice = true;
}
 
Example #22
Source File: StringSwitch.java    From AnoleFix with MIT License 6 votes vote down vote up
/**
 * 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 #23
Source File: AsmDeltaSpikeProxyClassGenerator.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
@Override
public <T> Class<T> generateProxyClass(ClassLoader classLoader,
        Class<T> targetClass,
        String suffix,
        String superAccessorMethodSuffix,
        Class<?>[] additionalInterfaces,
        java.lang.reflect.Method[] delegateMethods,
        java.lang.reflect.Method[] interceptMethods)
{
    String proxyName = targetClass.getName() + suffix;
    String classFileName = proxyName.replace('.', '/');

    byte[] proxyBytes = generateProxyClassBytes(targetClass,
            classFileName, superAccessorMethodSuffix, additionalInterfaces, delegateMethods, interceptMethods);

    Class<T> proxyClass = (Class<T>) loadClass(classLoader, proxyName, proxyBytes,
            targetClass.getProtectionDomain());

    return proxyClass;
}
 
Example #24
Source File: Context.java    From datakernel with Apache License 2.0 6 votes vote down vote up
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 #25
Source File: ObjectCodeGenerator.java    From OpenPeripheral with MIT License 5 votes vote down vote up
@Override
public byte[] generate(String clsName, Class<?> targetClass, Set<Class<?>> exposedInterfaces, IndexedMethodMap methods, int methodsId) {
	ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);

	writer.visit(Opcodes.V1_6,
			Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_SUPER,
			clsName, null, BASE_TYPE.getInternalName(), Utils.getInterfaces(exposedInterfaces));

	Type targetType = Type.getType(targetClass);

	CommonMethodsBuilder builder = new CommonMethodsBuilder(writer, clsName, targetType);

	builder.addTargetField();
	builder.addMethodsField();

	builder.addClassInit(methodsId);
	createConstructors(writer, clsName, targetType);

	final Map<Method, Type> exposedMethods = Utils.getExposedMethods(exposedInterfaces);
	for (Map.Entry<Method, Type> e : exposedMethods.entrySet())
		builder.addExposedMethodBypass(e.getKey(), e.getValue());

	for (int i = 0; i < methods.size(); i++) {
		String name = methods.getMethodName(i);
		IMethodExecutor executor = methods.getMethod(i);
		builder.createScriptMethodWrapper(name, i, executor);
	}

	writer.visitEnd();

	return writer.toByteArray();
}
 
Example #26
Source File: IncrementalChangeVisitor.java    From AnoleFix with MIT License 5 votes vote down vote up
/**
 * Rewrites INVOKEVIRTUAL method calls.
 * <p/>
 * Virtual calls to protected methods are rewritten according to the following pseudo code:
 * before:
 * <code>
 * $value = $instance.protectedVirtual(arg1, arg2);
 * </code>
 * after:
 * <code>
 * $value = (unbox)$package/AndroidInstantRuntime.invokeProtectedMethod($instance,
 * new object[] {arg1, arg2}, new Class[] { String.class, Integer.class },
 * "protectedVirtual");
 * </code>
 */
private boolean handleVirtualOpcode(String owner, String name, String desc, boolean itf) {

    if (DEBUG) {
        System.out.println(
                "Virtual Method : " + name + ":" + desc + ":" + itf + ":" + isStatic);

    }
    AccessRight accessRight = getMethodAccessRight(owner, name, desc);
    if (accessRight == AccessRight.PUBLIC) {
        return false;
    }

    // for anything else, private, protected and package private, we must go through
    // reflection.
    // Stack : <receiver>
    //      <param_1>
    //      <param_2>
    //      ...
    //      <param_n>
    pushMethodRedirectArgumentsOnStack(name, desc);

    // Stack : <receiver>
    //      <array of parameter_values>
    //      <array of parameter_types>
    //      <method_name>
    invokeStatic(RUNTIME_TYPE, Method.getMethod(
            "Object invokeProtectedMethod(Object, Object[], Class[], String)"));
    // Stack : <return value or null if no return value>
    handleReturnType(desc);
    return true;
}
 
Example #27
Source File: WeavingClassVisitor.java    From glowroot with Apache License 2.0 5 votes vote down vote up
@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 #28
Source File: IncrementalVisitor.java    From AnoleFix with MIT License 5 votes vote down vote up
protected static void trace(GeneratorAdapter mv, String s1,
                            String s2, String s3) {
    mv.push(s1);
    mv.push(s2);
    mv.push(s3);
    mv.invokeStatic(Type.getType(PACKAGE + ".AndroidInstantRuntime"),
            Method.getMethod("void trace(String, String, String)"));
}
 
Example #29
Source File: ForbiddenViolation.java    From forbidden-apis with Apache License 2.0 5 votes vote down vote up
ForbiddenViolation(int groupId, Method targetMethod, String description, String locationInfo, int lineNo) {
  this.groupId = groupId;
  this.targetMethod = targetMethod;
  this.description = description;
  this.locationInfo = locationInfo;
  this.lineNo = lineNo;
}
 
Example #30
Source File: IncrementalVisitor.java    From AnoleFix with MIT License 5 votes vote down vote up
protected static void trace(GeneratorAdapter mv, String s1,
                            String s2, String s3, String s4) {
    mv.push(s1);
    mv.push(s2);
    mv.push(s3);
    mv.push(s4);
    mv.invokeStatic(Type.getType(PACKAGE + ".AndroidInstantRuntime"),
            Method.getMethod("void trace(String, String, String, String)"));
}