系列文章目录:

    使用Fortify进行代码静态分析(系列文章)

class does not implement equals(类未能实现Equals方法)

  示例: 

 protected void Check_Clicked(Object sender, EventArgs e)
{
for (int i = ; i < dgList.Items.Count; i++)
{
if (sender.Equals(dgList.Items[i].FindControl("cbxItem")))
{ }
}
}

   

    Fortify提示:

    Equals() is called on an object that does not implement Equals()。

  在未实现Equals的类上调用Equals()方法。

  详细解释:

When comparing objects, developers usually want to compare properties of objects. However, calling Equals() on a class (or any super class/interface) that does not explicitly implement Equals() results in a call to the Equals() method inherited from System.Object. Instead of comparing object member fields or other properties, Object.Equals() compares two object instances to see if they are the same. Although there are legitimate uses of Object.Equals(), it is often an indication of buggy code.

当比较对象时,开发人员通常想比较的是对象的属性或字段。但是,调用未显式实现Equals()方法的类、超类或者接口,会导致调用从System.Object的继承而来的Equals()方法。Objects.Equals()方法比较是为了比较两个对象是否相同,而不是比较它们的字段或者属性。虽然这种写法是合法的,但通常这也意味着代码Bug。

  Fortify错误示例:

 public class AccountGroup
{
private int gid; public int Gid
{
get { return gid; }
set { gid = value; }
}
}
...
public class CompareGroup
{
public bool compareGroups(AccountGroup group1, AccountGroup group2)
{
return group1.Equals(group2);//Equals() is not implemented in AccountGroup
}
}

Fortify建议:

Verify that the use of Object.Equals() is really the method you intend to call. If not, implement an Equals() method or use a different method for comparing objects.

确保调用Ojbect.Equals()方法确实是你需要调用的,否则,实现Equals()方法来进行对象的比较。

 Fortify推荐示例:

 public class AccountGroup
{
private int gid; public int Gid
{
get { return gid; }
set { gid = value; }
} public override Boolean Equals(Object obj)
{
if (obj == null)
return false;
if (this.GetType() != obj.GetType())
return false;
AccountGroup other = (AccountGroup)obj;
return (gid == other.Gid);
}
}
public class CompareGroup
{
public static bool compareGroups(AccountGroup group1, AccountGroup group2)
{
return group1.Equals(group2);
}
}

Class does not Implement Equals——Code Correctness(代码正确性)的更多相关文章

  1. Code Snippets 代码片段

    Code Snippets 代码片段       1.Title : 代码片段的标题 2.Summary : 代码片段的描述文字 3.Platform : 可以使用代码片段的平台,有IOS/OS X/ ...

  2. 05_动手动脑之String.equals()方法的实现代码

    Question: 请查看String.equals()方法的实现代码,注意学习其实现方法. Answer: java中的String.equals()方法的实现代码: equals()法是根类Obj ...

  3. 动手动脑之查看String.equals()方法的实现代码及解释

    动手动脑 请查看String.equals()方法的实现代码,注意学习其实现方法. 第一个是false,后三个是true. package stringtest; public class Strin ...

  4. Effective Java提升Code Coverage代码涵盖率 - 就是爱Java

    虽然我们已经有了测试程序,但是如何得知是否已完整测试了主程序?,透过Code Coverage代码涵盖率,我们可以快速地得知,目前系统中,有多少程序中被测试过,不考虑成本跟投资效益比,涵盖率越高,代表 ...

  5. 自动生成Code First代码

    自动生成Code First代码 在前面的文章中我们提到Entity Framework的“Code First”模式也同样可以基于现有数据库进行开发.今天就让我们一起看一下使用Entity Fram ...

  6. 第五次作业2、请将该code进行代码重构,使之模块化,并易于阅读和维护;

    1.请运行下面code,指出其功能: (需附运行结果截图,并用简短文字描述其功能) 显示了人的姓名.年龄 2.请将该code进行代码重构,使之模块化,并易于阅读和维护: 3.观看视频The Exper ...

  7. Code::Blocks代码自动提示设置及常用快捷键

    Code::Blocks代码自动提示设置及常用快捷键(适用windows和linux) 1)以下需要设置的地方均在Settings->Editor...弹出的对话框中. 2)不少命令都可针对当前 ...

  8. VS Code 用户自定义代码片段(React)

    VS Code 用户自定义代码片段(React) .jsxReact组件模板:javascriptreact.json { "Import React": { "pref ...

  9. dead code 死代码 无作用的代码

               DatasetVector datasetvector=(DatasetVector)dataset;           if (datasetvector == null) ...

随机推荐

  1. Python笔记(四):异常处理机制与 open()

    (一)  异常处理机制概述 就像日常生活中会遇到各种意外情况一样(例如:你可能考虑过如果中了500w该怎么做),代码运行过程中也会遇到这种意外情况,python提供了这么一种机制,处理意外情况(就像如 ...

  2. LeetCode题解之Sort List

    1.题目描述 2.问题分析 使用sort算法 3.代码 ListNode* sortList(ListNode* head) { if( head == NULL || head->next = ...

  3. Mac逆向--思维导图

  4. scp 利用 ssh 协议 复制文件

    有时候,我们使用 scp 命令可以解决我们很多问题: NAME scp — secure copy (remote file copy program) 使用举例:拷贝当前文件,到 系统 10.66. ...

  5. python 多进程和多线程的区别

    了解线程和进程 进程 程序:磁盘上的可执行二进制文件,并无运行状态. 进程:就是一个正在运行的任务实例(存活在内存里). 获取当前电脑的CPU核心数: pip install psutil >& ...

  6. VS2010使用Release进行调试的三个必须设置选项

    How to: Debug a Release Build You can debug a release build of an application. To debug a release bu ...

  7. 【转】Spring学习---Bean配置的三种方式(XML、注解、Java类)介绍与对比

    [原文]https://www.toutiao.com/i6594205115605844493/ Spring学习Bean配置的三种方式(XML.注解.Java类)介绍与对比 本文将详细介绍Spri ...

  8. MyEclipse免费使用

    import java.text.DecimalFormat; import java.text.NumberFormat; import java.text.SimpleDateFormat; im ...

  9. November 08th, 2017 Week 45th Wednesday

    Keep your face to the sunshine and you cannot see the shadow. 始终面朝阳光,我们就不会看到黑暗. I love sunshine, but ...

  10. SSL/TLS Diffie-Hellman Modulus <= 1024 位 (LogJam) 使用2048位或更高的Diffie-Hellman

    1.http://slproweb.com/products/Win32OpenSSL.html  下载 SSL 并安装 2.设置环境变量,例如工具安装在C:\OpenSSL-Win64,则将C:\O ...