1 /*
   2  * Copyright (c) 2013, 2015, 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  * @modules jdk.compiler/com.sun.tools.classfile
  30  * @run main RedundantByteCodeInArrayTest
  31  */
  32 
  33 import java.io.File;
  34 import java.io.IOException;
  35 
  36 import com.sun.tools.classfile.Attribute;
  37 import com.sun.tools.classfile.ClassFile;
  38 import com.sun.tools.classfile.Code_attribute;
  39 import com.sun.tools.classfile.Code_attribute.InvalidIndex;
  40 import com.sun.tools.classfile.ConstantPool;
  41 import com.sun.tools.classfile.ConstantPoolException;
  42 import com.sun.tools.classfile.Descriptor.InvalidDescriptor;
  43 import com.sun.tools.classfile.Method;
  44 
  45 public class RedundantByteCodeInArrayTest {
  46     public static void main(String[] args)
  47             throws IOException, ConstantPoolException, InvalidDescriptor, InvalidIndex {
  48         new RedundantByteCodeInArrayTest()
  49                 .checkClassFile(new File(System.getProperty("test.classes", "."),
  50                     RedundantByteCodeInArrayTest.class.getName() + ".class"));
  51     }
  52 
  53     void arrMethod(int[] array, int p, int inc) {
  54         array[p] += inc;
  55     }
  56 
  57     void checkClassFile(File file)
  58             throws IOException, ConstantPoolException, InvalidDescriptor, InvalidIndex {
  59         ClassFile classFile = ClassFile.read(file);
  60         ConstantPool constantPool = classFile.constant_pool;
  61 
  62         //lets get all the methods in the class file.
  63         for (Method method : classFile.methods) {
  64             if (method.getName(constantPool).equals("arrMethod")) {
  65                 Code_attribute code = (Code_attribute) method.attributes
  66                         .get(Attribute.Code);
  67                 if (code.max_locals > 4)
  68                     throw new AssertionError("Too many locals for method arrMethod");
  69             }
  70         }
  71     }
  72 }