Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make generated models from views inherit constructors from base model #315

Merged
merged 1 commit into from
Oct 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {

ext.KOTLIN_VERSION = "1.1.50"
ext.KOTLIN_VERSION = "1.1.51"
ext.ANDROID_PLUGIN_VERSION = "3.0.0-beta7"

repositories {
Expand Down
11 changes: 11 additions & 0 deletions epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,17 @@ private static long hashString64Bit(CharSequence str) {
return result;
}

/**
* Return the default layout resource to be used when creating views for this model. The resource
* will be inflated to create a view for the model; additionally the layout int is used as the
* views type in the RecyclerView.
* <p>
* This can be left unimplemented if you use the {@link EpoxyModelClass} annotation to define a
* layout.
* <p>
* This default value can be overridden with {@link #layout(int)} at runtime to change the layout
* dynamically.
*/
@LayoutRes
protected abstract int getDefaultLayout();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,17 @@
import java.util.List;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.TypeParameterElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;

import static com.airbnb.epoxy.Utils.getElementByName;
import static com.airbnb.epoxy.Utils.getEpoxyObjectType;

class BasicGeneratedModelInfo extends GeneratedModelInfo {
class BasicGeneratedModelInfo extends GeneratedModelInfo {

private final Elements elementUtils;

Expand All @@ -38,7 +34,7 @@ class BasicGeneratedModelInfo extends GeneratedModelInfo {
typeVariableNames.add(TypeVariableName.get(typeParameterElement));
}

collectOriginalClassConstructors(superClassElement);
constructors.addAll(getClassConstructors(superClassElement));
collectMethodsReturningClassType(superClassElement, typeUtils);

if (!typeVariableNames.isEmpty()) {
Expand Down Expand Up @@ -83,39 +79,22 @@ protected ClassName buildGeneratedModelName(TypeElement classElement) {
return ClassName.get(packageName, className + GENERATED_CLASS_NAME_SUFFIX);
}

/**
* Get information about constructors of the original class so we can duplicate them in the
* generated class and call through to super with the proper parameters
*/
private void collectOriginalClassConstructors(TypeElement originalClass) {
for (Element subElement : originalClass.getEnclosedElements()) {
if (subElement.getKind() == ElementKind.CONSTRUCTOR
&& !subElement.getModifiers().contains(Modifier.PRIVATE)) {
ExecutableElement castedSubElement = ((ExecutableElement) subElement);
List<? extends VariableElement> params = castedSubElement.getParameters();
constructors
.add(new ConstructorInfo(subElement.getModifiers(), buildParamSpecs(params),
castedSubElement.isVarArgs()));
private void buildAnnotationLists(List<? extends AnnotationMirror> annotationMirrors) {
for (AnnotationMirror annotationMirror : annotationMirrors) {
if (!annotationMirror.getElementValues().isEmpty()) {
// Not supporting annotations with values for now
continue;
}

ClassName annotationClass =
ClassName.bestGuess(annotationMirror.getAnnotationType().toString());
if (annotationClass.equals(ClassName.get(EpoxyModelClass.class))) {
// Don't include our own annotation
continue;
}

AnnotationSpec annotationSpec = AnnotationSpec.builder(annotationClass).build();
annotations.add(annotationSpec);
}
}

private void buildAnnotationLists(List<? extends AnnotationMirror> annotationMirrors) {
for (AnnotationMirror annotationMirror : annotationMirrors) {
if (!annotationMirror.getElementValues().isEmpty()) {
// Not supporting annotations with values for now
continue;
}

ClassName annotationClass =
ClassName.bestGuess(annotationMirror.getAnnotationType().toString());
if (annotationClass.equals(ClassName.get(EpoxyModelClass.class))) {
// Don't include our own annotation
continue;
}

AnnotationSpec annotationSpec = AnnotationSpec.builder(annotationClass).build();
annotations.add(annotationSpec);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,27 @@ abstract class GeneratedModelInfo {
*/
Size layoutParams = Size.NONE;

/**
* Get information about constructors of the original class so we can duplicate them in the
* generated class and call through to super with the proper parameters
*/
protected static List<ConstructorInfo> getClassConstructors(TypeElement classElement) {
List<ConstructorInfo> constructors = new ArrayList<>(2);

for (Element subElement : classElement.getEnclosedElements()) {
if (subElement.getKind() == ElementKind.CONSTRUCTOR
&& !subElement.getModifiers().contains(Modifier.PRIVATE)) {

ExecutableElement constructor = ((ExecutableElement) subElement);
List<? extends VariableElement> params = constructor.getParameters();
constructors.add(new ConstructorInfo(subElement.getModifiers(), buildParamSpecs(params),
constructor.isVarArgs()));
}
}

return constructors;
}

/**
* Get information about methods returning class type of the original class so we can duplicate
* them in the generated class for chaining purposes
Expand Down Expand Up @@ -102,7 +123,7 @@ protected void collectMethodsReturningClassType(TypeElement modelClass, Types ty
}
}

protected List<ParameterSpec> buildParamSpecs(List<? extends VariableElement> params) {
protected static List<ParameterSpec> buildParamSpecs(List<? extends VariableElement> params) {
List<ParameterSpec> result = new ArrayList<>();

for (VariableElement param : params) {
Expand Down
205 changes: 0 additions & 205 deletions epoxy-processor/src/main/java/com/airbnb/epoxy/ModelViewInfo.java

This file was deleted.

Loading