| 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.constpool; |
| 19 |
|
| 20 |
import java.io.DataOutputStream; |
| 21 |
import java.io.IOException; |
| 22 |
import java.util.HashMap; |
| 23 |
import java.util.LinkedHashMap; |
| 24 |
import java.util.Map; |
| 25 |
import java.util.Map.Entry; |
| 26 |
|
| 27 |
import org.jboss.classfilewriter.WritableEntry; |
| 28 |
import org.jboss.classfilewriter.util.ByteArrayDataOutputStream; |
| 29 |
|
| 30 |
|
| 31 |
public class ConstPool implements WritableEntry { |
| 32 |
|
| 33 |
private final LinkedHashMap<Integer, ConstPoolEntry> entries = new LinkedHashMap<Integer, ConstPoolEntry>(); |
| 34 |
|
| 35 |
private final Map<String, Integer> utf8Locations = new HashMap<String, Integer>(); |
| 36 |
private final Map<String, Integer> classLocations = new HashMap<String, Integer>(); |
| 37 |
private final Map<String, Integer> stringLocations = new HashMap<String, Integer>(); |
| 38 |
private final Map<NameAndType, Integer> nameAndTypeLocations = new HashMap<NameAndType, Integer>(); |
| 39 |
private final Map<MemberInfo, Integer> fieldLocations = new HashMap<MemberInfo, Integer>(); |
| 40 |
private final Map<MemberInfo, Integer> methodLocations = new HashMap<MemberInfo, Integer>(); |
| 41 |
private final Map<MemberInfo, Integer> interfaceMethodLocations = new HashMap<MemberInfo, Integer>(); |
| 42 |
private final Map<Integer, Integer> integerLocations = new HashMap<Integer, Integer>(); |
| 43 |
private final Map<Float, Integer> floatLocations = new HashMap<Float, Integer>(); |
| 44 |
private final Map<Long, Integer> longLocations = new HashMap<Long, Integer>(); |
| 45 |
private final Map<Double, Integer> doubleLocations = new HashMap<Double, Integer>(); |
| 46 |
|
| 47 |
private int count = 1; |
| 48 |
|
| 49 |
/** |
| 50 |
* The constant_pool_count field of the class file format |
| 51 |
*/ |
| 52 |
private Integer constPoolSize = 1; |
| 53 |
|
| 54 |
public Integer addUtf8Entry(String entry) { |
| 55 |
if (utf8Locations.containsKey(entry)) { |
| 56 |
return utf8Locations.get(entry); |
| 57 |
} |
| 58 |
final int index = count++; |
| 59 |
constPoolSize++; |
| 60 |
entries.put(index, new Utf8Entry(entry)); |
| 61 |
utf8Locations.put(entry, index); |
| 62 |
return index; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Adds a CONSTANT_Class_info to the const pool. This must be in internal form |
| 67 |
*/ |
| 68 |
public Integer addClassEntry(String className) { |
| 69 |
className = className.replace('.', '/'); |
| 70 |
if (classLocations.containsKey(className)) { |
| 71 |
return classLocations.get(className); |
| 72 |
} |
| 73 |
final Integer utf8Location = addUtf8Entry(className); |
| 74 |
final Integer index = count++; |
| 75 |
constPoolSize++; |
| 76 |
entries.put(index, new ClassEntry(utf8Location)); |
| 77 |
classLocations.put(className, index); |
| 78 |
return index; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Adds a CONSTANT_String_info to the const pool. |
| 83 |
*/ |
| 84 |
public Integer addStringEntry(String string) { |
| 85 |
if (stringLocations.containsKey(string)) { |
| 86 |
return stringLocations.get(string); |
| 87 |
} |
| 88 |
final Integer utf8Location = addUtf8Entry(string); |
| 89 |
final Integer index = count++; |
| 90 |
constPoolSize++; |
| 91 |
entries.put(index, new StringEntry(utf8Location)); |
| 92 |
stringLocations.put(string, index); |
| 93 |
return index; |
| 94 |
} |
| 95 |
|
| 96 |
public Integer addIntegerEntry(int entry) { |
| 97 |
if (integerLocations.containsKey(entry)) { |
| 98 |
return integerLocations.get(entry); |
| 99 |
} |
| 100 |
final Integer index = count++; |
| 101 |
constPoolSize++; |
| 102 |
entries.put(index, new IntegerEntry(entry)); |
| 103 |
integerLocations.put(entry, index); |
| 104 |
return index; |
| 105 |
} |
| 106 |
|
| 107 |
public Integer addFloatEntry(float entry) { |
| 108 |
if (floatLocations.containsKey(entry)) { |
| 109 |
return floatLocations.get(entry); |
| 110 |
} |
| 111 |
final Integer index = count++; |
| 112 |
constPoolSize++; |
| 113 |
entries.put(index, new FloatEntry(entry)); |
| 114 |
floatLocations.put(entry, index); |
| 115 |
return index; |
| 116 |
} |
| 117 |
|
| 118 |
public Integer addLongEntry(long entry) { |
| 119 |
if (longLocations.containsKey(entry)) { |
| 120 |
return longLocations.get(entry); |
| 121 |
} |
| 122 |
final Integer index = count++; |
| 123 |
count++; |
| 124 |
constPoolSize += 2; |
| 125 |
entries.put(index, new LongEntry(entry)); |
| 126 |
longLocations.put(entry, index); |
| 127 |
return index; |
| 128 |
} |
| 129 |
|
| 130 |
public Integer addDoubleEntry(double entry) { |
| 131 |
if (doubleLocations.containsKey(entry)) { |
| 132 |
return doubleLocations.get(entry); |
| 133 |
} |
| 134 |
final Integer index = count++; |
| 135 |
count++; |
| 136 |
constPoolSize += 2; |
| 137 |
entries.put(index, new DoubleEntry(entry)); |
| 138 |
doubleLocations.put(entry, index); |
| 139 |
return index; |
| 140 |
} |
| 141 |
|
| 142 |
public Integer addNameAndTypeEntry(String name, String type) { |
| 143 |
final NameAndType typeInfo = new NameAndType(name, type); |
| 144 |
if(nameAndTypeLocations.containsKey(typeInfo)) { |
| 145 |
return nameAndTypeLocations.get(typeInfo); |
| 146 |
} |
| 147 |
final Integer nameIndex = addUtf8Entry(name); |
| 148 |
final Integer typeIndex = addUtf8Entry(type); |
| 149 |
final Integer index = count++; |
| 150 |
constPoolSize++; |
| 151 |
entries.put(index,new NameAndTypeEntry(nameIndex, typeIndex)); |
| 152 |
nameAndTypeLocations.put(typeInfo, index); |
| 153 |
return index; |
| 154 |
} |
| 155 |
|
| 156 |
public Integer addFieldEntry(String className, String fieldName, String fieldType) { |
| 157 |
final NameAndType nameAndType = new NameAndType(fieldName, fieldType); |
| 158 |
final MemberInfo field = new MemberInfo(className, nameAndType); |
| 159 |
if (fieldLocations.containsKey(field)) { |
| 160 |
return fieldLocations.get(field); |
| 161 |
} |
| 162 |
final Integer nameAndTypeIndex = addNameAndTypeEntry(fieldName, fieldType); |
| 163 |
final Integer classIndex = addClassEntry(className); |
| 164 |
final Integer index = count++; |
| 165 |
constPoolSize++; |
| 166 |
entries.put(index, new FieldRefEntry(classIndex, nameAndTypeIndex)); |
| 167 |
fieldLocations.put(field, index); |
| 168 |
return index; |
| 169 |
} |
| 170 |
|
| 171 |
public Integer addMethodEntry(String className, String methodName, String descriptor) { |
| 172 |
final NameAndType nameAndType = new NameAndType(methodName, descriptor); |
| 173 |
final MemberInfo method = new MemberInfo(className, nameAndType); |
| 174 |
if (methodLocations.containsKey(method)) { |
| 175 |
return methodLocations.get(method); |
| 176 |
} |
| 177 |
final Integer nameAndTypeIndex = addNameAndTypeEntry(methodName, descriptor); |
| 178 |
final Integer classIndex = addClassEntry(className); |
| 179 |
final Integer index = count++; |
| 180 |
constPoolSize++; |
| 181 |
entries.put(index, new MethodRefEntry(classIndex, nameAndTypeIndex)); |
| 182 |
methodLocations.put(method, index); |
| 183 |
return index; |
| 184 |
} |
| 185 |
|
| 186 |
public Integer addInterfaceMethodEntry(String className, String methodName, String descriptor) { |
| 187 |
final NameAndType nameAndType = new NameAndType(methodName, descriptor); |
| 188 |
final MemberInfo method = new MemberInfo(className, nameAndType); |
| 189 |
if (interfaceMethodLocations.containsKey(method)) { |
| 190 |
return interfaceMethodLocations.get(method); |
| 191 |
} |
| 192 |
final Integer nameAndTypeIndex = addNameAndTypeEntry(methodName, descriptor); |
| 193 |
final Integer classIndex = addClassEntry(className); |
| 194 |
final Integer index = count++; |
| 195 |
constPoolSize++; |
| 196 |
entries.put(index, new InterfaceMethodRefEntry(classIndex, nameAndTypeIndex)); |
| 197 |
interfaceMethodLocations.put(method, index); |
| 198 |
return index; |
| 199 |
} |
| 200 |
|
| 201 |
public void write(ByteArrayDataOutputStream stream) throws IOException { |
| 202 |
stream.writeShort(constPoolSize); |
| 203 |
for (Entry<Integer, ConstPoolEntry> entry : entries.entrySet()) { |
| 204 |
entry.getValue().write(stream); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
private static final class NameAndType { |
| 209 |
private final String name, type; |
| 210 |
|
| 211 |
public NameAndType(String name, String type) { |
| 212 |
this.name = name; |
| 213 |
this.type = type; |
| 214 |
} |
| 215 |
|
| 216 |
@Override |
| 217 |
public int hashCode() { |
| 218 |
final int prime = 31; |
| 219 |
int result = 1; |
| 220 |
result = prime * result + ((name == null) ? 0 : name.hashCode()); |
| 221 |
result = prime * result + ((type == null) ? 0 : type.hashCode()); |
| 222 |
return result; |
| 223 |
} |
| 224 |
|
| 225 |
@Override |
| 226 |
public boolean equals(Object obj) { |
| 227 |
if (this == obj) |
| 228 |
return true; |
| 229 |
if (obj == null) |
| 230 |
return false; |
| 231 |
if (getClass() != obj.getClass()) |
| 232 |
return false; |
| 233 |
NameAndType other = (NameAndType) obj; |
| 234 |
if (name == null) { |
| 235 |
if (other.name != null) |
| 236 |
return false; |
| 237 |
} else if (!name.equals(other.name)) |
| 238 |
return false; |
| 239 |
if (type == null) { |
| 240 |
if (other.type != null) |
| 241 |
return false; |
| 242 |
} else if (!type.equals(other.type)) |
| 243 |
return false; |
| 244 |
return true; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
private static class MemberInfo { |
| 249 |
private final String className; |
| 250 |
private final NameAndType nameAndType; |
| 251 |
|
| 252 |
public MemberInfo(String className,NameAndType nameAndType) { |
| 253 |
this.className = className; |
| 254 |
this.nameAndType = nameAndType; |
| 255 |
} |
| 256 |
|
| 257 |
@Override |
| 258 |
public int hashCode() { |
| 259 |
final int prime = 31; |
| 260 |
int result = 1; |
| 261 |
result = prime * result + ((className == null) ? 0 : className.hashCode()); |
| 262 |
result = prime * result + ((nameAndType == null) ? 0 : nameAndType.hashCode()); |
| 263 |
return result; |
| 264 |
} |
| 265 |
|
| 266 |
@Override |
| 267 |
public boolean equals(Object obj) { |
| 268 |
if (this == obj) |
| 269 |
return true; |
| 270 |
if (obj == null) |
| 271 |
return false; |
| 272 |
if (getClass() != obj.getClass()) |
| 273 |
return false; |
| 274 |
MemberInfo other = (MemberInfo) obj; |
| 275 |
if (className == null) { |
| 276 |
if (other.className != null) |
| 277 |
return false; |
| 278 |
} else if (!className.equals(other.className)) |
| 279 |
return false; |
| 280 |
if (nameAndType == null) { |
| 281 |
if (other.nameAndType != null) |
| 282 |
return false; |
| 283 |
} else if (!nameAndType.equals(other.nameAndType)) |
| 284 |
return false; |
| 285 |
return true; |
| 286 |
} |
| 287 |
|
| 288 |
} |
| 289 |
} |