Skip to content

Commit

Permalink
[fixbug] Fix generation of MoxyReflector in library modules
Browse files Browse the repository at this point in the history
  • Loading branch information
senneco authored Nov 24, 2018
2 parents 095695a + c86af97 commit 1b44c5a
Show file tree
Hide file tree
Showing 13 changed files with 216 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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<? extends MvpView> getViewState() {
return new AppView$$State();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package multimodules.app;

import com.arellomobile.mvp.InjectViewState;
import com.arellomobile.mvp.MvpPresenter;

@InjectViewState
public class AppPresenter extends MvpPresenter<AppView> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package multimodules.app;

import com.arellomobile.mvp.viewstate.MvpViewState;

public class AppView$$State extends MvpViewState<AppView> implements AppView {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package multimodules.app;

import com.arellomobile.mvp.MvpView;

public interface AppView extends MvpView {
}
Original file line number Diff line number Diff line change
@@ -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<Class<?>, Object> sViewStateProviders;

private static Map<Class<?>, List<Object>> sPresenterBinders;

private static Map<Class<?>, 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<Object> getPresenterBinders(Class<?> delegated) {
return sPresenterBinders.get(delegated);
}

public static Object getStrategy(Class<?> strategyClass) {
return sStrategies.get(strategyClass);
}
}
Original file line number Diff line number Diff line change
@@ -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<? extends MvpView> getViewState() {
return new Lib1View$$State();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package multimodules.lib1;

import com.arellomobile.mvp.InjectViewState;
import com.arellomobile.mvp.MvpPresenter;

@InjectViewState
public class Lib1Presenter extends MvpPresenter<Lib1View> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package multimodules.lib1;

import com.arellomobile.mvp.viewstate.MvpViewState;

public class Lib1View$$State extends MvpViewState<Lib1View> implements Lib1View {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package multimodules.lib1;

import com.arellomobile.mvp.MvpView;

public interface Lib1View extends MvpView {
}
Original file line number Diff line number Diff line change
@@ -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<Class<?>, Object> sViewStateProviders;

private static Map<Class<?>, List<Object>> sPresenterBinders;

private static Map<Class<?>, Object> sStrategies;

static {
sViewStateProviders = new HashMap<>();
sViewStateProviders.put(Lib1Presenter.class, new Lib1Presenter$$ViewStateProvider());

sPresenterBinders = new HashMap<>();

sStrategies = new HashMap<>();
}

public static Map<Class<?>, Object> getViewStateProviders() {
return sViewStateProviders;
}

public static Map<Class<?>, List<Object>> getPresenterBinders() {
return sPresenterBinders;
}

public static Map<Class<?>, Object> getStrategies() {
return sStrategies;
}
}

0 comments on commit 1b44c5a

Please sign in to comment.