001: /* SynchronizedBlock Copyright (C) 1998-2002 Jochen Hoenicke.
002: *
003: * This program is free software; you can redistribute it and/or modify
004: * it under the terms of the GNU Lesser General Public License as published by
005: * the Free Software Foundation; either version 2, or (at your option)
006: * any later version.
007: *
008: * This program is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: * GNU General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public License
014: * along with this program; see the file COPYING.LESSER. If not, write to
015: * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
016: *
017: * $Id: SynchronizedBlock.java.in,v 4.2.2.1 2002/05/28 17:34:09 hoenicke Exp $
018: */
019:
020: package jode.flow;
021:
022: import jode.decompiler.LocalInfo;
023: import jode.decompiler.TabbedPrintWriter;
024: import jode.expr.Expression;
025: import jode.util.SimpleSet;
026:
027: import java.util.Set;
028:
029: /**
030: * This class represents a synchronized structured block.
031: *
032: * @author Jochen Hoenicke
033: */
034: public class SynchronizedBlock extends StructuredBlock {
035:
036: Expression object;
037: LocalInfo local;
038: boolean isEntered;
039:
040: StructuredBlock bodyBlock;
041:
042: public SynchronizedBlock(LocalInfo local) {
043: this .local = local;
044: }
045:
046: /**
047: * Sets the body block.
048: */
049: public void setBodyBlock(StructuredBlock body) {
050: bodyBlock = body;
051: body.outer = this ;
052: body.setFlowBlock(flowBlock);
053: }
054:
055: /**
056: * Returns all sub block of this structured block.
057: */
058: public StructuredBlock[] getSubBlocks() {
059: return new StructuredBlock[] { bodyBlock };
060: }
061:
062: /**
063: * Replaces the given sub block with a new block.
064: * @param oldBlock the old sub block.
065: * @param newBlock the new sub block.
066: * @return false, if oldBlock wasn't a direct sub block.
067: */
068: public boolean replaceSubBlock(StructuredBlock oldBlock,
069: StructuredBlock newBlock) {
070: if (bodyBlock == oldBlock)
071: bodyBlock = newBlock;
072: else
073: return false;
074: return true;
075: }
076:
077: public Set getDeclarables() {
078: Set used = new SimpleSet();
079: if (object != null)
080: object.fillDeclarables(used);
081: else
082: used.add(local);
083: return used;
084: }
085:
086: public void dumpInstruction(TabbedPrintWriter writer)
087: throws java.io.IOException {
088: if (!isEntered)
089: writer.println("MISSING MONITORENTER");
090: writer.print("synchronized (");
091: if (object != null)
092: object.dumpExpression(writer.EXPL_PAREN, writer);
093: else
094: writer.print(local.getName());
095: writer.print(")");
096: writer.openBrace();
097: writer.tab();
098: bodyBlock.dumpSource(writer);
099: 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: public boolean doTransformations() {
110: StructuredBlock last = flowBlock.lastModified;
111: return (!isEntered && CompleteSynchronized.enter(this, last))
112: || (isEntered && object == null && CompleteSynchronized
113: .combineObject(this, last));
114: }
115: }
|