1 /*
   2  * Copyright (c) 2014, 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  * @modules java.xml/com.sun.org.apache.bcel.internal.classfile
  27  *          java.xml/com.sun.org.apache.bcel.internal.generic
  28  * @bug 8003147
  29  * @summary Test port fix for BCEL bug 39695.
  30  */
  31 
  32 import java.io.FileOutputStream;
  33 import java.util.ArrayList;
  34 
  35 import org.testng.Assert;
  36 import org.testng.annotations.Test;
  37 
  38 import com.sun.org.apache.bcel.internal.classfile.ClassParser;
  39 import com.sun.org.apache.bcel.internal.classfile.ConstantClass;
  40 import com.sun.org.apache.bcel.internal.classfile.ConstantPool;
  41 import com.sun.org.apache.bcel.internal.classfile.ConstantUtf8;
  42 import com.sun.org.apache.bcel.internal.classfile.JavaClass;
  43 import com.sun.org.apache.bcel.internal.classfile.Method;
  44 import com.sun.org.apache.bcel.internal.generic.ClassGen;
  45 import com.sun.org.apache.bcel.internal.generic.MethodGen;
  46 
  47 public class Bug8003147Test {
  48 
  49     @Test
  50     public void test() throws Exception {
  51         String classfile = getClass().getResource("Bug8003147Test.class").getPath();
  52         JavaClass jc = new ClassParser(classfile).parse();
  53         // rename class
  54         ConstantPool cp = jc.getConstantPool();
  55         int cpIndex = ((ConstantClass) cp.getConstant(jc.getClassNameIndex())).getNameIndex();
  56         cp.setConstant(cpIndex, new ConstantUtf8("Bug8003147TestPrime"));
  57         ClassGen gen = new ClassGen(jc);
  58         Method[] methods = jc.getMethods();
  59         int index;
  60         for (index = 0; index < methods.length; index++) {
  61             if (methods[index].getName().equals("doSomething")) {
  62                 break;
  63             }
  64         }
  65         Method m = methods[index];
  66         MethodGen mg = new MethodGen(m, gen.getClassName(), gen.getConstantPool());
  67         gen.replaceMethod(m, mg.getMethod());
  68         String path = classfile.replace("Bug8003147Test", "Bug8003147TestPrime");
  69         gen.getJavaClass().dump(new FileOutputStream(path));
  70 
  71         try {
  72             Class.forName("Bug8003147TestPrime");
  73         } catch (ClassFormatError cfe) {
  74             cfe.printStackTrace();
  75             Assert.fail("modified version of class does not pass verification");
  76         }
  77     }
  78 
  79     public void doSomething(double d, ArrayList<Integer> list) {
  80     }
  81 }