diff --git a/build.gradle b/build.gradle index 6b020c80..7c5b156b 100644 --- a/build.gradle +++ b/build.gradle @@ -20,8 +20,8 @@ allprojects { } ext { - targetVersionCode = 12 - targetVersionName = "0.4.0" + targetVersionCode = 13 + targetVersionName = "0.4.1" } task clean(type: Delete) { delete rootProject.buildDir diff --git a/moxy-compiler/src/main/java/com/arellomobile/mvp/compiler/MvpCompiler.java b/moxy-compiler/src/main/java/com/arellomobile/mvp/compiler/MvpCompiler.java index 2bcdcb97..c8f2d33d 100644 --- a/moxy-compiler/src/main/java/com/arellomobile/mvp/compiler/MvpCompiler.java +++ b/moxy-compiler/src/main/java/com/arellomobile/mvp/compiler/MvpCompiler.java @@ -84,10 +84,18 @@ public boolean process(Set annotations, RoundEnvironment { checkInjectors(roundEnv, InjectPresenter.class, new PresenterInjectorRules(ElementKind.FIELD, Modifier.PUBLIC, Modifier.DEFAULT)); - processInjectors(roundEnv, InjectViewState.class, ElementKind.CLASS, new ViewStateProviderClassGenerator()); + ViewStateProviderClassGenerator viewStateProviderClassGenerator = new ViewStateProviderClassGenerator(); + processInjectors(roundEnv, InjectViewState.class, ElementKind.CLASS, viewStateProviderClassGenerator); processInjectors(roundEnv, InjectPresenter.class, ElementKind.FIELD, new PresenterBinderClassGenerator()); processInjectors(roundEnv, ParamsProvider.class, ElementKind.INTERFACE, new ParamsHolderClassGenerator()); - processInjectors(roundEnv, GenerateViewState.class, ElementKind.INTERFACE, new ViewStateClassGenerator()); + + ViewStateClassGenerator viewStateClassGenerator = new ViewStateClassGenerator(); + Set usedViews = viewStateProviderClassGenerator.getUsedViews(); + + for (TypeElement usedView : usedViews) + { + generateCode(ElementKind.INTERFACE, viewStateClassGenerator, usedView); + } return true; } @@ -100,9 +108,10 @@ private void checkInjectors(final RoundEnvironment roundEnv, Class 0) { - throw new RuntimeException("\n" + annotationRule.getErrorStack()); + throw new RuntimeException("\n" + errorStack); } } @@ -115,20 +124,30 @@ private void processInjectors(final RoundEnvironment roundEnv, Class classGeneratingParamsList = new ArrayList<>(); + generateCode(kind, classGenerator, annotatedElements); + } + } + + private void generateCode(ElementKind kind, ClassGenerator classGenerator, Element element) + { + if (element.getKind() != kind) + { + throw new RuntimeException(element + " must be " + kind.name()); + } + + List classGeneratingParamsList = new ArrayList<>(); - //noinspection unchecked - final boolean generated = classGenerator.generate(annotatedElements, classGeneratingParamsList); + //noinspection unchecked + final boolean generated = classGenerator.generate(element, classGeneratingParamsList); - if (!generated) - { - continue; - } + if (!generated) + { + return; + } - for (ClassGeneratingParams classGeneratingParams : classGeneratingParamsList) - { - createSourceFile(classGeneratingParams); - } + for (ClassGeneratingParams classGeneratingParams : classGeneratingParamsList) + { + createSourceFile(classGeneratingParams); } } diff --git a/moxy-compiler/src/main/java/com/arellomobile/mvp/compiler/Util.java b/moxy-compiler/src/main/java/com/arellomobile/mvp/compiler/Util.java index ac3c2848..bdb04760 100644 --- a/moxy-compiler/src/main/java/com/arellomobile/mvp/compiler/Util.java +++ b/moxy-compiler/src/main/java/com/arellomobile/mvp/compiler/Util.java @@ -26,7 +26,6 @@ import javax.lang.model.type.TypeMirror; import javax.lang.model.type.TypeVariable; import javax.lang.model.type.WildcardType; -import javax.tools.Diagnostic; /** * Utilities for handling types in annotation processors @@ -49,8 +48,6 @@ public static String fillGenerics(Map types, List 0) { result += separator; diff --git a/moxy-compiler/src/main/java/com/arellomobile/mvp/compiler/ViewStateProviderClassGenerator.java b/moxy-compiler/src/main/java/com/arellomobile/mvp/compiler/ViewStateProviderClassGenerator.java index ef186fd0..ae35420b 100644 --- a/moxy-compiler/src/main/java/com/arellomobile/mvp/compiler/ViewStateProviderClassGenerator.java +++ b/moxy-compiler/src/main/java/com/arellomobile/mvp/compiler/ViewStateProviderClassGenerator.java @@ -2,8 +2,10 @@ import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import com.arellomobile.mvp.DefaultView; import com.arellomobile.mvp.DefaultViewState; @@ -31,7 +33,13 @@ final class ViewStateProviderClassGenerator extends ClassGenerator { public static final String MVP_PRESENTER_CLASS = MvpPresenter.class.getCanonicalName(); - public static final String REGEX = ".*<(.*)>.*"; + + private Set mUsedViews; + + public ViewStateProviderClassGenerator() + { + mUsedViews = new HashSet<>(); + } @Override public boolean generate(TypeElement typeElement, List classGeneratingParamsList) @@ -40,9 +48,27 @@ public boolean generate(TypeElement typeElement, List cla final String viewClassName = parentClassName.substring(parentClassName.lastIndexOf(".") + 1); - String view = getViewClassFromGeneric(typeElement); - view = getViewClassFromAnnotationParams(typeElement, view); - String viewState = getViewStateClassFromAnnotationParams(typeElement, view); + String viewState = getViewStateClassFromAnnotationParams(typeElement); + if (viewState == null) + { + String view = getViewClassFromAnnotationParams(typeElement); + if (view == null) + { + view = getViewClassFromGeneric(typeElement); + } + + if (view != null) + { + TypeElement viewTypeElement = MvpCompiler.getElementUtils().getTypeElement(view); + if (viewTypeElement == null) + { + throw new IllegalArgumentException("View \"" + view + "\" for " + typeElement + " cannot be found"); + } + + mUsedViews.add(viewTypeElement); + viewState = Util.getFullClassName(viewTypeElement) + MvpProcessor.VIEW_STATE_SUFFIX; + } + } String builder = "package " + parentClassName.substring(0, parentClassName.lastIndexOf(".")) + ";\n" + "\n" + @@ -71,7 +97,7 @@ public boolean generate(TypeElement typeElement, List cla return true; } - private String getViewClassFromAnnotationParams(TypeElement typeElement, String viewClassName) + private String getViewClassFromAnnotationParams(TypeElement typeElement) { InjectViewState annotation = typeElement.getAnnotation(InjectViewState.class); String mvpViewClassName = ""; @@ -91,22 +117,22 @@ private String getViewClassFromAnnotationParams(TypeElement typeElement, String mvpViewClassName = Util.getFullClassName(value); } - if (!mvpViewClassName.isEmpty() && !DefaultView.class.getName().equals(mvpViewClassName)) + if (mvpViewClassName.isEmpty() || DefaultView.class.getName().equals(mvpViewClassName)) { - return mvpViewClassName; + return null; } - return viewClassName; + return mvpViewClassName; } - private String getViewStateClassFromAnnotationParams(TypeElement typeElement, String viewClassName) + private String getViewStateClassFromAnnotationParams(TypeElement typeElement) { InjectViewState annotation = typeElement.getAnnotation(InjectViewState.class); String mvpViewStateClassName = ""; if (annotation != null) { - TypeMirror value = null; + TypeMirror value; try { annotation.value(); @@ -114,22 +140,16 @@ private String getViewStateClassFromAnnotationParams(TypeElement typeElement, St catch (MirroredTypeException mte) { value = mte.getTypeMirror(); + mvpViewStateClassName = value.toString(); } - - mvpViewStateClassName = Util.getFullClassName(value); - } - - if (!mvpViewStateClassName.isEmpty() && !DefaultViewState.class.getName().equals(mvpViewStateClassName)) - { - return mvpViewStateClassName; } - if (viewClassName.length() == 0) + if (mvpViewStateClassName.isEmpty() || DefaultViewState.class.getName().equals(mvpViewStateClassName)) { - throw new IllegalArgumentException("MvpPresenter(superclass for " + typeElement.getSimpleName() + ") doesn't know, what is target MvpView type of this presenter."); + return null; } - return viewClassName + MvpProcessor.VIEW_STATE_SUFFIX; + return mvpViewStateClassName; } private String getViewClassFromGeneric(TypeElement typeElement) @@ -140,7 +160,7 @@ private String getViewClassFromGeneric(TypeElement typeElement) if (!typeElement.getTypeParameters().isEmpty()) { - MvpCompiler.getMessager().printMessage(Diagnostic.Kind.WARNING, "Your " + typeElement.getSimpleName() + " is typed. @InjectViewState may generate wrong code. Your can set view class manually."); + MvpCompiler.getMessager().printMessage(Diagnostic.Kind.WARNING, "Your " + typeElement.getSimpleName() + " is typed. @InjectViewState may generate wrong code. Your can set view/view state class manually."); } while (superclass.getKind() != TypeKind.NONE) @@ -164,10 +184,7 @@ private String getViewClassFromGeneric(TypeElement typeElement) if (superclassElement.toString().equals(MVP_PRESENTER_CLASS)) { // MvpPresenter is typed only on View class - // Here we detect this view and return full name of its view state - String name = fillGenerics(parentTypes, typeArguments); - TypeElement viewElement = MvpCompiler.getElementUtils().getTypeElement(name); - return Util.getFullClassName(viewElement); + return fillGenerics(parentTypes, typeArguments); } parentTypes = types; @@ -177,4 +194,9 @@ private String getViewClassFromGeneric(TypeElement typeElement) return ""; } + + public Set getUsedViews() + { + return mUsedViews; + } } diff --git a/moxy/src/main/java/com/arellomobile/mvp/GenerateViewState.java b/moxy/src/main/java/com/arellomobile/mvp/GenerateViewState.java index dfa84cc4..13c07321 100644 --- a/moxy/src/main/java/com/arellomobile/mvp/GenerateViewState.java +++ b/moxy/src/main/java/com/arellomobile/mvp/GenerateViewState.java @@ -8,8 +8,11 @@ /** *

Generate view state class for annotated view interface.

*

Generated class implements this view interface.

+ * + * @deprecated As of release 0.4.1, {@link InjectViewState} generate view state, if it needed */ @Target(value = TYPE) +@Deprecated public @interface GenerateViewState { }