Skip to content

Commit

Permalink
Use reflection for security manager invocations and prefer getTargetE…
Browse files Browse the repository at this point in the history
…xception over getCause for invocation target exceptions.
  • Loading branch information
raphw committed Aug 5, 2021
1 parent 625d105 commit ca9d3ab
Show file tree
Hide file tree
Showing 26 changed files with 150 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private static <T> T doPrivileged(PrivilegedAction<T> 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) {
Expand Down Expand Up @@ -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());
}
}
}
Expand Down Expand Up @@ -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());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void testConstructorThrowsException() throws Exception {
constructor.newInstance();
fail();
} catch (InvocationTargetException exception) {
throw (Exception) exception.getCause();
throw (Exception) exception.getTargetException();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void testConstructorThrowsException() throws Exception {
constructor.newInstance();
fail();
} catch (InvocationTargetException exception) {
throw (Exception) exception.getCause();
throw (Exception) exception.getTargetException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void testConstructorThrowsException() throws Exception {
constructor.newInstance();
fail();
} catch (InvocationTargetException exception) {
throw (Exception) exception.getCause();
throw (Exception) exception.getTargetException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down Expand Up @@ -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());
}
}

Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void testProxyInterceptor() throws Exception {
constructor.newInstance();
fail();
} catch (InvocationTargetException exception) {
throw (UnsupportedOperationException) exception.getCause();
throw (UnsupportedOperationException) exception.getTargetException();
}
}

Expand All @@ -29,7 +29,7 @@ public void testAccessorInterceptor() throws Exception {
constructor.newInstance();
fail();
} catch (InvocationTargetException exception) {
throw (UnsupportedOperationException) exception.getCause();
throw (UnsupportedOperationException) exception.getTargetException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions byte-buddy-dep/src/main/java/net/bytebuddy/build/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Void, Void>(((TypeNotPresentException) cause).typeName()));
} else if (cause instanceof EnumConstantNotPresentException) {
Expand All @@ -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);
Expand Down Expand Up @@ -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<Void, Void>(((TypeNotPresentException) cause).typeName());
} else if (cause instanceof EnumConstantNotPresentException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public void clean(Reference<? extends ClassLoader> 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());
}
}

Expand All @@ -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());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down Expand Up @@ -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());
}
}
}
Expand Down Expand Up @@ -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());
}
}
}
Expand Down
Loading

0 comments on commit ca9d3ab

Please sign in to comment.