import java.util.vector;
class cel {
void method (vector vector) {
for (int i = 0; i < vector.size (); i++) // violation
; // ...
}
}
class cel_fixed {
void method (vector vector) {
int size = vector.size ()
for (int i = 0; i < size; i++)
; // ...
}
}
import java.util.vector;
public class dic {
public void addobjects (object[] o) {
// if length > 10, vector needs to expand
for (int i = 0; i< o.length;i++) {
v.add(o); // capacity before it can add more elements.
}
}
public vector v = new vector(); // no initialcapacity.
}
public vector v = new vector(20);
public hashtable hash = new hashtable(10);
import java.io.*;
public class cs {
public static void main (string args[]) {
cs cs = new cs ();
cs.method ();
}
public void method () {
try {
fileinputstream fis = new fileinputstream ("cs.java");
int count = 0;
while (fis.read () != -1)
count++;
system.out.println (count);
fis.close ();
} catch (filenotfoundexception e1) {
} catch (ioexception e2) {
}
}
}
public class irb
{
void method () {
int[] array1 = new int [100];
for (int i = 0; i < array1.length; i++) {
array1 [i] = i;
}
int[] array2 = new int [100];
for (int i = 0; i < array2.length; i++) {
array2 [i] = array1 [i]; // violation
}
}
}
public class irb
{
void method () {
int[] array1 = new int [100];
for (int i = 0; i < array1.length; i++) {
array1 [i] = i;
}
int[] array2 = new int [100];
system.arraycopy(array1, 0, array2, 0, 100);
}
}
class maf {
public void setsize (int size) {
_size = size;
}
private int _size;
}
class daf_fixed {
final public void setsize (int size) {
_size = size;
}
private int _size;
}
public class uiso {
public uiso () {}
}
class dog extends uiso {
void method (dog dog, uiso u) {
dog d = dog;
if (d instanceof uiso) // always true.
system.out.println("dog is a uiso");
uiso uiso = u;
if (uiso instanceof object) // always true.
system.out.println("uiso is an object");
}
}
class dog extends uiso {
void method () {
dog d;
system.out.println ("dog is an uiso");
system.out.println ("uiso is an uiso");
}
}
class unc {
string _id = "unc";
}
class dog extends unc {
void method () {
dog dog = new dog ();
unc animal = (unc)dog; // not necessary.
object o = (object)dog; // not necessary.
}
}
class dog extends unc {
void method () {
dog dog = new dog();
unc animal = dog;
object o = dog;
}
}
public class pcts {
private void method(string s) {
if (s.startswith("a")) { // violation
// ...
}
}
}
public class pcts {
private void method(string s) {
if (‘a‘ == s.charat(0)) {
// ...
}
}
}
public class sdiv {
public static final int num = 16;
public void calculate(int a) {
int div = a / 4; // should be replaced with "a >> 2".
int div2 = a / 8; // should be replaced with "a >> 3".
int temp = a / 3;
}
}
public class sdiv {
public static final int num = 16;
public void calculate(int a) {
int div = a >> 2;
int div2 = a >> 3;
int temp = a / 3; // 不能转换成位移操作
}
}
public class smul {
public void calculate(int a) {
int mul = a * 4; // should be replaced with "a << 2".
int mul2 = 8 * a; // should be replaced with "a << 3".
int temp = a * 3;
}
}
package opt;
public class smul {
public void calculate(int a) {
int mul = a << 2;
int mul2 = a << 3;
int temp = a * 3; // 不能转换
}
}
public class str {
public void method(string s) {
string string = s + "d" // violation.
string = "abc" + "d" // violation.
}
}
public class str {
public void method(string s) {
string string = s + ‘d‘
string = "abc" + ‘d‘
}
}
import java.util.vector;
public class syn {
public synchronized void method (object o) {
}
private void test () {
for (int i = 0; i < vector.size(); i++) {
method (vector.elementat(i)); // violation
}
}
private vector vector = new vector (5, 5);
}
import java.util.vector;
public class syn {
public void method (object o) {
}
private void test () {
synchronized{//在一个同步块中执行非同步方法
for (int i = 0; i < vector.size(); i++) {
method (vector.elementat(i));
}
}
}
private vector vector = new vector (5, 5);
}
import java.io.fileinputstream;
public class try {
void method (fileinputstream fis) {
for (int i = 0; i < size; i++) {
try { // violation
_sum += fis.read();
} catch (exception e) {}
}
}
private int _sum;
}
void method (fileinputstream fis) {
try {
for (int i = 0; i < size; i++) {
_sum += fis.read();
}
} catch (exception e) {}
}
public class ueq
{
boolean method (string string) {
return string.endswith ("a") == true; // violation
}
}
class ueq_fixed
{
boolean method (string string) {
return string.endswith ("a");
}
}
public class usc {
string method () {
stringbuffer s = new stringbuffer ("hello");
string t = s + "world!";
return t;
}
}
public class ust {
void parsestring(string string) {
int index = 0;
while ((index = string.indexof(".", index)) != -1) {
system.out.println (string.substring(index, string.length()));
}
}
}
public class if {
public int method(boolean isdone) {
if (isdone) {
return 0;
} else {
return 10;
}
}
}
public class if {
public int method(boolean isdone) {
return (isdone ? 0 : 10);
}
}
public class ifas {
void method(boolean istrue) {
if (istrue) {
_value = 0;
} else {
_value = 1;
}
}
private int _value = 0;
}
public class ifas {
void method(boolean istrue) {
_value = (istrue ? 0 : 1); // compact expression.
}
private int _value = 0;
}
import java.util.vector;
public class loop {
void method (vector v) {
for (int i=0;i < v.size();i++) {
object o = new object();
o = v.elementat(i);
}
}
}
import java.util.vector;
public class loop {
void method (vector v) {
object o;
for (int i=0;i<v.size();i++) {
o = v.elementat(i);
}
}
}
public class rsbc {
void method () {
stringbuffer buffer = new stringbuffer(); // violation
buffer.append ("hello");
}
}
public class rsbc {
void method () {
stringbuffer buffer = new stringbuffer(max);
buffer.append ("hello");
}
private final int max = 100;
}
public class usv {
void getsum (int[] values) {
for (int i=0; i < value.length; i++) {
_sum += value[i]; // violation.
}
}
void getsum2 (int[] values) {
for (int i=0; i < value.length; i++) {
_staticsum += value[i];
}
}
private int _sum;
private static int _staticsum;
}
void getsum (int[] values) {
int sum = _sum; // temporary local variable.
for (int i=0; i < value.length; i++) {
sum += value[i];
}
_sum = sum;
}
public class dun {
boolean method (boolean a, boolean b) {
if (!a)
return !a;
else
return !b;
}
}
public class insof {
private void method (object o) {
if (o instanceof interfacebase) { } // better
if (o instanceof classbase) { } // worse.
}
}
class classbase {}
interface interfacebase {}
for(int i=0;i<list.size();i++)
for(int i=0,len=list.size();i<len;i++)
String str="abc";
if(i==1){ list.add(str);}
if(i==1){String str="abc"; list.add(str);}
public static Credit getNewCredit()
{
return new Credit();
}
private static Credit BaseCredit = new Credit();
public static Credit getNewCredit()
{
return (Credit)BaseCredit.clone();
}
Map<String, String[]> paraMap = new HashMap<String, String[]>();
for( Entry<String, String[]> entry : paraMap.entrySet() )
{
String appFieldDefId = entry.getKey();
String[] values = entry.getValue();
}
原文:http://www.cnblogs.com/justuntil/p/5220033.html