From 35122f9e2b2f0c0d78579eba4310cb4823c2f198 Mon Sep 17 00:00:00 2001 From: Evgeny Kursakov Date: Fri, 26 Oct 2018 09:49:50 +0300 Subject: [PATCH 1/2] Add multimodules test and fix bug #220 --- .../reflector/MoxyReflectorGenerator.java | 4 +-- .../mvp/compiler/CompilerTest.java | 7 ++++ .../mvp/compiler/MultiModulesTest.java | 29 ++++++++++++++++ .../Lib1Presenter$$ViewStateProvider.java | 12 +++++++ .../multimodules/lib1/Lib1Presenter.java | 8 +++++ .../multimodules/lib1/Lib1View$$State.java | 6 ++++ .../resources/multimodules/lib1/Lib1View.java | 6 ++++ .../multimodules/lib1/MoxyReflector.java | 34 +++++++++++++++++++ 8 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 moxy-compiler/src/test/java/com/arellomobile/mvp/compiler/MultiModulesTest.java create mode 100644 moxy-compiler/src/test/resources/multimodules/lib1/Lib1Presenter$$ViewStateProvider.java create mode 100644 moxy-compiler/src/test/resources/multimodules/lib1/Lib1Presenter.java create mode 100644 moxy-compiler/src/test/resources/multimodules/lib1/Lib1View$$State.java create mode 100644 moxy-compiler/src/test/resources/multimodules/lib1/Lib1View.java create mode 100644 moxy-compiler/src/test/resources/multimodules/lib1/MoxyReflector.java diff --git a/moxy-compiler/src/main/java/com/arellomobile/mvp/compiler/reflector/MoxyReflectorGenerator.java b/moxy-compiler/src/main/java/com/arellomobile/mvp/compiler/reflector/MoxyReflectorGenerator.java index c00dc74e..2bf16a20 100644 --- a/moxy-compiler/src/main/java/com/arellomobile/mvp/compiler/reflector/MoxyReflectorGenerator.java +++ b/moxy-compiler/src/main/java/com/arellomobile/mvp/compiler/reflector/MoxyReflectorGenerator.java @@ -90,13 +90,13 @@ public static JavaFile generate(String destinationPackage, classBuilder.addMethod(MethodSpec.methodBuilder("getViewStateProviders") .addModifiers(Modifier.PUBLIC, Modifier.STATIC) .returns(MAP_CLASS_TO_OBJECT_TYPE_NAME) - .addStatement("return viewStateProvider.getViewState()") + .addStatement("return sViewStateProviders") .build()); classBuilder.addMethod(MethodSpec.methodBuilder("getPresenterBinders") .addModifiers(Modifier.PUBLIC, Modifier.STATIC) .returns(MAP_CLASS_TO_LIST_OF_OBJECT_TYPE_NAME) - .addStatement("return sViewStateProviders") + .addStatement("return sPresenterBinders") .build()); classBuilder.addMethod(MethodSpec.methodBuilder("getStrategies") diff --git a/moxy-compiler/src/test/java/com/arellomobile/mvp/compiler/CompilerTest.java b/moxy-compiler/src/test/java/com/arellomobile/mvp/compiler/CompilerTest.java index dc4f7d33..052fe9fc 100644 --- a/moxy-compiler/src/test/java/com/arellomobile/mvp/compiler/CompilerTest.java +++ b/moxy-compiler/src/test/java/com/arellomobile/mvp/compiler/CompilerTest.java @@ -39,6 +39,13 @@ protected Compilation compileSourcesWithProcessor(JavaFileObject... sources) { .compile(sources); } + protected Compilation compileLibSourcesWithProcessor(String moxyReflectorPackage, JavaFileObject... sources) { + return javac() + .withOptions("-implicit:none", "-AmoxyReflectorPackage=" + moxyReflectorPackage) + .withProcessors(new MvpCompiler()) + .compile(sources); + } + /** * Asserts that all files from {@code exceptedGeneratedFiles} exists in {@code actualGeneratedFiles} * and have equivalent bytecode diff --git a/moxy-compiler/src/test/java/com/arellomobile/mvp/compiler/MultiModulesTest.java b/moxy-compiler/src/test/java/com/arellomobile/mvp/compiler/MultiModulesTest.java new file mode 100644 index 00000000..9084f709 --- /dev/null +++ b/moxy-compiler/src/test/java/com/arellomobile/mvp/compiler/MultiModulesTest.java @@ -0,0 +1,29 @@ +package com.arellomobile.mvp.compiler; + +import com.google.testing.compile.Compilation; + +import org.junit.Test; + +import javax.tools.JavaFileObject; + +import static com.google.testing.compile.CompilationSubject.assertThat; +import static com.google.testing.compile.JavaFileObjects.forResource; + +public class MultiModulesTest extends CompilerTest { + + @Test + public void testCompileLibraryModule() throws Exception { + JavaFileObject presenter = forResource("multimodules/lib1/Lib1Presenter.java"); + JavaFileObject view = forResource("multimodules/lib1/Lib1View.java"); + + Compilation compilation = compileLibSourcesWithProcessor("multimodules.lib1", presenter, view); + Compilation exceptedCompilation = compileSources( + forResource("multimodules/lib1/Lib1Presenter$$ViewStateProvider.java"), + forResource("multimodules/lib1/Lib1View$$State.java"), + forResource("multimodules/lib1/MoxyReflector.java") + ); + + assertThat(compilation).succeededWithoutWarnings(); + assertExceptedFilesGenerated(compilation.generatedFiles(), exceptedCompilation.generatedFiles()); + } +} diff --git a/moxy-compiler/src/test/resources/multimodules/lib1/Lib1Presenter$$ViewStateProvider.java b/moxy-compiler/src/test/resources/multimodules/lib1/Lib1Presenter$$ViewStateProvider.java new file mode 100644 index 00000000..c5f16557 --- /dev/null +++ b/moxy-compiler/src/test/resources/multimodules/lib1/Lib1Presenter$$ViewStateProvider.java @@ -0,0 +1,12 @@ +package multimodules.lib1; + +import com.arellomobile.mvp.MvpView; +import com.arellomobile.mvp.ViewStateProvider; +import com.arellomobile.mvp.viewstate.MvpViewState; + +public class Lib1Presenter$$ViewStateProvider extends ViewStateProvider { + @Override + public MvpViewState getViewState() { + return new Lib1View$$State(); + } +} diff --git a/moxy-compiler/src/test/resources/multimodules/lib1/Lib1Presenter.java b/moxy-compiler/src/test/resources/multimodules/lib1/Lib1Presenter.java new file mode 100644 index 00000000..ff74d536 --- /dev/null +++ b/moxy-compiler/src/test/resources/multimodules/lib1/Lib1Presenter.java @@ -0,0 +1,8 @@ +package multimodules.lib1; + +import com.arellomobile.mvp.InjectViewState; +import com.arellomobile.mvp.MvpPresenter; + +@InjectViewState +public class Lib1Presenter extends MvpPresenter { +} \ No newline at end of file diff --git a/moxy-compiler/src/test/resources/multimodules/lib1/Lib1View$$State.java b/moxy-compiler/src/test/resources/multimodules/lib1/Lib1View$$State.java new file mode 100644 index 00000000..d8f5495f --- /dev/null +++ b/moxy-compiler/src/test/resources/multimodules/lib1/Lib1View$$State.java @@ -0,0 +1,6 @@ +package multimodules.lib1; + +import com.arellomobile.mvp.viewstate.MvpViewState; + +public class Lib1View$$State extends MvpViewState implements Lib1View { +} \ No newline at end of file diff --git a/moxy-compiler/src/test/resources/multimodules/lib1/Lib1View.java b/moxy-compiler/src/test/resources/multimodules/lib1/Lib1View.java new file mode 100644 index 00000000..9460cb0a --- /dev/null +++ b/moxy-compiler/src/test/resources/multimodules/lib1/Lib1View.java @@ -0,0 +1,6 @@ +package multimodules.lib1; + +import com.arellomobile.mvp.MvpView; + +public interface Lib1View extends MvpView { +} \ No newline at end of file diff --git a/moxy-compiler/src/test/resources/multimodules/lib1/MoxyReflector.java b/moxy-compiler/src/test/resources/multimodules/lib1/MoxyReflector.java new file mode 100644 index 00000000..be9b3a2b --- /dev/null +++ b/moxy-compiler/src/test/resources/multimodules/lib1/MoxyReflector.java @@ -0,0 +1,34 @@ +package multimodules.lib1; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public final class MoxyReflector { + private static Map, Object> sViewStateProviders; + + private static Map, List> sPresenterBinders; + + private static Map, Object> sStrategies; + + static { + sViewStateProviders = new HashMap<>(); + sViewStateProviders.put(Lib1Presenter.class, new Lib1Presenter$$ViewStateProvider()); + + sPresenterBinders = new HashMap<>(); + + sStrategies = new HashMap<>(); + } + + public static Map, Object> getViewStateProviders() { + return sViewStateProviders; + } + + public static Map, List> getPresenterBinders() { + return sPresenterBinders; + } + + public static Map, Object> getStrategies() { + return sStrategies; + } +} From c86af978f89d20f7b85f516760f2e98a78845f17 Mon Sep 17 00:00:00 2001 From: Evgeny Kursakov Date: Sat, 27 Oct 2018 21:03:09 +0300 Subject: [PATCH 2/2] Add compiler test for RegisterMoxyReflectorPackages annotation --- .../mvp/compiler/MultiModulesTest.java | 43 +++++++++++++++-- .../app/AppPresenter$$ViewStateProvider.java | 12 +++++ .../multimodules/app/AppPresenter.java | 8 ++++ .../multimodules/app/AppView$$State.java | 6 +++ .../resources/multimodules/app/AppView.java | 6 +++ .../multimodules/app/MoxyReflector.java | 47 +++++++++++++++++++ 6 files changed, 117 insertions(+), 5 deletions(-) create mode 100644 moxy-compiler/src/test/resources/multimodules/app/AppPresenter$$ViewStateProvider.java create mode 100644 moxy-compiler/src/test/resources/multimodules/app/AppPresenter.java create mode 100644 moxy-compiler/src/test/resources/multimodules/app/AppView$$State.java create mode 100644 moxy-compiler/src/test/resources/multimodules/app/AppView.java create mode 100644 moxy-compiler/src/test/resources/multimodules/app/MoxyReflector.java diff --git a/moxy-compiler/src/test/java/com/arellomobile/mvp/compiler/MultiModulesTest.java b/moxy-compiler/src/test/java/com/arellomobile/mvp/compiler/MultiModulesTest.java index 9084f709..fe767ff8 100644 --- a/moxy-compiler/src/test/java/com/arellomobile/mvp/compiler/MultiModulesTest.java +++ b/moxy-compiler/src/test/java/com/arellomobile/mvp/compiler/MultiModulesTest.java @@ -8,21 +8,54 @@ import static com.google.testing.compile.CompilationSubject.assertThat; import static com.google.testing.compile.JavaFileObjects.forResource; +import static com.google.testing.compile.JavaFileObjects.forSourceLines; public class MultiModulesTest extends CompilerTest { @Test - public void testCompileLibraryModule() throws Exception { - JavaFileObject presenter = forResource("multimodules/lib1/Lib1Presenter.java"); - JavaFileObject view = forResource("multimodules/lib1/Lib1View.java"); + public void testLibraryModule() throws Exception { + JavaFileObject[] sources = { + forResource("multimodules/lib1/Lib1Presenter.java"), + forResource("multimodules/lib1/Lib1View.java") + }; - Compilation compilation = compileLibSourcesWithProcessor("multimodules.lib1", presenter, view); - Compilation exceptedCompilation = compileSources( + JavaFileObject[] generatedSources = { forResource("multimodules/lib1/Lib1Presenter$$ViewStateProvider.java"), forResource("multimodules/lib1/Lib1View$$State.java"), forResource("multimodules/lib1/MoxyReflector.java") + }; + + Compilation compilation = compileLibSourcesWithProcessor("multimodules.lib1", sources); + Compilation exceptedCompilation = compileSources(generatedSources); + + assertThat(compilation).succeededWithoutWarnings(); + assertExceptedFilesGenerated(compilation.generatedFiles(), exceptedCompilation.generatedFiles()); + } + + @Test + public void testRegisterMoxyReflectorPackages() throws Exception { + JavaFileObject someClientClass = forSourceLines("multimodules.app.App", + "package multimodules.app;", + "import com.arellomobile.mvp.RegisterMoxyReflectorPackages;", + "@RegisterMoxyReflectorPackages(\"multimodules.lib1\")", + "public class App {}" ); + JavaFileObject[] sources = new JavaFileObject[]{ + forResource("multimodules/app/AppPresenter.java"), + forResource("multimodules/app/AppView.java"), + someClientClass + }; + + JavaFileObject[] exceptedSources = new JavaFileObject[]{ + forResource("multimodules/app/AppPresenter$$ViewStateProvider.java"), + forResource("multimodules/app/AppView$$State.java"), + forResource("multimodules/app/MoxyReflector.java") + }; + + Compilation compilation = compileSourcesWithProcessor(sources); + Compilation exceptedCompilation = compileSources(exceptedSources); + assertThat(compilation).succeededWithoutWarnings(); assertExceptedFilesGenerated(compilation.generatedFiles(), exceptedCompilation.generatedFiles()); } diff --git a/moxy-compiler/src/test/resources/multimodules/app/AppPresenter$$ViewStateProvider.java b/moxy-compiler/src/test/resources/multimodules/app/AppPresenter$$ViewStateProvider.java new file mode 100644 index 00000000..60033e91 --- /dev/null +++ b/moxy-compiler/src/test/resources/multimodules/app/AppPresenter$$ViewStateProvider.java @@ -0,0 +1,12 @@ +package multimodules.app; + +import com.arellomobile.mvp.MvpView; +import com.arellomobile.mvp.ViewStateProvider; +import com.arellomobile.mvp.viewstate.MvpViewState; + +public class AppPresenter$$ViewStateProvider extends ViewStateProvider { + @Override + public MvpViewState getViewState() { + return new AppView$$State(); + } +} diff --git a/moxy-compiler/src/test/resources/multimodules/app/AppPresenter.java b/moxy-compiler/src/test/resources/multimodules/app/AppPresenter.java new file mode 100644 index 00000000..199ebe48 --- /dev/null +++ b/moxy-compiler/src/test/resources/multimodules/app/AppPresenter.java @@ -0,0 +1,8 @@ +package multimodules.app; + +import com.arellomobile.mvp.InjectViewState; +import com.arellomobile.mvp.MvpPresenter; + +@InjectViewState +public class AppPresenter extends MvpPresenter { +} \ No newline at end of file diff --git a/moxy-compiler/src/test/resources/multimodules/app/AppView$$State.java b/moxy-compiler/src/test/resources/multimodules/app/AppView$$State.java new file mode 100644 index 00000000..e0777a94 --- /dev/null +++ b/moxy-compiler/src/test/resources/multimodules/app/AppView$$State.java @@ -0,0 +1,6 @@ +package multimodules.app; + +import com.arellomobile.mvp.viewstate.MvpViewState; + +public class AppView$$State extends MvpViewState implements AppView { +} \ No newline at end of file diff --git a/moxy-compiler/src/test/resources/multimodules/app/AppView.java b/moxy-compiler/src/test/resources/multimodules/app/AppView.java new file mode 100644 index 00000000..5ad2c6a5 --- /dev/null +++ b/moxy-compiler/src/test/resources/multimodules/app/AppView.java @@ -0,0 +1,6 @@ +package multimodules.app; + +import com.arellomobile.mvp.MvpView; + +public interface AppView extends MvpView { +} \ No newline at end of file diff --git a/moxy-compiler/src/test/resources/multimodules/app/MoxyReflector.java b/moxy-compiler/src/test/resources/multimodules/app/MoxyReflector.java new file mode 100644 index 00000000..7750af41 --- /dev/null +++ b/moxy-compiler/src/test/resources/multimodules/app/MoxyReflector.java @@ -0,0 +1,47 @@ +package com.arellomobile.mvp; + +import java.lang.Class; +import java.lang.Object; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import multimodules.app.AppPresenter; +import multimodules.app.AppPresenter$$ViewStateProvider; + +public final class MoxyReflector { + private static Map, Object> sViewStateProviders; + + private static Map, List> sPresenterBinders; + + private static Map, Object> sStrategies; + + static { + sViewStateProviders = new HashMap<>(); + sViewStateProviders.put(AppPresenter.class, new AppPresenter$$ViewStateProvider()); + + sPresenterBinders = new HashMap<>(); + + sStrategies = new HashMap<>(); + + sViewStateProviders.putAll(multimodules.lib1.MoxyReflector.getViewStateProviders()); + sPresenterBinders.putAll(multimodules.lib1.MoxyReflector.getPresenterBinders()); + sStrategies.putAll(multimodules.lib1.MoxyReflector.getStrategies()); + } + + public static Object getViewState(Class presenterClass) { + ViewStateProvider viewStateProvider = (ViewStateProvider) sViewStateProviders.get(presenterClass); + if (viewStateProvider == null) { + return null; + } + + return viewStateProvider.getViewState(); + } + + public static List getPresenterBinders(Class delegated) { + return sPresenterBinders.get(delegated); + } + + public static Object getStrategy(Class strategyClass) { + return sStrategies.get(strategyClass); + } +} \ No newline at end of file