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