boolean isStatic = targetMethod.getOpcode() == Opcodes.INVOKESTATIC; instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getType(RobolectricInternals.class).getInternalName(), "intercept", "(Ljava/lang/String;Ljava/lang/Object;[Ljava/lang/Object;Ljava/lang/Class;)Ljava/lang/Object;", false)); break; case Type.LONG: instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, Type.getInternalName(Long.class))); instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Long.class), "longValue", Type.getMethodDescriptor(Type.LONG_TYPE), false)); break; instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Float.class), "floatValue", Type.getMethodDescriptor(Type.FLOAT_TYPE), false)); break; instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Double.class), "doubleValue", Type.getMethodDescriptor(Type.DOUBLE_TYPE), false)); break; instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Boolean.class), "booleanValue", Type.getMethodDescriptor(Type.BOOLEAN_TYPE), false)); break; instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Integer.class), "intValue", Type.getMethodDescriptor(Type.INT_TYPE), false)); break; instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Short.class), "shortValue", Type.getMethodDescriptor(Type.SHORT_TYPE), false)); break; instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Byte.class), "byteValue", Type.getMethodDescriptor(Type.BYTE_TYPE), false));
List<Type> args = Arrays.asList(Type.getArgumentTypes(method.desc)); AtomicInteger count = new AtomicInteger(IntStream.range(0, args.size()) .filter(i -> !args.get(i).getClassName().equals(String.class.getName())) .findFirst() .orElse(0)); && handle.getTag() != Opcodes.H_INVOKESTATIC)) { start = new Insn<>(null, method.getPrevious()) .prev() .filter(Filters.is(MethodInsnNode.class))
|| getOrCreateKotlinClass().test(node)) { String owner = Type.getReturnType(node.desc).getClassName(); AbstractInsnNode prev = node.getPrevious(); if (prev instanceof FieldInsnNode && ((MethodInsnNode) n).name.equals("toEnum")) { toType = loadType(loader, ((Type) cst).getClassName()); AbstractInsnNode next = node.getNext(); if (next instanceof MethodInsnNode) { String joobyKt = ((MethodInsnNode) next).owner; } else if (n instanceof TypeInsnNode) { TypeInsnNode typeInsn = (TypeInsnNode) n; if (typeInsn.getOpcode() == Opcodes.CHECKCAST) { return loadType(loader, typeInsn.desc);
mi = new MethodInsnNode(INVOKESTATIC, cw.getName(), sw.getShimMethodName(), sw.getShimDesc(), cw.isInterface()); mi.accept(mv);
@SuppressWarnings({"unchecked"}) private void createNewInstanceMethod(ParserClassNode classNode) { MethodNode method = new MethodNode(ACC_PUBLIC, "newInstance", "()L" + Types.BASE_PARSER.getInternalName() + ';', null, null); InsnList instructions = method.instructions; instructions.add(new TypeInsnNode(NEW, classNode.name)); instructions.add(new InsnNode(DUP)); instructions.add(new MethodInsnNode(INVOKESPECIAL, classNode.name, "<init>", "()V", classNode.isInterface())); instructions.add(new InsnNode(ARETURN)); classNode.methods.add(method); }
if (!(methodNode.getPrevious().getOpcode() == Opcodes.ICONST_1 || methodNode.getPrevious().getOpcode() == Opcodes.ICONST_0)) { MethodInsnNode booleanHelperInvoke = new MethodInsnNode( || types[types.length - 1 - i] == Type.DOUBLE_TYPE) { if (types[types.length - 1 - i] == Type.BOOLEAN_TYPE) { booleanHelperPushParameter = new MethodInsnNode( new Type[] { Type.INT_TYPE })); } else { booleanHelperPushParameter = new MethodInsnNode( booleanHelperPushParameter = new MethodInsnNode( MethodInsnNode booleanHelperInvoke = new MethodInsnNode( boolean objectNeedCast = false; if (types[i] == Type.BOOLEAN_TYPE) { booleanHelperPopParameter = new MethodInsnNode( new Type[] {})); } else if (types[i] == Type.CHAR_TYPE) { booleanHelperPopParameter = new MethodInsnNode( new Type[] {})); } else if (types[i] == Type.BYTE_TYPE) { booleanHelperPopParameter = new MethodInsnNode( new Type[] {}));
void writeSysCall(String internalClassName, int INVOKE_TYPE, String method, Class returnClazz, Class... paramClasses) { Type[] args = new Type[paramClasses.length]; if (INVOKE_TYPE == INVOKEVIRTUAL || INVOKE_TYPE == INVOKEINTERFACE) stackPop(); // this for (int i = 0; i < args.length; i++) { args[i] = Type.getType(paramClasses[i]); stackPop(); } code.add(new MethodInsnNode( INVOKE_TYPE, internalClassName, method, Type.getMethodDescriptor(Type.getType(returnClazz), args), false )); if (returnClazz != void.class) { stackPush(null, StackItem.Type.valueOf(returnClazz)); } }
@SuppressWarnings("unchecked") private void transformWrapperCalls(MethodNode mn) { Iterator<AbstractInsnNode> iterator = mn.instructions.iterator(); List<Class<?>> wrapperClasses = getWrapperClasses(); for(Class<?> wrapperClass : wrapperClasses) { if(wrapperClass.getName().equals(ownerName)) { if(methodInsnNode.getOpcode() == Opcodes.INVOKESTATIC) { logger.debug("Replacing call "+methodInsnNode.name); methodInsnNode.owner = PackageInfo.getEvoSuitePackageWithSlash()+"/testcarver/wrapper/" + methodInsnNode.owner; Type[] parameterTypes = Type.getArgumentTypes(methodInsnNode.desc); try { Class<?>[] parameterClasses = new Class<?>[parameterTypes.length]; int pos = 0; for(Type parameter : parameterTypes) { switch(parameter.getSort()) { case Type.OBJECT: parameterClasses[pos++] = Class.forName(parameter.getClassName()); break; case Type.BOOLEAN: if(methodInsnNode.getOpcode() != Opcodes.INVOKESTATIC) { methodInsnNode.setOpcode(Opcodes.INVOKESTATIC); Type[] args = Type.getArgumentTypes(methodInsnNode.desc); Type returnType = Type.getReturnType(methodInsnNode.desc);
private static InsnList throwExceptionsList(MethodNode methodNode, ClassNode classNode) { try { String runtimeException = Type.getInternalName(RuntimeException.class); Constructor<RuntimeException> constructor = RuntimeException.class.getConstructor(String.class); InsnList instructions = new InsnList(); instructions.add( new TypeInsnNode(Opcodes.NEW, runtimeException)); instructions.add(new InsnNode(Opcodes.DUP)); String className = classNode.name.replace('/', '.'); instructions.add(new LdcInsnNode("Method " + methodNode.name + " in " + className + " not mocked. " + "See http://g.co/androidstudio/not-mocked for details.")); instructions.add(new MethodInsnNode( Opcodes.INVOKESPECIAL, runtimeException, CONSTRUCTOR, Type.getType(constructor).getDescriptor(), false)); instructions.add(new InsnNode(Opcodes.ATHROW)); return instructions; } catch (NoSuchMethodException e) { throw new RuntimeException(e); } } }
private static InsnList generateSliceInstructions(final MethodInsnNode methodInsn) { final InsnList list = new InsnList(); // stack: this list.add(new TypeInsnNode(NEW, methodInsn.owner)); // stack: new, this list.add(new InsnNode(DUP)); // stack: new, new, this list.add(new MethodInsnNode(INVOKESPECIAL, methodInsn.owner, "<init>", "()V")); // stack: new, this list.add(new MethodInsnNode(INVOKESTATIC, MAPPED_HELPER_JVM, "slice", "(L" + MAPPED_OBJECT_JVM + ";L" + MAPPED_OBJECT_JVM + ";)L" + MAPPED_OBJECT_JVM + ";")); // stack: new return list; }
private static InsnList newByteString(ByteString value) { InsnList il = new InsnList(); il.add(new LdcInsnNode(value.toRawString())); il.add(new MethodInsnNode(INVOKESTATIC, Type.getInternalName(ByteString.class), "fromRaw", Type.getMethodDescriptor( Type.getType(ByteString.class), Type.getType(String.class)), false)); return il; }
/** * Insert a call to the reference equality check helper function * * @param opcode * @param position * @param list */ public void insertPushEquals(int opcode, JumpInsnNode position, InsnList list) { MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "isEqual", Type.getMethodDescriptor(Type.INT_TYPE, new Type[] { Type.getType(Object.class), Type.getType(Object.class), Type.INT_TYPE }), false); list.insertBefore(position, new InsnNode(Opcodes.DUP2)); list.insertBefore(position, new LdcInsnNode(opcode)); list.insertBefore(position, equalCheck); //list.insertBefore(position, // new LdcInsnNode(getBranchID(currentMethodNode, position))); insertBranchIdPlaceholder(currentMethodNode, position); MethodInsnNode push = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "pushPredicate", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.INT_TYPE, Type.INT_TYPE }), false); list.insertBefore(position, push); }
static void box(final Type type, ListIterator<AbstractInsnNode> instructions) { if (type.getSort() == OBJECT || type.getSort() == ARRAY) { return; } if (Type.VOID_TYPE.equals(type)) { instructions.add(new InsnNode(Opcodes.ACONST_NULL)); } else { Type boxed = getBoxedType(type); instructions.add(new TypeInsnNode(Opcodes.NEW, boxed.getInternalName())); if (type.getSize() == 2) { // Pp -> Ppo -> oPpo -> ooPpo -> ooPp -> o instructions.add(new InsnNode(Opcodes.DUP_X2)); instructions.add(new InsnNode(Opcodes.DUP_X2)); instructions.add(new InsnNode(Opcodes.POP)); } else { // p -> po -> opo -> oop -> o instructions.add(new InsnNode(Opcodes.DUP_X1)); instructions.add(new InsnNode(Opcodes.SWAP)); } instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, boxed.getInternalName(), "<init>", "(" + type.getDescriptor() + ")V", false)); } }
private void insertDoubleComparisonL(AbstractInsnNode position, InsnList list) { MethodInsnNode get = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "doubleSubL", Type.getMethodDescriptor(Type.INT_TYPE, new Type[] { Type.DOUBLE_TYPE, Type.DOUBLE_TYPE }), false); list.insert(position, get); list.remove(position); }
protected InsnList resumptionHandler(LabelNode label) { InsnList il = new InsnList(); il.add(label); il.add(ASMUtils.frameSame1(UnresolvedControlThrowable.class)); il.add(createSnapshot()); // register snapshot with the control exception il.add(new MethodInsnNode( INVOKEVIRTUAL, Type.getInternalName(UnresolvedControlThrowable.class), "resolve", Type.getMethodType( Type.getType(ResolvedControlThrowable.class), Type.getType(Resumable.class), Type.getType(Object.class)).getDescriptor(), false)); // rethrow il.add(new InsnNode(ATHROW)); return il; }
InsnList extraInstructions = new InsnList(); for (FieldNode fieldNode : (List<FieldNode>) classNode.fields) { FieldMetadata fieldMetadata = classMetadata.getFieldMetadata(fieldNode.name); extraInstructions.add(new VarInsnNode(ALOAD, 0)); String referenceName = Type.getType(referenceDesc).getInternalName(); extraInstructions.add(new TypeInsnNode(NEW, referenceName)); extraInstructions.add(new InsnNode(DUP)); extraInstructions.add( new MethodInsnNode(INVOKESPECIAL, referenceName, "<init>", "()V")); extraInstructions.add(new FieldInsnNode(
if (an.desc.equals(PAR_NEST.getDescriptor())) { execMethod.maxLocals++; InsnList preamble = new InsnList(); preamble.add(new TypeInsnNode(NEW, ARRAY_LIST.getInternalName())); preamble.add(new InsnNode(DUP)); preamble.add(new MethodInsnNode(INVOKESPECIAL, ARRAY_LIST.getInternalName(), "<init>", "()V")); preamble.add(new VarInsnNode(ASTORE, listIndex)); Iterator<AbstractInsnNode> execInstIter = execMethod.instructions.iterator(); methodInstr.setOpcode(INVOKESTATIC); methodInstr.name = methodInstr.name + "$static$callable$creator"; for (MethodNode staticCreated : staticMethodsToAdd) { midterm.add(new MethodInsnNode(INVOKEVIRTUAL, ARRAY_LIST.getInternalName(), "add", "(Ljava/lang/Object;)Z")); if (methodInstr.getNext().getOpcode() != POP) { midterm.add(new InsnNode(POP)); finish.add(new MethodInsnNode(INVOKESTATIC, TRANSACTION.getInternalName(), "current", "()Ljvstm/Transaction;")); finish.add(new MethodInsnNode(INVOKEVIRTUAL, TRANSACTION.getInternalName(), "manageNestedParallelTxs", "(Ljava/util/List;)Ljava/util/List;")); finish.add(new MethodInsnNode(INVOKEVIRTUAL, cn.name, combinerMethod.name, combinerMethod.desc));
@SuppressWarnings({"unchecked"}) private void createConstuctor(ParserClassNode classNode, MethodNode constructor) { MethodNode newConstructor = new MethodNode(ACC_PUBLIC, constructor.name, constructor.desc, constructor.signature, (String[]) constructor.exceptions.toArray(new String[constructor.exceptions.size()])); InsnList instructions = newConstructor.instructions; instructions.add(new VarInsnNode(ALOAD, 0)); instructions.add(createArgumentLoaders(constructor.desc)); instructions.add(new MethodInsnNode(INVOKESPECIAL, classNode.getParentType().getInternalName(), "<init>", constructor.desc, classNode.isInterface())); instructions.add(new InsnNode(RETURN)); classNode.methods.add(newConstructor); }
sourceReader.accept(sourceNode, 0); Type targetType = Type.getObjectType(targetNode.name); ListIterator<AbstractInsnNode> instructions = m.instructions.iterator(); while (instructions.hasNext()) { AbstractInsnNode node = instructions.next(); if (node instanceof MethodInsnNode) { MethodInsnNode call = (MethodInsnNode) node; if (Type.getObjectType(call.owner).getClassName().equals(data.source)) { call.owner = targetType.getInternalName(); if (call.getOpcode() == INVOKEINTERFACE) { call.setOpcode(INVOKEVIRTUAL); call.itf = false;
/** * Intercepts the method using the invokedynamic bytecode instruction available in Java 7+. * Should be called through interceptInvokeVirtualMethod, not directly. */ private void interceptInvokeVirtualMethodWithInvokeDynamic( ListIterator<AbstractInsnNode> instructions, MethodInsnNode targetMethod) { instructions.remove(); // remove the method invocation Type type = Type.getObjectType(targetMethod.owner); String description = targetMethod.desc; String owner = type.getClassName(); if (targetMethod.getOpcode() != Opcodes.INVOKESTATIC) { String thisType = type.getDescriptor(); description = "(" + thisType + description.substring(1); } instructions.add(new InvokeDynamicInsnNode(targetMethod.name, description, BOOTSTRAP_INTRINSIC, owner)); } }