< prev index next >

test/jdk/java/lang/instrument/NamedBuffer.java

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. --- 1,7 ---- /* ! * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.
*** 82,87 **** --- 82,142 ---- resultBuffer, 0, actualSize); return resultBuffer; } + + static final String DEST = System.getProperty("test.classes"); + static final boolean VERBOSE = false; + + static boolean checkMatch(byte[] buf, byte[] name, int begIdx) { + if (buf.length < name.length + begIdx) { + return false; + } + for (int i = 0; i < name.length; i++) { + if (buf[i + begIdx] != name[i]) { + return false; + } + } + return true; + } + + // This function reads a class file from disk and replaces the first character of + // the name with the one passed as "replace". Then goes through the bytecodes to + // replace all instances of the name of the class with the new name. The + // redefinition tests use this to redefine Host$ classes with precompiled class files + // Xost.java, Yost.java and Zost.java. + static byte[] + bytesForHostClass(char replace, String className) throws Throwable { + String tail = className.substring(1); + String origClassName = "" + replace + tail; + File clsfile = new File(DEST + "/" + origClassName + ".class"); + + if (VERBOSE) { + System.out.println(" Reading bytes from " + clsfile); + } + byte[] buf = null; + try (FileInputStream str = new FileInputStream(clsfile)) { + buf = loadBufferFromStream(str); + } + + boolean found = false; + int dollarSignIdx = className.indexOf('$'); + int ptrnLen = (dollarSignIdx == -1) ? className.length() : dollarSignIdx; + byte[] ptrnBytes = origClassName.substring(0, ptrnLen).getBytes(); + byte firstByte = className.getBytes()[0]; + + for (int i = 0; i < buf.length - ptrnLen; i++) { + if (checkMatch(buf, ptrnBytes, i)) { + if (VERBOSE) { + System.out.println("Appear to have found " + origClassName + " starting at " + i); + } + buf[i] = firstByte; + found = true; + } + } + if (!found) { + throw new Error("Could not locate '" + ptrnBytes + "' name in byte array"); + } + return buf; + } }
< prev index next >