KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > jvm > DefaultAttributor


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;
20
21 import org.apache.bcel.classfile.JavaClass;
22 import org.apache.bcel.classfile.Method;
23 import org.apache.bcel.classfile.Field;
24
25 /**
26  * default Attributor implementation to set method and fiel attributes
27  * at class load time. Note this is critical functionality, esp.
28  * with respect to threading
29  */

30 public class DefaultAttributor implements Attributor {
31   
32   // <2do> we should turn atomicity and scheduling relevance into general
33
// MethodInfo attributes, to keep it consistent with object and field attrs
34

35   public boolean isMethodAtomic (JavaClass jc, Method mth, String JavaDoc uniqueName) {
36     
37     // per default, we set all standard library methods atomic
38
// (aren't we nicely optimistic, are we?)
39
if (jc.getPackageName().startsWith( "java.")) {
40       String JavaDoc clsName = jc.getClassName();
41       
42       // except of the signal methods, of course
43
if (clsName.equals("java.lang.Object")) {
44         if (uniqueName.startsWith("wait(") ||
45             uniqueName.equals("notify()")) {
46           return false;
47         }
48       } else if (clsName.equals("java.lang.Thread")) {
49         if (uniqueName.equals("join()")) {
50           return false;
51         }
52       }
53       
54       return true;
55     }
56     
57     return false;
58   }
59   
60   /**
61    * is calling a certain method in the context of multiple runnable threads
62    * scheduling relevant (i.e. has to be considered as a step boundary
63    */

64   public int getSchedulingRelevance (JavaClass jc, Method mth, String JavaDoc uniqueName) {
65     String JavaDoc cls = jc.getClassName();
66     
67     if (cls.equals("java.lang.Thread")) {
68       if (uniqueName.equals("start()")) {
69         // <2do> - this is not required if we re-compute reachability after
70
// a thread becomes runnable, so that subsequent field/lock accesses
71
// depending on the number of runnables will break correctly. Check
72
// what other insns (beyond ref-field assignment) might be in the same
73
// category (on-the-fly remark w/o saving the state)
74

75         //return MethodInfo.SR_ALWAYS;
76
} else if (uniqueName.equals("yield()") ||
77           uniqueName.equals("sleep(J)") ||
78           uniqueName.equals("join()")) {
79         return MethodInfo.SR_RUNNABLES;
80       }
81     } else if (cls.equals("java.lang.Object")) {
82       if ( /*uniqueName.equals("wait()") || */
83           uniqueName.equals("wait(J)") ||
84           uniqueName.equals("notify()")) {
85         return MethodInfo.SR_RUNNABLES;
86       }
87     } else if (cls.equals("gov.nasa.jpf.jvm.Verify")) {
88       // note that the Verify.randoms do not fall into this category,
89
// since they are always step boundaries
90
if (uniqueName.equals("beginAtomic()") ||
91           uniqueName.equals("endAtomic()")) {
92         //return MethodInfo.SR_ALWAYS;
93
}
94     }
95
96     if (mth.isSynchronized()) {
97       return MethodInfo.SR_SYNC;
98     }
99
100     return MethodInfo.SR_NEVER;
101   }
102   
103   /**
104    * answer the type based object attributes for this class. See
105    * ElementInfo for valid choices
106    */

107   public int getObjectAttributes (JavaClass jc) {
108     String JavaDoc clsName = jc.getClassName();
109     
110     // very very simplistic for now
111
if (clsName.equals("java.lang.String") ||
112        clsName.equals("java.lang.Integer") ||
113        clsName.equals("java.lang.Long") ||
114        clsName.equals("java.lang.Class")
115         /* ..and a lot more.. */
116        ) {
117       return ElementInfo.ATTR_IMMUTABLE;
118     } else {
119       return 0;
120     }
121   }
122   
123   public int getFieldAttributes (JavaClass jc, Field f) {
124     String JavaDoc clsName = jc.getClassName();
125     String JavaDoc fName = f.getName();
126     
127     if (clsName.equals("java.lang.ThreadGroup")) {
128       if (fName.equals("threads")) {
129         return ElementInfo.ATTR_PROP_MASK & ~ElementInfo.ATTR_TSHARED;
130       }
131     }
132     
133     return ElementInfo.ATTR_PROP_MASK;
134   }
135 }
136
137
Popular Tags