Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Prev Previous commit
Next Next commit
Add support for ANDROIDX_VIEW_PAGER and ANDROIDX_PAGE_CHANGE_LISTENER
  • Loading branch information
dodgex committed Jul 1, 2018
commit 3ff291632741c5cd4fe0360f41a00f47b7a838db
Original file line number Diff line number Diff line change
Expand Up @@ -766,8 +766,9 @@ public void isPreferenceFragmentClassPresent(Element element, ElementValidation
}

public void isViewPagerClassPresent(ElementValidation validation) {
if (!isClassPresent(CanonicalNameConstants.VIEW_PAGER)) {
validation.addError("The class " + CanonicalNameConstants.VIEW_PAGER + " cannot be found. You have to include support v4 library");
if (!isClassPresent(CanonicalNameConstants.VIEW_PAGER) && !isClassPresent(CanonicalNameConstants.ANDROIDX_VIEW_PAGER)) {
validation.addError("The classes " + CanonicalNameConstants.VIEW_PAGER + " and " + CanonicalNameConstants.ANDROIDX_VIEW_PAGER
+ " cannot be found. You have to include support-v4 or androidx.viewpager library");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,15 @@ public PageChangeHolder getPageChangeHolder(JFieldRef idRef, TypeMirror viewPara
}

private PageChangeHolder createPageChangeHolder(JFieldRef idRef, TypeMirror viewParameterType, boolean hasAddOnPageChangeListenerMethod) {
JDefinedClass onPageChangeListenerClass = getCodeModel().anonymousClass(getClasses().PAGE_CHANGE_LISTENER);
AbstractJClass viewClass = getClasses().VIEW_PAGER;
AbstractJClass viewClass;
JDefinedClass onPageChangeListenerClass;
if (getProcessingEnvironment().getElementUtils().getTypeElement(CanonicalNameConstants.ANDROIDX_VIEW_PAGER) == null) {
viewClass = getClasses().VIEW_PAGER;
onPageChangeListenerClass = getCodeModel().anonymousClass(getClasses().PAGE_CHANGE_LISTENER);
} else {
viewClass = getClasses().ANDROIDX_VIEW_PAGER;
onPageChangeListenerClass = getCodeModel().anonymousClass(getClasses().ANDROIDX_PAGE_CHANGE_LISTENER);
}
if (viewParameterType != null) {
viewClass = getJClass(viewParameterType.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeMirror;

import org.androidannotations.AndroidAnnotationsEnvironment;
import org.androidannotations.ElementValidation;
Expand Down Expand Up @@ -51,6 +52,15 @@ protected void validate(Element element, ElementValidation validation) {

protected boolean hasAddOnPageChangeListenerMethod() {
TypeElement viewPager = getProcessingEnvironment().getElementUtils().getTypeElement(CanonicalNameConstants.VIEW_PAGER);
return hasTargetMethod(viewPager, "addOnPageChangeListener");
TypeElement androidxViewPager = getProcessingEnvironment().getElementUtils().getTypeElement(CanonicalNameConstants.ANDROIDX_VIEW_PAGER);
return hasTargetMethod(viewPager, "addOnPageChangeListener") || hasTargetMethod(androidxViewPager, "addOnPageChangeListener");
}

protected boolean isViewPagerParameter(TypeMirror parameterType) {
TypeElement viewPagerTypeElement = annotationHelper.typeElementFromQualifiedName(CanonicalNameConstants.VIEW_PAGER);
TypeElement androidxViewPagerTypeElement = annotationHelper.typeElementFromQualifiedName(CanonicalNameConstants.ANDROIDX_VIEW_PAGER);
TypeMirror viewPagerType = viewPagerTypeElement == null ? null : viewPagerTypeElement.asType();
TypeMirror androidxViewPagerType = androidxViewPagerTypeElement == null ? null : androidxViewPagerTypeElement.asType();
return viewPagerType != null && annotationHelper.isSubtype(parameterType, viewPagerType) || androidxViewPagerType != null && annotationHelper.isSubtype(parameterType, androidxViewPagerType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void validate(Element element, ElementValidation validation) {
super.validate(element, validation);

validatorHelper.param.anyOrder() //
.type(CanonicalNameConstants.VIEW_PAGER).optional() //
.anyOfTypes(CanonicalNameConstants.VIEW_PAGER, CanonicalNameConstants.ANDROIDX_VIEW_PAGER).optional() //
.primitiveOrWrapper(TypeKind.INT).optional() //
.validate((ExecutableElement) element, validation);
}
Expand All @@ -71,8 +71,7 @@ public void process(Element element, EComponentWithViewSupportHolder holder) {
if (parameterType.getKind() == TypeKind.INT || CanonicalNameConstants.INTEGER.equals(parameterType.toString())) {
stateParameterPosition = i;
} else {
TypeMirror viewPagerType = annotationHelper.typeElementFromQualifiedName(CanonicalNameConstants.VIEW_PAGER).asType();
if (annotationHelper.isSubtype(parameterType, viewPagerType)) {
if (isViewPagerParameter(parameterType)) {
viewPagerParameterPosition = i;
viewPagerParameterType = parameterType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected void validate(Element element, ElementValidation validation) {
super.validate(element, validation);

validatorHelper.param.inOrder() //
.type(CanonicalNameConstants.VIEW_PAGER).optional() //
.anyOfTypes(CanonicalNameConstants.VIEW_PAGER, CanonicalNameConstants.ANDROIDX_VIEW_PAGER).optional() //
.primitiveOrWrapper(TypeKind.INT).optional() //
.primitiveOrWrapper(TypeKind.FLOAT).optional() //
.primitiveOrWrapper(TypeKind.INT).optional() //
Expand Down Expand Up @@ -83,8 +83,7 @@ public void process(Element element, EComponentWithViewSupportHolder holder) thr
} else if (parameterType.getKind() == TypeKind.FLOAT || CanonicalNameConstants.FLOAT.equals(parameterType.toString())) {
positionOffsetParameterPosition = i;
} else {
TypeMirror viewPagerType = annotationHelper.typeElementFromQualifiedName(CanonicalNameConstants.VIEW_PAGER).asType();
if (annotationHelper.isSubtype(parameterType, viewPagerType)) {
if (isViewPagerParameter(parameterType)) {
viewPagerParameterPosition = i;
viewPagerParameterType = parameterType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected void validate(Element element, ElementValidation validation) {
super.validate(element, validation);

validatorHelper.param.anyOrder() //
.type(CanonicalNameConstants.VIEW_PAGER).optional() //
.anyOfTypes(CanonicalNameConstants.VIEW_PAGER, CanonicalNameConstants.ANDROIDX_VIEW_PAGER).optional() //
.primitiveOrWrapper(TypeKind.INT).optional() //
.validate((ExecutableElement) element, validation);
}
Expand All @@ -70,8 +70,7 @@ public void process(Element element, EComponentWithViewSupportHolder holder) thr
if (parameterType.getKind() == TypeKind.INT || CanonicalNameConstants.INTEGER.equals(parameterType.toString())) {
positionParameterPosition = i;
} else {
TypeMirror viewPagerType = annotationHelper.typeElementFromQualifiedName(CanonicalNameConstants.VIEW_PAGER).asType();
if (annotationHelper.isSubtype(parameterType, viewPagerType)) {
if (isViewPagerParameter(parameterType)) {
viewPagerParameterPosition = i;
viewPagerParameterType = parameterType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ public class Classes {
public final AbstractJClass ANDROIDX_PREFERENCE = refClass(CanonicalNameConstants.ANDROIDX_PREFERENCE);
public final AbstractJClass ANDROIDX_PREFERENCE_CLICK_LISTENER = refClass(CanonicalNameConstants.ANDROIDX_PREFERENCE_CLICK_LISTENER);
public final AbstractJClass ANDROIDX_PREFERENCE_CHANGE_LISTENER = refClass(CanonicalNameConstants.ANDROIDX_PREFERENCE_CHANGE_LISTENER);
public final AbstractJClass ANDROIDX_VIEW_PAGER = refClass(CanonicalNameConstants.ANDROIDX_VIEW_PAGER);
public final AbstractJClass ANDROIDX_PAGE_CHANGE_LISTENER = refClass(CanonicalNameConstants.ANDROIDX_PAGE_CHANGE_LISTENER);

/*
* HttpClient
Expand Down