6841419: classfile: add constant pool iterator

Reviewed-by: mcimadamore
This commit is contained in:
Jonathan Gibbons 2009-05-19 11:43:50 -07:00
parent 58eddef3f4
commit f05e74203e
3 changed files with 55 additions and 13 deletions

View file

@ -95,7 +95,7 @@ public class ClassTranslator
if (cp2 == null) { if (cp2 == null) {
ConstantPool.CPInfo[] pool2 = new ConstantPool.CPInfo[cp.size()]; ConstantPool.CPInfo[] pool2 = new ConstantPool.CPInfo[cp.size()];
boolean eq = true; boolean eq = true;
for (int i = 0; i < cp.size(); i++) { for (int i = 0; i < cp.size(); ) {
ConstantPool.CPInfo cpInfo; ConstantPool.CPInfo cpInfo;
try { try {
cpInfo = cp.get(i); cpInfo = cp.get(i);
@ -107,11 +107,7 @@ public class ClassTranslator
pool2[i] = cpInfo2; pool2[i] = cpInfo2;
if (cpInfo.getTag() != cpInfo2.getTag()) if (cpInfo.getTag() != cpInfo2.getTag())
throw new IllegalStateException(); throw new IllegalStateException();
switch (cpInfo.getTag()) { i += cpInfo.size();
case ConstantPool.CONSTANT_Double:
case ConstantPool.CONSTANT_Long:
i += 1;
}
} }
if (eq) if (eq)

View file

@ -118,13 +118,8 @@ public class ClassWriter {
ConstantPool pool = classFile.constant_pool; ConstantPool pool = classFile.constant_pool;
int size = pool.size(); int size = pool.size();
out.writeShort(size); out.writeShort(size);
try { for (CPInfo cpInfo: pool.entries())
for (int i = 1; i < size; ) { constantPoolWriter.write(cpInfo, out);
i += constantPoolWriter.write(pool.get(i), out);
}
} catch (ConstantPoolException e) {
throw new Error(e); // ??
}
} }
protected void writeFields() throws IOException { protected void writeFields() throws IOException {

View file

@ -26,6 +26,7 @@
package com.sun.tools.classfile; package com.sun.tools.classfile;
import java.io.IOException; import java.io.IOException;
import java.util.Iterator;
/** /**
* See JVMS3, section 4.5. * See JVMS3, section 4.5.
@ -223,6 +224,40 @@ public class ConstantPool {
throw new EntryNotFound(value); throw new EntryNotFound(value);
} }
public Iterable<CPInfo> entries() {
return new Iterable<CPInfo>() {
public Iterator<CPInfo> iterator() {
return new Iterator<CPInfo>() {
public boolean hasNext() {
return next < pool.length;
}
public CPInfo next() {
current = pool[next];
switch (current.getTag()) {
case CONSTANT_Double:
case CONSTANT_Long:
next += 2;
break;
default:
next += 1;
}
return current;
}
public void remove() {
throw new UnsupportedOperationException();
}
private CPInfo current;
private int next = 1;
};
}
};
}
private CPInfo[] pool; private CPInfo[] pool;
public interface Visitor<R,P> { public interface Visitor<R,P> {
@ -250,6 +285,12 @@ public class ConstantPool {
public abstract int getTag(); public abstract int getTag();
/** The number of slots in the constant pool used by this entry.
* 2 for CONSTANT_Double and CONSTANT_Long; 1 for everything else. */
public int size() {
return 1;
}
public abstract <R,D> R accept(Visitor<R,D> visitor, D data); public abstract <R,D> R accept(Visitor<R,D> visitor, D data);
protected final ConstantPool cp; protected final ConstantPool cp;
@ -349,6 +390,11 @@ public class ConstantPool {
return CONSTANT_Double; return CONSTANT_Double;
} }
@Override
public int size() {
return 2;
}
@Override @Override
public String toString() { public String toString() {
return "CONSTANT_Double_info[value: " + value + "]"; return "CONSTANT_Double_info[value: " + value + "]";
@ -462,6 +508,11 @@ public class ConstantPool {
return CONSTANT_Long; return CONSTANT_Long;
} }
@Override
public int size() {
return 2;
}
@Override @Override
public String toString() { public String toString() {
return "CONSTANT_Long_info[value: " + value + "]"; return "CONSTANT_Long_info[value: " + value + "]";