RedundantByteCodeInArrayTest.java revision 1620:823fb9229724
1/* 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 * @test 26 * @bug 8009170 27 * @summary Regression: javac generates redundant bytecode in assignop involving 28 * arrays 29 * @run main RedundantByteCodeInArrayTest 30 */ 31 32import java.io.File; 33import java.io.IOException; 34 35import com.sun.tools.classfile.Attribute; 36import com.sun.tools.classfile.ClassFile; 37import com.sun.tools.classfile.Code_attribute; 38import com.sun.tools.classfile.Code_attribute.InvalidIndex; 39import com.sun.tools.classfile.ConstantPool; 40import com.sun.tools.classfile.ConstantPoolException; 41import com.sun.tools.classfile.Descriptor.InvalidDescriptor; 42import com.sun.tools.classfile.Method; 43 44public class RedundantByteCodeInArrayTest { 45 public static void main(String[] args) 46 throws IOException, ConstantPoolException, InvalidDescriptor, InvalidIndex { 47 new RedundantByteCodeInArrayTest() 48 .checkClassFile(new File(System.getProperty("test.classes", "."), 49 RedundantByteCodeInArrayTest.class.getName() + ".class")); 50 } 51 52 void arrMethod(int[] array, int p, int inc) { 53 array[p] += inc; 54 } 55 56 void checkClassFile(File file) 57 throws IOException, ConstantPoolException, InvalidDescriptor, InvalidIndex { 58 ClassFile classFile = ClassFile.read(file); 59 ConstantPool constantPool = classFile.constant_pool; 60 61 //lets get all the methods in the class file. 62 for (Method method : classFile.methods) { 63 if (method.getName(constantPool).equals("arrMethod")) { 64 Code_attribute code = (Code_attribute) method.attributes 65 .get(Attribute.Code); 66 if (code.max_locals > 4) 67 throw new AssertionError("Too many locals for method arrMethod"); 68 } 69 } 70 } 71} 72