Skip to content

Commit

Permalink
Split catch() blocks so that javac doesn't generate a reference to th…
Browse files Browse the repository at this point in the history
…eir common supertype, ReflectiveOperationException.

ReflectiveOperationException isn't available on the old versions of Android that we support.

As far as we know, there is no actual problem with these references because no methods are called on them. However, the new version of Animal Sniffer we're updating to will flag them anyway, and I think that avoiding them is the safe thing to do.

Prepares for #3497

RELNOTES=n/a

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=252888170
  • Loading branch information
cpovirk authored and ronshapiro committed Jun 28, 2019
1 parent c168ba2 commit 15c9a93
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,14 @@ public static <T> T get(Class<T> type) {
constructor.setAccessible(true); // accessibility check is too slow
try {
return constructor.newInstance();
} catch (InstantiationException | IllegalAccessException impossible) {
/*
* Do not merge the 2 catch blocks below. javac would infer a type of
* ReflectiveOperationException, which Animal Sniffer would reject. (Old versions of
* Android don't *seem* to mind, but there might be edge cases of which we're unaware.)
*/
} catch (InstantiationException impossible) {
throw new AssertionError(impossible);
} catch (IllegalAccessException impossible) {
throw new AssertionError(impossible);
} catch (InvocationTargetException e) {
logger.log(Level.WARNING, "Exception while invoking default constructor.", e.getCause());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,14 @@ private Currency generateCurrency() {
@SuppressWarnings("unchecked") // getAvailableCurrencies() returns Set<Currency>.
Set<Currency> currencies = (Set<Currency>) method.invoke(null);
return pickInstance(currencies, Currency.getInstance(Locale.US));
} catch (NoSuchMethodException | InvocationTargetException notJava7) {
/*
* Do not merge the 2 catch blocks below. javac would infer a type of
* ReflectiveOperationException, which Animal Sniffer would reject. (Old versions of
* Android don't *seem* to mind, but there might be edge cases of which we're unaware.)
*/
} catch (NoSuchMethodException notJava7) {
return preJava7FreshCurrency();
} catch (InvocationTargetException notJava7) {
return preJava7FreshCurrency();
} catch (IllegalAccessException impossible) {
throw new AssertionError(impossible);
Expand Down
9 changes: 8 additions & 1 deletion android/guava/src/com/google/common/reflect/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,14 @@ String typeName(Type type) {
return (String) getTypeName.invoke(type);
} catch (NoSuchMethodException e) {
throw new AssertionError("Type.getTypeName should be available in Java 8");
} catch (InvocationTargetException | IllegalAccessException e) {
/*
* Do not merge the 2 catch blocks below. javac would infer a type of
* ReflectiveOperationException, which Animal Sniffer would reject. (Old versions of
* Android don't *seem* to mind, but there might be edge cases of which we're unaware.)
*/
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,16 @@ public static ThreadFactory platformThreadFactory() {
Class.forName("com.google.appengine.api.ThreadManager")
.getMethod("currentRequestThreadFactory")
.invoke(null);
} catch (IllegalAccessException | ClassNotFoundException | NoSuchMethodException e) {
/*
* Do not merge the 3 catch blocks below. javac would infer a type of
* ReflectiveOperationException, which Animal Sniffer would reject. (Old versions of Android
* don't *seem* to mind, but there might be edge cases of which we're unaware.)
*/
} catch (IllegalAccessException e) {
throw new RuntimeException("Couldn't invoke ThreadManager.currentRequestThreadFactory", e);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Couldn't invoke ThreadManager.currentRequestThreadFactory", e);
} catch (NoSuchMethodException e) {
throw new RuntimeException("Couldn't invoke ThreadManager.currentRequestThreadFactory", e);
} catch (InvocationTargetException e) {
throw Throwables.propagate(e.getCause());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,14 @@ public static <T> T get(Class<T> type) {
constructor.setAccessible(true); // accessibility check is too slow
try {
return constructor.newInstance();
} catch (InstantiationException | IllegalAccessException impossible) {
/*
* Do not merge the 2 catch blocks below. javac would infer a type of
* ReflectiveOperationException, which Animal Sniffer would reject. (Old versions of
* Android don't *seem* to mind, but there might be edge cases of which we're unaware.)
*/
} catch (InstantiationException impossible) {
throw new AssertionError(impossible);
} catch (IllegalAccessException impossible) {
throw new AssertionError(impossible);
} catch (InvocationTargetException e) {
logger.log(Level.WARNING, "Exception while invoking default constructor.", e.getCause());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,14 @@ private Currency generateCurrency() {
@SuppressWarnings("unchecked") // getAvailableCurrencies() returns Set<Currency>.
Set<Currency> currencies = (Set<Currency>) method.invoke(null);
return pickInstance(currencies, Currency.getInstance(Locale.US));
} catch (NoSuchMethodException | InvocationTargetException notJava7) {
/*
* Do not merge the 2 catch blocks below. javac would infer a type of
* ReflectiveOperationException, which Animal Sniffer would reject. (Old versions of
* Android don't *seem* to mind, but there might be edge cases of which we're unaware.)
*/
} catch (NoSuchMethodException notJava7) {
return preJava7FreshCurrency();
} catch (InvocationTargetException notJava7) {
return preJava7FreshCurrency();
} catch (IllegalAccessException impossible) {
throw new AssertionError(impossible);
Expand Down
9 changes: 8 additions & 1 deletion guava/src/com/google/common/reflect/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,14 @@ String typeName(Type type) {
return (String) getTypeName.invoke(type);
} catch (NoSuchMethodException e) {
throw new AssertionError("Type.getTypeName should be available in Java 8");
} catch (InvocationTargetException | IllegalAccessException e) {
/*
* Do not merge the 2 catch blocks below. javac would infer a type of
* ReflectiveOperationException, which Animal Sniffer would reject. (Old versions of
* Android don't *seem* to mind, but there might be edge cases of which we're unaware.)
*/
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
Expand Down
11 changes: 10 additions & 1 deletion guava/src/com/google/common/util/concurrent/MoreExecutors.java
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,16 @@ public static ThreadFactory platformThreadFactory() {
Class.forName("com.google.appengine.api.ThreadManager")
.getMethod("currentRequestThreadFactory")
.invoke(null);
} catch (IllegalAccessException | ClassNotFoundException | NoSuchMethodException e) {
/*
* Do not merge the 3 catch blocks below. javac would infer a type of
* ReflectiveOperationException, which Animal Sniffer would reject. (Old versions of Android
* don't *seem* to mind, but there might be edge cases of which we're unaware.)
*/
} catch (IllegalAccessException e) {
throw new RuntimeException("Couldn't invoke ThreadManager.currentRequestThreadFactory", e);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Couldn't invoke ThreadManager.currentRequestThreadFactory", e);
} catch (NoSuchMethodException e) {
throw new RuntimeException("Couldn't invoke ThreadManager.currentRequestThreadFactory", e);
} catch (InvocationTargetException e) {
throw Throwables.propagate(e.getCause());
Expand Down

0 comments on commit 15c9a93

Please sign in to comment.