All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.cucumber.java.MethodFormat Maven / Gradle / Ivy

package io.cucumber.java;

import io.cucumber.core.backend.CucumberBackendException;

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

/**
 * Helper class for formatting a method signature to a shorter form.
 */
final class MethodFormat {

    static final MethodFormat FULL = new MethodFormat("%qc.%m(%qa)");
    private static final Pattern METHOD_PATTERN = Pattern
            .compile("((?:static\\s|public\\s)+)([^\\s]*)\\s\\.?(.*)\\.([^\\(]*)\\(([^\\)]*)\\)(?: throws )?(.*)");
    private final MessageFormat format;

    /**
     * @param format the format string to use. There are several pattern tokens
     *               that can be used:
     *               
    *
  • %M: Modifiers
  • *
  • %qr: Qualified return type
  • *
  • %r: Unqualified return type
  • *
  • %qc: Qualified class
  • *
  • %c: Unqualified class
  • *
  • %m: Method name
  • *
  • %qa: Qualified arguments
  • *
  • %a: Unqualified arguments
  • *
  • %qe: Qualified exceptions
  • *
  • %e: Unqualified exceptions
  • *
  • %s: Code source
  • *
*/ private MethodFormat(String format) { String pattern = format .replaceAll("%qc", "{0}") .replaceAll("%m", "{1}") .replaceAll("%qa", "{2}"); this.format = new MessageFormat(pattern); } String format(Method method) { String signature = method.toGenericString(); Matcher matcher = METHOD_PATTERN.matcher(signature); if (matcher.find()) { String qc = matcher.group(3); String m = matcher.group(4); String qa = matcher.group(5); return format.format(new Object[] { qc, m, qa, }); } else { throw new CucumberBackendException("Cucumber bug: Couldn't format " + signature); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy