Currently developing a code smell detector for a uni assignment. I have made an abstract CodeSmell class, with two concrete subclasses - ClassLevelSmell and MethodLevelSmell. The CodeSmell class has a protected field of type SmellType. A sample constructor is as follows:
public ClassLevelSmell(ClassDefinition classDef, SmellType type){
smellyClass = classDef;
this.smell = type;
}
SmellType is an enum I defined, which looks like this:
public enum SmellType {
LONG_CLASS, LONG_METHOD, PRIMITIVE_OBSESSION, LONG_PARAM_LIST}
I then have a SmellDetector object, with numerous methods that compare the statistics of analyzed classes and methods (such as their number of lines, number of primitive declarations etc) and creates a new CodeSmell object if a smell is found. So my code for this looks like this:
private void detectLongClass(ClassDefinition classDef) {
if(classDef.getNumLines() > 250){
smells.add(new ClassLevelSmell(classDef, LONG_CLASS));
}
}
Each SmellDetector object has a field smells, an ArrayList of CodeSmells. However, I'm getting a compiler warning in eclipse when I try to pass the SmellType LONG_CLASS into the constructor for the ClassLevelMethod, telling me "LONG_CLASS cannot be resolved to a variable". Am I making some mistake with the use of Enumerated types? What do?
解决方案
To reference enum values you either need to use the qualified form with the class name or use static imports. So either make it:
smells.add(new ClassLevelSmell(classDef, SmellType.LONG_CLASS));
or do this:
// at the top of your file:
import static packagename.SmellType.*;
smells.add(new ClassLevelSmell(classDef, LONG_CLASS));