Android Open Source - CucumberAndroidGradle Android Classpath Method Scanner






From Project

Back to project page CucumberAndroidGradle.

License

The source code is released under:

Apache License

If you think the Android project CucumberAndroidGradle 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 cucumber.runtime.android;
/*from w  w  w  . ja  va2 s . co  m*/
import android.content.Context;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.runtime.CucumberException;
import cucumber.runtime.Utils;
import cucumber.runtime.java.StepDefAnnotation;
import ext.android.test.ClassPathPackageInfoSource;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class AndroidClasspathMethodScanner {
    private final Collection<Class<? extends Annotation>> mCucumberAnnotationClasses;
    private static ClassPathPackageInfoSource mSource;

    AndroidClasspathMethodScanner(Context context) {
        classPathPackageInfoSource(context);
        mCucumberAnnotationClasses = findCucumberAnnotationClasses();
    }

    public static ClassPathPackageInfoSource classPathPackageInfoSource(Context context) {
        if (mSource == null) {
            String apkPath = context.getPackageCodePath();
            ClassPathPackageInfoSource.setApkPaths(new String[]{apkPath});
            mSource = new ClassPathPackageInfoSource();
        }
        mSource.setClassLoader(context.getClassLoader());
        return mSource;
    }

    /**
     * Registers step definitions and hooks.
     *
     * @param androidBackend the backend where stepdefs and hooks will be registered
     * @param gluePaths      where to look
     */
    void scan(AndroidBackend androidBackend, List<String> gluePaths) {
        for (String gluePath : gluePaths) {
            for (Class<?> glueCodeClass : mSource.getPackageInfo(gluePath).getTopLevelClassesRecursive()) {
                while (glueCodeClass != null && glueCodeClass != Object.class && !Utils.isInstantiable(glueCodeClass)) {
                    // those can't be instantiated without container class present
                    glueCodeClass = glueCodeClass.getSuperclass();
                }
                if (glueCodeClass != null) {
                    for (Method method : glueCodeClass.getMethods()) {
                        scan(androidBackend, method, glueCodeClass);
                    }
                }
            }
        }
    }

    /**
     * Registers step definitions and hooks.
     *
     * @param androidBackend the backend where stepdefs and hooks will be registered.
     * @param method         a candidate for being a stepdef or hook.
     * @param glueCodeClass  the class where the method is declared.
     */
    void scan(AndroidBackend androidBackend, Method method, Class<?> glueCodeClass) {
        for (Class<? extends Annotation> cucumberAnnotationClass : mCucumberAnnotationClasses) {
            Annotation annotation = method.getAnnotation(cucumberAnnotationClass);
            if (annotation != null) {
                if (!method.getDeclaringClass().isAssignableFrom(glueCodeClass)) {
                    throw new CucumberException(String.format("%s isn't assignable from %s", method.getDeclaringClass(), glueCodeClass));
                }
                if (!glueCodeClass.equals(method.getDeclaringClass())) {
                    throw new CucumberException(String.format("You're not allowed to extend classes that define Step Definitions or hooks. %s extends %s", glueCodeClass, method.getDeclaringClass()));
                }
                if (isHookAnnotation(annotation)) {
                    androidBackend.addHook(annotation, method);
                } else if (isStepdefAnnotation(annotation)) {
                    androidBackend.addStepDefinition(annotation, method);
                }
            }
        }
    }

    @SuppressWarnings("unchecked")
    private Collection<Class<? extends Annotation>> findCucumberAnnotationClasses() {
        List<Class<? extends Annotation>> annotationClasses = new ArrayList<Class<? extends Annotation>>();
        for (Class clazz : mSource.getPackageInfo("cucumber.api").getTopLevelClassesRecursive()) {
            if (clazz.isAnnotation()) annotationClasses.add(clazz);
        }
        return annotationClasses;
    }

    private boolean isHookAnnotation(Annotation annotation) {
        Class<? extends Annotation> annotationClass = annotation.annotationType();
        return annotationClass.equals(Before.class) || annotationClass.equals(After.class);
    }

    private boolean isStepdefAnnotation(Annotation annotation) {
        Class<? extends Annotation> annotationClass = annotation.annotationType();
        return annotationClass.getAnnotation(StepDefAnnotation.class) != null;
    }
}




Java Source Code List

com.example.cucumberandroid.MainActivity.java
cucumber.api.android.CucumberInstrumentationTestRunner.java
cucumber.api.android.CucumberInstrumentation.java
cucumber.api.android.RunWithCucumber.java
cucumber.runtime.android.AndroidBackend.java
cucumber.runtime.android.AndroidClasspathMethodScanner.java
cucumber.runtime.android.AndroidFormatter.java
cucumber.runtime.android.AndroidJavaStepDefinition.java
cucumber.runtime.android.AndroidMethodFormat.java
cucumber.runtime.android.AndroidObjectFactory.java
cucumber.runtime.android.AndroidResourceLoader.java
cucumber.runtime.android.AndroidResource.java
cucumber.runtime.android.JavaHookDefinition.java
cucumber.runtime.android.JavaSnippet.java
ext.com.android.internal.os.LoggingPrintStream.java
ext.com.google.android.collect.Maps.java
ext.com.google.android.collect.Sets.java