/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
- /*
- * Copyright 2007 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
- package com.google.test.metric;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.List;
- import java.util.Map;
- import java.util.TreeMap;
- import java.util.TreeSet;
- public class ClassInfo {
- private final Map<String, MethodInfo> methods = new TreeMap<String, MethodInfo>();
- private final Map<String, FieldInfo> fields = new TreeMap<String, FieldInfo>();
- private final String name;
- private final boolean isInterface;
- private final ClassInfo superClass;
- private final List<ClassInfo> interfaces;
- private final String fileName;
- public ClassInfo(String name, boolean isInterface, ClassInfo superClass,
- List<ClassInfo> interfaces, String fileName) {
- this.isInterface = isInterface;
- this.superClass = superClass;
- this.interfaces = interfaces;
- this.fileName = fileName;
- this.name = name.replace("/", ".");
- }
- public String getName() {
- return name;
- }
- public ClassInfo getSuperClass() {
- return superClass;
- }
- public boolean isInterface() {
- return isInterface;
- }
- public MethodInfo getMethod(String methodName) {
- List<ClassInfo> superClasses = new ArrayList<ClassInfo>();
- superClasses.add(this);
- while (!superClasses.isEmpty()) {
- ClassInfo clazz = superClasses.remove(0);
- MethodInfo methodInfo = clazz.methods.get(methodName);
- if (methodInfo != null) {
- return methodInfo;
- }
- if (clazz.superClass != null) {
- superClasses.add(0, clazz.superClass);
- }
- superClasses.addAll(clazz.interfaces);
- }
- throw new MethodNotFoundException(this, methodName);
- }
- public void addMethod(MethodInfo methodInfo) {
- methods.put(methodInfo.getName(), methodInfo);
- }
- @Override
- public String toString() {
- return name;
- }
- public FieldInfo getField(String fieldName) {
- ClassInfo clazz = this;
- while (clazz != null) {
- FieldInfo fieldInfo = clazz.fields.get(fieldName);
- if (fieldInfo != null) {
- return fieldInfo;
- }
- clazz = clazz.superClass;
- }
- throw new FieldNotFoundException(this, fieldName);
- }
- public void addField(FieldInfo fieldInfo) {
- fields.put(fieldInfo.getName(), fieldInfo);
- }
- public Collection<MethodInfo> getMethods() {
- return methods.values();
- }
- public Collection<FieldInfo> getFields() {
- return fields.values();
- }
- public List<ClassInfo> getInterfaces() {
- return interfaces;
- }
- public Collection<MethodInfo> getSetters() {
- Collection<MethodInfo> setters = new TreeSet<MethodInfo>();
- if (superClass != null) {
- setters.addAll(superClass.getSetters());
- }
- for (MethodInfo method : methods.values()) {
- if (method.isSetter()) {
- setters.add(method);
- }
- }
- return setters;
- }
- /** When you have multiple constructors you need to know which one to use for marking
- * fields as injectables. The heuristic is that the constructor with most arguments
- * will probably be the constructor best suited for testing as it will give you highest
- * control over your field injection.
- */
- public MethodInfo getConstructorWithMostNonPrimitiveParameters() {
- // TODO(jwolter): It would seem more accurate a approximation of multiple constructors
- // if we would calculate the cost for all of them, and then add in only the highest,
- // or an average of them.
- MethodInfo constructor = null;
- int currentArgsCount = -1;
- for (MethodInfo methodInfo : getNonPrivateConstructors()) {
- int count = methodInfo.getNonPrimitiveArgCount();
- if (currentArgsCount < count) {
- constructor = methodInfo;
- currentArgsCount = count;
- }
- }
- return constructor;
- }
- public Collection<MethodInfo> getNonPrivateConstructors() {
- TreeSet<MethodInfo> constructors = new TreeSet<MethodInfo>();
- for (MethodInfo methodInfo : getMethods()) {
- if (methodInfo.isConstructor() && !methodInfo.isPrivate()) {
- constructors.add(methodInfo);
- }
- }
- return constructors;
- }
- public String getFileName() {
- return fileName;
- }
- public ClassInfo copy() {
- ClassInfo clazz = new ClassInfo(name, isInterface, superClass, interfaces, fileName);
- for (MethodInfo methodInfo : getMethods()) {
- clazz.addMethod(methodInfo);
- }
- return clazz;
- }
- }