use of org.evosuite.parameterize.InputVariable in project evosuite by EvoSuite.
the class TestCodeVisitor method getVariableName.
/**
* <p>
* getVariableName
* </p>
*
* @param var
* a {@link org.evosuite.testcase.variable.VariableReference} object.
* @return a {@link java.lang.String} object.
*/
public String getVariableName(VariableReference var) {
if (var instanceof ConstantValue) {
ConstantValue cval = (ConstantValue) var;
if (cval.getValue() != null && cval.getVariableClass().equals(Class.class)) {
return getClassName((Class<?>) cval.getValue()) + ".class";
}
return var.getName();
} else if (var instanceof InputVariable) {
return var.getName();
} else if (var instanceof FieldReference) {
VariableReference source = ((FieldReference) var).getSource();
GenericField field = ((FieldReference) var).getField();
if (source != null) {
String ret = "";
// If the method is not public and this is a subclass in a different package we need to cast
if (!field.isPublic() && !field.getDeclaringClass().equals(source.getVariableClass()) && source.isAssignableTo(field.getDeclaringClass())) {
String packageName1 = ClassUtils.getPackageName(field.getDeclaringClass());
String packageName2 = ClassUtils.getPackageName(source.getVariableClass());
if (!packageName1.equals(packageName2)) {
ret += "((" + getClassName(field.getDeclaringClass()) + ")" + getVariableName(source) + ")";
} else {
ret += getVariableName(source);
}
} else if (!source.isAssignableTo(field.getField().getDeclaringClass())) {
try {
// If the concrete source class has that field then it's ok
source.getVariableClass().getDeclaredField(field.getName());
ret = getVariableName(source);
} catch (NoSuchFieldException e) {
// If not we need to cast to the subtype
ret = "((" + getTypeName(field.getField().getDeclaringClass()) + ") " + getVariableName(source) + ")";
}
} else {
ret += getVariableName(source);
}
return ret + "." + field.getName();
} else
return getClassName(field.getField().getDeclaringClass()) + "." + field.getName();
} else if (var instanceof ArrayIndex) {
VariableReference array = ((ArrayIndex) var).getArray();
List<Integer> indices = ((ArrayIndex) var).getArrayIndices();
String result = getVariableName(array);
for (Integer index : indices) {
result += "[" + index + "]";
}
return result;
} else if (var instanceof ArrayReference) {
String className = var.getSimpleClassName();
// int num = 0;
// for (VariableReference otherVar : variableNames.keySet()) {
// if (!otherVar.equals(var)
// && otherVar.getVariableClass().equals(var.getVariableClass()))
// num++;
// }
String variableName = className.substring(0, 1).toLowerCase() + className.substring(1) + "Array";
variableName = variableName.replace('.', '_').replace("[]", "");
if (!variableNames.containsKey(var)) {
if (!nextIndices.containsKey(variableName)) {
nextIndices.put(variableName, 0);
}
int index = nextIndices.get(variableName);
nextIndices.put(variableName, index + 1);
variableName += index;
variableNames.put(var, variableName);
}
} else if (!variableNames.containsKey(var)) {
String className = var.getSimpleClassName();
// int num = 0;
// for (VariableReference otherVar : variableNames.keySet()) {
// if (otherVar.getVariableClass().equals(var.getVariableClass()))
// num++;
// }
String variableName = className.substring(0, 1).toLowerCase() + className.substring(1);
if (variableName.contains("[]")) {
variableName = variableName.replace("[]", "Array");
}
variableName = variableName.replace(".", "_");
// if (numObjectsOfType > 1 || className.equals(variableName)) {
if (CharUtils.isAsciiNumeric(variableName.charAt(variableName.length() - 1)))
variableName += "_";
if (!nextIndices.containsKey(variableName)) {
nextIndices.put(variableName, 0);
}
int index = nextIndices.get(variableName);
nextIndices.put(variableName, index + 1);
variableName += index;
// }
variableNames.put(var, variableName);
}
return variableNames.get(var);
}
Aggregations