1 /*
2 * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
3 */
4 /*
5 * Licensed to the Apache Software Foundation (ASF) under one or more
6 * contributor license agreements. See the NOTICE file distributed with
7 * this work for additional information regarding copyright ownership.
8 * The ASF licenses this file to You under the Apache License, Version 2.0
9 * (the "License"); you may not use this file except in compliance with
10 * the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 package com.sun.org.apache.bcel.internal.generic;
21
22 import java.util.HashMap;
32 import com.sun.org.apache.bcel.internal.classfile.ConstantInteger;
33 import com.sun.org.apache.bcel.internal.classfile.ConstantInterfaceMethodref;
34 import com.sun.org.apache.bcel.internal.classfile.ConstantInvokeDynamic;
35 import com.sun.org.apache.bcel.internal.classfile.ConstantLong;
36 import com.sun.org.apache.bcel.internal.classfile.ConstantMethodref;
37 import com.sun.org.apache.bcel.internal.classfile.ConstantNameAndType;
38 import com.sun.org.apache.bcel.internal.classfile.ConstantPool;
39 import com.sun.org.apache.bcel.internal.classfile.ConstantString;
40 import com.sun.org.apache.bcel.internal.classfile.ConstantUtf8;
41
42 /**
43 * This class is used to build up a constant pool. The user adds
44 * constants via `addXXX' methods, `addString', `addClass',
45 * etc.. These methods return an index into the constant
46 * pool. Finally, `getFinalConstantPool()' returns the constant pool
47 * built up. Intermediate versions of the constant pool can be
48 * obtained with `getConstantPool()'. A constant pool has capacity for
49 * Constants.MAX_SHORT entries. Note that the first (0) is used by the
50 * JVM and that Double and Long constants need two slots.
51 *
52 * @version $Id$
53 * @see Constant
54 * @LastModified: Jun 2019
55 */
56 public class ConstantPoolGen {
57
58 private static final int DEFAULT_BUFFER_SIZE = 256;
59 private int size;
60 private Constant[] constants;
61 private int index = 1; // First entry (0) used by JVM
62
63 private static final String METHODREF_DELIM = ":";
64 private static final String IMETHODREF_DELIM = "#";
65 private static final String FIELDREF_DELIM = "&";
66 private static final String NAT_DELIM = "%"; // Name and Type
67
68 private static class Index {
69
70 final int index;
71
72
73 Index(final int i) {
74 index = i;
164 sb.append(signature);
165 final String key = sb.toString();
166 sb.delete(0, sb.length());
167
168 if (!cp_table.containsKey(key)) {
169 cp_table.put(key, new Index(i));
170 }
171 } else if (c == null) { // entries may be null
172 // nothing to do
173 } else if (c instanceof ConstantInteger) {
174 // nothing to do
175 } else if (c instanceof ConstantLong) {
176 // nothing to do
177 } else if (c instanceof ConstantFloat) {
178 // nothing to do
179 } else if (c instanceof ConstantDouble) {
180 // nothing to do
181 } else if (c instanceof com.sun.org.apache.bcel.internal.classfile.ConstantMethodType) {
182 // TODO should this be handled somehow?
183 } else if (c instanceof com.sun.org.apache.bcel.internal.classfile.ConstantMethodHandle) {
184 // TODO should this be handled somehow?
185 } else {
186 assert false : "Unexpected constant type: " + c.getClass().getName();
187 }
188 }
189 }
190
191
192 /**
193 * Initialize with given constant pool.
194 */
195 public ConstantPoolGen(final ConstantPool cp) {
196 this(cp.getConstantPool());
197 }
198
199
200 /**
201 * Create empty constant pool.
202 */
203 public ConstantPoolGen() {
|
1 /*
2 * Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
3 */
4 /*
5 * Licensed to the Apache Software Foundation (ASF) under one or more
6 * contributor license agreements. See the NOTICE file distributed with
7 * this work for additional information regarding copyright ownership.
8 * The ASF licenses this file to You under the Apache License, Version 2.0
9 * (the "License"); you may not use this file except in compliance with
10 * the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 package com.sun.org.apache.bcel.internal.generic;
21
22 import java.util.HashMap;
32 import com.sun.org.apache.bcel.internal.classfile.ConstantInteger;
33 import com.sun.org.apache.bcel.internal.classfile.ConstantInterfaceMethodref;
34 import com.sun.org.apache.bcel.internal.classfile.ConstantInvokeDynamic;
35 import com.sun.org.apache.bcel.internal.classfile.ConstantLong;
36 import com.sun.org.apache.bcel.internal.classfile.ConstantMethodref;
37 import com.sun.org.apache.bcel.internal.classfile.ConstantNameAndType;
38 import com.sun.org.apache.bcel.internal.classfile.ConstantPool;
39 import com.sun.org.apache.bcel.internal.classfile.ConstantString;
40 import com.sun.org.apache.bcel.internal.classfile.ConstantUtf8;
41
42 /**
43 * This class is used to build up a constant pool. The user adds
44 * constants via `addXXX' methods, `addString', `addClass',
45 * etc.. These methods return an index into the constant
46 * pool. Finally, `getFinalConstantPool()' returns the constant pool
47 * built up. Intermediate versions of the constant pool can be
48 * obtained with `getConstantPool()'. A constant pool has capacity for
49 * Constants.MAX_SHORT entries. Note that the first (0) is used by the
50 * JVM and that Double and Long constants need two slots.
51 *
52 * @see Constant
53 * @LastModified: Jan 2020
54 */
55 public class ConstantPoolGen {
56
57 private static final int DEFAULT_BUFFER_SIZE = 256;
58 private int size;
59 private Constant[] constants;
60 private int index = 1; // First entry (0) used by JVM
61
62 private static final String METHODREF_DELIM = ":";
63 private static final String IMETHODREF_DELIM = "#";
64 private static final String FIELDREF_DELIM = "&";
65 private static final String NAT_DELIM = "%"; // Name and Type
66
67 private static class Index {
68
69 final int index;
70
71
72 Index(final int i) {
73 index = i;
163 sb.append(signature);
164 final String key = sb.toString();
165 sb.delete(0, sb.length());
166
167 if (!cp_table.containsKey(key)) {
168 cp_table.put(key, new Index(i));
169 }
170 } else if (c == null) { // entries may be null
171 // nothing to do
172 } else if (c instanceof ConstantInteger) {
173 // nothing to do
174 } else if (c instanceof ConstantLong) {
175 // nothing to do
176 } else if (c instanceof ConstantFloat) {
177 // nothing to do
178 } else if (c instanceof ConstantDouble) {
179 // nothing to do
180 } else if (c instanceof com.sun.org.apache.bcel.internal.classfile.ConstantMethodType) {
181 // TODO should this be handled somehow?
182 } else if (c instanceof com.sun.org.apache.bcel.internal.classfile.ConstantMethodHandle) {
183 // TODO should this be handled somehow?
184 } else if (c instanceof com.sun.org.apache.bcel.internal.classfile.ConstantModule) {
185 // TODO should this be handled somehow?
186 } else if (c instanceof com.sun.org.apache.bcel.internal.classfile.ConstantPackage) {
187 // TODO should this be handled somehow?
188 } else {
189 assert false : "Unexpected constant type: " + c.getClass().getName();
190 }
191 }
192 }
193
194
195 /**
196 * Initialize with given constant pool.
197 */
198 public ConstantPoolGen(final ConstantPool cp) {
199 this(cp.getConstantPool());
200 }
201
202
203 /**
204 * Create empty constant pool.
205 */
206 public ConstantPoolGen() {
|