Skip to content

Commit

Permalink
Add inheritence support
Browse files Browse the repository at this point in the history
  • Loading branch information
emilsjolander committed Jun 28, 2015
1 parent 97219dd commit 9791dad
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.squareup.javapoet.TypeSpec;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand All @@ -22,9 +23,11 @@
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import javax.tools.Diagnostic;
Expand Down Expand Up @@ -96,15 +99,7 @@ private TypeSpec getBuilderSpec(Element annotatedElement) {
List<Element> optional = new ArrayList<>();
List<Element> all = new ArrayList<>();

for (Element e : annotatedElement.getEnclosedElements()) {
if (e.getAnnotation(Extra.class) != null) {
if (e.getAnnotation(se.emilsjolander.intentbuilder.Optional.class) != null) {
optional.add(e);
} else {
required.add(e);
}
}
}
getAnnotatedFields(annotatedElement, required, optional);
all.addAll(required);
all.addAll(optional);

Expand Down Expand Up @@ -164,4 +159,23 @@ private TypeSpec getBuilderSpec(Element annotatedElement) {
return builder.build();
}

private void getAnnotatedFields(Element annotatedElement, List<Element> required, List<Element> optional) {
for (Element e : annotatedElement.getEnclosedElements()) {
if (e.getAnnotation(Extra.class) != null) {
if (e.getAnnotation(se.emilsjolander.intentbuilder.Optional.class) != null) {
optional.add(e);
} else {
required.add(e);
}
}
}

List<? extends TypeMirror> superTypes = Processor.instance.typeUtils.directSupertypes(annotatedElement.asType());
TypeMirror superClassType = superTypes.size() > 0 ? superTypes.get(0) : null;
Element superClass = superClassType == null ? null : Processor.instance.typeUtils.asElement(superClassType);
if (superClass != null && superClass.getKind() == ElementKind.CLASS) {
getAnnotatedFields(superClass, required, optional);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package se.emilsjolander.intentbuilder.sample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import se.emilsjolander.intentbuilder.Extra;
import se.emilsjolander.intentbuilder.IntentBuilder;
import se.emilsjolander.intentbuilder.Optional;

@IntentBuilder
public class MySubClass extends MySuperClass {

@Extra @Optional
String three;

@Extra @Optional
String four;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MySubClassIntentBuilder.inject(getIntent(), this);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package se.emilsjolander.intentbuilder.sample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import se.emilsjolander.intentbuilder.Extra;
import se.emilsjolander.intentbuilder.IntentBuilder;
import se.emilsjolander.intentbuilder.Optional;

public class MySuperClass extends AppCompatActivity {

@Extra
String one;

@Extra
String two;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

}

0 comments on commit 9791dad

Please sign in to comment.