Debian Codegraph @

src/main/java/org/jboss/classfilewriter/util/Boxing.java

raw
1 /*
2 * JBoss, Home of Professional Open Source.
3 *
4 * Copyright 2012 Red Hat, Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.jboss.classfilewriter.util;
19
20 import org.jboss.classfilewriter.code.CodeAttribute;
21
22 /**
23 * This class is responsible for generating bytecode fragments to box/unbox whatever happens to be on the top of the stack.
24 *
25 * It is the calling codes responsibility to make sure that the correct type is on the stack
26 *
27 * @author Stuart Douglas
28 *
29 */
30 public class Boxing {
31
32 static public void boxIfNessesary(CodeAttribute ca, String desc) {
33 if (desc.length() == 1) {
34 char type = desc.charAt(0);
35 switch (type) {
36 case 'I':
37 boxInt(ca);
38 break;
39 case 'J':
40 boxLong(ca);
41 break;
42 case 'S':
43 boxShort(ca);
44 break;
45 case 'F':
46 boxFloat(ca);
47 break;
48 case 'D':
49 boxDouble(ca);
50 break;
51 case 'B':
52 boxByte(ca);
53 break;
54 case 'C':
55 boxChar(ca);
56 break;
57 case 'Z':
58 boxBoolean(ca);
59 break;
60 default:
61 throw new RuntimeException("Cannot box unkown primitive type: " + type);
62 }
63 }
64 }
65
66 static public CodeAttribute unbox(CodeAttribute ca, String desc) {
67 char type = desc.charAt(0);
68 switch (type) {
69 case 'I':
70 return unboxInt(ca);
71 case 'J':
72 return unboxLong(ca);
73 case 'S':
74 return unboxShort(ca);
75 case 'F':
76 return unboxFloat(ca);
77 case 'D':
78 return unboxDouble(ca);
79 case 'B':
80 return unboxByte(ca);
81 case 'C':
82 return unboxChar(ca);
83 case 'Z':
84 return unboxBoolean(ca);
85 }
86 throw new RuntimeException("Cannot unbox unkown primitive type: " + type);
87 }
88
89 static public void boxInt(CodeAttribute bc) {
90 bc.invokestatic("java.lang.Integer", "valueOf", "(I)Ljava/lang/Integer;");
91 }
92
93 static public void boxLong(CodeAttribute bc) {
94 bc.invokestatic("java.lang.Long", "valueOf", "(J)Ljava/lang/Long;");
95 }
96
97 static public void boxShort(CodeAttribute bc) {
98 bc.invokestatic("java.lang.Short", "valueOf", "(S)Ljava/lang/Short;");
99 }
100
101 static public void boxByte(CodeAttribute bc) {
102 bc.invokestatic("java.lang.Byte", "valueOf", "(B)Ljava/lang/Byte;");
103 }
104
105 static public void boxFloat(CodeAttribute bc) {
106 bc.invokestatic("java.lang.Float", "valueOf", "(F)Ljava/lang/Float;");
107 }
108
109 static public void boxDouble(CodeAttribute bc) {
110 bc.invokestatic("java.lang.Double", "valueOf", "(D)Ljava/lang/Double;");
111 }
112
113 static public void boxChar(CodeAttribute bc) {
114 bc.invokestatic("java.lang.Character", "valueOf", "(C)Ljava/lang/Character;");
115 }
116
117 static public void boxBoolean(CodeAttribute bc) {
118 bc.invokestatic("java.lang.Boolean", "valueOf", "(Z)Ljava/lang/Boolean;");
119 }
120
121 // unboxing
122
123 static public CodeAttribute unboxInt(CodeAttribute bc) {
124 bc.checkcast("java.lang.Number");
125 bc.invokevirtual("java.lang.Number", "intValue", "()I");
126 return bc;
127 }
128
129 static public CodeAttribute unboxLong(CodeAttribute bc) {
130 bc.checkcast("java.lang.Number");
131 bc.invokevirtual("java.lang.Number", "longValue", "()J");
132 return bc;
133 }
134
135 static public CodeAttribute unboxShort(CodeAttribute bc) {
136 bc.checkcast("java.lang.Number");
137 bc.invokevirtual("java.lang.Number", "shortValue", "()S");
138 return bc;
139 }
140
141 static public CodeAttribute unboxByte(CodeAttribute bc) {
142 bc.checkcast("java.lang.Number");
143 bc.invokevirtual("java.lang.Number", "byteValue", "()B");
144 return bc;
145 }
146
147 static public CodeAttribute unboxFloat(CodeAttribute bc) {
148 bc.checkcast("java.lang.Number");
149 bc.invokevirtual("java.lang.Number", "floatValue", "()F");
150 return bc;
151 }
152
153 static public CodeAttribute unboxDouble(CodeAttribute bc) {
154 bc.checkcast("java.lang.Number");
155 bc.invokevirtual("java.lang.Number", "doubleValue", "()D");
156 return bc;
157 }
158
159 static public CodeAttribute unboxChar(CodeAttribute bc) {
160 bc.checkcast("java.lang.Character");
161 bc.invokevirtual("java.lang.Character", "charValue", "()C");
162 return bc;
163 }
164
165 static public CodeAttribute unboxBoolean(CodeAttribute bc) {
166 bc.checkcast("java.lang.Boolean");
167 bc.invokevirtual("java.lang.Boolean", "booleanValue", "()Z");
168 return bc;
169 }
170
171 }