6841419: classfile: add constant pool iterator

Tue, 19 May 2009 11:43:50 -0700

author
jjg
date
Tue, 19 May 2009 11:43:50 -0700
changeset 282
fc634a593812
parent 281
f838537fb1ac
child 283
cd0630109de5

6841419: classfile: add constant pool iterator
Reviewed-by: mcimadamore

src/share/classes/com/sun/tools/classfile/ClassTranslator.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/classfile/ClassWriter.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/classfile/ConstantPool.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/classfile/ClassTranslator.java	Tue May 19 11:33:13 2009 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/classfile/ClassTranslator.java	Tue May 19 11:43:50 2009 -0700
     1.3 @@ -95,7 +95,7 @@
     1.4          if (cp2 == null) {
     1.5              ConstantPool.CPInfo[] pool2 = new ConstantPool.CPInfo[cp.size()];
     1.6              boolean eq = true;
     1.7 -            for (int i = 0; i < cp.size(); i++) {
     1.8 +            for (int i = 0; i < cp.size(); ) {
     1.9                  ConstantPool.CPInfo cpInfo;
    1.10                  try {
    1.11                      cpInfo = cp.get(i);
    1.12 @@ -107,11 +107,7 @@
    1.13                  pool2[i] = cpInfo2;
    1.14                  if (cpInfo.getTag() != cpInfo2.getTag())
    1.15                      throw new IllegalStateException();
    1.16 -                switch (cpInfo.getTag()) {
    1.17 -                    case ConstantPool.CONSTANT_Double:
    1.18 -                    case ConstantPool.CONSTANT_Long:
    1.19 -                        i += 1;
    1.20 -                }
    1.21 +                i += cpInfo.size();
    1.22              }
    1.23  
    1.24              if (eq)
     2.1 --- a/src/share/classes/com/sun/tools/classfile/ClassWriter.java	Tue May 19 11:33:13 2009 -0700
     2.2 +++ b/src/share/classes/com/sun/tools/classfile/ClassWriter.java	Tue May 19 11:43:50 2009 -0700
     2.3 @@ -118,13 +118,8 @@
     2.4          ConstantPool pool = classFile.constant_pool;
     2.5          int size = pool.size();
     2.6          out.writeShort(size);
     2.7 -        try {
     2.8 -            for (int i = 1; i < size; ) {
     2.9 -                i += constantPoolWriter.write(pool.get(i), out);
    2.10 -            }
    2.11 -        } catch (ConstantPoolException e) {
    2.12 -            throw new Error(e); // ??
    2.13 -        }
    2.14 +        for (CPInfo cpInfo: pool.entries())
    2.15 +            constantPoolWriter.write(cpInfo, out);
    2.16      }
    2.17  
    2.18      protected void writeFields() throws IOException {
     3.1 --- a/src/share/classes/com/sun/tools/classfile/ConstantPool.java	Tue May 19 11:33:13 2009 -0700
     3.2 +++ b/src/share/classes/com/sun/tools/classfile/ConstantPool.java	Tue May 19 11:43:50 2009 -0700
     3.3 @@ -26,6 +26,7 @@
     3.4  package com.sun.tools.classfile;
     3.5  
     3.6  import java.io.IOException;
     3.7 +import java.util.Iterator;
     3.8  
     3.9  /**
    3.10   * See JVMS3, section 4.5.
    3.11 @@ -223,6 +224,40 @@
    3.12          throw new EntryNotFound(value);
    3.13      }
    3.14  
    3.15 +    public Iterable<CPInfo> entries() {
    3.16 +        return new Iterable<CPInfo>() {
    3.17 +            public Iterator<CPInfo> iterator() {
    3.18 +                return new Iterator<CPInfo>() {
    3.19 +
    3.20 +                    public boolean hasNext() {
    3.21 +                        return next < pool.length;
    3.22 +                    }
    3.23 +
    3.24 +                    public CPInfo next() {
    3.25 +                        current = pool[next];
    3.26 +                        switch (current.getTag()) {
    3.27 +                            case CONSTANT_Double:
    3.28 +                            case CONSTANT_Long:
    3.29 +                                next += 2;
    3.30 +                                break;
    3.31 +                            default:
    3.32 +                                next += 1;
    3.33 +                        }
    3.34 +                        return current;
    3.35 +                    }
    3.36 +
    3.37 +                    public void remove() {
    3.38 +                        throw new UnsupportedOperationException();
    3.39 +                    }
    3.40 +
    3.41 +                    private CPInfo current;
    3.42 +                    private int next = 1;
    3.43 +
    3.44 +                };
    3.45 +            }
    3.46 +        };
    3.47 +    }
    3.48 +
    3.49      private CPInfo[] pool;
    3.50  
    3.51      public interface Visitor<R,P> {
    3.52 @@ -250,6 +285,12 @@
    3.53  
    3.54          public abstract int getTag();
    3.55  
    3.56 +        /** The number of slots in the constant pool used by this entry.
    3.57 +         * 2 for CONSTANT_Double and CONSTANT_Long; 1 for everything else. */
    3.58 +        public int size() {
    3.59 +            return 1;
    3.60 +        }
    3.61 +
    3.62          public abstract <R,D> R accept(Visitor<R,D> visitor, D data);
    3.63  
    3.64          protected final ConstantPool cp;
    3.65 @@ -350,6 +391,11 @@
    3.66          }
    3.67  
    3.68          @Override
    3.69 +        public int size() {
    3.70 +            return 2;
    3.71 +        }
    3.72 +
    3.73 +        @Override
    3.74          public String toString() {
    3.75              return "CONSTANT_Double_info[value: " + value + "]";
    3.76          }
    3.77 @@ -463,6 +509,11 @@
    3.78          }
    3.79  
    3.80          @Override
    3.81 +        public int size() {
    3.82 +            return 2;
    3.83 +        }
    3.84 +
    3.85 +        @Override
    3.86          public String toString() {
    3.87              return "CONSTANT_Long_info[value: " + value + "]";
    3.88          }

mercurial