Android Open Source - CucumberAndroidGradle Android Method Format






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;
/* w  w  w. j a  v  a 2 s. c  om*/
import cucumber.runtime.CucumberException;

import java.lang.reflect.Method;
import java.text.MessageFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class AndroidMethodFormat {
    private static final Pattern METHOD_PATTERN = Pattern.compile("((?:static\\s|public\\s)+)([^\\s]*)\\s\\.?(.*)\\.([^\\(]*)\\(([^\\)]*)\\)(?: throws )?(.*)");
    private static final String PACKAGE_PATTERN = "[^,]*\\.";
    private final MessageFormat format;
    public static final AndroidMethodFormat SHORT = new AndroidMethodFormat("%c.%m(%a)");
    public static final AndroidMethodFormat FULL = new AndroidMethodFormat("%qc.%m(%a) in %s");

    /**
     * @param format the format string to use. There are several pattern tokens that can be used:
     *               <ul>
     *               <li><strong>%M</strong>: Modifiers</li>
     *               <li><strong>%qr</strong>: Qualified return type</li>
     *               <li><strong>%r</strong>: Unqualified return type</li>
     *               <li><strong>%qc</strong>: Qualified class</li>
     *               <li><strong>%c</strong>: Unqualified class</li>
     *               <li><strong>%m</strong>: Method name</li>
     *               <li><strong>%qa</strong>: Qualified arguments</li>
     *               <li><strong>%a</strong>: Unqualified arguments</li>
     *               <li><strong>%qe</strong>: Qualified exceptions</li>
     *               <li><strong>%e</strong>: Unqualified exceptions</li>
     *               <li><strong>%s</strong>: Code source</li>
     *               </ul>
     */
    private AndroidMethodFormat(String format) {
        String pattern = format
                .replaceAll("%M", "{0}")
                .replaceAll("%r", "{1}")
                .replaceAll("%qc", "{2}")
                .replaceAll("%m", "{3}")
                .replaceAll("%qa", "{4}")
                .replaceAll("%qe", "{5}")
                .replaceAll("%c", "{6}")
                .replaceAll("%a", "{7}")
                .replaceAll("%e", "{8}")
                .replaceAll("%s", "{9}");
        this.format = new MessageFormat(pattern);
    }

    public String format(Method method) {
        String signature = method.toGenericString();
        Matcher matcher = METHOD_PATTERN.matcher(signature);
        if (matcher.find()) {
            String M = matcher.group(1);
            String r = matcher.group(2);
            String qc = matcher.group(3);
            String m = matcher.group(4);
            String qa = matcher.group(5);
            String qe = matcher.group(6);
            String c = qc.replaceAll(PACKAGE_PATTERN, "");
            String a = qa.replaceAll(PACKAGE_PATTERN, "");
            String e = qe.replaceAll(PACKAGE_PATTERN, "");
            // getProtectionDomain() returns null on Android
            String s = method.getDeclaringClass().getName();

            return format.format(new Object[]{
                    M,
                    r,
                    qc,
                    m,
                    qa,
                    qe,
                    c,
                    a,
                    e,
                    s
            });
        } else {
            throw new CucumberException("Cucumber bug: Couldn't format " + signature);
        }
    }
}




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