1 /* ScannerState.java
2  Copyright (C) 2005 Free Software Foundation, Inc.
3 
4  This file is part of GNU Classpath.
5 
6  GNU Classpath is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2, or (at your option)
9  any later version.
10 
11  GNU Classpath is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with GNU Classpath; see the file COPYING.  If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301 USA.
20 
21  Linking this library statically or dynamically with other modules is
22  making a combined work based on this library.  Thus, the terms and
23  conditions of the GNU General Public License cover the whole
24  combination.
25 
26  As a special exception, the copyright holders of this library give you
27  permission to link this library with independent modules to produce an
28  executable, regardless of the license terms of these independent
29  modules, and to copy and distribute the resulting executable under
30  terms of your choice, provided that you also meet, for each linked
31  independent module, the terms and conditions of the license of that
32  module.  An independent module is a module which is not derived from
33  or based on this library.  If you modify this library, you may extend
34  this exception to your version of the library, but you are not
35  obligated to do so.  If you do not wish to do so, delete this
36  exception statement from your version. */
37 
38 
39 package gnu.java.beans.encoder;
40 
41 import java.util.HashMap;
42 
43 /** <p>Provides the infrastructure for the state machine and the transition
44  * mechanism.</p>
45  *
46  * <p>Each states knows a set of successor. There can be one successor for
47  * every transition variant. Furthermore a state knows about a default
48  * successor which is taken when there is no special setup for a
49  * transition.</p>
50  *
51  * @author Robert Schuster (robertschuster@fsfe.org)
52  *
53  */
54 public abstract class ScannerState
55 {
56 
57   static final int TRANSITION_METHOD_INVOCATION = 0;
58 
59   static final int TRANSITION_STATIC_METHOD_INVOCATION = 1;
60 
61   static final int TRANSITION_STATIC_FIELD_ACCESS = 2;
62 
63   static final int TRANSITION_CLASS_RESOLUTION = 3;
64 
65   static final int TRANSITION_OBJECT_INSTANTIATION = 4;
66 
67   static final int TRANSITION_PRIMITIVE_INSTANTIATION = 5;
68 
69   static final int TRANSITION_OBJECT_ARRAY_INSTANTIATION = 6;
70 
71   static final int TRANSITION_PRIMITIVE_ARRAY_INSTANTIATION = 7;
72 
73   static final int TRANSITION_ARRAY_SET = 8;
74 
75   static final int TRANSITION_ARRAY_GET = 9;
76 
77   static final int TRANSITION_LIST_SET = 10;
78 
79   static final int TRANSITION_LIST_GET = 11;
80 
81   static final int TRANSITION_NULL_OBJECT = 12;
82 
83   static final int TRANSITION_STRING_REFERENCE = 13;
84 
85   static final int TRANSITION_OBJECT_REFERENCE = 14;
86 
87   static final int TRANSITION_FIRST = 0;
88 
89   static final int TRANSITION_LAST = 14;
90 
91   static final String DEFAULT_STATE_NAME = "default";
92 
93   String defaultSuccessor = DEFAULT_STATE_NAME;
94 
95   static String[] transitionNames = { "METHOD_INVOCATION", "STATIC_METHOD_INVOCATION",
96                               "STATIC_FIELD_ACCESS", "CLASS_RESOLUTION",
97                               "OBJECT_INSTANTIATION",
98                               "PRIMITIVE_INSTANTIATION", "OBJECT_ARRAY_INSTANTIATION",
99                               "PRIMITIVE_ARRAY_INSTANTIATION",
100                               "ARRAY_SET", "ARRAY_GET", "LIST_SET", "LIST_GET",
101                               "NULL_OBJECT", "STRING_REFERENCE", "OBJECT_REFERENCE" };
102 
103   /**
104    * Stores the transition setup as the relation
105    * transition->successor's state name.
106    */
107   HashMap transitions = new HashMap();
108 
109   int calls;
110 
111   Context context;
112 
113   String name;
114 
init(String newName)115   final void init(String newName)
116   {
117     assert (name == null);
118 
119     name = newName;
120   }
121 
getName()122   final String getName()
123   {
124     return name;
125   }
126 
enter(Context ctx)127   final void enter(Context ctx)
128   {
129     calls++;
130     context = ctx;
131 
132     enterImpl(ctx);
133   }
134 
enterImpl(Context ctx)135   protected void enterImpl(Context ctx)
136   {
137   }
138 
context()139   final Context context()
140   {
141     return context;
142   }
143 
getCalls()144   final int getCalls()
145   {
146     return calls;
147   }
148 
149   /**
150    * <p>Stores a successor's state name for a certain transition.</p>
151    *
152    * <p>This method is only used at the configuration time of the state
153    * machine.</p>
154    *
155    * @param transition One of the transition constants.
156    * @param stateName The state name of the successor.
157    */
putSuccessor(int transition, String stateName)158   final void putSuccessor(int transition, String stateName)
159   {
160     assert (transition >= TRANSITION_FIRST && transition <= TRANSITION_LAST) :
161       "Transition identifier '" + transition + "' is unknown.";
162 
163     transitions.put(new Integer(transition), stateName);
164   }
165 
166   /** <p>Retrieves a the state name of a successor for the given transition
167    * constant.</p>
168    *
169    * <p>Returns the default successor's state name if no special setup was
170    * prepared.</p>
171    *
172    * @param transition One of the transition constants.
173    * @return The state name of the successor.
174    */
getSuccessor(int transition)175   final String getSuccessor(int transition)
176   {
177     String state = (String) transitions.get(new Integer(transition));
178 
179     return (state == null) ? defaultSuccessor : state;
180   }
181 
182   /**
183    * Sets the name for the default successor state.
184    *
185    * @param newDefaultSuccessor The default successor's state name.
186    */
setDefaultSuccessor(String newDefaultSuccessor)187   final void setDefaultSuccessor(String newDefaultSuccessor)
188   {
189     defaultSuccessor = newDefaultSuccessor;
190   }
191 
methodInvocation(String methodName)192   abstract void methodInvocation(String methodName);
193 
staticMethodInvocation(String className, String methodName)194   abstract void staticMethodInvocation(String className, String methodName);
195 
staticFieldAccess(String className, String fieldName)196   abstract void staticFieldAccess(String className, String fieldName);
197 
classResolution(String className)198   abstract void classResolution(String className);
199 
objectInstantiation(String className, ObjectId objectId)200   abstract void objectInstantiation(String className, ObjectId objectId);
201 
primitiveInstantiation(String primitiveName, String valueAsString)202   abstract void primitiveInstantiation(String primitiveName,
203                                        String valueAsString);
204 
objectArrayInstantiation(String arrayClassName, String lengthAsString, ObjectId objectId)205   abstract void objectArrayInstantiation(String arrayClassName, String lengthAsString, ObjectId objectId);
206 
primitiveArrayInstantiation(String arrayClassName, String lengthAsString, ObjectId objectId)207   abstract void primitiveArrayInstantiation(String arrayClassName, String lengthAsString, ObjectId objectId);
208 
arraySet(String indexAsString)209   abstract void arraySet(String indexAsString);
210 
arrayGet(String indexAsString)211   abstract void arrayGet(String indexAsString);
212 
listGet()213   abstract void listGet();
214 
listSet()215   abstract void listSet();
216 
nullObject()217   abstract void nullObject();
218 
stringReference(String string)219   abstract void stringReference(String string);
220 
objectReference(ObjectId id)221   abstract void objectReference(ObjectId id);
222 
223   /**
224    * <p>A special event that does not provoke a direct transition.</p>
225    *
226    * <p>Instead the transition is done by the <code>ScanEngine</code>: It goes
227    * back to the previous state and just uses this method to inform the state
228    * about this happening.</p>
229    */
end()230   abstract void end();
231 
enter()232   void enter()
233   {
234   }
235 
236 }
237