
com.google.test.metric.ClassInfo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of testability-explorer Show documentation
Show all versions of testability-explorer Show documentation
A tool that looks at java bytecodes and helps you
identify hard to test code.
The newest version!
/*
* 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 methods = new TreeMap();
private final Map fields = new TreeMap();
private final String name;
private final boolean isInterface;
private final ClassInfo superClass;
private final List interfaces;
private final String fileName;
public ClassInfo(String name, boolean isInterface, ClassInfo superClass,
List 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 superClasses = new ArrayList();
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 getMethods() {
return methods.values();
}
public Collection getFields() {
return fields.values();
}
public List getInterfaces() {
return interfaces;
}
public Collection getSetters() {
Collection setters = new TreeSet();
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 getNonPrivateConstructors() {
TreeSet constructors = new TreeSet();
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;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy