KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jode > flow > CreateCheckNull


1 /* CreateCheckNull Copyright (C) 1999-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: CreateCheckNull.java,v 1.11.4.1 2002/05/28 17:34:08 hoenicke Exp $
18  */

19
20 package jode.flow;
21 import jode.expr.*;
22 import jode.type.Type;
23 import jode.decompiler.LocalInfo;
24
25 public class CreateCheckNull {
26
27     /* Situation:
28      *
29      * javac:
30      * DUP
31      * POP.getClass();
32      *
33      * jikes:
34      * DUP
35      * if (POP == null)
36      * throw null;
37      */

38
39     /**
40      * Transforms the code
41      * <pre>
42      * DUP
43      * POP.getClass()
44      * </pre>
45      * to a CheckNullOperator. This is what javac generates when it
46      * calls ".new" on an operand.
47      */

48     public static boolean transformJavac(InstructionContainer ic,
49                      StructuredBlock last) {
50         if (!(last.outer instanceof SequentialBlock)
51         || !(ic.getInstruction() instanceof Operator)
52         || !(last.outer.getSubBlocks()[0] instanceof SpecialBlock))
53             return false;
54
55     SpecialBlock dup = (SpecialBlock) last.outer.getSubBlocks()[0];
56     if (dup.type != SpecialBlock.DUP
57         || dup.count != 1 || dup.depth != 0)
58         return false;
59        
60     Operator ce = (Operator) ic.getInstruction();
61
62     if (!(ce.getOperator() instanceof PopOperator)
63         || !(ce.getSubExpressions()[0] instanceof InvokeOperator))
64         return false;
65
66         InvokeOperator getClassCall
67         = (InvokeOperator) ce.getSubExpressions()[0];
68     if (!getClassCall.getMethodName().equals("getClass")
69         || !(getClassCall.getMethodType().toString()
70          .equals("()Ljava/lang/Class;")))
71         return false;
72
73     LocalInfo li = new LocalInfo();
74     ic.setInstruction(new CheckNullOperator(Type.tUObject, li));
75     last.replace(last.outer);
76         return true;
77     }
78
79     /**
80      * Transforms the code
81      * <pre>
82      * DUP
83      * if (POP == null) {
84      * throw null
85      * GOTO END_OF_METHOD // not checked
86      * }
87      * </pre>
88      * to a CheckNullOperator. This is what jikes generates when it
89      * calls ".new" on an operand.
90      */

91     public static boolean transformJikes(IfThenElseBlock ifBlock,
92                      StructuredBlock last) {
93         if (!(last.outer instanceof SequentialBlock)
94         || !(last.outer.getSubBlocks()[0] instanceof SpecialBlock)
95         || ifBlock.elseBlock != null
96         || !(ifBlock.thenBlock instanceof ThrowBlock))
97             return false;
98
99     SpecialBlock dup = (SpecialBlock) last.outer.getSubBlocks()[0];
100     if (dup.type != SpecialBlock.DUP
101         || dup.count != 1 || dup.depth != 0)
102         return false;
103        
104     if (!(ifBlock.cond instanceof CompareUnaryOperator))
105         return false;
106     CompareUnaryOperator cmpOp = (CompareUnaryOperator) ifBlock.cond;
107     if (cmpOp.getOperatorIndex() != Operator.EQUALS_OP
108         || !(cmpOp.getCompareType().isOfType(Type.tUObject)))
109         return false;
110
111     LocalInfo li = new LocalInfo();
112     InstructionContainer ic =
113         new InstructionBlock(new CheckNullOperator(Type.tUObject, li));
114     ifBlock.flowBlock.removeSuccessor(ifBlock.thenBlock.jump);
115     ic.moveJump(ifBlock.jump);
116     if (last == ifBlock) {
117         ic.replace(last.outer);
118         last = ic;
119     } else {
120         ic.replace(ifBlock);
121         last.replace(last.outer);
122     }
123         return true;
124     }
125 }
126
Popular Tags