001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.lib.profiler.instrumentation;
042:
043: import org.netbeans.lib.profiler.classfile.DynamicClassInfo;
044: import org.netbeans.lib.profiler.utils.MiscUtils;
045: import java.io.IOException;
046:
047: /**
048: * This class contains functionality to rewrite a given complete class file, replacing given
049: * methodinfos and appending the constant pool.
050: *
051: * @author Misha Dmitriev
052: */
053: public class ClassRewriter {
054: //~ Methods ------------------------------------------------------------------------------------------------------------------
055:
056: public static byte[] rewriteClassFile(DynamicClassInfo clazz,
057: byte[][] replacementMethodInfos, int nAddedCPEntries,
058: byte[] addedCPContents) {
059: // Now assemble a new class file. First get original class file bytes.
060: byte[] origBytes = null;
061:
062: try {
063: origBytes = clazz.getClassFileBytes();
064: } catch (IOException ex) {
065: // Shouldn't happen, so a message just in case
066: MiscUtils
067: .internalError("ClassRewriter: can't get original class file bytes for class "
068: + clazz.getName()
069: + "\nIOException message = "
070: + ex.getMessage()); // NOI18N
071: }
072:
073: // Compute new class file length.
074: int newLen = origBytes.length;
075:
076: // First add new constant pool size
077: newLen += addedCPContents.length;
078:
079: // Now add differences between new and old method sizes
080: int nMethods = clazz.getMethodNames().length;
081:
082: for (int i = 0; i < nMethods; i++) {
083: if (replacementMethodInfos[i] != null) {
084: newLen += (replacementMethodInfos[i].length - clazz
085: .getOrigMethodInfoLength(i));
086: }
087: }
088:
089: byte[] res = new byte[newLen];
090:
091: // Copy over unchanged contents from old class file, copy/add changed contents, and adjust some counts
092: int destPos = 0;
093: // Copy preamble and original constant pool unchanged
094: System.arraycopy(origBytes, 0, res, destPos, clazz
095: .getOrigIntermediateDataStartOfs());
096: destPos += clazz.getOrigIntermediateDataStartOfs();
097: // Copy our new constant pool extension
098: System.arraycopy(addedCPContents, 0, res, destPos,
099: addedCPContents.length);
100: destPos += addedCPContents.length;
101:
102: // Adjust the cpool count
103: int newCPCount = clazz.getOrigCPoolCount() + nAddedCPEntries;
104: int pos = clazz.getOrigCPoolStartOfs();
105: res[pos] = (byte) ((newCPCount >> 8) & 255);
106: res[pos + 1] = (byte) (newCPCount & 255);
107:
108: // Copy intermediate data and fields unchanged
109: int count = clazz.getOrigMethodsStartOfs()
110: - clazz.getOrigIntermediateDataStartOfs();
111: System
112: .arraycopy(origBytes, clazz
113: .getOrigIntermediateDataStartOfs(), res,
114: destPos, count);
115: destPos += count;
116:
117: // Now copy all new methodInfos. First write the method count
118: res[destPos] = (byte) ((nMethods >> 8) & 255);
119: res[destPos + 1] = (byte) (nMethods & 255);
120: destPos += 2;
121:
122: // Write methodInfos
123: for (int i = 0; i < nMethods; i++) {
124: if (replacementMethodInfos[i] != null) {
125: System.arraycopy(replacementMethodInfos[i], 0, res,
126: destPos, replacementMethodInfos[i].length);
127: destPos += replacementMethodInfos[i].length;
128: } else {
129: byte[] origMethodInfo = clazz.getOrigMethodInfo(i);
130: System.arraycopy(origMethodInfo, 0, res, destPos,
131: origMethodInfo.length);
132: destPos += origMethodInfo.length;
133: }
134: }
135:
136: // Copy what remains - class attributes
137: count = origBytes.length - clazz.getOrigAttrsStartOfs();
138: System.arraycopy(origBytes, clazz.getOrigAttrsStartOfs(), res,
139: destPos, count);
140:
141: // For debugging
142: //if (clazz.getName().equals("profilertestapp/Main")) {
143: // saveClassFileToDisk(clazz, res);
144: //}
145: return res;
146: }
147:
148: public static void saveToDisk(String name, byte[] classBytes) {
149: name = name.replace('/', '_'); // NOI18N
150:
151: try {
152: System.err.print("*** Gonna save bytecode " + name
153: + " to disk... "); // NOI18N
154:
155: java.io.OutputStream out = new java.io.FileOutputStream(
156: new java.io.File(name + ".class")); // NOI18N
157: out.write(classBytes);
158: out.close();
159: System.err.println("done"); // NOI18N
160: } catch (Exception ex) {
161: System.err
162: .println("*** In RecursiveMethodInstrumentor.saveClassFileToDisk caught ex = "
163: + ex); // NOI18N
164: }
165: }
166:
167: private static void saveClassFileToDisk(DynamicClassInfo clazz,
168: byte[] replacementClassFile) {
169: saveToDisk(clazz.getName(), replacementClassFile);
170: }
171: }
|