List of usage examples for org.objectweb.asm Opcodes GETFIELD
int GETFIELD
To view the source code for org.objectweb.asm Opcodes GETFIELD.
Click Source Link
From source file:com.google.code.jconts.instrument.gen.AsyncMethodAdapter.java
License:Apache License
@Override public void visitMaxs(int maxStack, int maxLocals) { // Table switch at the end of the method mv.visitLabel(dispatchLabel);//from w ww . java2 s .c om Label dflt = new Label(); // Load index target.visitVarInsn(Opcodes.ILOAD, 1 + info.thisOffset); int[] keys = new int[dispatchTable.size()]; for (int i = 0; i < keys.length; ++i) { keys[i] = i; } mv.visitLookupSwitchInsn(dflt, keys, dispatchTable.toArray(new Label[0])); // FIXME: ...throw exception mv.visitLabel(dflt); mv.visitInsn(Opcodes.RETURN); // catch block mv.visitLabel(catchLabel); // invoke Continuation#setException(Throwable t) target.visitVarInsn(Opcodes.ALOAD, info.isStatic() ? 0 : 1); mv.visitFieldInsn(Opcodes.GETFIELD, info.stateClassName, CONTINUATION_FIELD, CONTINUATION_DESC); mv.visitInsn(Opcodes.SWAP); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, CONTINUATION_NAME, CONTINUATION_SET_EXCEPTION_NAME, CONTINUATION_SET_EXCEPTION_DESC); mv.visitInsn(Opcodes.RETURN); // FIXME: evaluate properly super.visitMaxs(maxStack + 4 + info.thisOffset, maxLocals + 2); }
From source file:com.google.code.jconts.instrument.gen.ComputationClassGenerator.java
License:Apache License
private void generateExecute(ClassVisitor cv) { final String name = info.computationClassName; final Type outerType = Type.getObjectType(info.owner); // Generate execute(Continuation<T> cont); MethodVisitor mv = cv.visitMethod(Opcodes.ACC_FINAL | Opcodes.ACC_PUBLIC, COMPUTATION_EXECUTE_NAME, COMPUTATION_EXECUTE_DESC, executeSignature(), null); mv.visitCode();//from w ww. j av a 2s. c o m Label start = new Label(); Label end = new Label(); mv.visitLabel(start); // Load outer this if (!info.isStatic()) { mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, name, "this$0", outerType.getDescriptor()); } // Load state field mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, name, "state", stateDesc); // state.continuation = continuation mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, name, "state", stateDesc); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitFieldInsn(Opcodes.PUTFIELD, info.stateClassName, CONTINUATION_FIELD, CONTINUATION_DESC); // Initial state (0) mv.visitIntInsn(Opcodes.BIPUSH, 0); mv.visitMethodInsn(info.isStatic() ? Opcodes.INVOKESTATIC : Opcodes.INVOKEVIRTUAL, info.owner, info.name + "$async", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { info.stateType, Type.INT_TYPE })); mv.visitInsn(Opcodes.RETURN); mv.visitLabel(end); mv.visitLocalVariable("this", 'L' + name + ';', signature, start, end, 0); if (!info.isStatic()) { mv.visitLocalVariable("this$0", outerType.getDescriptor(), null, start, end, 1); } SignatureWriter sign = new SignatureWriter(); contSignature(sign); mv.visitLocalVariable("continuation", CONTINUATION_DESC, sign.toString(), start, end, 1 + info.thisOffset); mv.visitMaxs(3 + info.thisOffset, 2 + info.thisOffset); mv.visitEnd(); }
From source file:com.google.code.jconts.instrument.gen.ContinuationClassGenerator.java
License:Apache License
private void generateExecute(ClassVisitor cv, boolean execute) { final String name = info.continuationClassName; final Type outerType = Type.getObjectType(info.owner); // Generate invoke(T result); String signature = null;/*w ww . jav a 2 s . co m*/ if (execute) { SignatureWriter sign = new SignatureWriter(); sign.visitParameterType().visitTypeVariable("T"); sign.visitReturnType().visitBaseType('V'); // void signature = sign.toString(); } MethodVisitor mv = cv.visitMethod(Opcodes.ACC_FINAL | Opcodes.ACC_PUBLIC, execute ? CONTINUATION_INVOKE_NAME : CONTINUATION_SET_EXCEPTION_NAME, execute ? CONTINUATION_INVOKE_DESC : CONTINUATION_SET_EXCEPTION_DESC, signature, null); mv.visitCode(); Label start = new Label(); Label end = new Label(); mv.visitLabel(start); // Load outer this if (!info.isStatic()) { mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, name, "this$0", outerType.getDescriptor()); } // state.result = result or state.exception = throwable mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, name, "state", stateDesc); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitFieldInsn(Opcodes.PUTFIELD, info.stateClassName, execute ? "result" : "exception", execute ? OBJECT_DESC : THROWABLE_DESC); // Load state field mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, name, "state", stateDesc); // Continue from this index or index+1 (for exception) mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, name, "index", "I"); if (!execute) { mv.visitInsn(Opcodes.ICONST_1); mv.visitInsn(Opcodes.IADD); } mv.visitMethodInsn(info.isStatic() ? Opcodes.INVOKESTATIC : Opcodes.INVOKEVIRTUAL, info.owner, info.name + "$async", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { info.stateType, Type.INT_TYPE })); mv.visitInsn(Opcodes.RETURN); mv.visitLabel(end); mv.visitLocalVariable("this", 'L' + name + ';', signature, start, end, 0); if (!info.isStatic()) { mv.visitLocalVariable("this$0", outerType.getDescriptor(), null, start, end, 1); } mv.visitLocalVariable("result", OBJECT_DESC, "TT;", start, end, 1 + info.thisOffset); mv.visitMaxs(3 + info.thisOffset + (execute ? 0 : 1), 2 + info.thisOffset); mv.visitEnd(); }
From source file:com.google.devtools.build.android.desugar.BytecodeTypeInference.java
License:Open Source License
@Override public void visitFieldInsn(int opcode, String owner, String name, String desc) { switch (opcode) { case Opcodes.GETSTATIC: pushDescriptor(desc);//from ww w. j av a 2 s. c o m break; case Opcodes.PUTSTATIC: popDescriptor(desc); break; case Opcodes.GETFIELD: pop(); pushDescriptor(desc); break; case Opcodes.PUTFIELD: popDescriptor(desc); pop(); break; default: throw new RuntimeException( "Unhandled opcode " + opcode + ", owner=" + owner + ", name=" + name + ", desc" + desc); } super.visitFieldInsn(opcode, owner, name, desc); }
From source file:com.google.devtools.build.android.desugar.CorePackageRenamerTest.java
License:Open Source License
@Test public void testSymbolRewrite() throws Exception { MockClassVisitor out = new MockClassVisitor(); CorePackageRenamer renamer = new CorePackageRenamer(out, new CoreLibrarySupport(new CoreLibraryRewriter(""), null, ImmutableList.of("java/time/"), ImmutableList.of(), ImmutableList.of("java/util/A#m->java/time/B"), ImmutableList.of())); MethodVisitor mv = renamer.visitMethod(0, "test", "()V", null, null); mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/time/Instant", "now", "()Ljava/time/Instant;", false); assertThat(out.mv.owner).isEqualTo("j$/time/Instant"); assertThat(out.mv.desc).isEqualTo("()Lj$/time/Instant;"); // Ignore moved methods but not their descriptors mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/util/A", "m", "()Ljava/time/Instant;", false); assertThat(out.mv.owner).isEqualTo("java/util/A"); assertThat(out.mv.desc).isEqualTo("()Lj$/time/Instant;"); // Ignore arbitrary other methods but not their descriptors mv.visitMethodInsn(Opcodes.INVOKESTATIC, "other/time/Instant", "now", "()Ljava/time/Instant;", false); assertThat(out.mv.owner).isEqualTo("other/time/Instant"); assertThat(out.mv.desc).isEqualTo("()Lj$/time/Instant;"); mv.visitFieldInsn(Opcodes.GETFIELD, "other/time/Instant", "now", "Ljava/time/Instant;"); assertThat(out.mv.owner).isEqualTo("other/time/Instant"); assertThat(out.mv.desc).isEqualTo("Lj$/time/Instant;"); }
From source file:com.google.devtools.build.wireless.testing.java.injector.WhiteBoxMethodAdapter.java
License:Apache License
/** * Injects visit putfield and putstatic instructions by logging the new value * which is stored into the field.//from w w w .j a va2s . c o m */ @Override public void visitFieldInsn(int opcode, String owner, String name, String desc) { boolean instrument = false; Matcher fieldMatcher = fieldInclusionPattern.matcher(name); boolean matchField = fieldMatcher.find(); if (matchOwnerClass && matchField && (isField(opcode) || isStaticField(opcode))) { instrument = true; logger.info("\t[SetField]: " + owner + "." + name + " desc " + desc); } if (instrument && isField(opcode)) { // duplicate and store the value stackServant.store(desc, accessedFieldValueIndexInStack); stackServant.duplicateAndStore("L" + owner + ";", accessedFieldOwnerIndexInStack); stackServant.load(desc, accessedFieldValueIndexInStack); } mv.visitFieldInsn(opcode, owner, name, desc); if (instrument) { printServant.startPrinting(); printServant.printString("[WhiteBox][Set]\t"); if (isField(opcode)) { stackServant.load("L" + owner + ";", accessedFieldOwnerIndexInStack); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, owner, "toString", "()L" + ClassNames.JAVA_LANG_STRING + ";"); printServant.print("L" + ClassNames.JAVA_LANG_STRING + ";"); } else { printServant.printString(owner); } printServant.printString("." + name + " Invoked="); timeServant.loadCurrentTimeMillis(); printServant.print("J"); // add value type to output string printServant.printString(" value="); // if the field is not static we must retrieve a pointer to the owner int code = -1; if (isField(opcode)) { stackServant.load("L" + owner + ";", accessedFieldOwnerIndexInStack); code = Opcodes.GETFIELD; } else { code = Opcodes.GETSTATIC; } mv.visitFieldInsn(code, owner, name, desc); printServant.println(desc); // save and print printServant.stopPrinting(); } }
From source file:com.google.test.metric.asm.MethodVisitorBuilder.java
License:Apache License
public void visitFieldInsn(final int opcode, String owner, final String name, final String desc) { owner = namer.nameClass(owner);//from w w w.j av a2s . c o m switch (opcode) { case Opcodes.PUTSTATIC: recorder.add(new PutFieldRunnable(repository, owner, name, desc, true)); break; case Opcodes.PUTFIELD: recorder.add(new PutFieldRunnable(repository, owner, name, desc, false)); break; case Opcodes.GETSTATIC: recorder.add(new GetFieldRunnable(repository, owner, name, desc, true)); break; case Opcodes.GETFIELD: recorder.add(new GetFieldRunnable(repository, owner, name, desc, false)); break; } }
From source file:com.googlecode.ddom.weaver.compound.CompoundClassGenerator.java
License:Apache License
@Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, name, desc, signature, exceptions); if (mv != null) { Type[] argumentTypes = Type.getArgumentTypes(desc); mv.visitCode();/* w w w . java 2 s.co m*/ Label l0 = new Label(); mv.visitLabel(l0); for (int i = 0; i < componentClasses.length; i++) { String componentClass = Util.classNameToInternalName(componentClasses[i]); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, className, "c" + i, "L" + componentClass + ";"); for (int j = 0; j < argumentTypes.length; j++) { mv.visitVarInsn(argumentTypes[j].getOpcode(Opcodes.ILOAD), j + 1); } mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, ifaceName, name, desc); } mv.visitInsn(Opcodes.RETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + className + ";", null, l0, l1, 0); mv.visitMaxs(argumentTypes.length + 1, argumentTypes.length + 1); mv.visitEnd(); } return null; }
From source file:com.googlecode.ddom.weaver.ext.ModelExtensionFactoryImplementation.java
License:Apache License
public void accept(ClassVisitor classVisitor) { // Note: the name chosen here must match what is expected in ExtensionFactoryLocator String name = Util.classNameToInternalName(implementationInfo.getFactoryInterface().getName() + "$$Impl"); String factoryInterfaceName = Util .classNameToInternalName(implementationInfo.getFactoryInterface().getName()); classVisitor.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, name, null, "java/lang/Object", new String[] { factoryInterfaceName }); {/*from www . ja v a 2s. co m*/ FieldVisitor fw = classVisitor.visitField(Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_STATIC, "INSTANCE", "L" + factoryInterfaceName + ";", null, null); if (fw != null) { fw.visitEnd(); } } { FieldVisitor fw = classVisitor.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, "delegates", "Ljava/util/Map;", null, null); if (fw != null) { fw.visitEnd(); } } { MethodVisitor mv = classVisitor.visitMethod(Opcodes.ACC_PRIVATE, "<init>", "()V", null, null); if (mv != null) { mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); // Call constructor from superclass mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); // Create delegates map mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitTypeInsn(Opcodes.NEW, "java/util/HashMap"); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/HashMap", "<init>", "()V"); mv.visitFieldInsn(Opcodes.PUTFIELD, name, "delegates", "Ljava/util/Map;"); // Populate delegates map for (ModelExtensionInfo modelExtensionInfo : implementationInfo.getModelExtensions()) { for (ModelExtensionInterfaceInfo extensionInterface : modelExtensionInfo .getExtensionInterfaces()) { if (!extensionInterface.isAbstract()) { // TODO: this is stupid; we should not recreate the info object here ModelExtensionClassInfo modelExtensionClassInfo = new ModelExtensionClassInfo( implementationInfo.getImplementation(), modelExtensionInfo.getRootInterface(), extensionInterface); String factoryDelegateImplName = Util.classNameToInternalName( modelExtensionClassInfo.getFactoryDelegateImplementationClassName()); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, name, "delegates", "Ljava/util/Map;"); mv.visitLdcInsn(Type.getObjectType(Util.classNameToInternalName( modelExtensionClassInfo.getExtensionInterface().getClassInfo().getName()))); mv.visitTypeInsn(Opcodes.NEW, factoryDelegateImplName); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, factoryDelegateImplName, "<init>", "()V"); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitInsn(Opcodes.POP); } } } mv.visitInsn(Opcodes.RETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + name + ";", null, l0, l1, 0); mv.visitMaxs(4, 1); mv.visitEnd(); } } String factoryDelegateInterfaceName = Util .classNameToInternalName(implementationInfo.getFactoryDelegateInterfaceName()); String getDelegateDesc = "(Ljava/lang/Class;)L" + factoryDelegateInterfaceName + ";"; { MethodVisitor mv = classVisitor.visitMethod(Opcodes.ACC_PRIVATE, "getDelegate", getDelegateDesc, null, null); if (mv != null) { mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, name, "delegates", "Ljava/util/Map;"); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;"); mv.visitTypeInsn(Opcodes.CHECKCAST, factoryDelegateInterfaceName); mv.visitInsn(Opcodes.ARETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + name + ";", null, l0, l1, 0); mv.visitLocalVariable("extensionInterface", "Ljava/lang/Class;", null, l0, l1, 1); mv.visitMaxs(2, 2); mv.visitEnd(); } } String implementationName = Util.classNameToInternalName(implementationInfo.getImplementation().getName()); for (ConstructorInfo constructor : implementationInfo.getConstructors()) { Type[] constructorArgumentTypes = constructor.getArgumentTypes(); Type[] argumentTypes = new Type[constructorArgumentTypes.length + 1]; argumentTypes[0] = Type.getObjectType("java/lang/Class"); System.arraycopy(constructorArgumentTypes, 0, argumentTypes, 1, constructorArgumentTypes.length); MethodVisitor mv = classVisitor.visitMethod(Opcodes.ACC_PUBLIC, "create", Type.getMethodDescriptor(Type.getObjectType(implementationName), argumentTypes), null, null); if (mv != null) { mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(Opcodes.ALOAD, 1); Label l1 = new Label(); mv.visitJumpInsn(Opcodes.IFNONNULL, l1); mv.visitTypeInsn(Opcodes.NEW, implementationName); mv.visitInsn(Opcodes.DUP); for (int i = 0; i < constructorArgumentTypes.length; i++) { mv.visitVarInsn(constructorArgumentTypes[i].getOpcode(Opcodes.ILOAD), i + 2); } mv.visitMethodInsn(Opcodes.INVOKESPECIAL, implementationName, "<init>", constructor.getDescriptor()); mv.visitInsn(Opcodes.ARETURN); mv.visitLabel(l1); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, name, "getDelegate", getDelegateDesc); for (int i = 0; i < constructorArgumentTypes.length; i++) { mv.visitVarInsn(constructorArgumentTypes[i].getOpcode(Opcodes.ILOAD), i + 2); } mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, factoryDelegateInterfaceName, "create", constructor.getFactoryDelegateMethodDescriptor()); mv.visitInsn(Opcodes.ARETURN); Label l3 = new Label(); mv.visitLabel(l3); mv.visitLocalVariable("this", "L" + name + ";", null, l0, l3, 0); mv.visitLocalVariable("extensionInterface", "Ljava/lang/Class;", null, l0, l3, 1); for (int i = 0; i < constructorArgumentTypes.length; i++) { mv.visitLocalVariable("arg" + i, constructorArgumentTypes[i].getDescriptor(), null, l0, l3, i + 2); } mv.visitMaxs(argumentTypes.length + 1, argumentTypes.length + 1); mv.visitEnd(); } } { MethodVisitor mv = classVisitor.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null); if (mv != null) { mv.visitCode(); mv.visitTypeInsn(Opcodes.NEW, name); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, name, "<init>", "()V"); mv.visitFieldInsn(Opcodes.PUTSTATIC, name, "INSTANCE", "L" + factoryInterfaceName + ";"); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(2, 0); mv.visitEnd(); } } classVisitor.visitEnd(); }
From source file:com.hea3ven.hardmodetweaks.core.ClassTransformerHardModeTweaks.java
License:Open Source License
private void patchWorldClientTick(MethodNode method, boolean obfuscated) { Iterator<AbstractInsnNode> iter = method.instructions.iterator(); // Replace//from w ww .j a v a2 s . c om // > this.setWorldTime(this.getWorldTime() + 1L); // To // > TimeTweaksManager.addTick(this.provider); int index = 0; while (iter.hasNext()) { AbstractInsnNode currentNode = iter.next(); if (currentNode.getOpcode() == Opcodes.INVOKEVIRTUAL) { MethodInsnNode methodInsnNode = (MethodInsnNode) currentNode; if (WORLD_CLIENT_GET_WORLD_TIME.matchesNode(methodInsnNode, "()J", obfuscated) && currentNode.getNext().getOpcode() == Opcodes.LCONST_1) { index = method.instructions.indexOf(currentNode) - 1; } } } if (index == 0) error("Could not find call in WorldServer.tick method"); method.instructions.remove(method.instructions.get(index)); method.instructions.remove(method.instructions.get(index)); method.instructions.remove(method.instructions.get(index)); method.instructions.remove(method.instructions.get(index)); method.instructions.remove(method.instructions.get(index)); method.instructions.insertBefore(method.instructions.get(index), new FieldInsnNode(Opcodes.GETFIELD, WORLD.getPath(obfuscated), WORLD_PROVIDER.get(obfuscated), "L" + WORLD_PROVIDER_CLASS.getPath(obfuscated) + ";")); method.instructions.insert(method.instructions.get(index), new MethodInsnNode(Opcodes.INVOKESTATIC, "com/hea3ven/hardmodetweaks/TimeTweaksManager", "addTick", "(L" + WORLD_PROVIDER_CLASS.getPath(obfuscated) + ";)V")); }