Skip to content

Commit

Permalink
Merge pull request #117 from ekursakov/multi-module-moxy-reflector
Browse files Browse the repository at this point in the history
Allow to specify MoxyReflector package and use it from another module
  • Loading branch information
senneco authored May 4, 2017
2 parents d5daf0e + d2d68fc commit 5167d89
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@

public class MoxyReflectorGenerator {

public static String generate(List<String> presenterClassNames, Set<TypeElement> presentersContainers, Set<String> strategyClasses) {
public static String generate(String moxyReflectorPackage,
List<String> presenterClassNames,
Set<TypeElement> presentersContainers,
Set<String> strategyClasses,
List<String> additionalMoxyReflectorsPackages) {
Map<TypeElement, TypeElement> extendingMap = new HashMap<>();

for (TypeElement presentersContainer : presentersContainers) {
Expand Down Expand Up @@ -60,7 +64,7 @@ public static String generate(List<String> presenterClassNames, Set<TypeElement>
}
}

String builder = "package com.arellomobile.mvp;\n" +
String builder = "package " + moxyReflectorPackage + ";\n" +
"\n" +
"import java.util.Arrays;\n" +
"import java.util.HashMap;\n" +
Expand Down Expand Up @@ -118,8 +122,19 @@ public int compare(Map.Entry<TypeElement, List<TypeElement>> entry1, Map.Entry<T
builder += "\t\tsStrategies.put(" + strategyClass + ", new " + strategyClass.substring(0, strategyClass.lastIndexOf('.')) + "());\n";
}

builder += "\t}\n" +
"\t\n" +
Collections.sort(additionalMoxyReflectorsPackages);

for (String pkg : additionalMoxyReflectorsPackages) {
builder += "\t\t\n";
builder += "\t\tsViewStateProviders.putAll(" + pkg + ".MoxyReflector.getViewStateProviders());\n";
builder += "\t\tsPresenterBinders.putAll(" + pkg + ".MoxyReflector.getPresenterBinders());\n";
builder += "\t\tsStrategies.putAll(" + pkg + ".MoxyReflector.getStrategies());\n";
}

builder += "\t}\n";

if (moxyReflectorPackage.equals("com.arellomobile.mvp")) {
builder += "\t\n" +
"\tpublic static Object getViewState(Class<?> presenterClass) {\n" +
"\t\tViewStateProvider viewStateProvider = (ViewStateProvider) sViewStateProviders.get(presenterClass);\n" +
"\t\tif (viewStateProvider == null) {\n" +
Expand All @@ -132,11 +147,26 @@ public int compare(Map.Entry<TypeElement, List<TypeElement>> entry1, Map.Entry<T
"\tpublic static List<Object> getPresenterBinders(Class<?> delegated) {\n" +
"\t\treturn sPresenterBinders.get(delegated);\n" +
"\t}\n" +
"\n" +
"\tpublic static Object getStrategy(Class<?> strategyClass) {\n" +
"\t\treturn sStrategies.get(strategyClass);\n" +
"\t}\n" +
"}\n";

} else {
builder += "\t\n" +
"\tpublic static Map<Class<?>, Object> getViewStateProviders() {\n" +
"\t\treturn sViewStateProviders;\n" +
"\t}\n" +
"\n" +
"\tpublic static Map<Class<?>, List<Object>> getPresenterBinders() {\n" +
"\t\treturn sPresenterBinders;\n" +
"\t}\n" +
"\n" +
"\tpublic static Map<Class<?>, Object> getStrategies() {\n" +
"\t\treturn sStrategies;\n" +
"\t}\n" +
"}\n";
}
return builder;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.arellomobile.mvp.compiler;

import com.arellomobile.mvp.GenerateViewState;
import com.arellomobile.mvp.InjectViewState;
import com.arellomobile.mvp.RegisterMoxyReflectorPackages;
import com.arellomobile.mvp.presenter.InjectPresenter;
import com.google.auto.service.AutoService;

import java.io.IOException;
import java.io.Writer;
import java.lang.annotation.Annotation;
Expand All @@ -10,11 +16,6 @@
import java.util.Map;
import java.util.Set;

import com.arellomobile.mvp.GenerateViewState;
import com.arellomobile.mvp.InjectViewState;
import com.arellomobile.mvp.presenter.InjectPresenter;
import com.google.auto.service.AutoService;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
Expand Down Expand Up @@ -47,16 +48,6 @@ public class MvpCompiler extends AbstractProcessor {
private static Elements sElementUtils;
private static Map<String, String> sOptions;

@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);

sMessager = processingEnv.getMessager();
sTypeUtils = processingEnv.getTypeUtils();
sElementUtils = processingEnv.getElementUtils();
sOptions = processingEnv.getOptions();
}

public static Messager getMessager() {
return sMessager;
}
Expand All @@ -69,10 +60,24 @@ public static Elements getElementUtils() {
return sElementUtils;
}

@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);

sMessager = processingEnv.getMessager();
sTypeUtils = processingEnv.getTypeUtils();
sElementUtils = processingEnv.getElementUtils();
sOptions = processingEnv.getOptions();
}

@Override
public Set<String> getSupportedAnnotationTypes() {
Set<String> supportedAnnotationTypes = new HashSet<>();
Collections.addAll(supportedAnnotationTypes, InjectPresenter.class.getCanonicalName(), InjectViewState.class.getCanonicalName(), GenerateViewState.class.getCanonicalName());
Collections.addAll(supportedAnnotationTypes,
InjectPresenter.class.getCanonicalName(),
InjectViewState.class.getCanonicalName(),
RegisterMoxyReflectorPackages.class.getCanonicalName(),
GenerateViewState.class.getCanonicalName());
return supportedAnnotationTypes;
}

Expand Down Expand Up @@ -113,17 +118,46 @@ private boolean throwableProcess(RoundEnvironment roundEnv) {
generateCode(ElementKind.INTERFACE, viewStateClassGenerator, usedView);
}

String moxyReflector = MoxyReflectorGenerator.generate(viewStateProviderClassGenerator.getPresenterClassNames(), presenterBinderClassGenerator.getPresentersContainers(), viewStateClassGenerator.getStrategyClasses());
String moxyReflectorPackage = sOptions.get("moxyReflectorPackage");

if (moxyReflectorPackage == null) {
moxyReflectorPackage = "com.arellomobile.mvp";
}

List<String> additionalMoxyReflectorPackages = getAdditionalMoxyReflectorPackages(roundEnv);

String moxyReflector = MoxyReflectorGenerator.generate(
moxyReflectorPackage,
viewStateProviderClassGenerator.getPresenterClassNames(),
presenterBinderClassGenerator.getPresentersContainers(),
viewStateClassGenerator.getStrategyClasses(),
additionalMoxyReflectorPackages);

ClassGeneratingParams classGeneratingParams = new ClassGeneratingParams();
classGeneratingParams.setName("com.arellomobile.mvp.MoxyReflector");
classGeneratingParams.setName(moxyReflectorPackage + ".MoxyReflector");
classGeneratingParams.setBody(moxyReflector);

createSourceFile(classGeneratingParams);

return true;
}

private List<String> getAdditionalMoxyReflectorPackages(RoundEnvironment roundEnv) {
List<String> result = new ArrayList<>();

for (Element element : roundEnv.getElementsAnnotatedWith(RegisterMoxyReflectorPackages.class)) {
if (element.getKind() != ElementKind.CLASS) {
getMessager().printMessage(Diagnostic.Kind.ERROR, element + " must be " + ElementKind.CLASS.name() + ", or not mark it as @" + RegisterMoxyReflectorPackages.class.getSimpleName());
}

String[] packages = element.getAnnotation(RegisterMoxyReflectorPackages.class).value();

Collections.addAll(result, packages);
}

return result;
}


private void checkInjectors(final RoundEnvironment roundEnv, Class<? extends Annotation> clazz, AnnotationRule annotationRule) {
for (Element annotatedElement : roundEnv.getElementsAnnotatedWith(clazz)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.arellomobile.mvp;

import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.TYPE;

/**
* Register MoxyReflector packages from other modules
*/
@Target(value = TYPE)
public @interface RegisterMoxyReflectorPackages {
String[] value();
}

0 comments on commit 5167d89

Please sign in to comment.