Search in sources :

Example 31 with TypeDescription

use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.

the class LambdaFactory method register.

/**
     * Registers a class file transformer together with a factory for creating a lambda expression. It is possible to call this method independently
     * of the class loader's context as the supplied injector makes sure that the manipulated collection is the one that is held by the system class
     * loader.
     *
     * @param classFileTransformer The class file transformer to register.
     * @param classFileFactory     The lambda class file factory to use. This factory must define a visible instance method with the signature
     *                             {@code byte[] make(Object, String, Object, Object, Object, Object, boolean, List, List, Collection}. The arguments provided
     *                             are the invokedynamic call site's lookup object, the lambda method's name, the factory method's type, the lambda method's
     *                             type, the target method's handle, the specialized method type of the lambda expression, a boolean to indicate
     *                             serializability, a list of marker interfaces, a list of additional bridges and a collection of class file transformers to
     *                             apply.
     * @return {@code true} if this is the first registered transformer. This indicates that the {@code LambdaMetafactory} must be instrumented to delegate
     * to this alternative factory.
     */
@SuppressWarnings("all")
public static boolean register(ClassFileTransformer classFileTransformer, Object classFileFactory) {
    try {
        TypeDescription typeDescription = new TypeDescription.ForLoadedType(LambdaFactory.class);
        Class<?> lambdaFactory = ClassInjector.UsingReflection.ofSystemClassLoader().inject(Collections.singletonMap(typeDescription, ClassFileLocator.ForClassLoader.read(LambdaFactory.class).resolve())).get(typeDescription);
        @SuppressWarnings("unchecked") Map<ClassFileTransformer, Object> classFileTransformers = (Map<ClassFileTransformer, Object>) lambdaFactory.getField(FIELD_NAME).get(null);
        synchronized (classFileTransformers) {
            try {
                return classFileTransformers.isEmpty();
            } finally {
                classFileTransformers.put(classFileTransformer, lambdaFactory.getConstructor(Object.class, Method.class).newInstance(classFileFactory, classFileFactory.getClass().getMethod("make", Object.class, String.class, Object.class, Object.class, Object.class, Object.class, boolean.class, List.class, List.class, Collection.class)));
            }
        }
    } catch (RuntimeException exception) {
        throw exception;
    } catch (Exception exception) {
        throw new IllegalStateException("Could not register class file transformer", exception);
    }
}
Also used : ClassFileTransformer(java.lang.instrument.ClassFileTransformer) TypeDescription(net.bytebuddy.description.type.TypeDescription) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 32 with TypeDescription

use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.

the class MethodAttributeAppenderForInstrumentedMethodOtherTest method testExcludingFactory.

@Test
public void testExcludingFactory() throws Exception {
    TypeDescription typeDescription = mock(TypeDescription.class);
    assertThat(MethodAttributeAppender.ForInstrumentedMethod.EXCLUDING_RECEIVER.make(typeDescription), is((MethodAttributeAppender) MethodAttributeAppender.ForInstrumentedMethod.EXCLUDING_RECEIVER));
}
Also used : TypeDescription(net.bytebuddy.description.type.TypeDescription) Test(org.junit.Test)

Example 33 with TypeDescription

use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.

the class MethodAttributeAppenderForInstrumentedMethodOtherTest method testIncludingFactory.

@Test
public void testIncludingFactory() throws Exception {
    TypeDescription typeDescription = mock(TypeDescription.class);
    assertThat(MethodAttributeAppender.ForInstrumentedMethod.INCLUDING_RECEIVER.make(typeDescription), is((MethodAttributeAppender) MethodAttributeAppender.ForInstrumentedMethod.INCLUDING_RECEIVER));
}
Also used : TypeDescription(net.bytebuddy.description.type.TypeDescription) Test(org.junit.Test)

Example 34 with TypeDescription

use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.

the class MethodAttributeAppenderForInstrumentedMethodTest method testJdkTypeIsFiltered.

@Test
@SuppressWarnings("unchecked")
public void testJdkTypeIsFiltered() throws Exception {
    when(annotationValueFilter.isRelevant(any(AnnotationDescription.class), any(MethodDescription.InDefinedShape.class))).thenReturn(true);
    AnnotationDescription annotationDescription = mock(AnnotationDescription.class);
    TypeDescription annotationType = mock(TypeDescription.class);
    when(annotationType.getDeclaredMethods()).thenReturn(new MethodList.Empty<MethodDescription.InDefinedShape>());
    when(annotationDescription.getRetention()).thenReturn(RetentionPolicy.RUNTIME);
    when(annotationDescription.getAnnotationType()).thenReturn(annotationType);
    when(annotationType.getActualName()).thenReturn("jdk.internal.Sample");
    when(methodDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.Explicit(annotationDescription));
    when(methodDescription.getParameters()).thenReturn((ParameterList) new ParameterList.Empty<ParameterDescription>());
    when(methodDescription.getReturnType()).thenReturn(TypeDescription.Generic.VOID);
    when(methodDescription.getTypeVariables()).thenReturn(new TypeList.Generic.Empty());
    when(methodDescription.getExceptionTypes()).thenReturn(new TypeList.Generic.Empty());
    methodAttributeAppender.apply(methodVisitor, methodDescription, annotationValueFilter);
    verifyZeroInteractions(methodVisitor);
}
Also used : AnnotationDescription(net.bytebuddy.description.annotation.AnnotationDescription) TypeDescription(net.bytebuddy.description.type.TypeDescription) MethodList(net.bytebuddy.description.method.MethodList) AnnotationList(net.bytebuddy.description.annotation.AnnotationList) Test(org.junit.Test) AnnotationDescriptionBuilderTest(net.bytebuddy.description.annotation.AnnotationDescriptionBuilderTest)

Example 35 with TypeDescription

use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.

the class InvokeDynamicTest method testNegativeArgumentThrowsException.

@Test(expected = IllegalArgumentException.class)
@JavaVersionRule.Enforce(7)
public void testNegativeArgumentThrowsException() 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).withArgument(-1);
}
Also used : TypeDescription(net.bytebuddy.description.type.TypeDescription) Test(org.junit.Test)

Aggregations

TypeDescription (net.bytebuddy.description.type.TypeDescription)177 Test (org.junit.Test)155 MethodDescription (net.bytebuddy.description.method.MethodDescription)75 ByteBuddy (net.bytebuddy.ByteBuddy)26 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 MethodList (net.bytebuddy.description.method.MethodList)8 Field (java.lang.reflect.Field)7 Map (java.util.Map)7 FieldDescription (net.bytebuddy.description.field.FieldDescription)5 LoadedTypeInitializer (net.bytebuddy.implementation.LoadedTypeInitializer)5 Before (org.junit.Before)5 MethodVisitor (org.objectweb.asm.MethodVisitor)5 Serializable (java.io.Serializable)4 URLClassLoader (java.net.URLClassLoader)4 TypeList (net.bytebuddy.description.type.TypeList)4 HashMap (java.util.HashMap)3