01: /*
02: * Javassist, a Java-bytecode translator toolkit.
03: * Copyright (C) 1999-2006 Shigeru Chiba. All Rights Reserved.
04: *
05: * The contents of this file are subject to the Mozilla Public License Version
06: * 1.1 (the "License"); you may not use this file except in compliance with
07: * the License. Alternatively, the contents of this file may be used under
08: * the terms of the GNU Lesser General Public License Version 2.1 or later.
09: *
10: * Software distributed under the License is distributed on an "AS IS" basis,
11: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12: * for the specific language governing rights and limitations under the
13: * License.
14: */
15:
16: package javassist.runtime;
17:
18: /**
19: * A support class for implementing <code>$cflow</code>.
20: * This support class is required at runtime
21: * only if <code>$cflow</code> is used.
22: *
23: * @see javassist.CtBehavior#useCflow(String)
24: */
25: public class Cflow extends ThreadLocal {
26: private static class Depth {
27: private int depth;
28:
29: Depth() {
30: depth = 0;
31: }
32:
33: int get() {
34: return depth;
35: }
36:
37: void inc() {
38: ++depth;
39: }
40:
41: void dec() {
42: --depth;
43: }
44: }
45:
46: protected synchronized Object initialValue() {
47: return new Depth();
48: }
49:
50: /**
51: * Increments the counter.
52: */
53: public void enter() {
54: ((Depth) get()).inc();
55: }
56:
57: /**
58: * Decrements the counter.
59: */
60: public void exit() {
61: ((Depth) get()).dec();
62: }
63:
64: /**
65: * Returns the value of the counter.
66: */
67: public int value() {
68: return ((Depth) get()).get();
69: }
70: }
|