Source Code Cross Referenced for ConstOperator.java in  » Development » jode » jode » expr » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Development » jode » jode.expr 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* ConstOperator 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: ConstOperator.java,v 4.21.2.2 2002/05/28 17:34:06 hoenicke Exp $
018:         */
019:
020:        package jode.expr;
021:
022:        import jode.type.Type;
023:        import jode.type.IntegerType;
024:        import jode.decompiler.TabbedPrintWriter;
025:
026:        public class ConstOperator extends NoArgOperator {
027:            Object value;
028:            boolean isInitializer = false;
029:
030:            private static final Type tBoolConstInt = new IntegerType(
031:                    IntegerType.IT_I | IntegerType.IT_C | IntegerType.IT_Z
032:                            | IntegerType.IT_S | IntegerType.IT_B);
033:
034:            public ConstOperator(Object constant) {
035:                super (Type.tUnknown);
036:                if (constant instanceof  Boolean) {
037:                    updateParentType(Type.tBoolean);
038:                    constant = new Integer(
039:                            ((Boolean) constant).booleanValue() ? 1 : 0);
040:                } else if (constant instanceof  Integer) {
041:                    int intVal = ((Integer) constant).intValue();
042:                    updateParentType((intVal == 0 || intVal == 1) ? tBoolConstInt
043:                            : (intVal < Short.MIN_VALUE || intVal > Character.MAX_VALUE) ? Type.tInt
044:                                    : new IntegerType(
045:                                            (intVal < Byte.MIN_VALUE) ? IntegerType.IT_S
046:                                                    | IntegerType.IT_I
047:                                                    : (intVal < 0) ? IntegerType.IT_S
048:                                                            | IntegerType.IT_B
049:                                                            | IntegerType.IT_I
050:                                                            : (intVal <= Byte.MAX_VALUE) ? (IntegerType.IT_S
051:                                                                    | IntegerType.IT_B
052:                                                                    | IntegerType.IT_C | IntegerType.IT_I)
053:                                                                    : (intVal <= Short.MAX_VALUE) ? IntegerType.IT_S
054:                                                                            | IntegerType.IT_C
055:                                                                            | IntegerType.IT_I
056:                                                                            : IntegerType.IT_C
057:                                                                                    | IntegerType.IT_I));
058:                } else if (constant instanceof  Long)
059:                    updateParentType(Type.tLong);
060:                else if (constant instanceof  Float)
061:                    updateParentType(Type.tFloat);
062:                else if (constant instanceof  Double)
063:                    updateParentType(Type.tDouble);
064:                else if (constant instanceof  String)
065:                    updateParentType(Type.tString);
066:                else if (constant == null)
067:                    updateParentType(Type.tUObject);
068:                else
069:                    throw new IllegalArgumentException(
070:                            "Illegal constant type: " + constant.getClass());
071:                value = constant;
072:            }
073:
074:            public Object getValue() {
075:                return value;
076:            }
077:
078:            /**
079:             * Return true, if this value is a one of the given type.
080:             * This is used for ++ and -- instructions.
081:             * @param type the type for which this must be a one.  This may
082:             * be different from the type this value actually is.
083:             */
084:            public boolean isOne(Type type) {
085:                if (type instanceof  IntegerType) {
086:                    return (value instanceof  Integer && ((Integer) value)
087:                            .intValue() == 1);
088:                } else if (type == Type.tLong) {
089:                    return (value instanceof  Long && ((Long) value).longValue() == 1L);
090:                } else if (type == Type.tFloat) {
091:                    return (value instanceof  Float && ((Float) value)
092:                            .floatValue() == 1.0f);
093:                } else if (type == Type.tDouble) {
094:                    return (value instanceof  Double && ((Double) value)
095:                            .doubleValue() == 1.0);
096:                }
097:                return false;
098:            }
099:
100:            public int getPriority() {
101:                return 1000;
102:            }
103:
104:            public boolean opEquals(Operator o) {
105:                if (o instanceof  ConstOperator) {
106:                    Object otherValue = ((ConstOperator) o).value;
107:                    return value == null ? otherValue == null : value
108:                            .equals(otherValue);
109:                }
110:                return false;
111:            }
112:
113:            public void makeInitializer(Type type) {
114:                isInitializer = true;
115:            }
116:
117:            private static String quoted(String str) {
118:                StringBuffer result = new StringBuffer("\"");
119:                for (int i = 0; i < str.length(); i++) {
120:                    char c;
121:                    switch (c = str.charAt(i)) {
122:                    case '\0':
123:                        result.append("\\0");
124:                        break;
125:                    case '\t':
126:                        result.append("\\t");
127:                        break;
128:                    case '\n':
129:                        result.append("\\n");
130:                        break;
131:                    case '\r':
132:                        result.append("\\r");
133:                        break;
134:                    case '\\':
135:                        result.append("\\\\");
136:                        break;
137:                    case '\"':
138:                        result.append("\\\"");
139:                        break;
140:                    default:
141:                        if (c < 32) {
142:                            String oct = Integer.toOctalString(c);
143:                            result.append(
144:                                    "\\000".substring(0, 4 - oct.length()))
145:                                    .append(oct);
146:                        } else if (c >= 32 && c < 127)
147:                            result.append(str.charAt(i));
148:                        else {
149:                            String hex = Integer.toHexString(c);
150:                            result.append(
151:                                    "\\u0000".substring(0, 6 - hex.length()))
152:                                    .append(hex);
153:                        }
154:                    }
155:                }
156:                return result.append("\"").toString();
157:            }
158:
159:            public String toString() {
160:                String strVal = String.valueOf(value);
161:                if (type.isOfType(Type.tBoolean)) {
162:                    int intVal = ((Integer) value).intValue();
163:                    if (intVal == 0)
164:                        return "false";
165:                    else if (intVal == 1)
166:                        return "true";
167:                    else
168:                        throw new jode.AssertError(
169:                                "boolean is neither false nor true");
170:                }
171:                if (type.getHint().equals(Type.tChar)) {
172:                    char c = (char) ((Integer) value).intValue();
173:                    switch (c) {
174:                    case '\0':
175:                        return "\'\\0\'";
176:                    case '\t':
177:                        return "\'\\t\'";
178:                    case '\n':
179:                        return "\'\\n\'";
180:                    case '\r':
181:                        return "\'\\r\'";
182:                    case '\\':
183:                        return "\'\\\\\'";
184:                    case '\"':
185:                        return "\'\\\"\'";
186:                    case '\'':
187:                        return "\'\\\'\'";
188:                    }
189:                    if (c < 32) {
190:                        String oct = Integer.toOctalString(c);
191:                        return "\'\\000".substring(0, 5 - oct.length()) + oct
192:                                + "\'";
193:                    }
194:                    if (c >= 32 && c < 127)
195:                        return "\'" + c + "\'";
196:                    else {
197:                        String hex = Integer.toHexString(c);
198:                        return "\'\\u0000".substring(0, 7 - hex.length()) + hex
199:                                + "\'";
200:                    }
201:                } else if (type.equals(Type.tString)) {
202:                    return quoted(strVal);
203:                } else if (parent != null) {
204:                    int opindex = parent.getOperatorIndex();
205:                    if (opindex >= OPASSIGN_OP + ADD_OP
206:                            && opindex < OPASSIGN_OP + ASSIGN_OP)
207:                        opindex -= OPASSIGN_OP;
208:
209:                    if (opindex >= AND_OP && opindex < AND_OP + 3) {
210:                        /* For bit wise and/or/xor change representation.
211:                         */
212:                        if (type.isOfType(Type.tUInt)) {
213:                            int i = ((Integer) value).intValue();
214:                            if (i < -1)
215:                                strVal = "~0x" + Integer.toHexString(-i - 1);
216:                            else
217:                                strVal = "0x" + Integer.toHexString(i);
218:                        } else if (type.equals(Type.tLong)) {
219:                            long l = ((Long) value).longValue();
220:                            if (l < -1)
221:                                strVal = "~0x" + Long.toHexString(-l - 1);
222:                            else
223:                                strVal = "0x" + Long.toHexString(l);
224:                        }
225:                    }
226:                }
227:                if (type.isOfType(Type.tLong))
228:                    return strVal + "L";
229:                if (type.isOfType(Type.tFloat)) {
230:                    if (strVal.equals("NaN"))
231:                        return "Float.NaN";
232:                    if (strVal.equals("-Infinity"))
233:                        return "Float.NEGATIVE_INFINITY";
234:                    if (strVal.equals("Infinity"))
235:                        return "Float.POSITIVE_INFINITY";
236:                    return strVal + "F";
237:                }
238:                if (type.isOfType(Type.tDouble)) {
239:                    if (strVal.equals("NaN"))
240:                        return "Double.NaN";
241:                    if (strVal.equals("-Infinity"))
242:                        return "Double.NEGATIVE_INFINITY";
243:                    if (strVal.equals("Infinity"))
244:                        return "Double.POSITIVE_INFINITY";
245:                    return strVal;
246:                }
247:                if (!type.isOfType(Type.tInt)
248:                        && (type.getHint().equals(Type.tByte) || type.getHint()
249:                                .equals(Type.tShort))
250:                        && !isInitializer
251:                        && !(parent instanceof  StoreInstruction
252:                                && parent.getOperatorIndex() != ASSIGN_OP && parent.subExpressions[1] == this )) {
253:                    /* One of the strange things in java.  All constants
254:                     * are int and must be explicitly casted to byte,...,short.
255:                     * But in assignments and initializers this cast is unnecessary.
256:                     * See JLS section 5.2
257:                     */
258:                    return "(" + type.getHint() + ") " + strVal;
259:                }
260:
261:                return strVal;
262:            }
263:
264:            public void dumpExpression(TabbedPrintWriter writer)
265:                    throws java.io.IOException {
266:                writer.print(toString());
267:            }
268:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.