Debian Codegraph @

src/main/java/org/jboss/classfilewriter/code/StackEntry.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.code;
19
20 import java.io.DataOutputStream;
21 import java.io.IOException;
22
23 import org.jboss.classfilewriter.constpool.ConstPool;
24 import org.jboss.classfilewriter.util.DescriptorUtils;
25
26 /**
27 * represents the state of the stack or the local variable array.
28 * <p>
29 * This is written out as part of the StackMap attribute
30 *
31 * @author Stuart Douglas
32 *
33 */
34 public class StackEntry {
35
36 private final StackEntryType type;
37
38 private final String descriptor;
39
40 /**
41 * if this is OBJECT then this holds the const pool index
42 */
43 private final int descriptorIndex;
44
45 /**
46 * if this is an unititialized bytecode then this holds the absolute position of the new instruction that created it
47 */
48 private final int newInstructionLocation;
49
50 public StackEntry(StackEntryType type, String descriptor) {
51 if (type == StackEntryType.OBJECT) {
52 throw new RuntimeException("OBJECT stack entries must provide a const pool index for the class");
53 }
54 this.type = type;
55 if(descriptor != null && descriptor.contains(".")) {
56 throw new RuntimeException("invalid descriptor "+ descriptor);
57 }
58 this.descriptor = descriptor;
59 this.newInstructionLocation = -1;
60 this.descriptorIndex = -1;
61 }
62
63 public StackEntry(StackEntryType type, String descriptor, ConstPool pool) {
64 this.type = type;
65 this.descriptor = descriptor;
66 this.newInstructionLocation = -1;
67 if(descriptor.contains(".")) {
68 throw new RuntimeException("invalid descriptor " + descriptor);
69 }
70 if(type == StackEntryType.OBJECT) {
71 if (descriptor.charAt(0) == 'L') {
72 descriptorIndex = pool.addClassEntry(descriptor.substring(1, descriptor.length() - 1)); // strip the L and the ;
73 } else {
74 descriptorIndex = pool.addClassEntry(descriptor); // strip the L and the ;
75 }
76
77 } else {
78 descriptorIndex = -1;
79 }
80 }
81
82 public StackEntry(StackEntryType type, String descriptor, int newInstructionLocation) {
83 this.type = type;
84 this.descriptor = descriptor;
85 this.newInstructionLocation = newInstructionLocation;
86 this.descriptorIndex = -1;
87 if(descriptor != null && descriptor.contains(".")) {
88 throw new RuntimeException("invalid descriptor " + descriptor);
89 }
90 }
91
92 public String getDescriptor() {
93 return descriptor;
94 }
95
96 public int getNewInstructionLocation() {
97 return newInstructionLocation;
98 }
99
100 public StackEntryType getType() {
101 return type;
102 }
103
104 @Override
105 public String toString() {
106 if (type == StackEntryType.OBJECT) {
107 return "StackEntry [descriptor=" + descriptor + ", type=" + type
108 + "]";
109 }
110 return "StackEntry [type=" + type + "]";
111 }
112
113 public static StackEntry of(String descriptor, ConstPool pool) {
114 String desc = descriptor.replace('.', '/');
115 if(!DescriptorUtils.isPrimitive(desc)) {
116 return new StackEntry(StackEntryType.OBJECT, desc, pool);
117 }else {
118 char ret = desc.charAt(0);
119 switch (ret) {
120 case 'I':
121 case 'Z':
122 case 'S':
123 case 'B':
124 case 'C':
125 return new StackEntry(StackEntryType.INT, desc);
126 case 'F':
127 return new StackEntry(StackEntryType.FLOAT, desc);
128 case 'D':
129 return new StackEntry(StackEntryType.DOUBLE, desc);
130 case 'J':
131 return new StackEntry(StackEntryType.LONG, desc);
132 }
133 }
134 throw new RuntimeException("Unknown descriptor: " + desc);
135 }
136
137 public boolean isWide() {
138 return type == StackEntryType.DOUBLE || type == StackEntryType.LONG;
139 }
140
141 /**
142 * writes the entry to the stream
143 */
144 public void write(DataOutputStream dstream) throws IOException {
145 dstream.writeByte(type.ordinal());
146 if (type == StackEntryType.OBJECT) {
147 dstream.writeShort(descriptorIndex);
148 } else if (type == StackEntryType.UNITITIALIZED_OBJECT) {
149 dstream.writeShort(newInstructionLocation);
150 }
151 }
152 }