Skip to content

Commit

Permalink
Add error message if user make onCreate and onDestroy in base class f…
Browse files Browse the repository at this point in the history
…inal.

RELNOTES=n/a
PiperOrigin-RevId: 597332556
  • Loading branch information
wanyingd1996 authored and Dagger Team committed Jan 10, 2024
1 parent 662d823 commit 594a95e
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import dagger.internal.codegen.xprocessing.XElements;
import java.io.IOException;
import javax.lang.model.element.Modifier;
import javax.tools.Diagnostic;

/** Generates an Hilt Activity class for the @AndroidEntryPoint annotated class. */
public final class ActivityGenerator {
Expand Down Expand Up @@ -185,6 +186,14 @@ private MethodSpec getDefaultViewModelProviderFactory() {
private MethodSpec onCreateComponentActivity() {
XMethodElement nearestOverrideMethod =
requireNearestOverrideMethod(ActivityMethod.ON_CREATE, metadata);
if (nearestOverrideMethod.isFinal()) {
env.getMessager()
.printMessage(
Diagnostic.Kind.ERROR,
"Do not mark onCreate as final in base Activity class, as Hilt needs to override it"
+ " to inject SavedStateHandle.",
nearestOverrideMethod);
}
ParameterSpec.Builder parameterBuilder =
ParameterSpec.builder(AndroidClassNames.BUNDLE, "savedInstanceState");
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("onCreate");
Expand Down Expand Up @@ -246,6 +255,14 @@ private static boolean hasNullableAnnotation(XAnnotated element) {
private MethodSpec onDestroyComponentActivity() {
XMethodElement nearestOverrideMethod =
requireNearestOverrideMethod(ActivityMethod.ON_DESTROY, metadata);
if (nearestOverrideMethod.isFinal()) {
env.getMessager()
.printMessage(
Diagnostic.Kind.ERROR,
"Do not mark onDestroy as final in base Activity class, as Hilt needs to override it"
+ " to clean up SavedStateHandle.",
nearestOverrideMethod);
}
return MethodSpec.methodBuilder("onDestroy")
.addAnnotation(Override.class)
.addModifiers(XElements.getModifiers(nearestOverrideMethod))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,105 @@ public void generate_baseHiltComponentActivity() {
HiltCompilerTests.hiltCompiler(baseActivity, myActivity)
.compile(subject -> subject.hasErrorCount(0));
}

@Test
public void baseActivityHasFinalOnDestroy_fails() {
Source myActivity =
HiltCompilerTests.javaSource(
"test.MyActivity",
"package test;",
"",
"import dagger.hilt.android.AndroidEntryPoint;",
"",
"@AndroidEntryPoint(BaseActivity.class)",
"public class MyActivity extends Hilt_MyActivity {}");
Source baseActivity =
HiltCompilerTests.javaSource(
"test.BaseActivity",
"package test;",
"",
"import androidx.activity.ComponentActivity;",
"",
"public class BaseActivity extends ComponentActivity {",
" @Override public final void onDestroy() {}",
"}");
HiltCompilerTests.hiltCompiler(myActivity, baseActivity)
.compile(
subject -> {
subject.hasErrorCount(1);
subject.hasErrorContaining(
"Do not mark onDestroy as final in base Activity class, as Hilt needs to override"
+ " it to clean up SavedStateHandle");
});
}

@Test
public void baseActivityHasFinalOnCreate_fails() {
Source myActivity =
HiltCompilerTests.javaSource(
"test.MyActivity",
"package test;",
"",
"import dagger.hilt.android.AndroidEntryPoint;",
"",
"@AndroidEntryPoint(BaseActivity.class)",
"public class MyActivity extends Hilt_MyActivity {}");
Source baseActivity =
HiltCompilerTests.javaSource(
"test.BaseActivity",
"package test;",
"",
"import android.os.Bundle;",
"import androidx.activity.ComponentActivity;",
"",
"public class BaseActivity extends ComponentActivity {",
" @Override public final void onCreate(Bundle bundle) {}",
"}");
HiltCompilerTests.hiltCompiler(myActivity, baseActivity)
.compile(
subject -> {
subject.hasErrorCount(1);
subject.hasErrorContaining(
"Do not mark onCreate as final in base Activity class, as Hilt needs to override"
+ " it to inject SavedStateHandle");
});
}

@Test
public void secondBaseActivityHasFinalOnCreate_fails() {
Source myActivity =
HiltCompilerTests.javaSource(
"test.MyActivity",
"package test;",
"",
"import dagger.hilt.android.AndroidEntryPoint;",
"",
"@AndroidEntryPoint(BaseActivity.class)",
"public class MyActivity extends Hilt_MyActivity {}");
Source baseActivity =
HiltCompilerTests.javaSource(
"test.BaseActivity",
"package test;",
"",
"public class BaseActivity extends BaseActivity2 {}");
Source baseActivity2 =
HiltCompilerTests.javaSource(
"test.BaseActivity2",
"package test;",
"",
"import android.os.Bundle;",
"import androidx.activity.ComponentActivity;",
"",
"public class BaseActivity2 extends ComponentActivity {",
" @Override public final void onCreate(Bundle bundle) {}",
"}");
HiltCompilerTests.hiltCompiler(myActivity, baseActivity, baseActivity2)
.compile(
subject -> {
subject.hasErrorCount(1);
subject.hasErrorContaining(
"Do not mark onCreate as final in base Activity class, as Hilt needs to override"
+ " it to inject SavedStateHandle");
});
}
}

0 comments on commit 594a95e

Please sign in to comment.