KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jode > flow > CompleteSynchronized


1 /* CompleteSynchronized 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: CompleteSynchronized.java,v 4.14.4.1 2002/05/28 17:34:08 hoenicke Exp $
18  */

19
20 package jode.flow;
21 import jode.GlobalOptions;
22 import jode.expr.*;
23
24 public class CompleteSynchronized {
25
26     /**
27      * This combines the monitorenter into a synchronized statement
28      * @param flow The FlowBlock that is transformed
29      */

30     public static boolean enter(SynchronizedBlock synBlock,
31                                 StructuredBlock last) {
32
33         if (!(last.outer instanceof SequentialBlock))
34             return false;
35         
36         /* If the program is well formed, the following succeed */
37     SequentialBlock sequBlock = (SequentialBlock) synBlock.outer;
38     if (!(sequBlock.subBlocks[0] instanceof InstructionBlock))
39         return false;
40     
41     Expression monenter =
42         ((InstructionBlock) sequBlock.subBlocks[0]).getInstruction();
43     
44     if (!(monenter instanceof MonitorEnterOperator))
45         return false;
46
47     Expression loadOp =
48         ((MonitorEnterOperator)monenter).getSubExpressions()[0];
49     
50     if (!(loadOp instanceof LocalLoadOperator)
51         || (((LocalLoadOperator) loadOp).getLocalInfo()
52         != synBlock.local.getLocalInfo()))
53                 return false;
54             
55         if (GlobalOptions.verboseLevel > 0)
56             GlobalOptions.err.print('s');
57         
58         synBlock.isEntered = true;
59         synBlock.moveDefinitions(last.outer,last);
60         last.replace(last.outer);
61         return true;
62     }
63
64     /**
65      * This combines the initial expression describing the object
66      * into a synchronized statement
67      * @param flow The FlowBlock that is transformed
68      */

69     public static boolean combineObject(SynchronizedBlock synBlock,
70                                         StructuredBlock last) {
71
72         /* Is there another expression? */
73         if (!(last.outer instanceof SequentialBlock))
74             return false;
75     SequentialBlock sequBlock = (SequentialBlock) last.outer;
76
77     if (!(sequBlock.subBlocks[0] instanceof InstructionBlock))
78         return false;
79     InstructionBlock ib = (InstructionBlock) sequBlock.subBlocks[0];
80
81     if (!(ib.getInstruction() instanceof StoreInstruction))
82         return false;
83     StoreInstruction assign = (StoreInstruction) ib.getInstruction();
84
85     if (!(assign.getLValue() instanceof LocalStoreOperator))
86         return false;
87     LocalStoreOperator lvalue = (LocalStoreOperator) assign.getLValue();
88         
89     if (lvalue.getLocalInfo() != synBlock.local.getLocalInfo()
90         || assign.getSubExpressions()[1] == null)
91         return false;
92
93         synBlock.object = assign.getSubExpressions()[1];
94         synBlock.moveDefinitions(last.outer,last);
95         last.replace(last.outer);
96         return true;
97     }
98 }
99
Popular Tags