KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jode > flow > CreateClassField


1 /* CreateClassField 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: CreateClassField.java,v 1.8.2.2 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 CreateClassField {
26
27
28     public static boolean transform(IfThenElseBlock ifBlock,
29                     StructuredBlock last) {
30     // convert
31
// if (class$classname == null)
32
// class$classname = class$("java.lang.Object");
33
// to
34
// if (classname.class == null) {
35
// }
36
if (!(ifBlock.cond instanceof CompareUnaryOperator)
37         || !(((Operator)ifBlock.cond)
38          .getOperatorIndex() == Operator.EQUALS_OP)
39         || !(ifBlock.thenBlock instanceof InstructionBlock)
40         || ifBlock.elseBlock != null)
41             return false;
42
43     if (ifBlock.thenBlock.jump != null
44         && (ifBlock.jump == null
45         || (ifBlock.jump.destination
46             != ifBlock.thenBlock.jump.destination)))
47         return false;
48
49     CompareUnaryOperator cmp = (CompareUnaryOperator) ifBlock.cond;
50     Expression instr =
51         ((InstructionBlock)ifBlock.thenBlock).getInstruction();
52     if (!(cmp.getSubExpressions()[0] instanceof GetFieldOperator)
53         || !(instr instanceof StoreInstruction))
54         return false;
55
56     StoreInstruction store = (StoreInstruction) instr;
57     if (!(store.getLValue() instanceof PutFieldOperator))
58         return false;
59     PutFieldOperator put = (PutFieldOperator) store.getLValue();
60     if (put.getField() == null
61         || !put.matches((GetFieldOperator)cmp.getSubExpressions()[0])
62         || !(store.getSubExpressions()[1] instanceof InvokeOperator))
63         return false;
64
65     InvokeOperator invoke = (InvokeOperator) store.getSubExpressions()[1];
66     if (!invoke.isGetClass())
67         return false;
68
69     Expression param = invoke.getSubExpressions()[0];
70
71     if (param instanceof ConstOperator
72         && ((ConstOperator)param).getValue() instanceof String JavaDoc) {
73         String JavaDoc clazz = (String JavaDoc) ((ConstOperator)param).getValue();
74         if (put.getField().setClassConstant(clazz)) {
75         cmp.setSubExpressions
76             (0, new ClassFieldOperator(clazz.charAt(0) == '['
77                            ? Type.tType(clazz)
78                            : Type.tClass(clazz)));
79         EmptyBlock empty = new EmptyBlock();
80         empty.moveJump(ifBlock.thenBlock.jump);
81         ifBlock.setThenBlock(empty);
82         return true;
83         }
84     }
85     return false;
86     }
87 }
88
Popular Tags