List of usage examples for org.objectweb.asm Opcodes INVOKESTATIC
int INVOKESTATIC
To view the source code for org.objectweb.asm Opcodes INVOKESTATIC.
Click Source Link
From source file:com.google.devtools.build.android.desugar.CoreLibrarySupport.java
License:Open Source License
private void makeDispatchHelperMethod(ClassVisitor helper, EmulatedMethod method, ImmutableList<Class<?>> typechecks) { checkArgument(method.owner().isInterface()); String owner = method.owner().getName().replace('.', '/'); Type methodType = Type.getMethodType(method.descriptor()); String companionDesc = InterfaceDesugaring.companionDefaultMethodDescriptor(owner, method.descriptor()); MethodVisitor dispatchMethod = helper.visitMethod(method.access() | Opcodes.ACC_STATIC, method.name(), companionDesc, /*signature=*/ null, // signature is invalid due to extra "receiver" argument method.exceptions().toArray(EMPTY_LIST)); dispatchMethod.visitCode();//from www. j a v a2 s. com { // See if the receiver might come with its own implementation of the method, and call it. // We do this by testing for the interface type created by EmulatedInterfaceRewriter Label fallthrough = new Label(); String emulationInterface = renameCoreLibrary(owner); dispatchMethod.visitVarInsn(Opcodes.ALOAD, 0); // load "receiver" dispatchMethod.visitTypeInsn(Opcodes.INSTANCEOF, emulationInterface); dispatchMethod.visitJumpInsn(Opcodes.IFEQ, fallthrough); dispatchMethod.visitVarInsn(Opcodes.ALOAD, 0); // load "receiver" dispatchMethod.visitTypeInsn(Opcodes.CHECKCAST, emulationInterface); visitLoadArgs(dispatchMethod, methodType, 1 /* receiver already loaded above */); dispatchMethod.visitMethodInsn(Opcodes.INVOKEINTERFACE, emulationInterface, method.name(), method.descriptor(), /*itf=*/ true); dispatchMethod.visitInsn(methodType.getReturnType().getOpcode(Opcodes.IRETURN)); dispatchMethod.visitLabel(fallthrough); // Trivial frame for the branch target: same empty stack as before dispatchMethod.visitFrame(Opcodes.F_SAME, 0, EMPTY_FRAME, 0, EMPTY_FRAME); } // Next, check for subtypes with specialized implementations and call them for (Class<?> tested : typechecks) { Label fallthrough = new Label(); String testedName = tested.getName().replace('.', '/'); // In case of a class this must be a member move; for interfaces use the companion. String target = tested.isInterface() ? InterfaceDesugaring.getCompanionClassName(testedName) : checkNotNull(memberMoves.get(rewriter.unprefix(testedName) + '#' + method.name())); dispatchMethod.visitVarInsn(Opcodes.ALOAD, 0); // load "receiver" dispatchMethod.visitTypeInsn(Opcodes.INSTANCEOF, testedName); dispatchMethod.visitJumpInsn(Opcodes.IFEQ, fallthrough); dispatchMethod.visitVarInsn(Opcodes.ALOAD, 0); // load "receiver" dispatchMethod.visitTypeInsn(Opcodes.CHECKCAST, testedName); // make verifier happy visitLoadArgs(dispatchMethod, methodType, 1 /* receiver already loaded above */); dispatchMethod.visitMethodInsn(Opcodes.INVOKESTATIC, target, method.name(), InterfaceDesugaring.companionDefaultMethodDescriptor(testedName, method.descriptor()), /*itf=*/ false); dispatchMethod.visitInsn(methodType.getReturnType().getOpcode(Opcodes.IRETURN)); dispatchMethod.visitLabel(fallthrough); // Trivial frame for the branch target: same empty stack as before dispatchMethod.visitFrame(Opcodes.F_SAME, 0, EMPTY_FRAME, 0, EMPTY_FRAME); } // Call static type's default implementation in companion class dispatchMethod.visitVarInsn(Opcodes.ALOAD, 0); // load "receiver" visitLoadArgs(dispatchMethod, methodType, 1 /* receiver already loaded above */); dispatchMethod.visitMethodInsn(Opcodes.INVOKESTATIC, InterfaceDesugaring.getCompanionClassName(owner), method.name(), companionDesc, /*itf=*/ false); dispatchMethod.visitInsn(methodType.getReturnType().getOpcode(Opcodes.IRETURN)); dispatchMethod.visitMaxs(0, 0); dispatchMethod.visitEnd(); }
From source file:com.google.devtools.build.android.desugar.CoreLibrarySupportTest.java
License:Open Source License
@Test public void testGetCoreInterfaceRewritingTarget_staticInterfaceMethod() throws Exception { CoreLibrarySupport support = new CoreLibrarySupport(new CoreLibraryRewriter(""), Thread.currentThread().getContextClassLoader(), ImmutableList.of(), ImmutableList.of("java/util/Comparator"), ImmutableList.of(), ImmutableList.of()); assertThat(support.getCoreInterfaceRewritingTarget(Opcodes.INVOKESTATIC, "java/util/Comparator", "reverseOrder", "()Ljava/util/Comparator;", true)).isEqualTo(Comparator.class); }
From source file:com.google.devtools.build.android.desugar.CoreLibrarySupportTest.java
License:Open Source License
/** * Tests that call sites of renamed core libraries are treated like call sites in regular * {@link InterfaceDesugaring}.//from w w w . ja va 2 s. c o m */ @Test public void testGetCoreInterfaceRewritingTarget_renamed() throws Exception { CoreLibrarySupport support = new CoreLibrarySupport(new CoreLibraryRewriter(""), Thread.currentThread().getContextClassLoader(), ImmutableList.of("java/util/"), ImmutableList.of(), ImmutableList.of(), ImmutableList.of()); // regular invocations of default methods: ignored assertThat(support.getCoreInterfaceRewritingTarget(Opcodes.INVOKEINTERFACE, "java/util/Collection", "removeIf", "(Ljava/util/function/Predicate;)Z", true)).isNull(); assertThat(support.getCoreInterfaceRewritingTarget(Opcodes.INVOKEVIRTUAL, "java/util/ArrayList", "removeIf", "(Ljava/util/function/Predicate;)Z", false)).isNull(); // abstract methods: ignored assertThat(support.getCoreInterfaceRewritingTarget(Opcodes.INVOKEINTERFACE, "java/util/Collection", "size", "()I", true)).isNull(); // static interface method assertThat(support.getCoreInterfaceRewritingTarget(Opcodes.INVOKESTATIC, "java/util/Comparator", "reverseOrder", "()Ljava/util/Comparator;", true)).isEqualTo(Comparator.class); // invokespecial for default methods: find nearest definition assertThat(support.getCoreInterfaceRewritingTarget(Opcodes.INVOKESPECIAL, "java/util/List", "removeIf", "(Ljava/util/function/Predicate;)Z", true)).isEqualTo(Collection.class); // invokespecial on a class: ignore (even if there's an inherited default method) assertThat(support.getCoreInterfaceRewritingTarget(Opcodes.INVOKESPECIAL, "java/util/ArrayList", "removeIf", "(Ljava/util/function/Predicate;)Z", false)).isNull(); }
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.android.desugar.LambdaDesugaring.java
License:Open Source License
static int invokeOpcode(Handle invokedMethod) { switch (invokedMethod.getTag()) { case Opcodes.H_INVOKESTATIC: return Opcodes.INVOKESTATIC; case Opcodes.H_INVOKEVIRTUAL: return Opcodes.INVOKEVIRTUAL; case Opcodes.H_INVOKESPECIAL: case Opcodes.H_NEWINVOKESPECIAL: // Must be preceded by NEW return Opcodes.INVOKESPECIAL; case Opcodes.H_INVOKEINTERFACE: return Opcodes.INVOKEINTERFACE; default:/* ww w.ja v a2 s .c om*/ throw new UnsupportedOperationException("Don't know how to call " + invokedMethod); } }
From source file:com.google.devtools.build.wireless.testing.java.injector.coverage.GenerateCoverageClassAdapterTest.java
License:Apache License
/** * Verify that the instruction to collect coverage would be invoked before any * invocation of <code>MIDlet.notifyDestroyed()</code> by * {@link GenerateCoverageClassAdapter#visitMethod(int, String, String, String, String[])} * . //w w w . ja v a 2 s . c o m */ public void testVisitMethod_notifyDestroyed() { methodVisitorMock = createMock(MethodVisitor.class); methodVisitorMock.visitLdcInsn(eq(Platform.J2ME.getFileConnectionPrefix())); methodVisitorMock.visitMethodInsn(eq(Opcodes.INVOKESTATIC), eq(CoverageClassNames.COVERAGE_MANAGER), eq("writeReport"), eq("(L" + ClassNames.JAVA_LANG_STRING + ";)V")); methodVisitorMock.visitMethodInsn(eq(ACC_PUBLIC), eq(ClassNames.MIDLET), eq("notifyDestroyed"), eq("()v")); replay(methodVisitorMock); classVisitorMock = createClassVisitorMock(methodVisitorMock); classAdapterJ2ME = new GenerateCoverageClassAdapter(Platform.J2ME, classVisitorMock, null); classAdapterJ2ME.visit(0, ACC_PUBLIC, "Foo", "", ClassNames.JAVA_LANG_OBJECT, null); MethodVisitor mv = classAdapterJ2ME.visitMethod(ACC_PUBLIC, "bar", "()v", "", null); verify(classVisitorMock); mv.visitMethodInsn(ACC_PUBLIC, ClassNames.MIDLET, "notifyDestroyed", "()v"); verify(methodVisitorMock); assertTrue( "The returned instance should be an instance of " + ReportBeforeDestroyingMethodAdapter.class.getName(), mv instanceof ReportBeforeDestroyingMethodAdapter); }
From source file:com.google.devtools.build.wireless.testing.java.injector.j2me.J2mePrintServant.java
License:Apache License
/** * Logs the new build string with the J2ME Logger. * * @see PrintServant#finalizePrinting()/*from w ww. j ava2 s.co m*/ */ @Override protected void finalizePrinting() { mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, ClassNames.STRING_BUFFER, "toString", "()L" + ClassNames.JAVA_LANG_STRING + ";"); mv.visitLdcInsn(logIdentifier); mv.visitInsn(Opcodes.SWAP); mv.visitMethodInsn(Opcodes.INVOKESTATIC, J2meClassNames.LOG, "log", "(L" + ClassNames.JAVA_LANG_STRING + ";L" + ClassNames.JAVA_LANG_STRING + ";)V"); }
From source file:com.google.devtools.build.wireless.testing.java.injector.ReferenceTracerMethodAdapter.java
License:Apache License
/** * Injects, at the end of the method, which will be a constructor, a set of * instruction to register this instance in the wrapper. * /*from w ww .j av a 2s .c o m*/ * @param opcode The instruction code. * @see org.objectweb.asm.MethodAdapter#visitInsn(int) */ @Override public void visitInsn(int opcode) { if (isReturnInstruction(opcode) || opcode == Opcodes.ATHROW) { stackServant.loadThis(); mv.visitMethodInsn(Opcodes.INVOKESTATIC, setterClassName, setterMethodsName, "(L" + ownerClass + ";)V"); } mv.visitInsn(opcode); }
From source file:com.google.devtools.build.wireless.testing.java.injector.TimeServant.java
License:Apache License
/** * Puts a Long on top of the stack containing the current time msec. *//*from w w w . j a v a2 s . c o m*/ public void loadCurrentTimeMillis() { mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/System", "currentTimeMillis", "()J"); }
From source file:com.google.gwt.jvm.asm.NativeMethodDelegatingVisitor.java
License:Apache License
private void GwtNativeDispatch_getInstance() { delegate.visitMethodInsn(Opcodes.INVOKESTATIC, GwtNativeDispatch, "getInstance", "()L" + GwtNativeDispatch + ";"); }