6887895: CONSTANT_Class_info getBaseName does not handle arrays of primitives correctly

Tue, 13 Oct 2009 14:02:53 -0700

author
jjg
date
Tue, 13 Oct 2009 14:02:53 -0700
changeset 422
e526e39579ae
parent 419
c6d0c55b1aba
child 423
8a4543b30586

6887895: CONSTANT_Class_info getBaseName does not handle arrays of primitives correctly
Reviewed-by: ksrini

src/share/classes/com/sun/tools/classfile/ConstantPool.java file | annotate | diff | comparison | revisions
test/tools/javap/classfile/T6887895.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/classfile/ConstantPool.java	Mon Sep 28 16:48:30 2009 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/classfile/ConstantPool.java	Tue Oct 13 14:02:53 2009 -0700
     1.3 @@ -369,14 +369,33 @@
     1.4              return 3;
     1.5          }
     1.6  
     1.7 +        /**
     1.8 +         * Get the raw value of the class referenced by this constant pool entry.
     1.9 +         * This will either be the name of the class, in internal form, or a
    1.10 +         * descriptor for an array class.
    1.11 +         * @return the raw value of the class
    1.12 +         */
    1.13          public String getName() throws ConstantPoolException {
    1.14              return cp.getUTF8Value(name_index);
    1.15          }
    1.16  
    1.17 +        /**
    1.18 +         * If this constant pool entry identifies either a class or interface type,
    1.19 +         * or a possibly multi-dimensional array of a class of interface type,
    1.20 +         * return the name of the class or interface in internal form. Otherwise,
    1.21 +         * (i.e. if this is a possibly multi-dimensional array of a primitive type),
    1.22 +         * return null.
    1.23 +         * @return the base class or interface name
    1.24 +         */
    1.25          public String getBaseName() throws ConstantPoolException {
    1.26              String name = getName();
    1.27 -            int index = name.indexOf("[L") + 1;
    1.28 -            return name.substring(index);
    1.29 +            if (name.startsWith("[")) {
    1.30 +                int index = name.indexOf("[L");
    1.31 +                if (index == -1)
    1.32 +                    return null;
    1.33 +                return name.substring(index + 2, name.length() - 1);
    1.34 +            } else
    1.35 +                return name;
    1.36          }
    1.37  
    1.38          public int getDimensionCount() throws ConstantPoolException {
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/tools/javap/classfile/T6887895.java	Tue Oct 13 14:02:53 2009 -0700
     2.3 @@ -0,0 +1,121 @@
     2.4 +/*
     2.5 + * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
     2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.7 + *
     2.8 + * This code is free software; you can redistribute it and/or modify it
     2.9 + * under the terms of the GNU General Public License version 2 only, as
    2.10 + * published by the Free Software Foundation.
    2.11 + *
    2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    2.15 + * version 2 for more details (a copy is included in the LICENSE file that
    2.16 + * accompanied this code).
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License version
    2.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    2.21 + *
    2.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    2.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    2.24 + * have any questions.
    2.25 + */
    2.26 +
    2.27 +/*
    2.28 + * @test
    2.29 + * @bug 6887895
    2.30 + * @summary CONSTANT_Class_info getBaseName does not handle arrays of primitives correctly
    2.31 + */
    2.32 +
    2.33 +import java.io.*;
    2.34 +import java.net.*;
    2.35 +import java.util.*;
    2.36 +import com.sun.tools.classfile.*;
    2.37 +import com.sun.tools.classfile.ConstantPool.*;
    2.38 +
    2.39 +public class T6887895 {
    2.40 +    public static void main(String[] args) throws Exception {
    2.41 +        new T6887895().run();
    2.42 +    }
    2.43 +
    2.44 +    void run() throws Exception {
    2.45 +        Set<String> found = new TreeSet<String>();
    2.46 +
    2.47 +        ClassFile cf = getClassFile("T6887895$Test.class");
    2.48 +        for (CPInfo cpInfo: cf.constant_pool.entries()) {
    2.49 +            if (cpInfo instanceof CONSTANT_Class_info) {
    2.50 +                CONSTANT_Class_info info = (CONSTANT_Class_info) cpInfo;
    2.51 +                String name = info.getName();
    2.52 +                String baseName = info.getBaseName();
    2.53 +                System.out.println("found: " + name + " " + baseName);
    2.54 +                if (baseName != null)
    2.55 +                    found.add(baseName);
    2.56 +            }
    2.57 +        }
    2.58 +
    2.59 +        String[] expectNames = {
    2.60 +            "java/lang/Object",
    2.61 +            "java/lang/String",
    2.62 +            "T6887895",
    2.63 +            "T6887895$Test"
    2.64 +        };
    2.65 +
    2.66 +        Set<String> expect = new TreeSet<String>(Arrays.asList(expectNames));
    2.67 +        if (!found.equals(expect)) {
    2.68 +            System.err.println("found: " + found);
    2.69 +            System.err.println("expect: " + expect);
    2.70 +            throw new Exception("unexpected values found");
    2.71 +        }
    2.72 +    }
    2.73 +
    2.74 +    ClassFile getClassFile(String name) throws IOException, ConstantPoolException {
    2.75 +        URL url = getClass().getResource(name);
    2.76 +        InputStream in = url.openStream();
    2.77 +        try {
    2.78 +            return ClassFile.read(in);
    2.79 +        } finally {
    2.80 +            in.close();
    2.81 +        }
    2.82 +    }
    2.83 +
    2.84 +    class Test {
    2.85 +        void m() {
    2.86 +            boolean[] az = new boolean[0];
    2.87 +            boolean[][] aaz = new boolean[0][];
    2.88 +            boolean[][][] aaaz = new boolean[0][][];
    2.89 +
    2.90 +            byte[] ab = new byte[0];
    2.91 +            byte[][] aab = new byte[0][];
    2.92 +            byte[][][] aaab = new byte[0][][];
    2.93 +
    2.94 +            char[] ac = new char[0];
    2.95 +            char[][] aac = new char[0][];
    2.96 +            char[][][] aaac = new char[0][][];
    2.97 +
    2.98 +            double[] ad = new double[0];
    2.99 +            double[][] aad = new double[0][];
   2.100 +            double[][][] aaad = new double[0][][];
   2.101 +
   2.102 +            float[] af = new float[0];
   2.103 +            float[][] aaf = new float[0][];
   2.104 +            float[][][] aaaf = new float[0][][];
   2.105 +
   2.106 +            int[] ai = new int[0];
   2.107 +            int[][] aai = new int[0][];
   2.108 +            int[][][] aaai = new int[0][][];
   2.109 +
   2.110 +            long[] al = new long[0];
   2.111 +            long[][] aal = new long[0][];
   2.112 +            long[][][] aaal = new long[0][][];
   2.113 +
   2.114 +            short[] as = new short[0];
   2.115 +            short[][] aas = new short[0][];
   2.116 +            short[][][] aaas = new short[0][][];
   2.117 +
   2.118 +            String[] aS = new String[0];
   2.119 +            String[][] aaS = new String[0][];
   2.120 +            String[][][] aaaS = new String[0][][];
   2.121 +        }
   2.122 +    }
   2.123 +}
   2.124 +

mercurial