Android Open Source - PrettyBundle Extra Injector Class Builder






From Project

Back to project page PrettyBundle.

License

The source code is released under:

Apache License

If you think the Android project PrettyBundle listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.tale.prettybundle.internal;
/*www . ja v a2  s .  c o m*/
import android.os.Bundle;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeSpec;
import com.squareup.javapoet.TypeVariableName;
import com.tale.prettybundle.ExtraBinder;
import com.tale.prettybundle.Injector;
import com.tale.prettybundle.PrettyBundle;

import java.io.IOException;
import java.util.List;

import javax.annotation.processing.Filer;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;

/**
 * Created by tale on 2/5/15.
 */
public class ExtraInjectorClassBuilder {

    private final ExtraClassesGrouped extraClassesGrouped;

    public ExtraInjectorClassBuilder(ExtraClassesGrouped extraClassesGrouped) throws IllegalAccessException, NullPointerException {
        if (extraClassesGrouped == null) {
            throw new NullPointerException("activityExtrasGrouped must not be null");
        } else if (extraClassesGrouped.getExtraAnnotatedClassName() == null
                || extraClassesGrouped.getExtraAnnotatedClassName().trim().equals("")) {
            throw new IllegalAccessException("ActivityExtrasGrouped.getExtraAnnotatedClassName() must not return null");
        }

        this.extraClassesGrouped = extraClassesGrouped;
    }

    public void generateCode(Elements elementUtils, Filer filer) throws IOException {
        final String activityQualifiedClassName = extraClassesGrouped.getExtraAnnotatedClassName();
        final TypeElement typeElement = elementUtils.getTypeElement(activityQualifiedClassName);
        final String activityName = typeElement.getSimpleName().toString();

        final MethodSpec injectMethod = buildInjectMethod(elementUtils);

        ClassName injector = ClassName.get(Injector.class);
        ClassName activity = ClassName.get(extraClassesGrouped.getPackageName(), activityName);

        final TypeSpec activityInjectorClass = TypeSpec.classBuilder(activityName + PrettyBundle.INJECTOR_SUFFIX)
                .addModifiers(Modifier.PUBLIC)
                .addSuperinterface(ParameterizedTypeName.get(injector, activity))
                .addMethod(injectMethod).build();

        JavaFile javaFile = JavaFile.builder(extraClassesGrouped.getPackageName(), activityInjectorClass)
                .build();
        javaFile.writeTo(filer);
    }

    private MethodSpec buildInjectMethod(Elements elementUtils) {
        final MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("inject")
                .addAnnotation(Override.class)
                .addModifiers(Modifier.PUBLIC)
                .addParameter(TypeVariableName.get(extraClassesGrouped.getExtraAnnotatedClassName()), "target")
                .addParameter(Bundle.class, "extras");
        addInjectStatement(methodBuilder, "target", elementUtils);
        return methodBuilder
                .build();
    }

    private void addInjectStatement(MethodSpec.Builder methodBuilder, String targetName, Elements elementUtils) {
        final List<ExtraAnnotatedClass> extraAnnotatedClasses = extraClassesGrouped.getExtraAnnotatedClasses();
        if (extraAnnotatedClasses == null || extraAnnotatedClasses.size() == 0) {
            return;
        }
        methodBuilder.beginControlFlow("if(extras == null)")
                .addStatement("return")
                .endControlFlow();
        for (ExtraAnnotatedClass extraAnnotatedClass : extraAnnotatedClasses) {

            final ExtraBinder extraBinder = extraAnnotatedClass.getExtraBinder();
            methodBuilder
                    .beginControlFlow("if(extras.containsKey($S))", extraAnnotatedClass.getKey())
                    .addStatement("$L.setDataTypeClass($L.class)", ExtraBinder.class.getName() + "." + extraBinder.toString(), extraAnnotatedClass.getDataTypeQualifiedClassName())
                    .addStatement("$L.$L = $L.get(extras, $S)", targetName, extraAnnotatedClass.getKey(), ExtraBinder.class.getName() + "." + extraBinder.toString(), extraAnnotatedClass.getKey())
                    .endControlFlow();
        }
    }

}




Java Source Code List

com.tale.prettybundle.ExtraBinderProvider.java
com.tale.prettybundle.ExtraBinder.java
com.tale.prettybundle.Extra.java
com.tale.prettybundle.Injector.java
com.tale.prettybundle.PrettyBundle.java
com.tale.prettybundle.internal.ExtraAnnotatedClass.java
com.tale.prettybundle.internal.ExtraClassesGrouped.java
com.tale.prettybundle.internal.ExtraInjectorClassBuilder.java
com.tale.prettybundle.internal.ExtraUtilityClassBuilder.java
com.tale.prettybundle.internal.PrettyBundleProcessor.java
com.tale.prettybundle.internal.SupportedType.java
com.tale.prettybundle.sample.ActivitiesClassTest.java
com.tale.prettybundle.sample.ActivitiesTest.java
com.tale.prettybundle.sample.ApplicationTest.java
com.tale.prettybundle.sample.BaseActivityUnitTestCase.java
com.tale.prettybundle.sample.InjectArrayExtrasTest.java
com.tale.prettybundle.sample.InjectParcelableExtrasTest.java
com.tale.prettybundle.sample.InjectPrimaryTypeExtrasFragmentTest.java
com.tale.prettybundle.sample.InjectPrimaryTypeExtrasTest.java
com.tale.prettybundle.sample.InjectStringExtrasTest.java
com.tale.prettybundle.sample.MainActivity.java
com.tale.prettybundle.sample.MenuActivity.java
com.tale.prettybundle.sample.TestArrayExtrasActivity.java
com.tale.prettybundle.sample.TestExtraOnFragmentActivity.java
com.tale.prettybundle.sample.TestParcelableActivity.java
com.tale.prettybundle.sample.TestPrimaryTypeDisplayActivity.java
com.tale.prettybundle.sample.TestPrimaryTypeSetterActivity.java
com.tale.prettybundle.sample.TestStringExtra1Activity.java
com.tale.prettybundle.sample.TestStringExtra2Activity.java
com.tale.prettybundle.sample.data.Person.java
com.tale.prettybundle.sample.espresso.ExtViewActions.java
com.tale.prettybundle.sample.fragments.TestPrimaryExtraFragment.java
com.tale.prettybundle.sample.services.TestPrimaryTypeService.java