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

http://testability-explorer.googlecode.com/ · Java · 160 lines · 107 code · 31 blank · 22 comment · 4 complexity · fe8f2341712befbe64d187f935f61392 MD5 · raw file

  1. /*
  2. * Copyright 2007 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.google.test.metric;
  17. import com.google.test.metric.report.RemovePackageFormatter;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.Map;
  21. public class MethodCost {
  22. private final String className;
  23. private final String methodName;
  24. private final int lineNumber;
  25. private final boolean constructor;
  26. private final boolean aStatic;
  27. private final boolean staticInit;
  28. private final List<ViolationCost> costSources = new ArrayList<ViolationCost>();
  29. public boolean isConstructor() {
  30. return constructor;
  31. }
  32. public boolean isStatic() {
  33. return aStatic;
  34. }
  35. public Cost getDirectCost() {
  36. return directCost;
  37. }
  38. public Cost getDependentCost() {
  39. return dependentCost;
  40. }
  41. public Cost getConstructorDependentCost() {
  42. return constructorDependentCost;
  43. }
  44. private final Cost directCost = new Cost();
  45. private final Cost dependentCost = new Cost();
  46. private final Cost constructorDependentCost = new Cost();
  47. public static final String METHOD_NAME_ATTRIBUTE = "name";
  48. /**
  49. * @param className
  50. * @param methodName
  51. * name of the method, such as {@code void myMethod()}.
  52. * @param lineNumber
  53. * @param isStaticInit
  54. */
  55. public MethodCost(String className, String methodName, int lineNumber, boolean isConstructor,
  56. boolean isStatic,
  57. boolean isStaticInit) {
  58. this.className = className;
  59. this.methodName = methodName;
  60. this.lineNumber = lineNumber;
  61. constructor = isConstructor;
  62. aStatic = isStatic;
  63. staticInit = isStaticInit;
  64. }
  65. public Cost getTotalCost() {
  66. return new Cost().add(directCost).add(dependentCost).add(constructorDependentCost);
  67. }
  68. public Cost getNonConstructorCost() {
  69. return new Cost().add(directCost).add(dependentCost);
  70. }
  71. public String getMethodName() {
  72. return methodName;
  73. }
  74. public void addCostSource(ViolationCost costSource) {
  75. costSource.link(directCost, dependentCost, constructorDependentCost);
  76. costSources.add(costSource);
  77. }
  78. @Override
  79. public String toString() {
  80. return getMethodName() + toCostsString();
  81. }
  82. public String toCostsString() {
  83. return " [" + getTotalCost() + " / " + directCost + "]";
  84. }
  85. public int getMethodLineNumber() {
  86. return lineNumber;
  87. }
  88. public String getClassName() {
  89. return className;
  90. }
  91. public List<ViolationCost> getViolationCosts() {
  92. return costSources;
  93. }
  94. public List<ViolationCost> getImplicitViolationCosts() {
  95. return filterViolationCosts(true);
  96. }
  97. public List<ViolationCost> getExplicitViolationCosts() {
  98. return filterViolationCosts(false);
  99. }
  100. private List<ViolationCost> filterViolationCosts(boolean implicit) {
  101. List<ViolationCost> result = new ArrayList<ViolationCost>();
  102. for (ViolationCost cost : getViolationCosts()) {
  103. if (cost.isImplicit() == implicit) {
  104. result.add(cost);
  105. }
  106. }
  107. return result;
  108. }
  109. public Cost getCost() {
  110. return directCost;
  111. }
  112. public Map<String, Object> getAttributes() {
  113. Map<String, Object> map = getTotalCost().getAttributes();
  114. map.put("line", lineNumber);
  115. map.put(METHOD_NAME_ATTRIBUTE, methodName);
  116. return map;
  117. }
  118. public void link() {
  119. }
  120. public boolean isMainMethod() {
  121. return isStatic() && "void main(java.lang.String[])".equals(getMethodName());
  122. }
  123. public String getDescription() {
  124. return new RemovePackageFormatter().format(methodName);
  125. }
  126. public boolean isStaticInit() {
  127. return staticInit;
  128. }
  129. }