KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > jvm > bytecode > FieldInstruction


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package gov.nasa.jpf.jvm.bytecode;
20
21 import gov.nasa.jpf.Config;
22 import gov.nasa.jpf.JPFException;
23 import gov.nasa.jpf.jvm.ElementInfo;
24 import gov.nasa.jpf.jvm.ThreadInfo;
25 import gov.nasa.jpf.jvm.ClassInfo;
26
27 import org.apache.bcel.classfile.ConstantPool;
28 import org.apache.bcel.generic.ConstantPoolGen;
29 import org.apache.bcel.generic.Type;
30 import org.apache.bcel.generic.ReferenceType;
31 import gov.nasa.jpf.jvm.FieldInfo;
32 import gov.nasa.jpf.jvm.FieldLockInfo;
33
34 /**
35  * parent class for PUT/GET FIELD/STATIC insns
36  *
37  * <2do> there is a inheritance level missing to deal with instance/static
38  * fields - w/o the instance/static helper methods we would have to duplicate
39  * code in the getters/setters
40  */

41 public abstract class FieldInstruction extends Instruction implements VariableAccessor
42 {
43   static FieldLockInfo prototype; //optimization measure
44

45   protected String JavaDoc fname;
46   protected String JavaDoc className;
47   protected String JavaDoc varId;
48   
49   protected FieldInfo fi; // lazy eval, hence not public
50

51   protected int size;
52   protected boolean isReferenceField;
53   
54   public static void init (Config config) throws Config.Exception {
55     if (config.getBoolean("vm.por") && config.getBoolean("vm.por.sync_detection")) {
56       prototype = (FieldLockInfo) config.getEssentialInstance("vm.por.fieldlockinfo.class",
57                                                   FieldLockInfo.class);
58     }
59   }
60   
61   public void setPeer (org.apache.bcel.generic.Instruction i, ConstantPool cp) {
62     org.apache.bcel.generic.FieldInstruction fi;
63     ConstantPoolGen cpg;
64     
65     fi = (org.apache.bcel.generic.FieldInstruction) i;
66     cpg = ClassInfo.getConstantPoolGen(cp);
67     
68     fname = fi.getFieldName(cpg);
69     className = fi.getClassName(cpg);
70     
71     Type ft = fi.getFieldType(cpg);
72     if (ft instanceof ReferenceType) {
73       isReferenceField = true;
74     }
75     
76     size = ft.getSize();
77   }
78   
79   public abstract FieldInfo getFieldInfo ();
80      
81   public boolean isReferenceField () {
82     return isReferenceField;
83   }
84   
85   public String JavaDoc getId(ElementInfo ei) {
86     // <2do> - OUTCH, should be optimized
87
return (ei.toString() + '.' + fname);
88   }
89   
90   public String JavaDoc getVariableId () {
91     if (varId == null) {
92       varId = className + '.' + fname;
93     }
94     return varId;
95   }
96
97   /**
98    * is this field supposed to be protected by a lock?
99    * this only gets called if on-the-fly POR is in effect
100    */

101   protected boolean isLockProtected (ElementInfo ei, ThreadInfo ti) {
102     String JavaDoc id = getId(ei);
103     FieldLockInfo flInfo = ei.getFieldLockInfo( id);
104     FieldLockInfo flInfoNext;
105     
106     FieldInfo fi = getFieldInfo(); // so that we make sure it's computed
107

108     if (flInfo == null) {
109       try {
110         flInfoNext = (FieldLockInfo) prototype.clone();
111       } catch (Exception JavaDoc x) {
112         throw new JPFException(x); // pretty lame error handling here
113
}
114     } else {
115       flInfoNext = flInfo.checkProtection(ei,fi,ti);
116     }
117     
118     if (flInfoNext != flInfo) {
119       ei.setFieldLockInfo(id, flInfoNext);
120     }
121     
122     return flInfoNext.isProtected();
123   }
124 }
125
126
127
128
129
130
131
132
133
Popular Tags