-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #451 from GeraldineGalindo/master
Heuristics for variable naming
- Loading branch information
Showing
11 changed files
with
379 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
client/src/main/java/org/evosuite/testcase/utils/HeuristicsUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.evosuite.testcase.utils; | ||
import java.util.ArrayList; | ||
|
||
public class HeuristicsUtil { | ||
/** | ||
* List of particles of a method name that can be excluded or avoided when syggesting names | ||
*/ | ||
private static ArrayList<String> avoidableParticles = new ArrayList<String>(){ | ||
{ | ||
add("get"); | ||
add("to"); | ||
add("has"); | ||
add("is"); | ||
add("are"); | ||
} | ||
}; | ||
|
||
/** | ||
* Returns a boolean value that indicates if the first word of a method can be avoided/excluded | ||
* on method name suggestion | ||
* @return boolean | ||
*/ | ||
|
||
public static boolean containsAvoidableParticle(String firstWord){ | ||
return avoidableParticles.contains(firstWord); | ||
} | ||
/** | ||
* Separates camelcase strings and retrieves the parts in a list | ||
* @return ArrayList<String> | ||
*/ | ||
public static ArrayList<String> separateByCamelCase(String name){ | ||
ArrayList<String> separatedName = new ArrayList<>(); | ||
for (String word : name.split("(?<!(^|[A-Z]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])")) { | ||
separatedName.add(word); | ||
} | ||
return separatedName; | ||
} | ||
|
||
} |
97 changes: 97 additions & 0 deletions
97
client/src/main/java/org/evosuite/testcase/variable/name/HeuristicsVariableNameStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package org.evosuite.testcase.variable.name; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.evosuite.testcase.utils.HeuristicsUtil; | ||
import org.evosuite.testcase.variable.VariableReference; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
public class HeuristicsVariableNameStrategy extends AbstractVariableNameStrategy{ | ||
|
||
protected final Map<String, Integer> nextIndices = new ConcurrentHashMap<>(); | ||
/** | ||
* Dictionaries for naming information | ||
*/ | ||
protected Map<VariableReference, String> methodNames = new HashMap<>(); | ||
protected Map<VariableReference, String> argumentNames = new HashMap<>(); | ||
|
||
private TypeBasedVariableNameStrategy typeBasedVariableNameStrategy = new TypeBasedVariableNameStrategy(); | ||
@Override | ||
public String createNameForVariable(VariableReference variable) { | ||
String typeBasedName = typeBasedVariableNameStrategy.getPlainNameForVariable(variable); | ||
return getPrioritizedName(variable, typeBasedName); | ||
} | ||
|
||
/** | ||
* Returns the variable name + the corresponding index if and only if there is more than one repetition of the name, | ||
* otherwise, it returns the name without an index at last. | ||
* | ||
* Mainly used for Heuristic Renaming Strategy. | ||
* | ||
* @return String | ||
*/ | ||
private String getVariableWithIndexExcludingFirstAppearance(String variableName) { | ||
if (!this.nextIndices.containsKey(variableName)) { | ||
this.nextIndices.put(variableName, 0); | ||
} | ||
else { | ||
final int index = this.nextIndices.get(variableName); | ||
this.nextIndices.put(variableName, index + 1); | ||
variableName += this.nextIndices.get(variableName); | ||
} | ||
return variableName; | ||
} | ||
/** | ||
* Retrieve a suggested name based on method, argument and type information. | ||
* | ||
* The followed order for prioritizing is: | ||
* 1. Use argument suggestion, if not possible | ||
* 2. Use method suggestion + reductions, if not possible | ||
* 3. Use type suggestion, traditional naming. | ||
* | ||
* @return String | ||
*/ | ||
private String getPrioritizedName(final VariableReference var, String variableName) { | ||
final String methodCode = this.methodNames.get(var); | ||
final String arguments = this.argumentNames.get(var); | ||
if (arguments != null) { | ||
variableName = arguments; | ||
} | ||
else if (methodCode != null) { | ||
variableName = analyzeMethodName(methodCode); | ||
} | ||
if(variableName.equals(var.getSimpleClassName())){ | ||
variableName = "_" + variableName; | ||
} | ||
return variableName; | ||
} | ||
|
||
/** | ||
* Returns the suggested method name controlling camel case and excluding some particles | ||
* of the method names. | ||
* | ||
* @return String | ||
*/ | ||
private String analyzeMethodName(String methodCode) { | ||
String name = ""; | ||
ArrayList<String> methodName = HeuristicsUtil.separateByCamelCase(methodCode); | ||
if(methodCode.length() > 0){ | ||
if(HeuristicsUtil.containsAvoidableParticle(methodName.get(0)) && methodName.size() > 1){ | ||
name = StringUtils.join(methodName.subList(1,methodName.size()), ""); | ||
final char[] auxCharArray = name.toCharArray(); | ||
auxCharArray[0] = Character.toLowerCase(auxCharArray[0]); | ||
return new String(auxCharArray); | ||
} | ||
} | ||
return methodCode; | ||
} | ||
|
||
public void addVariableInformation(Map<String, Map<VariableReference, String>> information){ | ||
methodNames = information.get("MethodNames"); | ||
argumentNames = information.get("ArgumentNames"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.