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.wireprotocol;
042:
043: import java.io.IOException;
044: import java.io.ObjectInputStream;
045: import java.io.ObjectOutputStream;
046:
047: /**
048: * Conceptually, the base class for both InstrumentMethodGroupResponse and InstrumentMethodGroupCommand. However, we have to use
049: * an instance of this class in each of the above, plus some delegation, instead of normal inheritance, since the above classes
050: * have to extend Response and Command, respectively.
051: *
052: * @author Tomas Hurka
053: * @author Misha Dmitriev
054: */
055: public class InstrumentMethodGroupData {
056: //~ Instance fields ----------------------------------------------------------------------------------------------------------
057:
058: protected int[] instrMethodClassLoaderIds;
059: protected String[] instrMethodClasses;
060: protected boolean[] instrMethodLeaf;
061: protected byte[][] replacementClassFileBytes;
062: protected int addInfo;
063: protected int nClasses;
064: protected int nMethods;
065:
066: //~ Constructors -------------------------------------------------------------------------------------------------------------
067:
068: /** 1.5-style RedefineClasses() instrumentation constructor */
069: public InstrumentMethodGroupData(String[] instrMethodClasses,
070: int[] instrMethodClassLoaderIds,
071: byte[][] replacementClassFileBytes,
072: boolean[] instrMethodLeaf, int addInfo) {
073: nClasses = instrMethodClasses.length;
074: nMethods = (instrMethodLeaf != null) ? instrMethodLeaf.length
075: : 0;
076: this .instrMethodClasses = instrMethodClasses;
077: this .instrMethodClassLoaderIds = instrMethodClassLoaderIds;
078: this .replacementClassFileBytes = replacementClassFileBytes;
079: this .addInfo = addInfo;
080: }
081:
082: // Custom serializaion support
083: InstrumentMethodGroupData() {
084: }
085:
086: //~ Methods ------------------------------------------------------------------------------------------------------------------
087:
088: public int getAddInfo() {
089: return addInfo;
090: }
091:
092: public int[] getClassLoaderIds() {
093: return instrMethodClassLoaderIds;
094: }
095:
096: public boolean[] getInstrMethodLeaf() {
097: return instrMethodLeaf;
098: }
099:
100: public String[] getMethodClasses() {
101: return instrMethodClasses;
102: }
103:
104: public int getNClasses() {
105: return nClasses;
106: }
107:
108: public int getNMethods() {
109: return nMethods;
110: }
111:
112: public byte[][] getReplacementClassFileBytes() {
113: return replacementClassFileBytes;
114: }
115:
116: public void dump() {
117: if (instrMethodClasses == null) {
118: System.err.println("0 classes --"); // NOI18N
119:
120: return;
121: } else {
122: if (instrMethodClasses[0].startsWith("*FAKE")) { // NOI18N
123: System.err.println("Fake InstrMethodGroupBase --"); // NOI18N
124:
125: return;
126: }
127:
128: System.err.println(nClasses + " classes, " + nMethods
129: + " methods --"); // NOI18N
130: }
131:
132: int idx = 0;
133:
134: for (int i = 0; i < nClasses; i++) {
135: System.err.print("--Class " + instrMethodClasses[i] + ","
136: + instrMethodClassLoaderIds[i]); // NOI18N
137: System.err.println();
138: }
139: }
140:
141: // ------------------------ Debugging -------------------------
142: public String toString() {
143: return ((instrMethodClasses != null) ? (instrMethodClasses.length)
144: : 0)
145: + " classes."; // NOI18N
146: }
147:
148: void readObject(ObjectInputStream in) throws IOException {
149: nClasses = in.readInt();
150:
151: if (nClasses == 0) {
152: return;
153: }
154:
155: if ((instrMethodClasses == null)
156: || (nClasses > instrMethodClasses.length)) {
157: instrMethodClasses = new String[nClasses];
158: instrMethodClassLoaderIds = new int[nClasses];
159: }
160:
161: for (int i = 0; i < nClasses; i++) {
162: instrMethodClasses[i] = in.readUTF();
163: instrMethodClassLoaderIds[i] = in.readInt();
164: }
165:
166: nMethods = in.readInt();
167:
168: int code = in.read();
169:
170: if (code != 0) {
171: if ((instrMethodLeaf == null)
172: || (nMethods > instrMethodLeaf.length)) {
173: instrMethodLeaf = new boolean[nMethods];
174: }
175:
176: for (int i = 0; i < nMethods; i++) {
177: instrMethodLeaf[i] = in.readBoolean();
178: }
179: } else {
180: instrMethodLeaf = null;
181: }
182:
183: addInfo = in.readInt();
184:
185: if ((replacementClassFileBytes == null)
186: || (nClasses > replacementClassFileBytes.length)) {
187: replacementClassFileBytes = new byte[nClasses][];
188: }
189:
190: for (int i = 0; i < nClasses; i++) {
191: int len = in.readInt();
192:
193: if (len > 0) {
194: replacementClassFileBytes[i] = new byte[len];
195: in.readFully(replacementClassFileBytes[i]);
196: }
197: }
198: }
199:
200: void writeObject(ObjectOutputStream out) throws IOException {
201: if (instrMethodClasses == null) {
202: out.writeInt(0);
203:
204: return;
205: }
206:
207: out.writeInt(nClasses);
208:
209: for (int i = 0; i < nClasses; i++) {
210: out.writeUTF(instrMethodClasses[i]);
211: out.writeInt(instrMethodClassLoaderIds[i]);
212: }
213:
214: out.writeInt(nMethods);
215:
216: if (instrMethodLeaf != null) {
217: out.write(1);
218:
219: for (int i = 0; i < nMethods; i++) {
220: out.writeBoolean(instrMethodLeaf[i]);
221: }
222: } else {
223: out.write(0);
224: }
225:
226: out.writeInt(addInfo);
227:
228: for (int i = 0; i < nClasses; i++) {
229: if (replacementClassFileBytes[i] == null) {
230: out.writeInt(0);
231: } else {
232: out.writeInt(replacementClassFileBytes[i].length);
233: out.write(replacementClassFileBytes[i]);
234: }
235: }
236:
237: instrMethodClasses = null;
238: instrMethodClassLoaderIds = null;
239: instrMethodLeaf = null;
240: replacementClassFileBytes = null;
241: }
242: }
|