com.github.stephanenicolas.injectview.InjectViewProcessor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of injectview-plugin Show documentation
Show all versions of injectview-plugin Show documentation
Logs all life cycle methods of an Activity on Android
package com.github.stephanenicolas.injectview;
import com.github.stephanenicolas.afterburner.AfterBurner;
import com.github.stephanenicolas.afterburner.InsertableMethodBuilder;
import com.github.stephanenicolas.afterburner.exception.AfterBurnerImpossibleException;
import com.github.stephanenicolas.morpheus.commons.CtClassFilter;
import com.github.stephanenicolas.morpheus.commons.JavassistUtils;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import javassist.CannotCompileException;
import javassist.CtClass;
import javassist.CtConstructor;
import javassist.CtField;
import javassist.CtMethod;
import javassist.CtNewMethod;
import javassist.NotFoundException;
import javassist.build.IClassTransformer;
import javassist.build.JavassistBuildException;
import lombok.extern.slf4j.Slf4j;
import static com.github.stephanenicolas.morpheus.commons.JavassistUtils.findValidParamIndex;
import static com.github.stephanenicolas.morpheus.commons.JavassistUtils.getAllInjectedFieldsForAnnotation;
import static com.github.stephanenicolas.morpheus.commons.JavassistUtils.isActivity;
import static com.github.stephanenicolas.morpheus.commons.JavassistUtils.isFragment;
import static com.github.stephanenicolas.morpheus.commons.JavassistUtils.isSupportFragment;
import static com.github.stephanenicolas.morpheus.commons.JavassistUtils.isView;
import static java.lang.String.format;
/**
* Will inject all fields and fragments from XML.
*
*
*
* - for activities :
*
* - if they use @ContentView : right after super.onCreate
*
- if they don't use @ContentView : right after setContentView invocation in onCreate
*
- it doesn't matter if you supply your own version of onCreate or setContenView or not.
*
* - for fragments :
*
* - right after onViewCreated
*
- views are destroyed right after onViewDestroyed
*
- fragments must return a non null view for onViewCreated to be used by Android..
*
* - for views :
*
* - right after onFinishInflate
*
- onFinishInflate is called automatically by Android when inflating a view from XML
*
- onFinishInflate must be called manually in constructors of views with a single context
* argument. You should invoke it after inflating your layout manually.
*
* - for other classes (namely MVP presenters and view holder design patterns) :
*
* - right before any constructor with a single argument of type Activity, Fragment, or
* View
*
- static inner classes can only be processed if static
*
*
*
*
* @author SNI
*/
@Slf4j
public class InjectViewProcessor implements IClassTransformer {
private CtClassFilter injectViewfilter = new InjectViewCtClassFilter();
private AfterBurner afterBurner = new AfterBurner();
@Override
public boolean shouldTransform(CtClass candidateClass) throws JavassistBuildException {
try {
final List