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..fe767ff8 --- /dev/null +++ b/moxy-compiler/src/test/java/com/arellomobile/mvp/compiler/MultiModulesTest.java @@ -0,0 +1,62 @@ +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; +import static com.google.testing.compile.JavaFileObjects.forSourceLines; + +public class MultiModulesTest extends CompilerTest { + + @Test + public void testLibraryModule() throws Exception { + JavaFileObject[] sources = { + forResource("multimodules/lib1/Lib1Presenter.java"), + forResource("multimodules/lib1/Lib1View.java") + }; + + 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 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; + } +}