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