From ca9d3ab5b238d81fd09d62482b21132b019a68c1 Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Thu, 5 Aug 2021 22:07:10 +0200 Subject: [PATCH] Use reflection for security manager invocations and prefer getTargetException over getCause for invocation target exceptions. --- .../net/bytebuddy/agent/ByteBuddyAgent.java | 6 +- .../java/net/bytebuddy/agent/Installer.java | 2 +- .../net/bytebuddy/agent/AttacherTest.java | 2 +- .../bytebuddy/agent/ByteBuddyAgentTest.java | 2 +- .../net/bytebuddy/agent/InstallerTest.java | 2 +- .../android/AndroidClassLoadingStrategy.java | 6 +- ...sionBenchmarkByteBuddyInterceptorTest.java | 4 +- .../bytebuddy/agent/builder/AgentBuilder.java | 2 +- .../main/java/net/bytebuddy/build/Plugin.java | 4 +- .../annotation/AnnotationDescription.java | 6 +- .../net/bytebuddy/dynamic/NexusAccessor.java | 4 +- .../dynamic/loading/ByteArrayClassLoader.java | 6 +- .../dynamic/loading/ClassInjector.java | 156 ++++++++++-------- .../utility/dispatcher/JavaDispatcher.java | 2 +- .../asm/AdviceNoRegularReturnTest.java | 6 +- ...AdviceNoRegularReturnWithinAdviceTest.java | 2 +- .../java/net/bytebuddy/asm/AdviceTest.java | 42 ++--- .../net/bytebuddy/asm/AdviceTypeTest.java | 2 +- .../build/CachedReturnPluginTest.java | 4 +- .../AnnotationDescriptionLatentTest.java | 2 +- .../net/bytebuddy/dynamic/TargetTypeTest.java | 2 +- .../IgnoreForBindingVerifierTest.java | 2 +- .../annotation/RuntimeTypeVerifierTest.java | 2 +- .../matcher/ElementMatchersTest.java | 2 +- .../bytebuddy/utility/CompoundListTest.java | 2 +- .../build/maven/ByteBuddyMojoTest.java | 2 +- 26 files changed, 150 insertions(+), 124 deletions(-) diff --git a/byte-buddy-agent/src/main/java/net/bytebuddy/agent/ByteBuddyAgent.java b/byte-buddy-agent/src/main/java/net/bytebuddy/agent/ByteBuddyAgent.java index df17ff21c81..90a06e0343c 100644 --- a/byte-buddy-agent/src/main/java/net/bytebuddy/agent/ByteBuddyAgent.java +++ b/byte-buddy-agent/src/main/java/net/bytebuddy/agent/ByteBuddyAgent.java @@ -196,7 +196,7 @@ private static T doPrivileged(PrivilegedAction action) { } catch (ClassNotFoundException ignored) { return action.run(); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Failed to invoke access controller", exception.getCause()); + throw new IllegalStateException("Failed to invoke access controller", exception.getTargetException()); } catch (IllegalAccessException exception) { throw new IllegalStateException("Failed to access access controller", exception); } catch (NoSuchMethodException exception) { @@ -1383,7 +1383,7 @@ public String resolve() { } catch (IllegalAccessException exception) { throw new IllegalStateException("Cannot access Java 9 process API", exception); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Error when accessing Java 9 process API", exception.getCause()); + throw new IllegalStateException("Error when accessing Java 9 process API", exception.getTargetException()); } } } @@ -1655,7 +1655,7 @@ public boolean requiresExternalAttachment(String processId) { } catch (IllegalAccessException exception) { throw new IllegalStateException("Cannot access Java 9 process API", exception); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Error when accessing Java 9 process API", exception.getCause()); + throw new IllegalStateException("Error when accessing Java 9 process API", exception.getTargetException()); } } } diff --git a/byte-buddy-agent/src/main/java/net/bytebuddy/agent/Installer.java b/byte-buddy-agent/src/main/java/net/bytebuddy/agent/Installer.java index ee3473638d0..78127489dee 100644 --- a/byte-buddy-agent/src/main/java/net/bytebuddy/agent/Installer.java +++ b/byte-buddy-agent/src/main/java/net/bytebuddy/agent/Installer.java @@ -63,7 +63,7 @@ public static Instrumentation getInstrumentation() { } catch (NoSuchMethodException ignored) { /* security manager not available on current VM */ } catch (InvocationTargetException exception) { - Throwable cause = exception.getCause(); + Throwable cause = exception.getTargetException(); if (cause instanceof RuntimeException) { throw (RuntimeException) cause; } else { diff --git a/byte-buddy-agent/src/test/java/net/bytebuddy/agent/AttacherTest.java b/byte-buddy-agent/src/test/java/net/bytebuddy/agent/AttacherTest.java index 7a068e3c4b9..a2b56738e2c 100644 --- a/byte-buddy-agent/src/test/java/net/bytebuddy/agent/AttacherTest.java +++ b/byte-buddy-agent/src/test/java/net/bytebuddy/agent/AttacherTest.java @@ -46,7 +46,7 @@ public void testConstructorThrowsException() throws Exception { constructor.newInstance(); fail(); } catch (InvocationTargetException exception) { - throw (Exception) exception.getCause(); + throw (Exception) exception.getTargetException(); } } diff --git a/byte-buddy-agent/src/test/java/net/bytebuddy/agent/ByteBuddyAgentTest.java b/byte-buddy-agent/src/test/java/net/bytebuddy/agent/ByteBuddyAgentTest.java index c930d8029cd..34348c4c9dd 100644 --- a/byte-buddy-agent/src/test/java/net/bytebuddy/agent/ByteBuddyAgentTest.java +++ b/byte-buddy-agent/src/test/java/net/bytebuddy/agent/ByteBuddyAgentTest.java @@ -61,7 +61,7 @@ public void testConstructorThrowsException() throws Exception { constructor.newInstance(); fail(); } catch (InvocationTargetException exception) { - throw (Exception) exception.getCause(); + throw (Exception) exception.getTargetException(); } } } diff --git a/byte-buddy-agent/src/test/java/net/bytebuddy/agent/InstallerTest.java b/byte-buddy-agent/src/test/java/net/bytebuddy/agent/InstallerTest.java index a92dd6e557e..bc43f333b75 100644 --- a/byte-buddy-agent/src/test/java/net/bytebuddy/agent/InstallerTest.java +++ b/byte-buddy-agent/src/test/java/net/bytebuddy/agent/InstallerTest.java @@ -86,7 +86,7 @@ public void testConstructorThrowsException() throws Exception { constructor.newInstance(); fail(); } catch (InvocationTargetException exception) { - throw (Exception) exception.getCause(); + throw (Exception) exception.getTargetException(); } } } diff --git a/byte-buddy-android/src/main/java/net/bytebuddy/android/AndroidClassLoadingStrategy.java b/byte-buddy-android/src/main/java/net/bytebuddy/android/AndroidClassLoadingStrategy.java index d9fc34e50ab..0847cce890f 100644 --- a/byte-buddy-android/src/main/java/net/bytebuddy/android/AndroidClassLoadingStrategy.java +++ b/byte-buddy-android/src/main/java/net/bytebuddy/android/AndroidClassLoadingStrategy.java @@ -463,7 +463,7 @@ public ClassDefItem translate(DirectClassFile directClassFile, } catch (IllegalAccessException exception) { throw new IllegalStateException("Cannot access an Android dex file translation method", exception); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Cannot invoke Android dex file translation method", exception.getCause()); + throw new IllegalStateException("Cannot invoke Android dex file translation method", exception.getTargetException()); } } @@ -526,7 +526,7 @@ public ClassDefItem translate(DirectClassFile directClassFile, } catch (InstantiationException exception) { throw new IllegalStateException("Cannot instantiate dex context", exception); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Cannot invoke Android dex file translation method", exception.getCause()); + throw new IllegalStateException("Cannot invoke Android dex file translation method", exception.getTargetException()); } } @@ -772,7 +772,7 @@ public dalvik.system.DexFile loadDex(File privateDirectory, } catch (IllegalAccessException exception) { throw new IllegalStateException("Cannot access BaseDexClassLoader#addDexPath(String, boolean)", exception); } catch (InvocationTargetException exception) { - Throwable cause = exception.getCause(); + Throwable cause = exception.getTargetException(); if (cause instanceof IOException) { throw (IOException) cause; } else { diff --git a/byte-buddy-benchmark/src/test/java/net/bytebuddy/benchmark/ClassByExtensionBenchmarkByteBuddyInterceptorTest.java b/byte-buddy-benchmark/src/test/java/net/bytebuddy/benchmark/ClassByExtensionBenchmarkByteBuddyInterceptorTest.java index a7d95f41b69..b0c31800f9f 100644 --- a/byte-buddy-benchmark/src/test/java/net/bytebuddy/benchmark/ClassByExtensionBenchmarkByteBuddyInterceptorTest.java +++ b/byte-buddy-benchmark/src/test/java/net/bytebuddy/benchmark/ClassByExtensionBenchmarkByteBuddyInterceptorTest.java @@ -17,7 +17,7 @@ public void testProxyInterceptor() throws Exception { constructor.newInstance(); fail(); } catch (InvocationTargetException exception) { - throw (UnsupportedOperationException) exception.getCause(); + throw (UnsupportedOperationException) exception.getTargetException(); } } @@ -29,7 +29,7 @@ public void testAccessorInterceptor() throws Exception { constructor.newInstance(); fail(); } catch (InvocationTargetException exception) { - throw (UnsupportedOperationException) exception.getCause(); + throw (UnsupportedOperationException) exception.getTargetException(); } } } diff --git a/byte-buddy-dep/src/main/java/net/bytebuddy/agent/builder/AgentBuilder.java b/byte-buddy-dep/src/main/java/net/bytebuddy/agent/builder/AgentBuilder.java index fd48a9d20b6..73da134fb79 100644 --- a/byte-buddy-dep/src/main/java/net/bytebuddy/agent/builder/AgentBuilder.java +++ b/byte-buddy-dep/src/main/java/net/bytebuddy/agent/builder/AgentBuilder.java @@ -11143,7 +11143,7 @@ public ResettableClassFileTransformer make(ByteBuddy byteBuddy, } catch (InstantiationException exception) { throw new IllegalStateException("Cannot instantiate " + executingTransformer.getDeclaringClass(), exception); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Cannot invoke " + executingTransformer, exception.getCause()); + throw new IllegalStateException("Cannot invoke " + executingTransformer, exception.getTargetException()); } } } diff --git a/byte-buddy-dep/src/main/java/net/bytebuddy/build/Plugin.java b/byte-buddy-dep/src/main/java/net/bytebuddy/build/Plugin.java index 0797bfa6644..c6199ecdcfb 100644 --- a/byte-buddy-dep/src/main/java/net/bytebuddy/build/Plugin.java +++ b/byte-buddy-dep/src/main/java/net/bytebuddy/build/Plugin.java @@ -409,7 +409,7 @@ public Plugin instantiate() { } catch (IllegalAccessException exception) { throw new IllegalStateException("Failed to access " + constructor, exception); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Error during construction of" + constructor, exception.getCause()); + throw new IllegalStateException("Error during construction of" + constructor, exception.getTargetException()); } } } @@ -716,7 +716,7 @@ public Resolution resolve(int index, Class type) { } catch (IllegalAccessException exception) { throw new IllegalStateException(exception); } catch (InvocationTargetException exception) { - throw new IllegalStateException(exception.getCause()); + throw new IllegalStateException(exception.getTargetException()); } catch (NoSuchMethodException ignored) { return Resolution.Unresolved.INSTANCE; } diff --git a/byte-buddy-dep/src/main/java/net/bytebuddy/description/annotation/AnnotationDescription.java b/byte-buddy-dep/src/main/java/net/bytebuddy/description/annotation/AnnotationDescription.java index 76a2eaf6463..345a0e2fdb4 100644 --- a/byte-buddy-dep/src/main/java/net/bytebuddy/description/annotation/AnnotationDescription.java +++ b/byte-buddy-dep/src/main/java/net/bytebuddy/description/annotation/AnnotationDescription.java @@ -604,7 +604,7 @@ public S load() { try { annotationValues.put(property.getName(), asValue(property.invoke(annotation, NO_ARGUMENT), property.getReturnType())); } catch (InvocationTargetException exception) { - Throwable cause = exception.getCause(); + Throwable cause = exception.getTargetException(); if (cause instanceof TypeNotPresentException) { annotationValues.put(property.getName(), new AnnotationValue.ForMissingType(((TypeNotPresentException) cause).typeName())); } else if (cause instanceof EnumConstantNotPresentException) { @@ -616,7 +616,7 @@ public S load() { new MethodDescription.ForLoadedMethod(((AnnotationTypeMismatchException) cause).element()), ((AnnotationTypeMismatchException) cause).foundType())); } else if (!(cause instanceof IncompleteAnnotationException)) { - throw new IllegalStateException("Cannot read " + property, exception.getCause()); + throw new IllegalStateException("Cannot read " + property, cause); } } catch (IllegalAccessException exception) { throw new IllegalStateException("Cannot access " + property, exception); @@ -692,7 +692,7 @@ public S load() { } return asValue(method.invoke(annotation, NO_ARGUMENT), method.getReturnType()).filter(property); } catch (InvocationTargetException exception) { - Throwable cause = exception.getCause(); + Throwable cause = exception.getTargetException(); if (cause instanceof TypeNotPresentException) { return new AnnotationValue.ForMissingType(((TypeNotPresentException) cause).typeName()); } else if (cause instanceof EnumConstantNotPresentException) { diff --git a/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/NexusAccessor.java b/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/NexusAccessor.java index 8c9f93f7b6f..6ea2fa51505 100644 --- a/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/NexusAccessor.java +++ b/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/NexusAccessor.java @@ -301,7 +301,7 @@ public void clean(Reference reference) { } catch (IllegalAccessException exception) { throw new IllegalStateException("Cannot access: " + clean, exception); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Cannot invoke: " + clean, exception.getCause()); + throw new IllegalStateException("Cannot invoke: " + clean, exception.getTargetException()); } } @@ -318,7 +318,7 @@ public void register(String name, } catch (IllegalAccessException exception) { throw new IllegalStateException("Cannot access: " + register, exception); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Cannot invoke: " + register, exception.getCause()); + throw new IllegalStateException("Cannot invoke: " + register, exception.getTargetException()); } } } diff --git a/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/loading/ByteArrayClassLoader.java b/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/loading/ByteArrayClassLoader.java index d032b8b9083..a4b671cbc9f 100644 --- a/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/loading/ByteArrayClassLoader.java +++ b/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/loading/ByteArrayClassLoader.java @@ -549,7 +549,7 @@ public Object getClassLoadingLock(ByteArrayClassLoader classLoader, String name) } catch (IllegalAccessException exception) { throw new IllegalStateException("Cannot access class loading lock for " + name + " on " + classLoader, exception); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Error when getting " + name + " on " + classLoader, exception); + throw new IllegalStateException("Error when getting " + name + " on " + classLoader, exception.getTargetException()); } } @@ -617,7 +617,7 @@ public Object getClassLoadingLock(ByteArrayClassLoader classLoader, String name) } catch (IllegalAccessException exception) { throw new IllegalStateException("Cannot access class loading lock for " + name + " on " + classLoader, exception); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Error when getting " + name + " on " + classLoader, exception); + throw new IllegalStateException("Error when getting " + name + " on " + classLoader, exception.getTargetException()); } } } @@ -766,7 +766,7 @@ public Package apply(ByteArrayClassLoader classLoader, String name) { } catch (IllegalAccessException exception) { throw new IllegalStateException("Cannot access " + getDefinedPackage, exception); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Cannot invoke " + getDefinedPackage, exception.getCause()); + throw new IllegalStateException("Cannot invoke " + getDefinedPackage, exception.getTargetException()); } } } diff --git a/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/loading/ClassInjector.java b/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/loading/ClassInjector.java index 12fa41e06bd..f32a0437bbe 100644 --- a/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/loading/ClassInjector.java +++ b/byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/loading/ClassInjector.java @@ -136,9 +136,9 @@ class UsingReflection extends AbstractBase { private static final System SYSTEM = doPrivileged(JavaDispatcher.of(System.class)); /** - * A proxy for {@code java.lang.SecurityManager} if available. + * The {@code java.lang.SecurityManager#checkPermission} method or {@code null} if not available. */ - private static final SecurityManager SECURITY_MANAGER = doPrivileged(JavaDispatcher.of(SecurityManager.class)); + private static final Method CHECK_PERMISSION = doPrivileged(CheckPermissionAction.INSTANCE); /** * The class loader into which the classes are to be injected. @@ -515,7 +515,7 @@ public Initializable run() { return Direct.make(); } } catch (InvocationTargetException exception) { - return new Initializable.Unavailable(exception.getCause().getMessage()); + return new Initializable.Unavailable(exception.getTargetException().getMessage()); } catch (Exception exception) { return new Initializable.Unavailable(exception.getMessage()); } @@ -641,7 +641,9 @@ public Dispatcher initialize() { Object securityManager = SYSTEM.getSecurityManager(); if (securityManager != null) { try { - SECURITY_MANAGER.checkPermission(securityManager, SUPPRESS_ACCESS_CHECKS); + CHECK_PERMISSION.invoke(securityManager, SUPPRESS_ACCESS_CHECKS); + } catch (InvocationTargetException exception) { + return new Dispatcher.Unavailable(exception.getTargetException().getMessage()); } catch (Exception exception) { return new Dispatcher.Unavailable(exception.getMessage()); } @@ -658,7 +660,7 @@ public Class findClass(ClassLoader classLoader, String name) { } catch (IllegalAccessException exception) { throw new IllegalStateException("Could not access java.lang.ClassLoader#findClass", exception); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Error invoking java.lang.ClassLoader#findClass", exception.getCause()); + throw new IllegalStateException("Error invoking java.lang.ClassLoader#findClass", exception.getTargetException()); } } @@ -671,7 +673,7 @@ public Class defineClass(ClassLoader classLoader, String name, byte[] binaryR } catch (IllegalAccessException exception) { throw new IllegalStateException("Could not access java.lang.ClassLoader#defineClass", exception); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Error invoking java.lang.ClassLoader#defineClass", exception.getCause()); + throw new IllegalStateException("Error invoking java.lang.ClassLoader#defineClass", exception.getTargetException()); } } @@ -685,7 +687,7 @@ public Package getDefinedPackage(ClassLoader classLoader, String name) { } catch (IllegalAccessException exception) { throw new IllegalStateException("Could not access java.lang.ClassLoader#getDefinedPackage", exception); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Error invoking java.lang.ClassLoader#getDefinedPackage", exception.getCause()); + throw new IllegalStateException("Error invoking java.lang.ClassLoader#getDefinedPackage", exception.getTargetException()); } } @@ -698,7 +700,7 @@ public Package getPackage(ClassLoader classLoader, String name) { } catch (IllegalAccessException exception) { throw new IllegalStateException("Could not access java.lang.ClassLoader#getPackage", exception); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Error invoking java.lang.ClassLoader#getPackage", exception.getCause()); + throw new IllegalStateException("Error invoking java.lang.ClassLoader#getPackage", exception.getTargetException()); } } @@ -727,7 +729,7 @@ public Package definePackage(ClassLoader classLoader, } catch (IllegalAccessException exception) { throw new IllegalStateException("Could not access java.lang.ClassLoader#definePackage", exception); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Error invoking java.lang.ClassLoader#definePackage", exception.getCause()); + throw new IllegalStateException("Error invoking java.lang.ClassLoader#definePackage", exception.getTargetException()); } } @@ -771,7 +773,7 @@ public Object getClassLoadingLock(ClassLoader classLoader, String name) { } catch (IllegalAccessException exception) { throw new IllegalStateException("Could not access java.lang.ClassLoader#getClassLoadingLock", exception); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Error invoking java.lang.ClassLoader#getClassLoadingLock", exception.getCause()); + throw new IllegalStateException("Error invoking java.lang.ClassLoader#getClassLoadingLock", exception.getTargetException()); } } } @@ -978,7 +980,9 @@ public Dispatcher initialize() { Object securityManager = SYSTEM.getSecurityManager(); if (securityManager != null) { try { - SECURITY_MANAGER.checkPermission(securityManager, SUPPRESS_ACCESS_CHECKS); + CHECK_PERMISSION.invoke(securityManager, SUPPRESS_ACCESS_CHECKS); + } catch (InvocationTargetException exception) { + return new Dispatcher.Unavailable(exception.getTargetException().getMessage()); } catch (Exception exception) { return new Dispatcher.Unavailable(exception.getMessage()); } @@ -993,9 +997,9 @@ public Object getClassLoadingLock(ClassLoader classLoader, String name) { try { return getClassLoadingLock.invoke(accessor, classLoader, name); } catch (IllegalAccessException exception) { - throw new IllegalStateException("Could not access (accessor)::getClassLoadingLock", exception); + throw new IllegalStateException("Could not access ClassLoader::getClassLoadingLock", exception); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Error invoking (accessor)::getClassLoadingLock", exception.getCause()); + throw new IllegalStateException("Error invoking ClassLoader::getClassLoadingLock", exception.getTargetException()); } } @@ -1006,9 +1010,9 @@ public Class findClass(ClassLoader classLoader, String name) { try { return (Class) findLoadedClass.invoke(accessor, classLoader, name); } catch (IllegalAccessException exception) { - throw new IllegalStateException("Could not access (accessor)::findLoadedClass", exception); + throw new IllegalStateException("Could not access ClassLoader::findLoadedClass", exception); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Error invoking (accessor)::findLoadedClass", exception.getCause()); + throw new IllegalStateException("Error invoking ClassLoader::findLoadedClass", exception.getTargetException()); } } @@ -1019,9 +1023,9 @@ public Class defineClass(ClassLoader classLoader, String name, byte[] binaryR try { return (Class) defineClass.invoke(accessor, classLoader, name, binaryRepresentation, 0, binaryRepresentation.length, protectionDomain); } catch (IllegalAccessException exception) { - throw new IllegalStateException("Could not access (accessor)::defineClass", exception); + throw new IllegalStateException("Could not access ClassLoader::defineClass", exception); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Error invoking (accessor)::defineClass", exception.getCause()); + throw new IllegalStateException("Error invoking ClassLoader::defineClass", exception.getTargetException()); } } @@ -1035,9 +1039,9 @@ public Package getDefinedPackage(ClassLoader classLoader, String name) { try { return (Package) getDefinedPackage.invoke(accessor, classLoader, name); } catch (IllegalAccessException exception) { - throw new IllegalStateException("Could not access (accessor)::getDefinedPackage", exception); + throw new IllegalStateException("Could not access ClassLoader::getDefinedPackage", exception); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Error invoking (accessor)::getDefinedPackage", exception.getCause()); + throw new IllegalStateException("Error invoking ClassLoader::getDefinedPackage", exception.getTargetException()); } } @@ -1048,9 +1052,9 @@ public Package getPackage(ClassLoader classLoader, String name) { try { return (Package) getPackage.invoke(accessor, classLoader, name); } catch (IllegalAccessException exception) { - throw new IllegalStateException("Could not access (accessor)::getPackage", exception); + throw new IllegalStateException("Could not access ClassLoader::getPackage", exception); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Error invoking (accessor)::getPackage", exception.getCause()); + throw new IllegalStateException("Error invoking ClassLoader::getPackage", exception.getTargetException()); } } @@ -1078,9 +1082,9 @@ public Package definePackage(ClassLoader classLoader, implementationVendor, sealBase); } catch (IllegalAccessException exception) { - throw new IllegalStateException("Could not access (accessor)::definePackage", exception); + throw new IllegalStateException("Could not access ClassLoader::definePackage", exception); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Error invoking (accessor)::definePackage", exception.getCause()); + throw new IllegalStateException("Error invoking ClassLoader::definePackage", exception.getTargetException()); } } } @@ -1233,7 +1237,9 @@ public Dispatcher initialize() { Object securityManager = SYSTEM.getSecurityManager(); if (securityManager != null) { try { - SECURITY_MANAGER.checkPermission(securityManager, SUPPRESS_ACCESS_CHECKS); + CHECK_PERMISSION.invoke(securityManager, SUPPRESS_ACCESS_CHECKS); + } catch (InvocationTargetException exception) { + return new Dispatcher.Unavailable(exception.getTargetException().getMessage()); } catch (Exception exception) { return new Dispatcher.Unavailable(exception.getMessage()); } @@ -1250,7 +1256,7 @@ public Class findClass(ClassLoader classLoader, String name) { } catch (IllegalAccessException exception) { throw new IllegalStateException("Could not access java.lang.ClassLoader#findClass", exception); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Error invoking java.lang.ClassLoader#findClass", exception.getCause()); + throw new IllegalStateException("Error invoking java.lang.ClassLoader#findClass", exception.getTargetException()); } } @@ -1263,7 +1269,7 @@ public Class defineClass(ClassLoader classLoader, String name, byte[] binaryR } catch (IllegalAccessException exception) { throw new IllegalStateException("Could not access java.lang.ClassLoader#defineClass", exception); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Error invoking java.lang.ClassLoader#defineClass", exception.getCause()); + throw new IllegalStateException("Error invoking java.lang.ClassLoader#defineClass", exception.getTargetException()); } } @@ -1277,7 +1283,7 @@ public Package getDefinedPackage(ClassLoader classLoader, String name) { } catch (IllegalAccessException exception) { throw new IllegalStateException("Could not access java.lang.ClassLoader#getDefinedPackage", exception); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Error invoking java.lang.ClassLoader#getDefinedPackage", exception.getCause()); + throw new IllegalStateException("Error invoking java.lang.ClassLoader#getDefinedPackage", exception.getTargetException()); } } @@ -1290,7 +1296,7 @@ public Package getPackage(ClassLoader classLoader, String name) { } catch (IllegalAccessException exception) { throw new IllegalStateException("Could not access java.lang.ClassLoader#getPackage", exception); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Error invoking java.lang.ClassLoader#getPackage", exception.getCause()); + throw new IllegalStateException("Error invoking java.lang.ClassLoader#getPackage", exception.getTargetException()); } } @@ -1319,7 +1325,7 @@ public Package definePackage(ClassLoader classLoader, } catch (IllegalAccessException exception) { throw new IllegalStateException("Could not access java.lang.ClassLoader#definePackage", exception); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Error invoking java.lang.ClassLoader#definePackage", exception.getCause()); + throw new IllegalStateException("Error invoking java.lang.ClassLoader#definePackage", exception.getTargetException()); } } @@ -1363,7 +1369,7 @@ public Object getClassLoadingLock(ClassLoader classLoader, String name) { } catch (IllegalAccessException exception) { throw new IllegalStateException("Could not access java.lang.ClassLoader#getClassLoadingLock", exception); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Error invoking java.lang.ClassLoader#getClassLoadingLock", exception.getCause()); + throw new IllegalStateException("Error invoking java.lang.ClassLoader#getClassLoadingLock", exception.getTargetException()); } } } @@ -1475,21 +1481,6 @@ public Package definePackage(ClassLoader classLoader, } } - /** - * A proxy of {@code java.lang.SecurityManager}. - */ - @JavaDispatcher.Proxied("java.lang.SecurityManager") - protected interface SecurityManager { - - /** - * Checks the given permissions on the supplied security manager. - * - * @param value The security manager to use. - * @param permission The permissions to validate. - */ - void checkPermission(Object value, Permission permission); - } - /** * A proxy of {@code java.lang.System}. */ @@ -1505,6 +1496,30 @@ protected interface System { @JavaDispatcher.Defaults Object getSecurityManager(); } + + /** + * A privileged action to resolve {@code java.lang.SecurityManager#checkPermission} method or + * {@code null}, if this method is not available. + */ + protected enum CheckPermissionAction implements PrivilegedAction { + + /** + * The singleton instance. + */ + INSTANCE; + + /** + * {@inheritDoc} + */ + @SuppressFBWarnings(value = "REC_CATCH_EXCEPTION", justification = "Exception should not be rethrown but be nulled out") + public Method run() { + try { + return Class.forName("java.lang.SecurityManager").getMethod("checkPermission", Permission.class); + } catch (Exception ignored) { + return null; + } + } + } } /** @@ -1625,7 +1640,7 @@ public Map> injectRaw(Map types) { try { result.put(entry.getKey(), (Class) DEFINE_CLASS.invoke(lookup, entry.getValue())); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Failed to define " + entry.getKey() + " using " + lookup, exception.getCause()); + throw new IllegalStateException("Failed to define " + entry.getKey() + " using " + lookup, exception.getTargetException()); } catch (Exception exception) { throw new IllegalStateException("Failed to define " + entry.getKey() + " using " + lookup, exception); } @@ -1731,9 +1746,9 @@ class UsingUnsafe extends AbstractBase { private static final System SYSTEM = doPrivileged(JavaDispatcher.of(System.class)); /** - * A proxy for {@code java.lang.SecurityManager} if available. + * The {@code java.lang.SecurityManager#checkPermission} method or {@code null} if not available. */ - private static final SecurityManager SECURITY_MANAGER = doPrivileged(JavaDispatcher.of(SecurityManager.class)); + private static final Method CHECK_PERMISSION = doPrivileged(CheckPermissionAction.INSTANCE); /** * A lock for the bootstrap loader when injecting. @@ -2021,7 +2036,9 @@ public Dispatcher initialize() { Object securityManager = SYSTEM.getSecurityManager(); if (securityManager != null) { try { - SECURITY_MANAGER.checkPermission(securityManager, SUPPRESS_ACCESS_CHECKS); + CHECK_PERMISSION.invoke(securityManager, SUPPRESS_ACCESS_CHECKS); + } catch (InvocationTargetException exception) { + return new Unavailable(exception.getTargetException().getMessage()); } catch (Exception exception) { return new Unavailable(exception.getMessage()); } @@ -2044,7 +2061,7 @@ public Class defineClass(ClassLoader classLoader, String name, byte[] binaryR } catch (IllegalAccessException exception) { throw new IllegalStateException("Could not access Unsafe::defineClass", exception); } catch (InvocationTargetException exception) { - throw new IllegalStateException("Error invoking Unsafe::defineClass", exception.getCause()); + throw new IllegalStateException("Error invoking Unsafe::defineClass", exception.getTargetException()); } } } @@ -2284,21 +2301,6 @@ public void apply(AccessibleObject accessibleObject) { } } - /** - * A proxy of {@code java.lang.SecurityManager}. - */ - @JavaDispatcher.Proxied("java.lang.SecurityManager") - protected interface SecurityManager { - - /** - * Checks the given permissions on the supplied security manager. - * - * @param value The security manager to use. - * @param permission The permissions to validate. - */ - void checkPermission(Object value, Permission permission); - } - /** * A proxy of {@code java.lang.System}. */ @@ -2314,6 +2316,30 @@ protected interface System { @JavaDispatcher.Defaults Object getSecurityManager(); } + + /** + * A privileged action to resolve {@code java.lang.SecurityManager#checkPermission} method or + * {@code null}, if this method is not available. + */ + protected enum CheckPermissionAction implements PrivilegedAction { + + /** + * The singleton instance. + */ + INSTANCE; + + /** + * {@inheritDoc} + */ + @SuppressFBWarnings(value = "REC_CATCH_EXCEPTION", justification = "Exception should not be rethrown but be nulled out") + public Method run() { + try { + return Class.forName("java.lang.SecurityManager").getMethod("checkPermission", Permission.class); + } catch (Exception ignored) { + return null; + } + } + } } /** diff --git a/byte-buddy-dep/src/main/java/net/bytebuddy/utility/dispatcher/JavaDispatcher.java b/byte-buddy-dep/src/main/java/net/bytebuddy/utility/dispatcher/JavaDispatcher.java index 212b0da3210..4623b81ccf5 100644 --- a/byte-buddy-dep/src/main/java/net/bytebuddy/utility/dispatcher/JavaDispatcher.java +++ b/byte-buddy-dep/src/main/java/net/bytebuddy/utility/dispatcher/JavaDispatcher.java @@ -1098,7 +1098,7 @@ public Object invoke(Object proxy, Method method, Object[] argument) throws Thro return dispatcher.invoke(argument); } } catch (InvocationTargetException exception) { - throw exception.getCause(); + throw exception.getTargetException(); } } catch (RuntimeException exception) { throw exception; diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/asm/AdviceNoRegularReturnTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/asm/AdviceNoRegularReturnTest.java index c845e87bdad..b2307ce1676 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/asm/AdviceNoRegularReturnTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/asm/AdviceNoRegularReturnTest.java @@ -54,7 +54,7 @@ public void testNoRegularReturnWithSkip() throws Exception { type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()); fail(); } catch (InvocationTargetException exception) { - assertThat(exception.getCause(), instanceOf(RuntimeException.class)); + assertThat(exception.getTargetException(), instanceOf(RuntimeException.class)); } } @@ -70,7 +70,7 @@ public void testNoRegularReturnWithoutHandler() throws Exception { type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()); fail(); } catch (InvocationTargetException exception) { - assertThat(exception.getCause(), instanceOf(RuntimeException.class)); + assertThat(exception.getTargetException(), instanceOf(RuntimeException.class)); } } @@ -86,7 +86,7 @@ public void testNoRegularReturnWithHandler() throws Exception { type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()); fail(); } catch (InvocationTargetException exception) { - assertThat(exception.getCause(), instanceOf(RuntimeException.class)); + assertThat(exception.getTargetException(), instanceOf(RuntimeException.class)); } } diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/asm/AdviceNoRegularReturnWithinAdviceTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/asm/AdviceNoRegularReturnWithinAdviceTest.java index 33c00e31357..d644454b767 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/asm/AdviceNoRegularReturnWithinAdviceTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/asm/AdviceNoRegularReturnWithinAdviceTest.java @@ -74,7 +74,7 @@ public void testNoRegularReturn() throws Exception { type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()); fail(); } catch (InvocationTargetException exception) { - assertThat(exception.getCause(), instanceOf(RuntimeException.class)); + assertThat(exception.getTargetException(), instanceOf(RuntimeException.class)); } } diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/asm/AdviceTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/asm/AdviceTest.java index 84cb8cf7747..788fda58253 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/asm/AdviceTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/asm/AdviceTest.java @@ -625,7 +625,7 @@ public void testAdviceNotSkipExceptionImplicit() throws Exception { type.getDeclaredMethod(FOO + BAR).invoke(type.getDeclaredConstructor().newInstance()); fail(); } catch (InvocationTargetException exception) { - assertThat(exception.getCause(), instanceOf(RuntimeException.class)); + assertThat(exception.getTargetException(), instanceOf(RuntimeException.class)); } assertThat(type.getDeclaredField(ENTER).get(null), is((Object) 1)); assertThat(type.getDeclaredField(EXIT).get(null), is((Object) 1)); @@ -644,7 +644,7 @@ public void testAdviceNotSkipExceptionImplicitNested() throws Exception { type.getDeclaredMethod(FOO + BAR).invoke(type.getDeclaredConstructor().newInstance()); fail(); } catch (InvocationTargetException exception) { - assertThat(exception.getCause(), instanceOf(RuntimeException.class)); + assertThat(exception.getTargetException(), instanceOf(RuntimeException.class)); } assertThat(type.getDeclaredField(ENTER).get(null), is((Object) 2)); assertThat(type.getDeclaredField(EXIT).get(null), is((Object) 2)); @@ -662,7 +662,7 @@ public void testAdviceSkipExceptionImplicit() throws Exception { type.getDeclaredMethod(FOO + BAR).invoke(type.getDeclaredConstructor().newInstance()); fail(); } catch (InvocationTargetException exception) { - assertThat(exception.getCause(), instanceOf(RuntimeException.class)); + assertThat(exception.getTargetException(), instanceOf(RuntimeException.class)); } assertThat(type.getDeclaredField(ENTER).get(null), is((Object) 1)); assertThat(type.getDeclaredField(EXIT).get(null), is((Object) 0)); @@ -680,7 +680,7 @@ public void testAdviceNotSkipExceptionExplicit() throws Exception { type.getDeclaredMethod(BAR + BAZ).invoke(type.getDeclaredConstructor().newInstance()); fail(); } catch (InvocationTargetException exception) { - assertThat(exception.getCause(), instanceOf(NullPointerException.class)); + assertThat(exception.getTargetException(), instanceOf(NullPointerException.class)); } assertThat(type.getDeclaredField(ENTER).get(null), is((Object) 1)); assertThat(type.getDeclaredField(EXIT).get(null), is((Object) 1)); @@ -699,7 +699,7 @@ public void testAdviceNotSkipExceptionExplicitNested() throws Exception { type.getDeclaredMethod(BAR + BAZ).invoke(type.getDeclaredConstructor().newInstance()); fail(); } catch (InvocationTargetException exception) { - assertThat(exception.getCause(), instanceOf(NullPointerException.class)); + assertThat(exception.getTargetException(), instanceOf(NullPointerException.class)); } assertThat(type.getDeclaredField(ENTER).get(null), is((Object) 2)); assertThat(type.getDeclaredField(EXIT).get(null), is((Object) 2)); @@ -717,7 +717,7 @@ public void testAdviceSkipExceptionExplicit() throws Exception { type.getDeclaredMethod(BAR + BAZ).invoke(type.getDeclaredConstructor().newInstance()); fail(); } catch (InvocationTargetException exception) { - assertThat(exception.getCause(), instanceOf(NullPointerException.class)); + assertThat(exception.getTargetException(), instanceOf(NullPointerException.class)); } assertThat(type.getDeclaredField(ENTER).get(null), is((Object) 1)); assertThat(type.getDeclaredField(EXIT).get(null), is((Object) 0)); @@ -786,7 +786,7 @@ public void testExceptionWhenThrown() throws Exception { type.getDeclaredMethod(FOO + BAR).invoke(type.getDeclaredConstructor().newInstance()); fail(); } catch (InvocationTargetException exception) { - assertThat(exception.getCause(), instanceOf(RuntimeException.class)); + assertThat(exception.getTargetException(), instanceOf(RuntimeException.class)); } assertThat(type.getDeclaredField(THROWABLE).get(null), instanceOf(RuntimeException.class)); } @@ -803,7 +803,7 @@ public void testAdviceThrowOnEnter() throws Exception { type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()); fail(); } catch (InvocationTargetException exception) { - assertThat(exception.getCause(), instanceOf(RuntimeException.class)); + assertThat(exception.getTargetException(), instanceOf(RuntimeException.class)); } assertThat(type.getDeclaredField(ENTER).get(null), is((Object) 1)); assertThat(type.getDeclaredField(INSIDE).get(null), is((Object) 0)); @@ -822,7 +822,7 @@ public void testAdviceThrowOnExit() throws Exception { type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()); fail(); } catch (InvocationTargetException exception) { - assertThat(exception.getCause(), instanceOf(RuntimeException.class)); + assertThat(exception.getTargetException(), instanceOf(RuntimeException.class)); } assertThat(type.getDeclaredField(ENTER).get(null), is((Object) 1)); assertThat(type.getDeclaredField(INSIDE).get(null), is((Object) 1)); @@ -855,7 +855,7 @@ public void testAdviceThrowNotSuppressedOnEnter() throws Exception { type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()); fail(); } catch (InvocationTargetException exception) { - assertThat(exception.getCause(), instanceOf(Exception.class)); + assertThat(exception.getTargetException(), instanceOf(Exception.class)); } assertThat(type.getDeclaredField(ENTER).get(null), is((Object) 1)); assertThat(type.getDeclaredField(INSIDE).get(null), is((Object) 0)); @@ -874,7 +874,7 @@ public void testAdviceThrowNotSuppressedOnExit() throws Exception { type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()); fail(); } catch (InvocationTargetException exception) { - assertThat(exception.getCause(), instanceOf(Exception.class)); + assertThat(exception.getTargetException(), instanceOf(Exception.class)); } assertThat(type.getDeclaredField(ENTER).get(null), is((Object) 1)); assertThat(type.getDeclaredField(INSIDE).get(null), is((Object) 1)); @@ -1144,7 +1144,7 @@ public void testExceptionTypeAdvice() throws Exception { type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()); fail(); } catch (InvocationTargetException exception) { - assertThat(exception.getCause(), instanceOf(IllegalStateException.class)); + assertThat(exception.getTargetException(), instanceOf(IllegalStateException.class)); } assertThat(type.getDeclaredField(ENTER).get(null), is((Object) 1)); assertThat(type.getDeclaredField(EXIT).get(null), is((Object) 1)); @@ -1162,7 +1162,7 @@ public void testExceptionNotCatchedAdvice() throws Exception { type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()); fail(); } catch (InvocationTargetException exception) { - assertThat(exception.getCause(), instanceOf(Exception.class)); + assertThat(exception.getTargetException(), instanceOf(Exception.class)); } assertThat(type.getDeclaredField(ENTER).get(null), is((Object) 1)); assertThat(type.getDeclaredField(EXIT).get(null), is((Object) 0)); @@ -1180,7 +1180,7 @@ public void testExceptionCatchedAdvice() throws Exception { type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()); fail(); } catch (InvocationTargetException exception) { - assertThat(exception.getCause(), instanceOf(RuntimeException.class)); + assertThat(exception.getTargetException(), instanceOf(RuntimeException.class)); } assertThat(type.getDeclaredField(ENTER).get(null), is((Object) 1)); assertThat(type.getDeclaredField(EXIT).get(null), is((Object) 1)); @@ -1198,7 +1198,7 @@ public void testExceptionCatchedWithExchangeAdvice() throws Exception { type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()); fail(); } catch (InvocationTargetException exception) { - assertThat(exception.getCause(), instanceOf(IOException.class)); + assertThat(exception.getTargetException(), instanceOf(IOException.class)); } assertThat(type.getDeclaredField(ENTER).get(null), is((Object) 1)); assertThat(type.getDeclaredField(EXIT).get(null), is((Object) 1)); @@ -1216,7 +1216,7 @@ public void testNonAssignableCasting() throws Exception { type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()); fail(); } catch (InvocationTargetException exception) { - assertThat(exception.getCause(), instanceOf(ClassCastException.class)); + assertThat(exception.getTargetException(), instanceOf(ClassCastException.class)); } } @@ -1243,7 +1243,7 @@ public void testPrimitiveNonAssignableCasting() throws Exception { type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()); fail(); } catch (InvocationTargetException exception) { - assertThat(exception.getCause(), instanceOf(ClassCastException.class)); + assertThat(exception.getTargetException(), instanceOf(ClassCastException.class)); } } @@ -1386,7 +1386,7 @@ public void testExceptionRethrowing() throws Exception { type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()); fail(); } catch (InvocationTargetException exception) { - assertThat(exception.getCause().getClass().getName(), is(ExceptionWriterSample.class.getName())); + assertThat(exception.getTargetException().getClass().getName(), is(ExceptionWriterSample.class.getName())); } } @@ -1983,7 +1983,7 @@ public void testCannotInstantiateSuppressionMarker() throws Exception { constructor.newInstance(); fail(); } catch (InvocationTargetException exception) { - assertThat(exception.getCause(), instanceOf(UnsupportedOperationException.class)); + assertThat(exception.getTargetException(), instanceOf(UnsupportedOperationException.class)); } } @@ -1996,7 +1996,7 @@ public void testCannotInstantiateSkipDefaultValueMarker() throws Exception { constructor.newInstance(); fail(); } catch (InvocationTargetException exception) { - assertThat(exception.getCause(), instanceOf(UnsupportedOperationException.class)); + assertThat(exception.getTargetException(), instanceOf(UnsupportedOperationException.class)); } } @@ -2009,7 +2009,7 @@ public void testCannotInstantiateSkipNonDefaultValueMarker() throws Exception { constructor.newInstance(); fail(); } catch (InvocationTargetException exception) { - assertThat(exception.getCause(), instanceOf(UnsupportedOperationException.class)); + assertThat(exception.getTargetException(), instanceOf(UnsupportedOperationException.class)); } } diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/asm/AdviceTypeTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/asm/AdviceTypeTest.java index 07335918297..9773c94d763 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/asm/AdviceTypeTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/asm/AdviceTypeTest.java @@ -119,7 +119,7 @@ public void testAdviceWithException() throws Exception { assertThat(type.getDeclaredMethod(BAR, this.type, this.type).invoke(type.getDeclaredConstructor().newInstance(), value, value), is((Object) value)); fail(); } catch (InvocationTargetException exception) { - assertThat(exception.getCause(), instanceOf(RuntimeException.class)); + assertThat(exception.getTargetException(), instanceOf(RuntimeException.class)); } assertThat(type.getDeclaredField(ENTER).get(null), is((Object) 1)); assertThat(type.getDeclaredField(EXIT).get(null), is((Object) 1)); diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/build/CachedReturnPluginTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/build/CachedReturnPluginTest.java index 1fabbc46781..5b48425d97f 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/build/CachedReturnPluginTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/build/CachedReturnPluginTest.java @@ -85,8 +85,8 @@ public void testCannotConstructAdvice() throws Exception { try { constructor.newInstance(); fail(); - } catch (InvocationTargetException e) { - throw (Exception) e.getCause(); + } catch (InvocationTargetException exception) { + throw (Exception) exception.getTargetException(); } } diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/description/annotation/AnnotationDescriptionLatentTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/description/annotation/AnnotationDescriptionLatentTest.java index bbff69c5d1d..b5a4e6ab041 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/description/annotation/AnnotationDescriptionLatentTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/description/annotation/AnnotationDescriptionLatentTest.java @@ -69,7 +69,7 @@ private static AnnotationDescription build(Annotation annotation) throws Excepti throw new IllegalArgumentException("Unexpected annotation property: " + method); } } catch (InvocationTargetException exception) { - Throwable cause = exception.getCause(); + Throwable cause = exception.getTargetException(); if (cause instanceof TypeNotPresentException) { builder = builder.define(method.getName(), new AnnotationValue.ForMissingType(((TypeNotPresentException) cause).typeName())); } else if (cause instanceof EnumConstantNotPresentException) { diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/TargetTypeTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/TargetTypeTest.java index 29a6b5c4640..0031bcf6e70 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/TargetTypeTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/TargetTypeTest.java @@ -65,7 +65,7 @@ public void testConstructorIsHidden() throws Exception { constructor.newInstance(); fail(); } catch (InvocationTargetException exception) { - assertThat(exception.getCause().getClass(), CoreMatchers.>is(UnsupportedOperationException.class)); + assertThat(exception.getTargetException().getClass(), CoreMatchers.>is(UnsupportedOperationException.class)); } } diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/implementation/bind/annotation/IgnoreForBindingVerifierTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/implementation/bind/annotation/IgnoreForBindingVerifierTest.java index 577cf9a65e1..d013ac6597e 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/implementation/bind/annotation/IgnoreForBindingVerifierTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/implementation/bind/annotation/IgnoreForBindingVerifierTest.java @@ -66,7 +66,7 @@ public void testInstantiation() throws Exception { constructor.newInstance(); fail(); } catch (InvocationTargetException exception) { - throw (UnsupportedOperationException) exception.getCause(); + throw (UnsupportedOperationException) exception.getTargetException(); } } } diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/implementation/bind/annotation/RuntimeTypeVerifierTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/implementation/bind/annotation/RuntimeTypeVerifierTest.java index 8971f9bcd8a..3032976cb41 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/implementation/bind/annotation/RuntimeTypeVerifierTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/implementation/bind/annotation/RuntimeTypeVerifierTest.java @@ -64,7 +64,7 @@ public void testInstantiation() throws Exception { constructor.newInstance(); fail(); } catch (InvocationTargetException exception) { - throw (UnsupportedOperationException) exception.getCause(); + throw (UnsupportedOperationException) exception.getTargetException(); } } } diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/matcher/ElementMatchersTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/matcher/ElementMatchersTest.java index 3ecaedbd8a2..b93c82f172b 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/matcher/ElementMatchersTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/matcher/ElementMatchersTest.java @@ -1282,7 +1282,7 @@ public void testConstructorIsHidden() throws Exception { constructor.newInstance(); fail(); } catch (InvocationTargetException exception) { - throw (UnsupportedOperationException) exception.getCause(); + throw (UnsupportedOperationException) exception.getTargetException(); } } diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/utility/CompoundListTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/utility/CompoundListTest.java index 90cda7dc158..fa22b572974 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/utility/CompoundListTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/utility/CompoundListTest.java @@ -32,7 +32,7 @@ public void testConstruction() throws Throwable { constructor.newInstance(); fail(); } catch (InvocationTargetException exception) { - throw exception.getCause(); + throw exception.getTargetException(); } } diff --git a/byte-buddy-maven-plugin/src/test/java/net/bytebuddy/build/maven/ByteBuddyMojoTest.java b/byte-buddy-maven-plugin/src/test/java/net/bytebuddy/build/maven/ByteBuddyMojoTest.java index 159490d99cb..18a140c37d5 100644 --- a/byte-buddy-maven-plugin/src/test/java/net/bytebuddy/build/maven/ByteBuddyMojoTest.java +++ b/byte-buddy-maven-plugin/src/test/java/net/bytebuddy/build/maven/ByteBuddyMojoTest.java @@ -170,7 +170,7 @@ public void testLiveInitializerAllowed() throws Exception { assertMethod(classLoader.loadClass("foo.Bar"), FOO, QUX); fail(); } catch (InvocationTargetException exception) { - assertThat(exception.getCause(), instanceOf(NullPointerException.class)); + assertThat(exception.getTargetException(), instanceOf(NullPointerException.class)); } } finally { for (File file : files) {