hashCode and equals for Signatures

This commit is contained in:
oowekyala
2017-05-23 21:36:04 +02:00
parent f83ee39c65
commit 5b622e6cf5
3 changed files with 39 additions and 16 deletions

View File

@ -10,30 +10,31 @@ import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
* @author Clément Fournier
*/
public class FieldSignature extends Signature {
public final boolean isStatic;
public final boolean isFinal;
public FieldSignature(Visibility visibility, boolean isStatic, boolean isFinal) {
super(visibility);
this.isStatic = isStatic;
this.isFinal = isFinal;
}
@Override
public boolean equals(Object o) {
//TODO
return o instanceof FieldSignature;
}
public static FieldSignature buildFor(ASTFieldDeclaration node) {
return new FieldSignature(Visibility.get(node), node.isStatic(), node.isFinal());
}
@Override
public int hashCode() {
//TODO
return 0;
public boolean equals(Object o) {
if (o instanceof FieldSignature) {
FieldSignature f = (FieldSignature) o;
return super.equals(o) && f.isFinal == isFinal && f.isStatic == isStatic;
}
return false;
}
@Override
public int hashCode() {
return super.hashCode() * 16 + (isStatic ? 1 : 0) * 32 + (isFinal ? 1 : 0);
}
}

View File

@ -28,12 +28,15 @@ public class OperationSignature extends Signature {
* Builds an operation signature from a method declaration.
*
* @param node The method declaration
*
* @return The signature of the parameter
*/
public static OperationSignature buildFor(ASTMethodDeclaration node) {
// TODO better getter or setter detection
boolean isGetterOrSetter = node.getName().startsWith("get") || node.getName().startsWith("set");
Role role = isGetterOrSetter ? Role.GETTER_OR_SETTER : node.isStatic() ? Role.STATIC : Role.METHOD;
boolean isGetterOrSetter = node.getName().startsWith("get")
|| node.getName().startsWith("set");
Role role = isGetterOrSetter ? Role.GETTER_OR_SETTER :
node.isStatic() ? Role.STATIC : Role.METHOD;
return new OperationSignature(Visibility.get(node), role, node.isAbstract());
}
@ -42,6 +45,7 @@ public class OperationSignature extends Signature {
* Builds an operation signature from a constructor declaration.
*
* @param node The constructor declaration
*
* @return The signature of the parameter
*/
public static OperationSignature buildFor(ASTConstructorDeclaration node) {
@ -50,12 +54,16 @@ public class OperationSignature extends Signature {
@Override
public boolean equals(Object o) {
return o instanceof OperationSignature;
if (o instanceof OperationSignature) {
return super.equals(o) && role == ((OperationSignature) o).role
&& isAbstract == ((OperationSignature) o).isAbstract;
}
return false;
}
@Override
public int hashCode() {
return role.hashCode() * 2 + visibility.hashCode() * 4 + (isAbstract ? 1 : 0);
return super.hashCode() * 2 + role.hashCode() * 4 + (isAbstract ? 1 : 0);
}
/**

View File

@ -19,6 +19,19 @@ public abstract class Signature {
this.visibility = visibility;
}
@Override
public boolean equals(Object o) {
if (o instanceof Signature) {
return visibility == ((Signature) o).visibility;
}
return false;
}
@Override
public int hashCode() {
return visibility.hashCode() * 2;
}
/**
* The visibility of a node.
*/
@ -31,6 +44,7 @@ public abstract class Signature {
* Returns the Visibility enum key for a node
*
* @param node A node
*
* @return The visibility enum key for a node
*/
public static Visibility get(AbstractJavaAccessNode node) {