KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > jvm > choice > IntChoiceFromStaticVarSet


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

20 /*
21  * Created on Sep 8, 2005
22  *
23  * TODO To change the template for this generated file go to
24  * Window - Preferences - Java - Code Style - Code Templates
25  */

26 package gov.nasa.jpf.jvm.choice;
27
28 import gov.nasa.jpf.Config;
29 import gov.nasa.jpf.jvm.IntChoiceGenerator;
30 import gov.nasa.jpf.jvm.JVM;
31 import gov.nasa.jpf.jvm.StaticElementInfo;
32 import gov.nasa.jpf.jvm.ClassInfo;
33 /**
34  * @author jpenix
35  *
36  * choose from a set of static variables (auch as an enumeration)
37  * specified in a configuration as
38  * bob.class = IntChoiceFromStaticVarSet
39  * bob.values = {MyClass.ONE, MyClass.TWO, MyOtherClass.OTHER}
40  * where "bob" is the choice id.
41  *
42  * choices can then made using: getInt("bob");
43  */

44 /**
45  * @author jpenix
46  *
47  * TODO To change the template for this generated type comment go to
48  * Window - Preferences - Java - Code Style - Code Templates
49  */

50 public class IntChoiceFromStaticVarSet extends IntChoiceGenerator {
51
52     String JavaDoc[] values;
53     int count = -1;
54     
55     /**
56      * @param conf - JPF configuration object
57      * @param id - String identifier for the choice instance. this should be unique.
58      */

59     public IntChoiceFromStaticVarSet(Config conf, String JavaDoc id) {
60         super(id);
61         values = conf.getStringArray(id + ".values");
62     }
63
64     /* (non-Javadoc)
65      * @see gov.nasa.jpf.jvm.IntChoiceGenerator#getNextChoice()
66      */

67     public int getNextChoice(JVM vm) {
68         
69         //split out class name and variable name
70
String JavaDoc[] varId = values[count].split("[.]+");
71
72         ClassInfo ci = ClassInfo.getClassInfo(varId[0]);
73         StaticElementInfo ei = vm.getKernelState().sa.get(ci.getName());
74         int ret = ei.getIntField(varId[1], null);
75
76         // print "Choice: bob = MyClass.ONE(1)"
77
vm.println("Choice: "+id + " = " + values[count] + "("+ret+")");
78         return ret;
79     }
80
81     /* (non-Javadoc)
82      * @see gov.nasa.jpf.jvm.ChoiceGenerator#hasMoreChoices()
83      */

84     public boolean hasMoreChoices(JVM vm) {
85         if (count < values.length-1)
86             return true;
87         else
88             return false;
89     }
90
91     /* (non-Javadoc)
92      * @see gov.nasa.jpf.jvm.ChoiceGenerator#advance(gov.nasa.jpf.jvm.JVM)
93      */

94     public void advance(JVM vm) {
95         count++;
96     }
97
98 }
99
Popular Tags