PageRenderTime 27ms CodeModel.GetById 16ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/testability-explorer/src/main/java/com/google/test/metric/report/RemovePackageFormatter.java

http://testability-explorer.googlecode.com/
Java | 35 lines | 17 code | 5 blank | 13 comment | 1 complexity | c25045384bc9edb6f0263c15f6ca929c MD5 | raw file
 1// Copyright 2009 Google Inc. All Rights Reserved.
 2
 3package com.google.test.metric.report;
 4
 5import java.util.regex.Matcher;
 6import java.util.regex.Pattern;
 7
 8/**
 9 * @author alexeagle@google.com (Alex Eagle)
10 */
11public class RemovePackageFormatter {
12  private final Pattern pattern = Pattern.compile("([0-9A-Z\\$a-z_\\.]*)");
13
14  /**
15   * Shorten the name of a method, removing any package and class names of the method
16   * and its parameters.
17   * "t.n.e(p.e, a.b)" -> "e(e, b)"
18   * @param name any identifier, such as a method name or type name
19   * @return a shortened version
20   */
21  public String format(String name) {
22    //TODO(alexeagle): make sure this doesn't cause ambiguity
23    Matcher matcher = pattern.matcher(name);
24    StringBuffer result = new StringBuffer();
25    while (matcher.find()) {
26      String identifier = matcher.group();
27      String simple = identifier.substring(identifier.lastIndexOf(".") + 1);
28      // Have to escape dollar signs, or they look like capture group identifiers
29      simple = simple.replaceAll("\\$", "\\\\\\$");
30      matcher.appendReplacement(result, simple);
31    }
32    return result.toString();
33  }
34
35}