jdk/test/java/lang/instrument/ilib/Inject.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8543 e5ec12a932da
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8543
diff changeset
     2
 * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
package ilib;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.FileOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.DataOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class Inject implements RuntimeConstants {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    public static byte[] instrumentation(Options opt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
                                         ClassLoader loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
                                         String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
                                         byte[] classfileBuffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        ClassReaderWriter c = new ClassReaderWriter(classfileBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        (new Inject(className, c, loader == null, opt)).doit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        return c.result();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static boolean verbose = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    final String className;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    final ClassReaderWriter c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    final boolean isSystem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    final Options options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    int constantPoolCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    int methodsCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    int methodsCountPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    int profiler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    int wrappedTrackerIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    int thisClassIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    TrackerInjector callInjector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    TrackerInjector allocInjector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    TrackerInjector defaultInjector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    static interface TrackerInjector extends Injector {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        void reinit(int tracker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        int stackSize(int currentSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static class SimpleInjector implements TrackerInjector {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        byte[] injection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        public int stackSize(int currentSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            return currentSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        public void reinit(int tracker) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            injection = new byte[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            injection[0] = (byte)opc_invokestatic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            injection[1] = (byte)(tracker >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            injection[2] = (byte)tracker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        public byte[] bytecodes(String className, String methodName, int location) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            return injection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    static class ObjectInjector implements TrackerInjector {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        byte[] injection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        public int stackSize(int currentSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            return currentSize + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        public void reinit(int tracker) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            injection = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            injection[0] = (byte)opc_dup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            injection[1] = (byte)opc_invokestatic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            injection[2] = (byte)(tracker >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            injection[3] = (byte)tracker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        public byte[] bytecodes(String className, String methodName, int location) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return injection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    class IndexedInjector implements TrackerInjector {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        int counter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        int tracker;
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   110
        List<Info> infoList = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        public int stackSize(int currentSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            return currentSize + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        public void reinit(int tracker) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            this.tracker = tracker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        void dump(File outDir, String filename) throws IOException {
8543
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   121
            try (FileOutputStream fileOut =
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   122
                     new FileOutputStream(new File(outDir, filename));
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   123
                 DataOutputStream dataOut = new DataOutputStream(fileOut))
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   124
            {
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   125
                String currentClassName = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
8543
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   127
                dataOut.writeInt(infoList.size());
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   128
                for (Iterator<Info> it = infoList.iterator(); it.hasNext(); ) {
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   129
                    Info info = it.next();
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   130
                    if (!info.className.equals(currentClassName)) {
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   131
                        dataOut.writeInt(123456); // class name marker
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   132
                        currentClassName = info.className;
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   133
                        dataOut.writeUTF(currentClassName);
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   134
                    }
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   135
                    dataOut.writeInt(info.location);
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   136
                    dataOut.writeUTF(info.methodName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        public byte[] bytecodes(String className, String methodName, int location) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            byte[] injection = new byte[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            int injectedIndex = options.fixedIndex != 0? options.fixedIndex : ++counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            infoList.add(new Info(counter, className, methodName, location));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            injection[0] = (byte)opc_sipush;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            injection[1] = (byte)(injectedIndex >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            injection[2] = (byte)injectedIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            injection[3] = (byte)opc_invokestatic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            injection[4] = (byte)(tracker >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            injection[5] = (byte)tracker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            return injection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    Inject(String className, ClassReaderWriter c, boolean isSystem, Options options) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        this.className = className;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        this.c = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        this.isSystem = isSystem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        this.options = options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    void doit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        c.copy(4 + 2 + 2); // magic min/maj version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        int constantPoolCountPos = c.generatedPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        constantPoolCount = c.copyU2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        // copy old constant pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        c.copyConstantPool(constantPoolCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            System.out.println("ConstantPool expanded from: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                               constantPoolCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        profiler = addClassToConstantPool(options.trackerClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (options.shouldInstrumentNew || options.shouldInstrumentObjectInit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            if (options.shouldInstrumentIndexed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                if (allocInjector == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    // first time - create it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    allocInjector = new IndexedInjector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                int allocTracker = addMethodToConstantPool(profiler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                                                           options.allocTrackerMethodName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                                                           "(I)V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                allocInjector.reinit(allocTracker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            } else if (options.shouldInstrumentObject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                if (allocInjector == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    // first time - create it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    allocInjector = new ObjectInjector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                int allocTracker = addMethodToConstantPool(profiler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                                                           options.allocTrackerMethodName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                                                           "(Ljava/lang/Object;)V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                allocInjector.reinit(allocTracker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                if (allocInjector == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    // first time - create it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    allocInjector = new SimpleInjector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                int allocTracker = addMethodToConstantPool(profiler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                                                           options.allocTrackerMethodName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                                           "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                allocInjector.reinit(allocTracker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            defaultInjector = allocInjector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (options.shouldInstrumentCall) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            if (options.shouldInstrumentIndexed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                if (callInjector == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    // first time - create it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    callInjector = new IndexedInjector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                int callTracker = addMethodToConstantPool(profiler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                                          options.callTrackerMethodName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                                                          "(I)V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                callInjector.reinit(callTracker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                if (callInjector == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    // first time - create it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    callInjector = new SimpleInjector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                int callTracker = addMethodToConstantPool(profiler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                                                          options.callTrackerMethodName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                                                          "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                callInjector.reinit(callTracker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            defaultInjector = callInjector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            System.out.println("To: " + constantPoolCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        c.setSection(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        c.copy(2 + 2 + 2);  // access, this, super
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        int interfaceCount = c.copyU2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            System.out.println("interfaceCount: " + interfaceCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        c.copy(interfaceCount * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        copyFields(); // fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        copyMethods(); // methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        int attrCountPos = c.generatedPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        int attrCount = c.copyU2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            System.out.println("class attrCount: " + attrCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        // copy the class attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        copyAttrs(attrCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        c.randomAccessWriteU2(constantPoolCountPos, constantPoolCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    void copyFields() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        int count = c.copyU2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            System.out.println("fields count: " + count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        for (int i = 0; i < count; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            c.copy(6); // access, name, descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            int attrCount = c.copyU2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                System.out.println("field attr count: " + attrCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            copyAttrs(attrCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    void copyMethods() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        methodsCountPos = c.generatedPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        methodsCount = c.copyU2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        int initialMethodsCount = methodsCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            System.out.println("methods count: " + methodsCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        for (int i = 0; i < initialMethodsCount; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            copyMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    void copyMethod() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        int accessFlags = c.copyU2();// access flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (options.shouldInstrumentNativeMethods && (accessFlags & ACC_NATIVE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            wrapNativeMethod(accessFlags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        int nameIndex = c.copyU2();  // name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        String methodName = c.constantPoolString(nameIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        c.copyU2();                  // descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        int attrCount = c.copyU2();  // attribute count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            System.out.println("methods attr count: " + attrCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        for (int i = 0; i < attrCount; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            copyAttrForMethod(methodName, accessFlags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    void wrapNativeMethod(int accessFlags) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        // first, copy the native method with the name changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        // accessFlags have already been copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        int nameIndex = c.readU2();        // name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        String methodName = c.constantPoolString(nameIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        String wrappedMethodName = options.wrappedPrefix + methodName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        int wrappedNameIndex = writeCPEntryUtf8(wrappedMethodName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        c.writeU2(wrappedNameIndex);       // change to the wrapped name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        int descriptorIndex = c.copyU2();  // descriptor index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        int attrCount = c.copyU2();        // attribute count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        // need to replicate these attributes (esp Exceptions) in wrapper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        // so mark this location so we can rewind
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        c.markLocalPositionStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        for (int i = 0; i < attrCount; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            copyAttrForMethod(methodName, accessFlags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            System.err.println("   wrapped: " + methodName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        // now write the wrapper method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        c.writeU2(accessFlags & ~ACC_NATIVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        c.writeU2(nameIndex);           // original unwrapped name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        c.writeU2(descriptorIndex);     // descriptor is the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        c.writeU2(attrCount + 1);       // wrapped plus a code attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        // rewind to wrapped attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        c.rewind();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        for (int i = 0; i < attrCount; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            copyAttrForMethod(methodName, accessFlags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        // generate a Code attribute for the wrapper method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        int wrappedIndex = addMethodToConstantPool(getThisClassIndex(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                                                   wrappedNameIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                                                   descriptorIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        String descriptor = c.constantPoolString(descriptorIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        createWrapperCodeAttr(nameIndex, accessFlags, descriptor, wrappedIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        // increment method count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        c.randomAccessWriteU2(methodsCountPos, ++methodsCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    void copyAttrs(int attrCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        for (int i = 0; i < attrCount; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            copyAttr();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    void copyAttr() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        c.copy(2);             // name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        int len = c.copyU4();  // attr len
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            System.out.println("attr len: " + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        c.copy(len);           // attribute info
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    void copyAttrForMethod(String methodName, int accessFlags) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        int nameIndex = c.copyU2();   // name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        // check for Code attr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        if (nameIndex == c.codeAttributeIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                copyCodeAttr(methodName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            } catch (IOException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                System.err.println("Code Exception - " + exc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            int len = c.copyU4();     // attr len
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                System.out.println("method attr len: " + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            c.copy(len);              // attribute info
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    void copyAttrForCode(InjectBytecodes ib) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        int nameIndex = c.copyU2();   // name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        // check for Code attr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        if (nameIndex == c.lineNumberAttributeIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            ib.copyLineNumberAttr();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        } else if (nameIndex == c.localVarAttributeIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            ib.copyLocalVarAttr();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            int len = c.copyU4();     // attr len
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                System.out.println("code attr len: " + len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            c.copy(len);              // attribute info
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    void copyCodeAttr(String methodName) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            System.out.println("Code attr found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        int attrLengthPos = c.generatedPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        int attrLength = c.copyU4();        // attr len
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        int maxStack = c.readU2();          // max stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        c.writeU2(defaultInjector == null? maxStack :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                  defaultInjector.stackSize(maxStack));  // big enough for injected code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        c.copyU2();                         // max locals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        int codeLengthPos = c.generatedPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        int codeLength = c.copyU4();        // code length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        if (options.targetMethod != null && !options.targetMethod.equals(methodName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            c.copy(attrLength - 8); // copy remainder minus already copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        if (isSystem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            if (codeLength == 1 && methodName.equals("finalize")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    System.out.println("empty system finalizer not instrumented");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                c.copy(attrLength - 8); // copy remainder minus already copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            if (codeLength == 1 && methodName.equals("<init>")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    System.out.println("empty system constructor not instrumented");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                if (!options.shouldInstrumentObjectInit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    c.copy(attrLength - 8); // copy remainder minus already copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            if (methodName.equals("<clinit>")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    System.out.println("system class initializer not instrumented");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                c.copy(attrLength - 8); // copy remainder minus already copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        if (options.shouldInstrumentObjectInit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            && (!className.equals("java/lang/Object")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                || !methodName.equals("<init>"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            c.copy(attrLength - 8); // copy remainder minus already copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        InjectBytecodes ib = new InjectBytecodes(c, codeLength, className, methodName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        if (options.shouldInstrumentNew) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            ib.injectAfter(opc_new, allocInjector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            ib.injectAfter(opc_newarray, allocInjector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            ib.injectAfter(opc_anewarray, allocInjector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            ib.injectAfter(opc_multianewarray, allocInjector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        if (options.shouldInstrumentCall) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            ib.inject(0, callInjector.bytecodes(className, methodName, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        if (options.shouldInstrumentObjectInit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            ib.inject(0, allocInjector.bytecodes(className, methodName, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        ib.adjustOffsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        // fix up code length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        int newCodeLength = c.generatedPosition() - (codeLengthPos + 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        c.randomAccessWriteU4(codeLengthPos, newCodeLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            System.out.println("code length old: " + codeLength +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                               ", new: " + newCodeLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        ib.copyExceptionTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        int attrCount = c.copyU2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        for (int i = 0; i < attrCount; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            copyAttrForCode(ib);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        // fix up attr length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        int newAttrLength = c.generatedPosition() - (attrLengthPos + 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        c.randomAccessWriteU4(attrLengthPos, newAttrLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            System.out.println("attr length old: " + attrLength +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                               ", new: " + newAttrLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    int nextDescriptorIndex(String descriptor, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        switch (descriptor.charAt(index)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        case 'B': // byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        case 'C': // char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        case 'I': // int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        case 'S': // short
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        case 'Z': // boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        case 'F': // float
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        case 'D': // double
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        case 'J': // long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            return index + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        case 'L': // object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            int i = index + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            while (descriptor.charAt(i) != ';') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                ++i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            return i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        case '[': // array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            return nextDescriptorIndex(descriptor, index + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        throw new InternalError("should not reach here");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    int getWrappedTrackerIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        if (wrappedTrackerIndex == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            wrappedTrackerIndex = addMethodToConstantPool(profiler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                                                          options.wrappedTrackerMethodName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                                                          "(Ljava/lang/String;I)V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        return wrappedTrackerIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    int getThisClassIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        if (thisClassIndex == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            thisClassIndex = addClassToConstantPool(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        return thisClassIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    int computeMaxLocals(String descriptor, int accessFlags) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        int index = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        int slot = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        if ((accessFlags & ACC_STATIC) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            ++slot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        char type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        while ((type = descriptor.charAt(index)) != ')') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            case 'B': // byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            case 'C': // char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            case 'I': // int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            case 'S': // short
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            case 'Z': // boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            case 'F': // float
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            case 'L': // object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            case '[': // array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                ++slot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            case 'D': // double
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            case 'J': // long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                slot += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            index = nextDescriptorIndex(descriptor, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        return slot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    void createWrapperCodeAttr(int methodNameIndex, int accessFlags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                               String descriptor, int wrappedIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        int maxLocals = computeMaxLocals(descriptor, accessFlags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        c.writeU2(c.codeAttributeIndex);        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        int attrLengthPos = c.generatedPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        c.writeU4(0);                // attr len -- fix up below
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        c.writeU2(maxLocals + 4);    // max stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        c.writeU2(maxLocals);        // max locals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        int codeLengthPos = c.generatedPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        c.writeU4(0);                // code length -- fix up below
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        int methodStringIndex = writeCPEntryString(methodNameIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        c.writeU1(opc_ldc_w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        c.writeU2(methodStringIndex);  // send the method name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        c.writeU1(opc_sipush);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        c.writeU2(options.fixedIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        c.writeU1(opc_invokestatic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        c.writeU2(getWrappedTrackerIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        // set-up args
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        int index = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        int slot = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        if ((accessFlags & ACC_STATIC) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            c.writeU1(opc_aload_0);  // this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            ++slot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        char type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        while ((type = descriptor.charAt(index)) != ')') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            case 'B': // byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            case 'C': // char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            case 'I': // int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            case 'S': // short
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            case 'Z': // boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                c.writeU1(opc_iload);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                c.writeU1(slot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                ++slot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            case 'F': // float
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                c.writeU1(opc_fload);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                c.writeU1(slot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                ++slot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            case 'D': // double
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                c.writeU1(opc_dload);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                c.writeU1(slot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                slot += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            case 'J': // long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                c.writeU1(opc_lload);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                c.writeU1(slot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                slot += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            case 'L': // object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            case '[': // array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                c.writeU1(opc_aload);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                c.writeU1(slot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                ++slot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            index = nextDescriptorIndex(descriptor, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        // call the wrapped version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        if ((accessFlags & ACC_STATIC) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            c.writeU1(opc_invokevirtual);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            c.writeU1(opc_invokestatic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        c.writeU2(wrappedIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        // return correct type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        switch (descriptor.charAt(index+1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        case 'B': // byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        case 'C': // char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        case 'I': // int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        case 'S': // short
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        case 'Z': // boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            c.writeU1(opc_ireturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        case 'F': // float
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            c.writeU1(opc_freturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        case 'D': // double
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            c.writeU1(opc_dreturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        case 'J': // long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            c.writeU1(opc_lreturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        case 'L': // object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        case '[': // array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            c.writeU1(opc_areturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        case 'V': // void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            c.writeU1(opc_return);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        // end of code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        // fix up code length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        int newCodeLength = c.generatedPosition() - (codeLengthPos + 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        c.randomAccessWriteU4(codeLengthPos, newCodeLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        c.writeU2(0);                // exception table length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        c.writeU2(0);                // attribute count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        // fix up attr length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        int newAttrLength = c.generatedPosition() - (attrLengthPos + 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        c.randomAccessWriteU4(attrLengthPos, newAttrLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    int addClassToConstantPool(String className) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        int prevSection = c.setSection(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        int classNameIndex = writeCPEntryUtf8(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        int classIndex = writeCPEntryClass(classNameIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        c.setSection(prevSection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        return classIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    int addMethodToConstantPool(int classIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                                String methodName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                                String descr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        int prevSection = c.setSection(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        int methodNameIndex = writeCPEntryUtf8(methodName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        int descrIndex = writeCPEntryUtf8(descr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        c.setSection(prevSection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        return addMethodToConstantPool(classIndex, methodNameIndex, descrIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    int addMethodToConstantPool(int classIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                                int methodNameIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                                int descrIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        int prevSection = c.setSection(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        int nameAndTypeIndex = writeCPEntryNameAndType(methodNameIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                                                       descrIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        int methodIndex = writeCPEntryMethodRef(classIndex, nameAndTypeIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        c.setSection(prevSection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        return methodIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    int writeCPEntryUtf8(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        int prevSection = c.setSection(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        int len = str.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        c.writeU1(CONSTANT_UTF8); // Utf8 tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        c.writeU2(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        for (int i = 0; i < len; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            c.writeU1(str.charAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        c.setSection(prevSection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        return constantPoolCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    int writeCPEntryString(int utf8Index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        int prevSection = c.setSection(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        c.writeU1(CONSTANT_STRING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        c.writeU2(utf8Index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        c.setSection(prevSection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        return constantPoolCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    int writeCPEntryClass(int classNameIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        int prevSection = c.setSection(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        c.writeU1(CONSTANT_CLASS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        c.writeU2(classNameIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        c.setSection(prevSection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        return constantPoolCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    int writeCPEntryNameAndType(int nameIndex, int descrIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        int prevSection = c.setSection(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        c.writeU1(CONSTANT_NAMEANDTYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        c.writeU2(nameIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        c.writeU2(descrIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        c.setSection(prevSection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        return constantPoolCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    int writeCPEntryMethodRef(int classIndex, int nameAndTypeIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        int prevSection = c.setSection(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        c.writeU1(CONSTANT_METHOD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        c.writeU2(classIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        c.writeU2(nameAndTypeIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        c.setSection(prevSection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        return constantPoolCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
}