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

我分析的是前不久写的网络同步绘画服务器端,
分析得出了三个BUG.
BUG 1: Main.java中,BUG原因是外部的程序可以修改对象中的数据,所以不安全,以后注意.
public static Hashtable 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 ele=Main.getUsers().elements();
修改方法:
将Main.java中的原来方法删除,并添加两个新的方法,OK了!
public static int getUserSize(){
        return users.size();
    }
public static Enumeration 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.

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

你可能感兴趣的:(JAVA程序)