PageRenderTime 48ms CodeModel.GetById 26ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://testability-explorer.googlecode.com/
Java | 165 lines | 122 code | 20 blank | 23 comment | 19 complexity | 31e2e997b7b027d618ba0df6187ec854 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 */
 16package com.google.test.metric;
 17
 18import java.util.ArrayList;
 19import java.util.Collection;
 20import java.util.List;
 21import java.util.Map;
 22import java.util.TreeMap;
 23import java.util.TreeSet;
 24
 25public class ClassInfo {
 26
 27  private final Map<String, MethodInfo> methods = new TreeMap<String, MethodInfo>();
 28  private final Map<String, FieldInfo> fields = new TreeMap<String, FieldInfo>();
 29  private final String name;
 30  private final boolean isInterface;
 31  private final ClassInfo superClass;
 32  private final List<ClassInfo> interfaces;
 33  private final String fileName;
 34
 35  public ClassInfo(String name, boolean isInterface, ClassInfo superClass,
 36                   List<ClassInfo> interfaces, String fileName) {
 37    this.isInterface = isInterface;
 38    this.superClass = superClass;
 39    this.interfaces = interfaces;
 40    this.fileName = fileName;
 41    this.name = name.replace("/", ".");
 42  }
 43
 44  public String getName() {
 45    return name;
 46  }
 47
 48  public ClassInfo getSuperClass() {
 49    return superClass;
 50  }
 51
 52  public boolean isInterface() {
 53    return isInterface;
 54  }
 55
 56  public MethodInfo getMethod(String methodName) {
 57    List<ClassInfo> superClasses = new ArrayList<ClassInfo>();
 58    superClasses.add(this);
 59    while (!superClasses.isEmpty()) {
 60      ClassInfo clazz = superClasses.remove(0);
 61      MethodInfo methodInfo = clazz.methods.get(methodName);
 62      if (methodInfo != null) {
 63        return methodInfo;
 64      }
 65      if (clazz.superClass != null) {
 66        superClasses.add(0, clazz.superClass);
 67      }
 68      superClasses.addAll(clazz.interfaces);
 69    }
 70    throw new MethodNotFoundException(this, methodName);
 71  }
 72
 73  public void addMethod(MethodInfo methodInfo) {
 74    methods.put(methodInfo.getName(), methodInfo);
 75  }
 76
 77  @Override
 78  public String toString() {
 79    return name;
 80  }
 81
 82  public FieldInfo getField(String fieldName) {
 83    ClassInfo clazz = this;
 84    while (clazz != null) {
 85      FieldInfo fieldInfo = clazz.fields.get(fieldName);
 86      if (fieldInfo != null) {
 87        return fieldInfo;
 88      }
 89      clazz = clazz.superClass;
 90    }
 91    throw new FieldNotFoundException(this, fieldName);
 92  }
 93
 94  public void addField(FieldInfo fieldInfo) {
 95    fields.put(fieldInfo.getName(), fieldInfo);
 96  }
 97
 98  public Collection<MethodInfo> getMethods() {
 99    return methods.values();
100  }
101
102  public Collection<FieldInfo> getFields() {
103    return fields.values();
104  }
105
106  public List<ClassInfo> getInterfaces() {
107    return interfaces;
108  }
109
110  public Collection<MethodInfo> getSetters() {
111    Collection<MethodInfo> setters = new TreeSet<MethodInfo>();
112    if (superClass != null) {
113      setters.addAll(superClass.getSetters());
114    }
115    for (MethodInfo method : methods.values()) {
116      if (method.isSetter()) {
117        setters.add(method);
118      }
119    }
120    return setters;
121  }
122
123  /** When you have multiple constructors you need to know which one to use for marking
124   * fields as injectables. The heuristic is that the constructor with most arguments
125   * will probably be the constructor best suited for testing as it will give you highest
126   * control over your field injection.
127   */
128  public MethodInfo getConstructorWithMostNonPrimitiveParameters() {
129    // TODO(jwolter): It would seem more accurate a approximation of multiple constructors
130    // if we would calculate the cost for all of them, and then add in only the highest,
131    // or an average of them.
132    MethodInfo constructor = null;
133    int currentArgsCount = -1;
134    for (MethodInfo methodInfo : getNonPrivateConstructors()) {
135      int count = methodInfo.getNonPrimitiveArgCount();
136      if (currentArgsCount < count) {
137        constructor = methodInfo;
138        currentArgsCount = count;
139      }
140    }
141    return constructor;
142  }
143
144  public Collection<MethodInfo> getNonPrivateConstructors() {
145    TreeSet<MethodInfo> constructors = new TreeSet<MethodInfo>();
146    for (MethodInfo methodInfo : getMethods()) {
147      if (methodInfo.isConstructor() && !methodInfo.isPrivate()) {
148        constructors.add(methodInfo);
149      }
150    }
151    return constructors;
152  }
153
154  public String getFileName() {
155    return fileName;
156  }
157
158  public ClassInfo copy() {
159    ClassInfo clazz = new ClassInfo(name, isInterface, superClass, interfaces, fileName);
160    for (MethodInfo methodInfo : getMethods()) {
161      clazz.addMethod(methodInfo);
162    }
163    return clazz;
164  }
165}