KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > dava > internal > javaRep > DIntConstant


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 Jerome Miecznikowski
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 package soot.dava.internal.javaRep;
21
22 import soot.*;
23 import soot.jimple.*;
24
25 public class DIntConstant extends IntConstant
26 {
27     public Type type;
28
29     private DIntConstant(int value, Type type)
30     {
31     super( value);
32     this.type = type;
33     }
34
35     public static DIntConstant v(int value, Type type)
36     {
37         return new DIntConstant(value, type);
38     }
39
40     public String JavaDoc toString()
41     {
42     if (type != null)
43
44         if (type instanceof BooleanType) {
45         if (value == 0)
46             return "false";
47         else
48             return "true";
49         }
50
51         else if (type instanceof CharType) {
52         String JavaDoc ch = "";
53
54         switch (value) {
55
56         case 0x08: ch = "\\b"; break;
57         case 0x09: ch = "\\t"; break;
58         case 0x0a: ch = "\\n"; break;
59         case 0x0c: ch = "\\f"; break;
60         case 0x0d: ch = "\\r"; break;
61         case 0x22: ch = "\\\""; break;
62         case 0x27: ch = "\\'"; break;
63         case 0x5c: ch = "\\\\"; break;
64
65         default:
66             if ((value > 31) && (value < 127))
67             ch = new Character JavaDoc( (char) value).toString();
68
69             else {
70             ch = Integer.toHexString( value);
71
72             while (ch.length() < 4)
73                 ch = "0" + ch;
74
75             if (ch.length() > 4)
76                 ch = ch.substring( ch.length() - 4);
77             
78             ch = "\\u" + ch;
79             }
80         }
81         
82         return "'" + ch + "'";
83         }
84
85         else if (type instanceof ByteType)
86         return "(byte) " + new Integer JavaDoc(value).toString();
87
88     return new Integer JavaDoc(value).toString();
89     }
90 }
91
Popular Tags