今天试了下FindBugs分析了一个项目,结果如下.

我分析的是前不久写的网络同步绘画服务器端,
分析得出了三个BUG.
BUG 1: Main.java中,BUG原因是外部的程序可以修改对象中的数据,所以不安全,以后注意.
public static Hashtable<String,User> getUsers(){
        return users;
    }
Public static method may expose internal representation by returning array

A public static method returns a reference to an array that is part of the static state of the class. Any code that calls this method can freely modify the underlying array. One fix is to return a copy of the array.

其实这个方法,在后的程序中有两个地方用了:
out.writeUTF(KEYS.USERS+Main.getUsers().size());
Enumeration<User> ele=Main.getUsers().elements();
修改方法:
将Main.java中的原来方法删除,并添加两个新的方法,OK了!
public static int getUserSize(){
        return users.size();
    }
public static Enumeration<User> getUsers(){
        return users.elements();
}

BUG 2:Clinet.java中,这一个是因为,如果DBS.checkUser(name,pass)抛出异常,那么,user就为null,则抛出NullPointerException
User user=null;
        try {
            user=DBS.checkUser(name, pass);
            if(user==null){
                out.writeUTF(KEYS.ERROR+"密码错误");
                close();
                return;
            }
        } catch (SQLException e) {
            e.printStackTrace();
           
        }
        user.setPort(nport);//这一句有BUG.
Possible null pointer dereference in method on exception path

A reference value which is null on some exception control path is dereferenced here.  This may lead to a NullPointerException when the code is executed.  Note that because FindBugs currently does not prune infeasible exception paths, this may be a false warning.

Also note that FindBugs considers the default case of a switch statement to be an exception path, since the default case is often infeasible.

修改办法在e.printStackTrace()之后,加上return;

BUG 3:MyIcon.java没读懂意思,大概是这样退出不好.

item.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                System.exit(0);//这一句有BUG
            }
        });

Method invokes System.exit(...)

Invoking System.exit shuts down the entire Java virtual machine. This should only been done when it is appropriate. Such calls make it hard or impossible for your code to be invoked by other code. Consider throwing a RuntimeException instead.

还不知道具体原因,所以没法修正.
英语差就是不好.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值