盾怪网教程:是一个免费提供流行杀毒软件教程、在线学习分享的学习平台!

审查Java 代码的11种经常见出错

时间:2024/12/4作者:未知来源:盾怪网教程人气:

[摘要]width < 0) throw new IllegalArgumentException(); d.height = height; d.width = width; } public sy...
width < 0)

throw new IllegalArgumentException();

d.height = height;

d.width = width;

}

public synchronized Dimension getValues(){

// Ooops! Breaks encapsulation

return d;

}

}

Example类保证了它所存储的height和width值永远非负数,试图使用setValues()方法来设置负值会触发异常。不幸的是,由于getValues()返回d的引用,而不是d的拷贝,你可以编写如下的破坏性代码:

Example ex = new Example();

Dimension d = ex.getValues();

d.height = -5;

d.width = -10;

现在,Example对象拥有负值了!如果getValues() 的调用者永远也不设置返回的Dimension对象的width 和height值,那么仅凭测试是不可能检测到这类的错误。

不幸的是,随着时间的推移,客户代码可能会改变返回的Dimension对象的值,这个时候,追寻错误的根源是件枯燥且费时的事情,尤其是在多线程环境中。

更好的方式是让getValues()返回拷贝:

public synchronized Dimension getValues(){

return new Dimension (d.x, d.y);

}

现在,Example对象的内部状态就安全了。调用者可以根据需要改变它所得到的拷贝的状态,但是要修改Example对象的内部状态,必须通过setValues()才可以。

常见错误3#:不必要的克隆

我们现在知道了get方法应该返回内部数据对象的拷贝,而不是引用。但是,事情没有绝对:

/*** Example class.The value should never * be negative.*/

public class Example{

private Integer i = new Integer (0);

public Example (){ }

/*** Set x. x must be nonnegative* or an exception will be thrown*/

public synchronized void setValues (int x)

throws IllegalArgumentException{

if (x < 0)

throw new IllegalArgumentException();

i = new Integer (x);

}

public synchronized Integer getValue(){

// We can’t clone Integers so we makea copy this way.

return new Integer (i.intValue());

}

}

这段代码是安全的,但是就象在错误1#那样,又作了多余的工作。Integer对象,就象String对象那样,一旦被创建就是不可变的。因此,返回内部Integer对象,而不是它的拷贝,也是安全的。

方法getValue()应该被写为:

public synchronized Integer getValue(){

// ’i’ is immutable, so it is safe to return it instead of a copy.

return i;

}

Java程序比C++程序包含更多的不可变对象。JDK 所提供的若干不可变类包括:

·Boolean

·Byte

·Character

·Class

·Double

·Float

·Integer

·Long

·Short

·String

·大部分的Exception的子类

常见错误4# :自编代码来拷贝数组

Java允许你克隆数组,但是开发者通常会错误地编写如下的代码,问题在于如下的循环用三行做的事情,如果采用Object的clone方法用一行就可以完成:

public class Example{

private int[] copy;

/*** Save a copy of ’data’. ’data’ cannot be null.*/

public void saveCopy (int[] data){

copy = new int[data.length];

for (int i = 0; i < copy.length; ++i)

copy[i] = data[i];

}

}

这段代码是正确的,但却不必要地复杂。saveCopy()的一个更好的实现是:

void saveCopy (int[] data){

try{

copy = (int[])data.clone();

}catch (CloneNotSupportedException e){

// Can’t get here.

}

}

如果你经常克隆数组,编写如下的一个工具方法会是个好主意:

static int[] cloneArray (int[] data){

try{

return(int[])data.clone();

}catch(CloneNotSupportedException e){

// Can’t get here.

}

}

这样的话,我们的saveCopy看起来就更简洁了:

void saveCopy (int[] data){

copy = cloneArray ( data);

}

常见错误5#:拷贝错误的数据

有时候程序员知道必须返回一个拷贝,但是却不小心拷贝了错误的数据。由于仅仅做了部分的数据拷贝工作,下面的代码与程序员的意图有偏差:

import java.awt.Dimension;

/*** Example class. The height and width values should never * be

negative. */

public class Example{

static final public int TOTAL_VALUES = 10;

private Dimension[] d = new Dimension[TOTAL_VALUES];

public Example (){ }

/*** Set height and width. Both height and width must be

nonnegative * or an exception will be thrown. */

public synchronized void setValues (int index, int height, int width)

throws IllegalArgumentException{

if (height < 0

关键词:审查Java 代码的11种经常见出错




Copyright © 2012-2018 盾怪网教程(http://www.dunguai.com) .All Rights Reserved 网站地图 友情链接

免责声明:本站资源均来自互联网收集 如有侵犯到您利益的地方请及时联系管理删除,敬请见谅!

QQ:1006262270   邮箱:kfyvi376850063@126.com   手机版