boolean foundError = false;
HashMap<String, ClassMember> memberHashMap = new HashMap<String, ClassMember>(
classMembers.size());
for (ClassMember classMember : classMembers) {
if (classMember instanceof MethodDeclaration) {
MethodDeclaration method = (MethodDeclaration) classMember;
if (method.isStatic()) {
continue;
}
// prepare name
SimpleIdentifier name = method.getName();
if (name == null) {
continue;
}
boolean addThisMemberToTheMap = true;
boolean isGetter = method.isGetter();
boolean isSetter = method.isSetter();
boolean isOperator = method.isOperator();
boolean isMethod = !isGetter && !isSetter && !isOperator;
// Do lookups in the enclosing class (and the inherited member) if the member is a method or
// a setter for StaticWarningCode.CONFLICTING_INSTANCE_METHOD_SETTER warning.
if (isMethod) {
String setterName = name.getName() + "=";
Element enclosingElementOfSetter = null;
ClassMember conflictingSetter = memberHashMap.get(setterName);
if (conflictingSetter != null) {
enclosingElementOfSetter = conflictingSetter.getElement().getEnclosingElement();
} else {
ExecutableElement elementFromInheritance = inheritanceManager.lookupInheritance(
enclosingClass,
setterName);
if (elementFromInheritance != null) {
enclosingElementOfSetter = elementFromInheritance.getEnclosingElement();
}
}
if (enclosingElementOfSetter != null) {
// report problem
errorReporter.reportErrorForNode(
StaticWarningCode.CONFLICTING_INSTANCE_METHOD_SETTER,
name,
enclosingClass.getDisplayName(),
name.getName(),
enclosingElementOfSetter.getDisplayName());
foundError |= true;
addThisMemberToTheMap = false;
}
} else if (isSetter) {
String methodName = name.getName();
ClassMember conflictingMethod = memberHashMap.get(methodName);
if (conflictingMethod != null && conflictingMethod instanceof MethodDeclaration
&& !((MethodDeclaration) conflictingMethod).isGetter()) {
// report problem
errorReporter.reportErrorForNode(
StaticWarningCode.CONFLICTING_INSTANCE_METHOD_SETTER2,
name,
enclosingClass.getDisplayName(),
name.getName());
foundError |= true;
addThisMemberToTheMap = false;
}
}
// Finally, add this member into the HashMap.
if (addThisMemberToTheMap) {
if (method.isSetter()) {
memberHashMap.put(name.getName() + "=", method);
} else {
memberHashMap.put(name.getName(), method);
}
}