Search in sources :

Example 91 with TypeDescription

use of org.apache.beam.vendor.bytebuddy.v1_11_0.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);
}
Also used : TypeDescription(net.bytebuddy.description.type.TypeDescription) Test(org.junit.Test)

Example 92 with TypeDescription

use of org.apache.beam.vendor.bytebuddy.v1_11_0.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));
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) TypeDescription(net.bytebuddy.description.type.TypeDescription) ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 93 with TypeDescription

use of org.apache.beam.vendor.bytebuddy.v1_11_0.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())));
}
Also used : Field(java.lang.reflect.Field) DynamicType(net.bytebuddy.dynamic.DynamicType) TypeDescription(net.bytebuddy.description.type.TypeDescription) ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 94 with TypeDescription

use of org.apache.beam.vendor.bytebuddy.v1_11_0.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));
}
Also used : MethodDescription(net.bytebuddy.description.method.MethodDescription) TypeDescription(net.bytebuddy.description.type.TypeDescription) MethodList(net.bytebuddy.description.method.MethodList) Test(org.junit.Test)

Example 95 with TypeDescription

use of org.apache.beam.vendor.bytebuddy.v1_11_0.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));
}
Also used : MethodDescription(net.bytebuddy.description.method.MethodDescription) AnnotationList(net.bytebuddy.description.annotation.AnnotationList) TypeDescription(net.bytebuddy.description.type.TypeDescription) Test(org.junit.Test)

Aggregations

TypeDescription (net.bytebuddy.description.type.TypeDescription)178 Test (org.junit.Test)155 MethodDescription (net.bytebuddy.description.method.MethodDescription)75 ByteBuddy (net.bytebuddy.ByteBuddy)25 DynamicType (net.bytebuddy.dynamic.DynamicType)25 StackManipulation (net.bytebuddy.implementation.bytecode.StackManipulation)17 ClassFileLocator (net.bytebuddy.dynamic.ClassFileLocator)10 AnnotationList (net.bytebuddy.description.annotation.AnnotationList)9 AbstractTypeDescriptionTest (net.bytebuddy.description.type.AbstractTypeDescriptionTest)9 Map (java.util.Map)8 MethodList (net.bytebuddy.description.method.MethodList)8 Field (java.lang.reflect.Field)7 List (java.util.List)5 FieldDescription (net.bytebuddy.description.field.FieldDescription)5 LoadedTypeInitializer (net.bytebuddy.implementation.LoadedTypeInitializer)5 Serializable (java.io.Serializable)4 URLClassLoader (java.net.URLClassLoader)4 TypeList (net.bytebuddy.description.type.TypeList)4 Before (org.junit.Before)4 MethodVisitor (org.objectweb.asm.MethodVisitor)4