KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jode > flow > SynchronizedBlock


1 /* SynchronizedBlock Copyright (C) 1998-2002 Jochen Hoenicke.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU Lesser General Public License as published by
5  * the Free Software Foundation; either version 2, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; see the file COPYING.LESSER. If not, write to
15  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  * $Id: SynchronizedBlock.java.in,v 4.2.2.1 2002/05/28 17:34:09 hoenicke Exp $
18  */

19
20 package jode.flow;
21 import jode.decompiler.LocalInfo;
22 import jode.decompiler.TabbedPrintWriter;
23 import jode.expr.Expression;
24 import jode.util.SimpleSet;
25
26 import java.util.Set JavaDoc;
27
28 /**
29  * This class represents a synchronized structured block.
30  *
31  * @author Jochen Hoenicke
32  */

33 public class SynchronizedBlock extends StructuredBlock {
34
35     Expression object;
36     LocalInfo local;
37     boolean isEntered;
38
39     StructuredBlock bodyBlock;
40
41     public SynchronizedBlock(LocalInfo local) {
42         this.local = local;
43     }
44     
45     /**
46      * Sets the body block.
47      */

48     public void setBodyBlock(StructuredBlock body) {
49         bodyBlock = body;
50         body.outer = this;
51         body.setFlowBlock(flowBlock);
52     }
53
54     /**
55      * Returns all sub block of this structured block.
56      */

57     public StructuredBlock[] getSubBlocks() {
58     return new StructuredBlock[] { bodyBlock };
59     }
60
61     /**
62      * Replaces the given sub block with a new block.
63      * @param oldBlock the old sub block.
64      * @param newBlock the new sub block.
65      * @return false, if oldBlock wasn't a direct sub block.
66      */

67     public boolean replaceSubBlock(StructuredBlock oldBlock,
68                             StructuredBlock newBlock) {
69         if (bodyBlock == oldBlock)
70             bodyBlock = newBlock;
71         else
72             return false;
73         return true;
74     }
75
76     public Set getDeclarables() {
77     Set used = new SimpleSet();
78     if (object != null)
79         object.fillDeclarables(used);
80     else
81         used.add(local);
82     return used;
83     }
84
85     public void dumpInstruction(TabbedPrintWriter writer)
86     throws java.io.IOException JavaDoc
87     {
88         if (!isEntered)
89             writer.println("MISSING MONITORENTER");
90         writer.print("synchronized (");
91     if (object != null)
92         object.dumpExpression(writer.EXPL_PAREN, writer);
93     else
94         writer.print(local.getName());
95     writer.print(")");
96     writer.openBrace();
97         writer.tab();
98         bodyBlock.dumpSource(writer);
99         writer.untab();
100     writer.closeBrace();
101     }
102
103     public void simplify() {
104     if (object != null)
105         object = object.simplify();
106     super.simplify();
107     }
108     
109
110     public boolean doTransformations() {
111         StructuredBlock last = flowBlock.lastModified;
112         return (!isEntered && CompleteSynchronized.enter(this, last))
113             || (isEntered && object == null
114                 && CompleteSynchronized.combineObject(this, last));
115     }
116 }
117
Popular Tags