Skip to content

Commit

Permalink
Fix lint issue with Hilt testing classes that get compiled to referen…
Browse files Browse the repository at this point in the history
…ce invalid ReflectiveOperationException for API < 19.

RELNOTES=Fixes lint issue with Hilt testing classes that get compiled to reference invalid ReflectiveOperationException for API < 19.
PiperOrigin-RevId: 373458573
  • Loading branch information
bcorso authored and Dagger Team committed May 12, 2021
1 parent ee969fb commit e58abc3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,31 @@ public abstract class EarlySingletonComponentCreator {
private static final String EARLY_SINGLETON_COMPONENT_CREATOR_IMPL =
"dagger.hilt.android.internal.testing.EarlySingletonComponentCreatorImpl";

private static final String ERROR_MSG =
"The EarlyComponent was requested but does not exist. Check that you have annotated "
+ "your test class with @HiltAndroidTest and that the processor is running over your "
+ "test.";

static Object createComponent() {
try {
return Class.forName(EARLY_SINGLETON_COMPONENT_CREATOR_IMPL)
.asSubclass(EarlySingletonComponentCreator.class)
.getDeclaredConstructor()
.newInstance()
.create();
} catch (ClassNotFoundException
| NoSuchMethodException
| IllegalAccessException
| InstantiationException
| InvocationTargetException e) {
throw new RuntimeException(
"The EarlyComponent was requested but does not exist. Check that you have annotated "
+ "your test class with @HiltAndroidTest and that the processor is running over your "
+ "test.",
e);
// We catch each individual exception rather than using a multicatch because multi-catch will
// get compiled to the common but new super type ReflectiveOperationException, which is not
// allowed on API < 19. See b/187826710.
} catch (ClassNotFoundException e) {
throw new RuntimeException(ERROR_MSG, e);
} catch (NoSuchMethodException e) {
throw new RuntimeException(ERROR_MSG, e);
} catch (IllegalAccessException e) {
throw new RuntimeException(ERROR_MSG, e);
} catch (InstantiationException e) {
throw new RuntimeException(ERROR_MSG, e);
} catch (InvocationTargetException e) {
throw new RuntimeException(ERROR_MSG, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,31 @@ static TestComponentData get(Class<?> testClass) {
.getDeclaredConstructor()
.newInstance()
.get();
} catch (ClassNotFoundException
| NoSuchMethodException
| IllegalAccessException
| InstantiationException
| InvocationTargetException e) {
throw new RuntimeException(
String.format(
"Hilt test, %s, is missing generated file: %s. Check that the test class is "
+ " annotated with @HiltAndroidTest and that the processor is running over your"
+ " test.",
testClass.getSimpleName(),
generatedClassName),
e);
// We catch each individual exception rather than using a multicatch because multi-catch will
// get compiled to the common but new super type ReflectiveOperationException, which is not
// allowed on API < 19. See b/187826710.
} catch (ClassNotFoundException e) {
throw new RuntimeException(errorMessage(testClass, generatedClassName), e);
} catch (NoSuchMethodException e) {
throw new RuntimeException(errorMessage(testClass, generatedClassName), e);
} catch (IllegalAccessException e) {
throw new RuntimeException(errorMessage(testClass, generatedClassName), e);
} catch (InstantiationException e) {
throw new RuntimeException(errorMessage(testClass, generatedClassName), e);
} catch (InvocationTargetException e) {
throw new RuntimeException(errorMessage(testClass, generatedClassName), e);
}
}

private static String errorMessage(Class<?> testClass, String generatedClassName) {
return String.format(
"Hilt test, %s, is missing generated file: %s. Check that the test class is "
+ " annotated with @HiltAndroidTest and that the processor is running over your"
+ " test.",
testClass.getSimpleName(),
generatedClassName);
}

private static String getEnclosedClassName(Class<?> testClass) {
StringBuilder sb = new StringBuilder();
Class<?> currClass = testClass;
Expand Down

0 comments on commit e58abc3

Please sign in to comment.