1 /*
   2  * Copyright (c) 2014, 2016, 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 8003147
  27  * @summary Test port fix for BCEL bug 39695.
  28  */
  29 
  30 package parsers;
  31 
  32 import java.io.FileOutputStream;
  33 import java.io.FilePermission;
  34 
  35 import jaxp.library.JAXPTestUtilities;
  36 
  37 import org.testng.Assert;
  38 import org.testng.annotations.Listeners;
  39 import org.testng.annotations.Test;
  40 
  41 import com.sun.org.apache.bcel.internal.classfile.ClassParser;
  42 import com.sun.org.apache.bcel.internal.classfile.ConstantClass;
  43 import com.sun.org.apache.bcel.internal.classfile.ConstantPool;
  44 import com.sun.org.apache.bcel.internal.classfile.ConstantUtf8;
  45 import com.sun.org.apache.bcel.internal.classfile.JavaClass;
  46 import com.sun.org.apache.bcel.internal.classfile.Method;
  47 import com.sun.org.apache.bcel.internal.generic.ClassGen;
  48 import com.sun.org.apache.bcel.internal.generic.MethodGen;
  49 
  50 @Listeners({ jaxp.library.FilePolicy.class, jaxp.library.InternalAPIPolicy.class })
  51 public class Bug8003147Test {
  52 
  53     @Test
  54     public void test() throws Exception {
  55         // Note: com.sun.org.apache.bcel.internal.classfile.JavaClass doesn't
  56         // support InvokeDynamic, so can't use lambda, also can't use string1 +
  57         // string2, because javac will generate a dynamic call where invoking
  58         // string1.concat(string2), so create a separate Bug8003147TestClass
  59         JAXPTestUtilities.tryRunWithTmpPermission(() -> {
  60             String classfile = System.getProperty("test.classes") + "/parsers/Bug8003147TestClass.class";
  61             JavaClass jc = new ClassParser(classfile).parse();
  62 
  63             // rename class
  64             ConstantPool cp = jc.getConstantPool();
  65             int cpIndex = ((ConstantClass) cp.getConstant(jc.getClassNameIndex())).getNameIndex();
  66             cp.setConstant(cpIndex, new ConstantUtf8("parsers/Bug8003147TestClassPrime"));
  67             ClassGen gen = new ClassGen(jc);
  68             Method[] methods = jc.getMethods();
  69             int index;
  70             for (index = 0; index < methods.length; index++) {
  71                 if (methods[index].getName().equals("doSomething")) {
  72                     break;
  73                 }
  74             }
  75             Method m = methods[index];
  76             MethodGen mg = new MethodGen(m, gen.getClassName(), gen.getConstantPool());
  77             gen.replaceMethod(m, mg.getMethod());
  78             String path = classfile.replace("Bug8003147TestClass", "Bug8003147TestClassPrime");
  79             gen.getJavaClass().dump(new FileOutputStream(path));
  80 
  81             try {
  82                 Class.forName("parsers.Bug8003147TestClassPrime");
  83             } catch (ClassFormatError cfe) {
  84                 cfe.printStackTrace();
  85                 Assert.fail("modified version of class does not pass verification");
  86             }
  87         }, new FilePermission(System.getProperty("test.classes") + "/-", "read, write"));
  88     }
  89 }