Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message for Robolectric.buildActivity() if provided an abstract class #8997

Open
david-allison opened this issue Apr 16, 2024 · 0 comments

Comments

@david-allison
Copy link

david-allison commented Apr 16, 2024

Is your feature request related to a problem? Please describe.
A developer wanted help with their code producing the following error message

Kotlin

        val controller = Robolectric.buildActivity(AbstractClass::class.java)

Stack Trace

    java.lang.RuntimeException: error instantiating com.ichi2.anki.AbstractFlashcardViewer
        at org.robolectric.util.ReflectionHelpers.callConstructor(ReflectionHelpers.java:440)
        at org.robolectric.Robolectric.buildActivity(Robolectric.java:113)
        at org.robolectric.Robolectric.buildActivity(Robolectric.java:78)
        at com.ichi2.anki.AbstractFlashcardViewerCommandTest.testRefreshIfRequired(AbstractFlashcardViewerCommandTest.kt:163)
        Caused by:
        java.lang.InstantiationException
            at java.base/jdk.internal.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48)
            at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
            at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
            at org.robolectric.util.ReflectionHelpers.callConstructor(ReflectionHelpers.java:438)
            ... 3 more

Describe the solution you'd like
The error message should state that the provided class is abstract (and that abstract classes are not supported)

Describe alternatives you've considered
N/A

Additional context

Sample isAbstract checking code

java.lang.reflect.Modifier.isAbstract(clazz.getModifiers())

https://stackoverflow.com/a/1072895

public static <R> R callConstructor(
Class<? extends R> clazz, ClassParameter<?>... classParameters) {
perfStatsCollector.incrementCount("ReflectionHelpers.callConstructor-" + clazz.getName());
try {
final Class<?>[] classes = ClassParameter.getClasses(classParameters);
final Object[] values = ClassParameter.getValues(classParameters);
Constructor<? extends R> constructor = clazz.getDeclaredConstructor(classes);
constructor.setAccessible(true);
return constructor.newInstance(values);
} catch (InstantiationException e) {
throw new RuntimeException("error instantiating " + clazz.getName(), e);
} catch (InvocationTargetException e) {
if (e.getTargetException() instanceof RuntimeException) {
throw (RuntimeException) e.getTargetException();
}
if (e.getTargetException() instanceof Error) {
throw (Error) e.getTargetException();
}
throw new RuntimeException(e.getTargetException());
} catch (Exception e) {
throw new RuntimeException(e);
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant