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

Add subcomponents support in @AutoSubcomponent #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ protected JavaFile compose(SubcomponentSpec subcomponentSpec) {
builder.addMethod(exposeBuilder.build());
}

if (!subcomponentSpec.getSubcomponentsSpecs().isEmpty()) {
builder.addMethods(subcomponentSpec.getSubcomponentsSpecs());
}

TypeSpec typeSpec = builder.build();
return JavaFile.builder(subcomponentSpec.getClassName().packageName(), typeSpec).build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package autodagger.compiler;

import com.google.auto.common.MoreElements;
import com.google.auto.common.MoreTypes;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -15,6 +16,7 @@

import autodagger.AutoSubcomponent;
import autodagger.compiler.utils.AutoComponentExtractorUtil;
import dagger.Subcomponent;
import processorworkflow.AbstractExtractor;
import processorworkflow.Errors;
import processorworkflow.ExtractorUtils;
Expand All @@ -26,6 +28,7 @@ public class SubcomponentExtractor extends AbstractExtractor {

private List<TypeMirror> modulesTypeMirrors;
private List<TypeMirror> superinterfacesTypeMirrors;
private List<TypeMirror> subcomponentsTypeMirrors;
private AnnotationMirror scopeAnnotationTypeMirror;

public SubcomponentExtractor(Element element, Types types, Elements elements, Errors errors) {
Expand All @@ -37,6 +40,7 @@ public SubcomponentExtractor(Element element, Types types, Elements elements, Er
@Override
public void extract() {
modulesTypeMirrors = findTypeMirrors(element, AutoComponentExtractorUtil.ANNOTATION_MODULES);
subcomponentsTypeMirrors = findTypeMirrors(element, AutoComponentExtractorUtil.ANNOTATION_SUBCOMPONENTS);
if (!MoreElements.isAnnotationPresent(element, AutoSubcomponent.class)) {
return;
}
Expand All @@ -46,6 +50,8 @@ public void extract() {
}

private List<TypeMirror> findTypeMirrors(Element element, String name) {
boolean addsTo = name.equals(AutoComponentExtractorUtil.ANNOTATION_SUBCOMPONENTS);

List<TypeMirror> typeMirrors = new ArrayList<>();
List<AnnotationValue> values = ExtractorUtils.getValueFromAnnotation(element, AutoSubcomponent.class, name);
if (values != null) {
Expand All @@ -56,6 +62,13 @@ private List<TypeMirror> findTypeMirrors(Element element, String name) {

try {
TypeMirror tm = (TypeMirror) value.getValue();
if (addsTo) {
Element e = MoreTypes.asElement(tm);
if (!MoreElements.isAnnotationPresent(e, AutoSubcomponent.class) && !MoreElements.isAnnotationPresent(e, Subcomponent.class)) {
errors.addInvalid("@AutoComponent cannot declare a subcomponent that is not annotated with @Subcomponent or @AutoSubcomponent: %s", e.getSimpleName());
continue;
}
}
typeMirrors.add(tm);
} catch (Exception e) {
errors.addInvalid(e.getMessage());
Expand Down Expand Up @@ -104,6 +117,10 @@ public AnnotationMirror getScopeAnnotationTypeMirror() {
return scopeAnnotationTypeMirror;
}

public List<TypeMirror> getSubcomponentsTypeMirrors() {
return subcomponentsTypeMirrors;
}

public List<TypeMirror> getSuperinterfacesTypeMirrors() {
return superinterfacesTypeMirrors;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
package autodagger.compiler;

import com.google.auto.common.MoreElements;
import com.google.auto.common.MoreTypes;
import com.google.common.collect.ImmutableSet;
import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterSpec;
import com.squareup.javapoet.TypeName;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;

Expand Down Expand Up @@ -108,7 +119,52 @@ protected SubcomponentSpec build() {
// injector
subcomponentSpec.setInjectorSpecs(ProcessingUtil.getAdditions(extractor.getElement(), state.getInjectorExtractors()));

// subcomponents
subcomponentSpec.setSubcomponentsSpecs(getSubcomponents());

return subcomponentSpec;
}

private List<MethodSpec> getSubcomponents() {
if (extractor.getSubcomponentsTypeMirrors().isEmpty()) {
return Collections.emptyList();
}

List<MethodSpec> methodSpecs = new ArrayList<>(extractor.getSubcomponentsTypeMirrors().size());
for (TypeMirror typeMirror : extractor.getSubcomponentsTypeMirrors()) {
Element e = MoreTypes.asElement(typeMirror);
TypeName typeName;
String name;
if (MoreElements.isAnnotationPresent(e, AutoSubcomponent.class)) {
ClassName cls = AutoComponentClassNameUtil.getComponentClassName(e);
typeName = cls;
name = cls.simpleName();
} else {
typeName = TypeName.get(typeMirror);
name = e.getSimpleName().toString();
}

List<TypeMirror> modules = state.getSubcomponentModules(typeMirror);
List<ParameterSpec> parameterSpecs;
if(modules != null) {
parameterSpecs = new ArrayList<>(modules.size());
int count = 0;
for (TypeMirror moduleTypeMirror : modules) {
parameterSpecs.add(ParameterSpec.builder(TypeName.get(moduleTypeMirror), String.format("module%d", ++count)).build());
}
} else {
parameterSpecs = new ArrayList<>(0);
}

methodSpecs.add(MethodSpec.methodBuilder("plus" + name)
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
.addParameters(parameterSpecs)
.returns(typeName)
.build());
}

return methodSpecs;
}
}

}
10 changes: 10 additions & 0 deletions compiler/src/main/java/autodagger/compiler/SubcomponentSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeName;

import java.util.List;
Expand All @@ -17,6 +18,7 @@ public class SubcomponentSpec {
private List<AdditionSpec> exposeSpecs;
private List<TypeName> modulesTypeNames;
private List<TypeName> superinterfacesTypeNames;
private List<MethodSpec> subcomponentsSpecs;

public SubcomponentSpec(ClassName className) {
this.className = className;
Expand Down Expand Up @@ -65,4 +67,12 @@ public List<TypeName> getSuperinterfacesTypeNames() {
public void setSuperinterfacesTypeNames(List<TypeName> superinterfacesTypeNames) {
this.superinterfacesTypeNames = superinterfacesTypeNames;
}

public List<MethodSpec> getSubcomponentsSpecs() {
return subcomponentsSpecs;
}

public void setSubcomponentsSpecs(List<MethodSpec> subcomponentsSpecs) {
this.subcomponentsSpecs = subcomponentsSpecs;
}
}
10 changes: 10 additions & 0 deletions example/src/main/java/autodagger/example/FirstActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.Activity;
import android.os.Bundle;

import javax.inject.Inject;
import javax.inject.Named;

import autodagger.AutoComponent;
Expand All @@ -27,6 +28,7 @@
public class FirstActivity extends Activity {

private FirstActivityComponent component;
@Inject MySubObject1 mySubObject1;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -38,6 +40,8 @@ protected void onCreate(Bundle savedInstanceState) {
.moduleTwo(new ModuleTwo())
.build();
component.inject(this);

component.plusMySubObject2Component(new MySubObject2.Module(""), new MySubObject2.ModuleTwo()).plusMySubObject1Component().inject(mySubObject1);
}

@Module
Expand Down Expand Up @@ -70,6 +74,12 @@ public MyObject4 providesMyObject4Qualifier1() {
public MyObject4 providesMyObject4Qualifier2() {
return new MyObject4();
}

@Provides
@DaggerScope(FirstActivity.class)
public MySubObject1 providesMySubObject1() {
return new MySubObject1();
}
}

@Module
Expand Down
2 changes: 2 additions & 0 deletions example/src/main/java/autodagger/example/MySubObject1.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package autodagger.example;

import autodagger.AutoInjector;
import autodagger.AutoSubcomponent;

/**
* Created by lukasz on 04/12/15.
*/
@AutoSubcomponent
@DaggerScope(MySubObject1.class)
@AutoInjector
public class MySubObject1 {

}
15 changes: 11 additions & 4 deletions example/src/main/java/autodagger/example/MySubObject2.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
package autodagger.example;

import autodagger.AutoInjector;
import autodagger.AutoSubcomponent;

/**
* Created by lukasz on 04/12/15.
*/
@AutoSubcomponent(modules = {MySubObject2.Module.class, MySubObject2.ModuleTwo.class}, superinterfaces = MySubObject2.MyInterface.class)
@AutoSubcomponent(
modules = {MySubObject2.Module.class, MySubObject2.ModuleTwo.class},
superinterfaces = MySubObject2.MyInterface.class,
subcomponents = MySubObject1.class
)
@AutoInjector
@DaggerScope(MySubObject2.class)
public class MySubObject2 {

@dagger.Module
public class Module {

public static class Module {
public Module(String string) {
}
}

@dagger.Module
public class ModuleTwo {
public static class ModuleTwo {

}

Expand Down
5 changes: 5 additions & 0 deletions library/src/main/java/autodagger/AutoSubcomponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@
Class<?>[] modules() default {};

Class<?>[] superinterfaces() default {};

/**
* Subcomponents to be declared inside this component
*/
Class<?>[] subcomponents() default {};
}