From 5c0c4382ac86a248533aefb4f5a57ed74ded77a5 Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Wed, 13 Dec 2023 12:35:24 +0100 Subject: [PATCH] Adjust modifier resolution for native methods. --- .../description/method/MethodDescription.java | 11 ++++++++--- .../method/AbstractMethodDescriptionTest.java | 10 +++++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/byte-buddy-dep/src/main/java/net/bytebuddy/description/method/MethodDescription.java b/byte-buddy-dep/src/main/java/net/bytebuddy/description/method/MethodDescription.java index 3983d11bfa0..0e5173ab828 100644 --- a/byte-buddy-dep/src/main/java/net/bytebuddy/description/method/MethodDescription.java +++ b/byte-buddy-dep/src/main/java/net/bytebuddy/description/method/MethodDescription.java @@ -589,9 +589,14 @@ public int getActualModifiers() { * {@inheritDoc} */ public int getActualModifiers(boolean manifest) { - return manifest - ? getActualModifiers() & ~(Opcodes.ACC_ABSTRACT | Opcodes.ACC_NATIVE) - : getActualModifiers() & ~Opcodes.ACC_NATIVE | Opcodes.ACC_ABSTRACT; + int modifiers = getActualModifiers(); + if (manifest) { + return modifiers & ~(Opcodes.ACC_ABSTRACT | Opcodes.ACC_NATIVE); + } else if ((modifiers & (Opcodes.ACC_NATIVE | Opcodes.ACC_ABSTRACT)) == 0) { + return modifiers | Opcodes.ACC_ABSTRACT; + } else { + return modifiers; + } } /** diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/description/method/AbstractMethodDescriptionTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/description/method/AbstractMethodDescriptionTest.java index 37f968b694b..c26a8f41cd9 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/description/method/AbstractMethodDescriptionTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/description/method/AbstractMethodDescriptionTest.java @@ -40,7 +40,7 @@ public abstract class AbstractMethodDescriptionTest { @Rule public MethodRule javaVersionRule = new JavaVersionRule(); - protected Method firstMethod, secondMethod, thirdMethod, genericMethod, genericMethodWithRawException, genericMethodWithTypeVariable; + protected Method firstMethod, secondMethod, thirdMethod, genericMethod, genericMethodWithRawException, genericMethodWithTypeVariable, nativeMethod; protected Constructor firstConstructor, secondConstructor; @@ -72,6 +72,7 @@ public void setUp() throws Exception { genericMethod = GenericMethod.class.getDeclaredMethod("foo", Exception.class); genericMethodWithRawException = GenericMethod.class.getDeclaredMethod("bar", Exception.class); genericMethodWithTypeVariable = GenericMethod.class.getDeclaredMethod("qux"); + nativeMethod = NativeMethod.class.getDeclaredMethod("foo"); } @Test @@ -735,6 +736,8 @@ public void testGetActualModifiers() throws Exception { assertThat(describe(DeprecationSample.class.getDeclaredMethod("foo")).getActualModifiers(), is(Opcodes.ACC_PRIVATE | Opcodes.ACC_DEPRECATED)); assertThat(describe(firstMethod).getActualModifiers(true, Visibility.PUBLIC), is(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC)); assertThat(describe(secondMethod).getActualModifiers(false, Visibility.PRIVATE), is(Opcodes.ACC_PROTECTED | Opcodes.ACC_ABSTRACT)); + assertThat(describe(nativeMethod).getActualModifiers(false, Visibility.PRIVATE), is(Opcodes.ACC_NATIVE)); + assertThat(describe(nativeMethod).getActualModifiers(true, Visibility.PRIVATE), is(0)); } @Test @@ -960,6 +963,11 @@ void qux() { } } + static class NativeMethod { + + native void foo(); + } + private static class DeprecationSample { @Deprecated