Java Code Examples for net.bytebuddy.description.method.MethodDescription#ForLoadedMethod

The following examples show how to use net.bytebuddy.description.method.MethodDescription#ForLoadedMethod . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractTypeDescriptionTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@JavaVersionRule.Enforce(14)
@Ignore("Requires preview features")
public void testRecordComponentsField() throws Exception {
    Class<?> sampleRecord = Class.forName("net.bytebuddy.test.precompiled.SampleRecord");
    @SuppressWarnings("unchecked")
    Class<? extends Annotation> typeAnnotation = (Class<? extends Annotation>) Class.forName("net.bytebuddy.test.precompiled.TypeAnnotation");
    MethodDescription.InDefinedShape value = new MethodDescription.ForLoadedMethod(typeAnnotation.getMethod("value"));
    FieldDescription fieldDescription = describe(sampleRecord).getDeclaredFields().filter(named(FOO)).getOnly();
    assertThat(fieldDescription.getDeclaredAnnotations().size(), is(1));
    assertThat(fieldDescription.getDeclaredAnnotations().getOnly().getAnnotationType().represents(SampleAnnotation.class), is(true));
    assertThat(fieldDescription.getType().asErasure().represents(List.class), is(true));
    assertThat(fieldDescription.getType().getSort(), is(TypeDefinition.Sort.PARAMETERIZED));
    assertThat(fieldDescription.getType().getDeclaredAnnotations().size(), is(1));
    assertThat(fieldDescription.getType().getDeclaredAnnotations().getOnly().getAnnotationType().represents(typeAnnotation), is(true));
    assertThat(fieldDescription.getType().getDeclaredAnnotations().getOnly().prepare(typeAnnotation).getValue(value).resolve(), is((Object) 42));
    assertThat(fieldDescription.getType().getTypeArguments().size(), is(1));
    assertThat(fieldDescription.getType().getTypeArguments().getOnly().getSort(), is(TypeDefinition.Sort.NON_GENERIC));
    assertThat(fieldDescription.getType().getTypeArguments().getOnly().asErasure().represents(String.class), is(true));
    assertThat(fieldDescription.getType().getTypeArguments().getOnly().getDeclaredAnnotations().size(), is(1));
    assertThat(fieldDescription.getType().getTypeArguments().getOnly().getDeclaredAnnotations().getOnly().getAnnotationType().represents(typeAnnotation), is(true));
    assertThat(fieldDescription.getType().getTypeArguments().getOnly().getDeclaredAnnotations().getOnly().prepare(typeAnnotation).getValue(value).resolve(), is((Object) 84));
}
 
Example 2
Source File: AbstractTypeDescriptionTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@JavaVersionRule.Enforce(14)
@Ignore("javac does not currently add type annotation meta data correctly")
public void testRecordComponentsAccessor() throws Exception {
    Class<?> sampleRecord = Class.forName("net.bytebuddy.test.precompiled.SampleRecord");
    @SuppressWarnings("unchecked")
    Class<? extends Annotation> typeAnnotation = (Class<? extends Annotation>) Class.forName("net.bytebuddy.test.precompiled.TypeAnnotation");
    MethodDescription.InDefinedShape value = new MethodDescription.ForLoadedMethod(typeAnnotation.getMethod("value"));
    MethodDescription methodDescription = describe(sampleRecord).getDeclaredMethods().filter(named(FOO)).getOnly();
    assertThat(methodDescription.getDeclaredAnnotations().size(), is(1));
    assertThat(methodDescription.getDeclaredAnnotations().getOnly().getAnnotationType().represents(SampleAnnotation.class), is(true));
    assertThat(methodDescription.getReturnType().asErasure().represents(List.class), is(true));
    assertThat(methodDescription.getReturnType().getSort(), is(TypeDefinition.Sort.PARAMETERIZED));
    assertThat(methodDescription.getReturnType().getDeclaredAnnotations().size(), is(1));
    assertThat(methodDescription.getReturnType().getDeclaredAnnotations().getOnly().getAnnotationType().represents(typeAnnotation), is(true));
    assertThat(methodDescription.getReturnType().getDeclaredAnnotations().getOnly().prepare(typeAnnotation).getValue(value).resolve(), is((Object) 42));
    assertThat(methodDescription.getReturnType().getTypeArguments().size(), is(1));
    assertThat(methodDescription.getReturnType().getTypeArguments().getOnly().getSort(), is(TypeDefinition.Sort.NON_GENERIC));
    assertThat(methodDescription.getReturnType().getTypeArguments().getOnly().asErasure().represents(String.class), is(true));
    assertThat(methodDescription.getReturnType().getTypeArguments().getOnly().getDeclaredAnnotations().size(), is(1));
    assertThat(methodDescription.getReturnType().getTypeArguments().getOnly().getDeclaredAnnotations().getOnly().getAnnotationType().represents(typeAnnotation), is(true));
    assertThat(methodDescription.getReturnType().getTypeArguments().getOnly().getDeclaredAnnotations().getOnly().prepare(typeAnnotation).getValue(value).resolve(), is((Object) 84));
}
 
Example 3
Source File: AbstractTypeDescriptionTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@JavaVersionRule.Enforce(14)
@Ignore("javac does not currently add type annotation meta data correctly")
public void testRecordComponentsConstructorParameter() throws Exception {
    Class<?> sampleRecord = Class.forName("net.bytebuddy.test.precompiled.SampleRecord");
    @SuppressWarnings("unchecked")
    Class<? extends Annotation> typeAnnotation = (Class<? extends Annotation>) Class.forName("net.bytebuddy.test.precompiled.TypeAnnotation");
    MethodDescription.InDefinedShape value = new MethodDescription.ForLoadedMethod(typeAnnotation.getMethod("value"));
    ParameterDescription parameterDescription = describe(sampleRecord).getDeclaredMethods().filter(isConstructor()).getOnly().getParameters().getOnly();
    assertThat(parameterDescription.getDeclaredAnnotations().size(), is(1));
    assertThat(parameterDescription.getDeclaredAnnotations().getOnly().getAnnotationType().represents(SampleAnnotation.class), is(true));
    assertThat(parameterDescription.getType().asErasure().represents(List.class), is(true));
    assertThat(parameterDescription.getType().getSort(), is(TypeDefinition.Sort.PARAMETERIZED));
    assertThat(parameterDescription.getType().getDeclaredAnnotations().size(), is(1));
    assertThat(parameterDescription.getType().getDeclaredAnnotations().getOnly().getAnnotationType().represents(typeAnnotation), is(true));
    assertThat(parameterDescription.getType().getDeclaredAnnotations().getOnly().prepare(typeAnnotation).getValue(value).resolve(), is((Object) 42));
    assertThat(parameterDescription.getType().getTypeArguments().size(), is(1));
    assertThat(parameterDescription.getType().getTypeArguments().getOnly().getSort(), is(TypeDefinition.Sort.NON_GENERIC));
    assertThat(parameterDescription.getType().getTypeArguments().getOnly().asErasure().represents(String.class), is(true));
    assertThat(parameterDescription.getType().getTypeArguments().getOnly().getDeclaredAnnotations().size(), is(1));
    assertThat(parameterDescription.getType().getTypeArguments().getOnly().getDeclaredAnnotations().getOnly().getAnnotationType().represents(typeAnnotation), is(true));
    assertThat(parameterDescription.getType().getTypeArguments().getOnly().getDeclaredAnnotations().getOnly().prepare(typeAnnotation).getValue(value).resolve(), is((Object) 84));
}
 
Example 4
Source File: AbstractTypeDescriptionGenericTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testNestedInnerType() throws Exception {
    TypeDescription.Generic foo = describeReturnType(NestedInnerType.InnerType.class.getDeclaredMethod(FOO));
    assertThat(foo.getSort(), is(TypeDefinition.Sort.VARIABLE));
    assertThat(foo.getSymbol(), is(T));
    assertThat(foo.getUpperBounds().size(), is(1));
    assertThat(foo.getUpperBounds().getOnly(), is(TypeDescription.Generic.OBJECT));
    assertThat(foo.getTypeVariableSource(), is((TypeVariableSource) TypeDescription.ForLoadedType.of(NestedInnerType.class)));
    TypeDescription.Generic bar = describeReturnType(NestedInnerType.InnerType.class.getDeclaredMethod(BAR));
    assertThat(bar.getSort(), is(TypeDefinition.Sort.VARIABLE));
    assertThat(bar.getSymbol(), is(S));
    assertThat(bar.getUpperBounds().size(), is(1));
    assertThat(bar.getUpperBounds().getOnly(), is(foo));
    assertThat(bar.getTypeVariableSource(), is((TypeVariableSource) TypeDescription.ForLoadedType.of(NestedInnerType.InnerType.class)));
    TypeDescription.Generic qux = describeReturnType(NestedInnerType.InnerType.class.getDeclaredMethod(QUX));
    assertThat(qux.getSort(), is(TypeDefinition.Sort.VARIABLE));
    assertThat(qux.getSymbol(), is(U));
    assertThat(qux.getUpperBounds().size(), is(1));
    assertThat(qux.getUpperBounds().getOnly(), is(bar));
    MethodDescription quxMethod = new MethodDescription.ForLoadedMethod(NestedInnerType.InnerType.class.getDeclaredMethod(QUX));
    assertThat(qux.getTypeVariableSource(), is((TypeVariableSource) quxMethod));
}
 
Example 5
Source File: CodeGenUtil.java    From curiostack with MIT License 5 votes vote down vote up
/**
 * Returns a {@link StackManipulation} that returns the {@link
 * com.google.protobuf.Descriptors.EnumDescriptor} for the given enum field.
 */
static StackManipulation getEnumDescriptor(ProtoFieldInfo info) {
  Class<?> clz = info.enumClass();
  final MethodDescription.ForLoadedMethod getDescriptor;
  try {
    getDescriptor = new MethodDescription.ForLoadedMethod(clz.getDeclaredMethod("getDescriptor"));
  } catch (NoSuchMethodException e) {
    throw new IllegalStateException("Not an enum class: " + clz, e);
  }
  return MethodInvocation.invoke(getDescriptor);
}
 
Example 6
Source File: UndertowBuilderMethodMatcherTest.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Test
public void testMatch() throws Throwable {
    ElementMatcher<MethodDescription> matcher = named("addHttpListener").and(takesArgument(2, HttpHandler.class));
    Method method = Undertow.Builder.class.getMethod("addHttpListener", int.class, String.class, HttpHandler.class);
    MethodDescription md = new MethodDescription.ForLoadedMethod(method);
    boolean r = matcher.matches(md);
    Assert.assertTrue(r);
}
 
Example 7
Source File: AbstractTypeDescriptionTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
@JavaVersionRule.Enforce(14)
@Ignore("Requires preview features")
public void testRecordComponents() throws Exception {
    Class<?> sampleRecord = Class.forName("net.bytebuddy.test.precompiled.SampleRecord");
    assertThat(describe(sampleRecord).isRecord(), is(true));
    @SuppressWarnings("unchecked")
    Class<? extends Annotation> typeAnnotation = (Class<? extends Annotation>) Class.forName("net.bytebuddy.test.precompiled.TypeAnnotation");
    MethodDescription.InDefinedShape value = new MethodDescription.ForLoadedMethod(typeAnnotation.getMethod("value"));
    RecordComponentList<RecordComponentDescription.InDefinedShape> recordComponents = describe(sampleRecord).getRecordComponents();
    assertThat(recordComponents.size(), is(1));
    assertThat(recordComponents.getOnly().getActualName(), is(FOO));
    assertThat(recordComponents.getOnly().getAccessor(), is((MethodDescription) new MethodDescription.ForLoadedMethod(sampleRecord.getMethod(FOO))));
    assertThat(recordComponents.getOnly().getDeclaringType(), is((TypeDefinition) TypeDescription.ForLoadedType.of(sampleRecord)));
    assertThat(recordComponents.getOnly().getDeclaredAnnotations().size(), is(1));
    assertThat(recordComponents.getOnly().getDeclaredAnnotations().getOnly().getAnnotationType().represents(SampleAnnotation.class), is(true));
    assertThat(recordComponents.getOnly().getType().asErasure().represents(List.class), is(true));
    assertThat(recordComponents.getOnly().getType().getSort(), is(TypeDefinition.Sort.PARAMETERIZED));
    assertThat(recordComponents.getOnly().getType().getDeclaredAnnotations().size(), is(1));
    assertThat(recordComponents.getOnly().getType().getDeclaredAnnotations().getOnly().getAnnotationType().represents(typeAnnotation), is(true));
    assertThat(recordComponents.getOnly().getType().getDeclaredAnnotations().getOnly().prepare(typeAnnotation).getValue(value).resolve(), is((Object) 42));
    assertThat(recordComponents.getOnly().getType().getTypeArguments().size(), is(1));
    assertThat(recordComponents.getOnly().getType().getTypeArguments().getOnly().getSort(), is(TypeDefinition.Sort.NON_GENERIC));
    assertThat(recordComponents.getOnly().getType().getTypeArguments().getOnly().asErasure().represents(String.class), is(true));
    assertThat(recordComponents.getOnly().getType().getTypeArguments().getOnly().getDeclaredAnnotations().size(), is(1));
    assertThat(recordComponents.getOnly().getType().getTypeArguments().getOnly().getDeclaredAnnotations().getOnly().getAnnotationType().represents(typeAnnotation), is(true));
    assertThat(recordComponents.getOnly().getType().getTypeArguments().getOnly().getDeclaredAnnotations().getOnly().prepare(typeAnnotation).getValue(value).resolve(), is((Object) 84));
}
 
Example 8
Source File: GenericSignatureResolutionTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenericMethod() throws Exception {
    DynamicType.Unloaded<?> unloaded = new ByteBuddy()
            .redefine(GenericMethod.class)
            .method(named(FOO))
            .intercept(FixedValue.nullValue())
            .make();
    Class<?> type = unloaded.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    MethodDescription createdMethod = new MethodDescription.ForLoadedMethod(type.getDeclaredMethod(FOO, Exception.class));
    MethodDescription originalMethod = new MethodDescription.ForLoadedMethod(GenericMethod.class.getDeclaredMethod(FOO, Exception.class));
    assertThat(createdMethod.getTypeVariables(), is(originalMethod.getTypeVariables()));
    assertThat(createdMethod.getReturnType(), is(originalMethod.getReturnType()));
    assertThat(createdMethod.getParameters().getOnly().getType(), is(originalMethod.getParameters().getOnly().getType()));
    assertThat(createdMethod.getExceptionTypes().getOnly(), is(originalMethod.getExceptionTypes().getOnly()));
}
 
Example 9
Source File: GenericSignatureResolutionTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenericMethodWithoutGenericExceptionTypes() throws Exception {
    DynamicType.Unloaded<?> unloaded = new ByteBuddy()
            .redefine(GenericMethod.class)
            .method(named(BAR))
            .intercept(FixedValue.nullValue())
            .make();
    Class<?> type = unloaded.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    MethodDescription createdMethod = new MethodDescription.ForLoadedMethod(type.getDeclaredMethod(BAR, Object.class));
    MethodDescription originalMethod = new MethodDescription.ForLoadedMethod(GenericMethod.class.getDeclaredMethod(BAR, Object.class));
    assertThat(createdMethod.getTypeVariables(), is(originalMethod.getTypeVariables()));
    assertThat(createdMethod.getReturnType(), is(originalMethod.getReturnType()));
    assertThat(createdMethod.getParameters().getOnly().getType(), is(originalMethod.getParameters().getOnly().getType()));
    assertThat(createdMethod.getExceptionTypes().getOnly(), is(originalMethod.getExceptionTypes().getOnly()));
}
 
Example 10
Source File: Play2xInstrumentationTest.java    From skywalking with Apache License 2.0 4 votes vote down vote up
@Test
public void testMethodMatch() throws Exception {
    final ElementMatcher<MethodDescription> matcher = Play2xInstrumentation.getFiltersMethodMatcher();
    final MethodDescription method = new MethodDescription.ForLoadedMethod(EnabledFilters.class.getMethod("filters"));
    Assert.assertTrue(matcher.matches(method));
}
 
Example 11
Source File: RecordComponentDescription.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
@Override
public MethodDescription.InDefinedShape getAccessor() {
    return new MethodDescription.ForLoadedMethod(DISPATCHER.getAccessor(recordComponent));
}
 
Example 12
Source File: TypePoolDefaultLazyMethodListTest.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
protected MethodDescription.InDefinedShape asElement(Method element) {
    return new MethodDescription.ForLoadedMethod(element);
}