@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())); }
@Override public void apply( MethodVisitor methodVisitor, MethodDescription methodDescription, AnnotationValueFilter annotationValueFilter) { AnnotationAppender annotationAppender = new AnnotationAppender.Default(new AnnotationAppender.Target.OnMethod(methodVisitor)); annotationAppender = methodDescription .getReturnType() .accept( AnnotationAppender.ForTypeAnnotations.ofMethodReturnType( annotationAppender, annotationValueFilter)); annotationAppender = AnnotationAppender.ForTypeAnnotations.ofTypeVariable( annotationAppender, annotationValueFilter, AnnotationAppender.ForTypeAnnotations.VARIABLE_ON_INVOKEABLE, methodDescription.getTypeVariables()); for (AnnotationDescription annotation : methodDescription.getDeclaredAnnotations()) { annotationAppender = annotationAppender.append(annotation, annotationValueFilter); } for (ParameterDescription parameterDescription : methodDescription.getParameters()) { AnnotationAppender parameterAppender = new AnnotationAppender.Default( new AnnotationAppender.Target.OnMethodParameter( methodVisitor, parameterDescription.getIndex())); parameterAppender = parameterDescription .getType() .accept( AnnotationAppender.ForTypeAnnotations.ofMethodParameterType( parameterAppender, annotationValueFilter, parameterDescription.getIndex())); for (AnnotationDescription annotation : parameterDescription.getDeclaredAnnotations()) { parameterAppender = parameterAppender.append(annotation, annotationValueFilter); } } int exceptionTypeIndex = 0; for (TypeDescription.Generic exceptionType : methodDescription.getExceptionTypes()) { annotationAppender = exceptionType.accept( AnnotationAppender.ForTypeAnnotations.ofExceptionType( annotationAppender, annotationValueFilter, exceptionTypeIndex++)); } }