use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.
the class InvokeDynamicTest method testArgumentCannotAssignIllegalInstanceType.
@Test(expected = IllegalArgumentException.class)
@JavaVersionRule.Enforce(7)
public void testArgumentCannotAssignIllegalInstanceType() throws Exception {
Class<?> type = Class.forName(ARGUMENT_BOOTSTRAP);
TypeDescription typeDescription = new TypeDescription.ForLoadedType(type);
InvokeDynamic.bootstrap(typeDescription.getDeclaredMethods().filter(named(BOOTSTRAP)).getOnly()).invoke(QUX, String.class).withReference(new Object()).as(String.class);
}
use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.
the class InvokeDynamicTest method testBootstrapOfMethodsWithParametersWrapperReference.
@Test
@JavaVersionRule.Enforce(7)
public void testBootstrapOfMethodsWithParametersWrapperReference() throws Exception {
TypeDescription typeDescription = new TypeDescription.ForLoadedType(Class.forName(ARGUMENT_BOOTSTRAP));
Object value = new Object();
DynamicType.Loaded<Simple> dynamicType = new ByteBuddy().subclass(Simple.class).method(isDeclaredBy(Simple.class)).intercept(InvokeDynamic.bootstrap(typeDescription.getDeclaredMethods().filter(named(BOOTSTRAP)).getOnly()).invoke(BAR, String.class).withReference(BOOLEAN, BYTE, SHORT, CHARACTER, INTEGER, LONG, FLOAT, DOUBLE, FOO, CLASS, makeEnum(), makeMethodType(CLASS)).withReference(makeMethodHandle()).as(// avoid direct method handle
JavaType.METHOD_HANDLE.load()).withReference(value)).make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
assertThat(dynamicType.getLoaded().getDeclaredFields().length, is(14));
assertThat(dynamicType.getLoaded().getDeclaredConstructor().newInstance().foo(), is("" + BOOLEAN + BYTE + SHORT + CHARACTER + INTEGER + LONG + FLOAT + DOUBLE + FOO + CLASS + makeEnum() + makeMethodType(CLASS) + makeMethodHandle() + value));
}
use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.
the class InvokeDynamicTest method testBootstrapWithArrayArgumentsWithArguments.
@Test
@JavaVersionRule.Enforce(value = 7, hotSpot = 7)
public void testBootstrapWithArrayArgumentsWithArguments() throws Exception {
Class<?> type = Class.forName(PARAMETER_BOOTSTRAP);
Field field = type.getDeclaredField(ARGUMENTS_FIELD_NAME);
field.set(null, null);
TypeDescription typeDescription = new TypeDescription.ForLoadedType(type);
DynamicType.Loaded<Simple> dynamicType = new ByteBuddy().subclass(Simple.class).method(isDeclaredBy(Simple.class)).intercept(InvokeDynamic.bootstrap(typeDescription.getDeclaredMethods().filter(named(BOOTSTRAP_ARRAY_ARGUMENTS)).getOnly(), INTEGER, LONG, FLOAT, DOUBLE, FOO, CLASS, makeMethodType(CLASS), makeMethodHandle()).withoutArguments()).make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
assertThat(dynamicType.getLoaded().getDeclaredConstructor().newInstance().foo(), is(FOO));
Object[] arguments = (Object[]) field.get(null);
assertThat(arguments.length, is(8));
assertThat(arguments[0], is((Object) INTEGER));
assertThat(arguments[1], is((Object) LONG));
assertThat(arguments[2], is((Object) FLOAT));
assertThat(arguments[3], is((Object) DOUBLE));
assertThat(arguments[4], is((Object) FOO));
assertThat(arguments[5], is((Object) CLASS));
assertThat(arguments[6], is(makeMethodType(CLASS)));
assertThat(JavaConstant.MethodHandle.ofLoaded(arguments[7]), is(JavaConstant.MethodHandle.ofLoaded(makeMethodHandle())));
}
use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.
the class MethodGraphCompilerForDeclaredMethodsTest method testCompilationNonVirtual.
@Test
public void testCompilationNonVirtual() throws Exception {
TypeDescription typeDescription = mock(TypeDescription.class);
MethodDescription.InDefinedShape methodDescription = mock(MethodDescription.InDefinedShape.class);
when(typeDescription.getDeclaredMethods()).thenReturn(new MethodList.Explicit<MethodDescription.InDefinedShape>(methodDescription));
when(methodDescription.isVirtual()).thenReturn(false);
when(methodDescription.isBridge()).thenReturn(false);
when(methodDescription.isVisibleTo(typeDescription)).thenReturn(true);
MethodGraph.Linked methodGraph = MethodGraph.Compiler.ForDeclaredMethods.INSTANCE.compile(typeDescription);
assertThat(methodGraph.listNodes().size(), is(0));
}
use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.
the class MethodRegistryDefaultTest method testVisibilityBridgeIfNotMatchedAndVisible.
@Test
@SuppressWarnings("unchecked")
public void testVisibilityBridgeIfNotMatchedAndVisible() throws Exception {
when(instrumentedMethod.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
when(parameterDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
when(resolvedMethodFilter.matches(instrumentedMethod)).thenReturn(true);
when(firstFilter.matches(instrumentedMethod)).thenReturn(false);
when(secondFilter.matches(instrumentedMethod)).thenReturn(false);
when(resolvedMethodFilter.matches(instrumentedMethod)).thenReturn(true);
TypeDescription declaringType = mock(TypeDescription.class);
when(declaringType.asErasure()).thenReturn(declaringType);
when(instrumentedMethod.getDeclaringType()).thenReturn(declaringType);
when(thirdType.isPublic()).thenReturn(true);
when(instrumentedMethod.isPublic()).thenReturn(true);
when(declaringType.isPackagePrivate()).thenReturn(true);
TypeDescription.Generic superClass = mock(TypeDescription.Generic.class);
TypeDescription rawSuperClass = mock(TypeDescription.class);
when(superClass.asErasure()).thenReturn(rawSuperClass);
when(typeDescription.getSuperClass()).thenReturn(superClass);
MethodDescription.Token methodToken = mock(MethodDescription.Token.class);
when(instrumentedMethod.asToken(ElementMatchers.is(typeDescription))).thenReturn(methodToken);
when(methodToken.accept(any(TypeDescription.Generic.Visitor.class))).thenReturn(methodToken);
when(classFileVersion.isAtLeast(ClassFileVersion.JAVA_V5)).thenReturn(true);
MethodRegistry.Compiled methodRegistry = new MethodRegistry.Default().append(firstMatcher, firstHandler, firstFactory, transformer).append(secondMatcher, secondHandler, secondFactory, transformer).prepare(firstType, methodGraphCompiler, TypeValidation.ENABLED, methodFilter).compile(implementationTargetFactory, classFileVersion);
assertThat(methodRegistry.getInstrumentedType(), is(typeDescription));
assertThat(methodRegistry.getInstrumentedMethods().size(), is(1));
assertThat(methodRegistry.getTypeInitializer(), is(typeInitializer));
assertThat(methodRegistry.getLoadedTypeInitializer(), is(loadedTypeInitializer));
verify(firstHandler).prepare(firstType);
verify(secondHandler).prepare(secondType);
verifyZeroInteractions(firstFactory);
verifyZeroInteractions(secondFactory);
assertThat(methodRegistry.target(instrumentedMethod), instanceOf(TypeWriter.MethodPool.Record.ForDefinedMethod.OfVisibilityBridge.class));
}
Aggregations