Skip to content

Commit 6e88849

Browse files
committed
Add annotation removal.
1 parent 11c76cc commit 6e88849

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

byte-buddy-dep/src/main/java/net/bytebuddy/asm/AnnotationRemoval.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public MethodVisitor visitMethod(int modifiers, String internalName, String desc
169169
Map<Integer, Map<String, AnnotationDescription>> mappedParameterAnnotations = new HashMap<Integer, Map<String, AnnotationDescription>>();
170170
for (ParameterDescription parameter : methodDescription.getParameters()) {
171171
Map<String, AnnotationDescription> mappedAnnotations = new HashMap<String, AnnotationDescription>();
172-
for (AnnotationDescription annotation : methodDescription.getDeclaredAnnotations()) {
172+
for (AnnotationDescription annotation : parameter.getDeclaredAnnotations()) {
173173
mappedAnnotations.put(annotation.getAnnotationType().getDescriptor(), annotation);
174174
}
175175
mappedParameterAnnotations.put(parameter.getIndex(), mappedAnnotations);

byte-buddy-dep/src/test/java/net/bytebuddy/asm/AnnotationRemovalTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public void testRemoval() throws Exception {
2828
assertThat(type.getAnnotations().length, is(0));
2929
assertThat(type.getDeclaredField(FOO).getAnnotations().length, is(0));
3030
assertThat(type.getDeclaredConstructor().getAnnotations().length, is(0));
31+
assertThat(type.getDeclaredMethod(FOO, Void.class).getParameterAnnotations()[0].length, is(0));
3132
assertThat(type.getDeclaredMethod(BAR).getAnnotations().length, is(0));
3233
}
3334

@@ -42,6 +43,7 @@ public void testRemovalOnType() throws Exception {
4243
assertThat(type.getAnnotations().length, is(0));
4344
assertThat(type.getDeclaredField(FOO).getAnnotations().length, is(1));
4445
assertThat(type.getDeclaredConstructor().getAnnotations().length, is(1));
46+
assertThat(type.getDeclaredMethod(FOO, Void.class).getParameterAnnotations()[0].length, is(1));
4547
assertThat(type.getDeclaredMethod(BAR).getAnnotations().length, is(1));
4648
}
4749

@@ -56,6 +58,7 @@ public void testRemovalOnField() throws Exception {
5658
assertThat(type.getAnnotations().length, is(1));
5759
assertThat(type.getDeclaredField(FOO).getAnnotations().length, is(0));
5860
assertThat(type.getDeclaredConstructor().getAnnotations().length, is(1));
61+
assertThat(type.getDeclaredMethod(FOO, Void.class).getParameterAnnotations()[0].length, is(1));
5962
assertThat(type.getDeclaredMethod(BAR).getAnnotations().length, is(1));
6063
}
6164

@@ -70,6 +73,7 @@ public void testRemovalOnConstructor() throws Exception {
7073
assertThat(type.getAnnotations().length, is(1));
7174
assertThat(type.getDeclaredField(FOO).getAnnotations().length, is(1));
7275
assertThat(type.getDeclaredConstructor().getAnnotations().length, is(0));
76+
assertThat(type.getDeclaredMethod(FOO, Void.class).getParameterAnnotations()[0].length, is(1));
7377
assertThat(type.getDeclaredMethod(BAR).getAnnotations().length, is(1));
7478
}
7579

@@ -84,9 +88,25 @@ public void testRemovalOnMethod() throws Exception {
8488
assertThat(type.getAnnotations().length, is(1));
8589
assertThat(type.getDeclaredField(FOO).getAnnotations().length, is(1));
8690
assertThat(type.getDeclaredConstructor().getAnnotations().length, is(1));
91+
assertThat(type.getDeclaredMethod(FOO, Void.class).getParameterAnnotations()[0].length, is(1));
8792
assertThat(type.getDeclaredMethod(BAR).getAnnotations().length, is(0));
8893
}
8994

95+
@Test
96+
public void testRemovalOnMethodParameter() throws Exception {
97+
Class<?> type = new ByteBuddy()
98+
.redefine(Sample.class)
99+
.visit(AnnotationRemoval.strip(ElementMatchers.annotationType(named(SampleAnnotation.class.getName()))).onMethods(named(FOO)))
100+
.make()
101+
.load(SampleAnnotation.class.getClassLoader(), ClassLoadingStrategy.Default.CHILD_FIRST_PERSISTENT)
102+
.getLoaded();
103+
assertThat(type.getAnnotations().length, is(1));
104+
assertThat(type.getDeclaredField(FOO).getAnnotations().length, is(1));
105+
assertThat(type.getDeclaredConstructor().getAnnotations().length, is(1));
106+
assertThat(type.getDeclaredMethod(FOO, Void.class).getParameterAnnotations()[0].length, is(0));
107+
assertThat(type.getDeclaredMethod(BAR).getAnnotations().length, is(1));
108+
}
109+
90110
@SampleAnnotation
91111
private static class Sample {
92112

0 commit comments

Comments
 (0)