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);
}
}
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));
}
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));
}
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);
}
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);
}
Aggregations