第二步:透過第一步驟所設定的criterion,對產生的slice進行分析。尋找造成錯誤的變數criterion 所指向的物件是否來自其他元件的生成,並且透過某些指令將其傳遞給criterion,我們標記這
8 return stringArray.length;
9 } 7 }
Component : buyer 1
Component :wholesaler 1 package wholesaler;
2 import factor_A.material_A;
3 import factor_B.material_B;
4 public class TransProduct {
5 material_B productB=new material_B();
6 material_A productA=new material_A();
7 public Object getProduct(){
8 Object transObj;
9 Double i=new Double((Math.random()*10));
10 if(i.intValue()<5){
11
12 transObj=productA.getObjFromA();
13 }
14 else{
15
16 transObj=productB.getObjFromB();
17 }
18 return transObj;
19 } 20 }
Component : factor_A 1 package factor_A;
2 public class material_A { 3 private String product_A;
4 public Object getObjFromA(){
5 return product_A;
Component : factor_B 1 package factor_B;
2 public class material_B { 3 private Object product_B;
4 public Object getObjFromB(){
5 Object s=(Double)product_B;
6 return s;
Component score:4+3
Component score:16
Component score:4
Component score:6
Component : store
1 import warehouse.StoreArray;
2 import wholesaler.TransProduct;
3 import buyer.buyer;
4 public class Store {
5 public static void main(String[] args) {
6 TransProduct tp =new TransProduct();
7 8
9 int i=StoreArray.getArraySize()-1;
10 Object warehouse[] = StoreArray.getArray();
11 Object product=tp.getProduct();
12 while(i>=0){
z 本案例(ArrayStoreException)使用rule 11 (追蹤設定criterion的物件傳 遞生成敘述),weight為 2 所進行之分析。本案例當中有兩個criterion,位 於component :store第 13 行的變數warehouse以及變數product。這兩個變 數都為criterion,根據這兩個變數所將會標記出如圖 50的的敘述句,對於 變數x進行分析的說明如下:
1. 對於陣列變數 x 追蹤設定它的物件將會找到 component : store 當中的 第 10 行的"Object warehouse[] = StoreArray.getArray();",此行 敘述設定了變數 warehouse,因此標記此行。由於其設定變數 warehouse 的形式是一個物件呼叫方法的回傳物件,所以在此設定此物件方法 (StoreArray.getArray())的回傳物件為下一個要追蹤的物件。
2. 在 component :warehouse 當中追蹤 getArray()方法的回傳物件,第 5 行 的 變 數 stringArray 。 並 且 找 到 第 三 行 的 "static Object stringArray[]=new String[100];",此行為此變數的生成敘述,根據 規則除了標記此行之外還會在給予額外積分值。
以上是對變數 x 的物件傳遞進行追蹤的步驟以及標記的指令。下面則是對變 數 product 進行追蹤的步驟:
1. 對於變數product我們將會找到在圖 50裡component : store當中的第 11 行" Object product=tp.getProduct();"此行設定product的設 定,並且且將此物件的呼叫方法(tp.getProduct())的回傳物件作為下 一個要追蹤的物件。
2. 追 蹤 component : wholesaler 當 中 getProduct() 方 法 的 回 傳 物 件 transObj,將會得到在此圖 50 component : wholesaler當中的第 12 行 : "transObj=productA.getObjFromA();" 以 及 第 16 行 : " transObj=productB.getObjFromB();" 此 兩 行 會 設 定 欲 追 蹤 的 物 件 transObj。
3. 在圖 50對於component : factor_A當中的getObjFromA()當中函式的回
傳物件product_A會被設定為預追蹤的物件。然而在這些slice資訊當中 並沒有找到設定component : factor_A當中的getObjFromA()回傳物件 product_A的指令,所以並沒有標記任何指令。
4. 在圖 50對於component : factor_B當中函式getObjFromB()當中函式的 回傳物件s,則會追蹤到第 5 行"Object s=(Double)product_B;"設定 回傳物件s的值,並且標記之。而物件product_B在往上搜尋並沒有找尋 到設定此物件的敘述,因此此rule到此分析結束。
被 標 記 後 的 敘 述 句 將 會 得 到 weight 為 2 的 積 分 , 而 由 於 圖 50 當 中 的 component :warehouse 被 標 記 的 " static Object stringArray[]=new String[100];"這行敘述,代表這個設定criterion的物件並非從其他元件 取得,而是自己元件內所生成,因此根據規則會再加予weight為 2 的積分。
z 本案例(ArrayStoreException)最後將會使用rule 13(標記轉換物件型態的 敘述句),weight為 15。這個規則將會對設定criterion物件的敘述句,判斷 是否有部份敘述對物件進行了型態的轉換(type casting)。若發現類似敘 述,便給予較高的積分值(如圖 51)。
Component : warehouse 1 package warehouse;
2 public class StoreArray {
3 static Object stringArray[]=new String[100];
4 public static Object[] getArray(){
5 return stringArray;
Component : store
1 import warehouse.StoreArray;
2 import wholesaler.TransProduct;
3 import buyer.buyer;
4 public class Store {
5 public static void main(String[] args) {
6 TransProduct tp =new TransProduct();
7 8
9 int i=StoreArray.getArraySize()-1;
10 Object warehouse[] = StoreArray.getArray();
11 Object product=tp.getProduct();
12 while(i>=0){
Component : buyer 1
Component :wholesaler 1 package wholesaler;
2 import factor_A.material_A;
3 import factor_B.material_B;
4 public class TransProduct {
5 material_B productB=new material_B();
6 material_A productA=new material_A();
7 public Object getProduct(){
8 Object transObj;
9 Double i=new Double((Math.random()*10));
10 if(i.intValue()<5){
11
12 transObj=productA.getObjFromA();
13 }
14 else{
15
16 transObj=productB.getObjFromB();
17 }
18 return transObj;
19 } 20 }
Component : factor_A 1 package factor_A;
2 public class material_A { 3 private String product_A;
4 public Object getObjFromA(){
5 return product_A;
Component : factor_B 1 package factor_B;
2 public class material_B { 3 private Object product_B;
4 public Object getObjFromB(){