Skip to content

Commit

Permalink
Annotation @GenerateViewState was deprecated because @InjectViewState…
Browse files Browse the repository at this point in the history
… generates view state, if it needed
  • Loading branch information
senneco committed Feb 29, 2016
1 parent 716b733 commit 2e0576a
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 45 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,18 @@ public boolean process(Set<? extends TypeElement> 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<TypeElement> usedViews = viewStateProviderClassGenerator.getUsedViews();

for (TypeElement usedView : usedViews)
{
generateCode(ElementKind.INTERFACE, viewStateClassGenerator, usedView);
}

return true;
}
Expand All @@ -100,9 +108,10 @@ private void checkInjectors(final RoundEnvironment roundEnv, Class<? extends Ann
annotationRule.checkAnnotation(annotatedElement);
}

if (!annotationRule.getErrorStack().isEmpty())
String errorStack = annotationRule.getErrorStack();
if (errorStack != null && errorStack.length() > 0)
{
throw new RuntimeException("\n" + annotationRule.getErrorStack());
throw new RuntimeException("\n" + errorStack);
}
}

Expand All @@ -115,20 +124,30 @@ private void processInjectors(final RoundEnvironment roundEnv, Class<? extends A
throw new RuntimeException(annotatedElements + " must be " + kind.name() + ", or not mark it as @" + clazz.getSimpleName());
}

List<ClassGeneratingParams> 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<ClassGeneratingParams> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -49,8 +48,6 @@ public static String fillGenerics(Map<String, String> types, List<? extends Type

for (TypeMirror param : params)
{
MvpCompiler.getMessager().printMessage(Diagnostic.Kind.NOTE, "Param: " + param + ", type: " + param.getKind());

if (result.length() > 0)
{
result += separator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -31,7 +33,13 @@
final class ViewStateProviderClassGenerator extends ClassGenerator<TypeElement>
{
public static final String MVP_PRESENTER_CLASS = MvpPresenter.class.getCanonicalName();
public static final String REGEX = ".*<(.*)>.*";

private Set<TypeElement> mUsedViews;

public ViewStateProviderClassGenerator()
{
mUsedViews = new HashSet<>();
}

@Override
public boolean generate(TypeElement typeElement, List<ClassGeneratingParams> classGeneratingParamsList)
Expand All @@ -40,9 +48,27 @@ public boolean generate(TypeElement typeElement, List<ClassGeneratingParams> 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" +
Expand Down Expand Up @@ -71,7 +97,7 @@ public boolean generate(TypeElement typeElement, List<ClassGeneratingParams> cla
return true;
}

private String getViewClassFromAnnotationParams(TypeElement typeElement, String viewClassName)
private String getViewClassFromAnnotationParams(TypeElement typeElement)
{
InjectViewState annotation = typeElement.getAnnotation(InjectViewState.class);
String mvpViewClassName = "";
Expand All @@ -91,45 +117,39 @@ 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();
}
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)
Expand All @@ -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)
Expand All @@ -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;
Expand All @@ -177,4 +194,9 @@ private String getViewClassFromGeneric(TypeElement typeElement)

return "";
}

public Set<TypeElement> getUsedViews()
{
return mUsedViews;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
/**
* <p>Generate view state class for annotated view interface.</p>
* <p>Generated class implements this view interface.</p>
*
* @deprecated As of release 0.4.1, {@link InjectViewState} generate view state, if it needed
*/
@Target(value = TYPE)
@Deprecated
public @interface GenerateViewState
{
}

0 comments on commit 2e0576a

Please sign in to comment.