/** * Extracts a handle type for invoking the given method via invokespecial. * * @param methodDescription The method for which a handle type should be found. * @return The handle type for the given method. */ protected static HandleType ofSpecial(MethodDescription methodDescription) { if (methodDescription.isStatic() || methodDescription.isAbstract()) { throw new IllegalArgumentException( "Cannot invoke " + methodDescription + " via invokespecial"); } return methodDescription.isConstructor() ? INVOKE_SPECIAL_CONSTRUCTOR : INVOKE_SPECIAL; }
/** * Extracts a handle type for invoking the given method. * * @param methodDescription The method for which a handle type should be found. * @return The handle type for the given method. */ protected static HandleType of(MethodDescription methodDescription) { if (methodDescription.isStatic()) { return INVOKE_STATIC; } else if (methodDescription.isPrivate()) { return INVOKE_SPECIAL; } else if (methodDescription.isConstructor()) { return INVOKE_SPECIAL_CONSTRUCTOR; } else if (methodDescription.getDeclaringType().asRawType().isInterface()) { return INVOKE_INTERFACE; } else { return INVOKE_VIRTUAL; } }
@Test public void testSuperConstructorIsInvokable() throws Exception { when(superMethod.isConstructor()).thenReturn(true); Implementation.SpecialMethodInvocation specialMethodInvocation = implementationTarget.invokeSuper(superMethod, methodLookup); assertThat(specialMethodInvocation.isValid(), is(true)); assertThat(specialMethodInvocation.getMethodDescription(), is(superMethodConstructor)); assertThat(specialMethodInvocation.getTypeDescription(), is(superType)); MethodVisitor methodVisitor = mock(MethodVisitor.class); Implementation.Context implementationContext = mock(Implementation.Context.class); StackManipulation.Size size = specialMethodInvocation.apply(methodVisitor, implementationContext); verify(methodVisitor).visitMethodInsn(Opcodes.INVOKESPECIAL, BAR, QUXBAZ, BAZBAR, false); verifyNoMoreInteractions(methodVisitor); verifyZeroInteractions(implementationContext); assertThat(size.getSizeImpact(), is(0)); assertThat(size.getMaximalSize(), is(0)); }
@Override @Before public void setUp() throws Exception { when(parameterList.asTypeList()).thenReturn(parameterTypes); when(instrumentedType.getSupertype()).thenReturn(superType); when(superType.getDeclaredMethods()) .thenReturn(new MethodList.Explicit(Collections.singletonList(superMethodConstructor))); when(superType.getInternalName()).thenReturn(BAR); when(superMethod.getDeclaringType()).thenReturn(superType); when(superType.getStackSize()).thenReturn(StackSize.ZERO); when(superMethod.getReturnType()).thenReturn(returnType); when(superMethod.getInternalName()).thenReturn(BAZ); when(superMethod.getDescriptor()).thenReturn(FOOBAR); when(superMethod.getParameters()).thenReturn(parameterList); when(superMethodConstructor.isConstructor()).thenReturn(true); when(superMethodConstructor.getParameters()).thenReturn(parameterList); when(superMethodConstructor.getReturnType()).thenReturn(returnType); when(superMethodConstructor.isSpecializableFor(superType)).thenReturn(true); when(superMethodConstructor.getInternalName()).thenReturn(QUXBAZ); when(superMethodConstructor.getDescriptor()).thenReturn(BAZBAR); super.setUp(); }