jdk/test/java/lang/instrument/ATransformerManagementTestCase.java
author alanb
Fri, 07 Apr 2017 08:05:54 +0000
changeset 44545 83b611b88ac8
parent 39739 5d058d6bd245
permissions -rw-r--r--
8177530: Module system implementation refresh (4/2017) Reviewed-by: mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 13576
diff changeset
     2
 * Copyright (c) 2003, 2016, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.lang.instrument.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.security.ProtectionDomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Simple tests for the TransformerManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public abstract class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
ATransformerManagementTestCase
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    extends AInstrumentationTestCase
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private static final String redefinedClassName = "DummyClass";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    protected int   kModSamples = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    public final    ClassFileTransformer[] kTransformerSamples = new ClassFileTransformer[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            new MyClassFileTransformer(         Integer.toString(0)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            new MyClassFileTransformer( Integer.toString(1)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            new MyClassFileTransformer(         Integer.toString(2)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            new MyClassFileTransformer( Integer.toString(3)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            new MyClassFileTransformer(         Integer.toString(4)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            new MyClassFileTransformer( Integer.toString(5)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            new MyClassFileTransformer(         Integer.toString(6)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            new MyClassFileTransformer( Integer.toString(7)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            new MyClassFileTransformer(         Integer.toString(8)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            new MyClassFileTransformer( Integer.toString(9)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            new MyClassFileTransformer(         Integer.toString(10)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            new MyClassFileTransformer( Integer.toString(11)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            new MyClassFileTransformer( Integer.toString(12)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            new MyClassFileTransformer( Integer.toString(13)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            new MyClassFileTransformer(         Integer.toString(14)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            new MyClassFileTransformer( Integer.toString(15)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            new MyClassFileTransformer(         Integer.toString(16)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            new MyClassFileTransformer(         Integer.toString(17)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            new MyClassFileTransformer( Integer.toString(18)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private ArrayList           fTransformers;       // The list of transformers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private int                 fTransformerIndex;   // The number of transformers encountered
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private String              fDelayedFailure;     // Set non-null if failed in transformer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Constructor for ATransformerManagementTestCase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public ATransformerManagementTestCase(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Returns one of the sample transformers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @return a random transformer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    protected ClassFileTransformer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    getRandomTransformer()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        int randIndex = (int)Math.floor(Math.random() * kTransformerSamples.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        verbosePrint("Choosing random transformer #" + randIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return kTransformerSamples[randIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Method addTransformerToManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param transformer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    protected void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    addTransformerToManager(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        Instrumentation         manager,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        ClassFileTransformer    transformer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    {
13576
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   101
        addTransformerToManager(manager, transformer, false);
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   102
    }
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   103
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   104
    /**
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   105
     * Method addTransformerToManager.
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   106
     * @param manager
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   107
     * @param transformer
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   108
     * @param canRetransform
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   109
     */
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   110
    protected void
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   111
    addTransformerToManager(
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   112
        Instrumentation         manager,
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   113
        ClassFileTransformer    transformer,
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   114
        boolean                 canRetransform)
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   115
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (transformer != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            fTransformers.add(transformer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
13576
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   120
        manager.addTransformer(transformer, canRetransform);
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   121
        verbosePrint("Added transformer " + transformer
850377563282 7191322: add test for 7064927 to java.lang.instrument
dcubed
parents: 5506
diff changeset
   122
            + " with canRetransform=" + canRetransform);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Remove transformer from manager and list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @param manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @param transformer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    protected void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    removeTransformerFromManager(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        Instrumentation manager,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        ClassFileTransformer transformer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        assertTrue("Transformer not found in manager ("+transformer+")", manager.removeTransformer(transformer));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (transformer != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            fTransformers.remove(transformer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        verbosePrint("Removed transformer " + transformer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Decrements the transformer index as well as removes transformer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param fInst         manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param transformer       transformer to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param decrementIndex    the tranformer index gets out of sync with transformers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *                          that are removed from the manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    protected void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    removeTransformerFromManager(   Instrumentation manager,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                                    ClassFileTransformer transformer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                                    boolean decrementIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        removeTransformerFromManager(manager, transformer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (decrementIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            fTransformerIndex--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            verbosePrint("removeTransformerFromManager fTransformerIndex decremented to: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                         fTransformerIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * verify transformer by asserting that the number of transforms that occured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * is the same as the number of valid transformers added to the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @param manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    protected void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    verifyTransformers(Instrumentation manager)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        File f = new File(System.getProperty("test.classes", "."), redefinedClassName + ".class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        System.out.println("Reading test class from " + f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            InputStream redefineStream = new FileInputStream(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            byte[] bytes = NamedBuffer.loadBufferFromStream(redefineStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            ClassDefinition cd = new ClassDefinition(DummyClass.class, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            fInst.redefineClasses(new ClassDefinition[]{ cd });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            verbosePrint("verifyTransformers redefined " + redefinedClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        catch (IOException e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            fail("Could not load the class: " + redefinedClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        catch (ClassNotFoundException e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            fail("Could not find the class: " + redefinedClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        catch (UnmodifiableClassException e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            fail("Could not modify the class: " + redefinedClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        // Report any delayed failures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        assertTrue(fDelayedFailure, fDelayedFailure == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        assertEquals("The number of transformers that were run does not match the expected number added to manager",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                        fTransformers.size(), fTransformerIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Asserts that the transformer being checked by the manager is the correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * one (as far as order goes) and updates the number of transformers that have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * been called.  Note that since this is being called inside of a transformer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * a standard assert (which throws an exception) cannot be used since it would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * simply cancel the transformation and otherwise be ignored.  Instead, note
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * the failure for delayed processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @param ClassFileTransformer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    private void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    checkInTransformer(ClassFileTransformer transformer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        verbosePrint("checkInTransformer: " + transformer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (fDelayedFailure == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            if (fTransformers.size() <= fTransformerIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                String msg = "The number of transformers that have checked in (" +(fTransformerIndex+1) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    ") is greater number of tranformers created ("+fTransformers.size()+")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                fDelayedFailure = msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                System.err.println("Delayed failure: " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                verbosePrint("Delayed failure: " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            if (!fTransformers.get(fTransformerIndex).equals(transformer))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                String msg = "Transformer " + fTransformers.get(fTransformerIndex) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    " should be the same as " + transformer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                fDelayedFailure = msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                System.err.println("Delayed failure: " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                verbosePrint("Delayed failure: " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            fTransformerIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            verbosePrint("fTransformerIndex incremented to: " + fTransformerIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Create a new manager, a new transformer list, and initializes the number of transformers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * indexed to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    protected void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    setUp()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        super.setUp();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        fTransformers = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        fTransformerIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        fDelayedFailure = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        verbosePrint("setUp completed");
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
     * Sets the manager and transformers to null so that setUp needs to update.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    protected void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    tearDown()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        verbosePrint("tearDown beginning");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        fTransformers = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        super.tearDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *  Simple transformer that registers when it transforms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public class MyClassFileTransformer extends SimpleIdentityTransformer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        private final String fID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        public MyClassFileTransformer(String id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            fID = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            return MyClassFileTransformer.this.getClass().getName() + fID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        public byte[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        transform(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            ClassLoader loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            Class<?> classBeingRedefined,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            ProtectionDomain    protectionDomain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            byte[] classfileBuffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            // The transform testing is triggered by redefine, ignore others
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            if (classBeingRedefined != null) checkInTransformer(MyClassFileTransformer.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 13576
diff changeset
   293
            return super.transform(     loader,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 13576
diff changeset
   294
                                        className,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 13576
diff changeset
   295
                                        classBeingRedefined,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 13576
diff changeset
   296
                                        protectionDomain,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 13576
diff changeset
   297
                                        classfileBuffer);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 13576
diff changeset
   298
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 13576
diff changeset
   299
9d0388c6b336 8142968: Module System implementation
alanb
parents: 13576
diff changeset
   300
        public byte[]
9d0388c6b336 8142968: Module System implementation
alanb
parents: 13576
diff changeset
   301
        transform(
9d0388c6b336 8142968: Module System implementation
alanb
parents: 13576
diff changeset
   302
            Module module,
39739
5d058d6bd245 8159147: Add ClassLoader parameter to new ClassFileTransformer transform method
sspitsyn
parents: 36511
diff changeset
   303
            ClassLoader loader,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 13576
diff changeset
   304
            String className,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 13576
diff changeset
   305
            Class<?> classBeingRedefined,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 13576
diff changeset
   306
            ProtectionDomain    protectionDomain,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 13576
diff changeset
   307
            byte[] classfileBuffer) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 13576
diff changeset
   308
9d0388c6b336 8142968: Module System implementation
alanb
parents: 13576
diff changeset
   309
            // The transform testing is triggered by redefine, ignore others
9d0388c6b336 8142968: Module System implementation
alanb
parents: 13576
diff changeset
   310
            if (classBeingRedefined != null) checkInTransformer(MyClassFileTransformer.this);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 13576
diff changeset
   311
9d0388c6b336 8142968: Module System implementation
alanb
parents: 13576
diff changeset
   312
            return super.transform(     module,
39739
5d058d6bd245 8159147: Add ClassLoader parameter to new ClassFileTransformer transform method
sspitsyn
parents: 36511
diff changeset
   313
                                        loader,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                                        className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                                        classBeingRedefined,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                                        protectionDomain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                                        classfileBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * Class loader that does nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    public class MyClassLoader extends ClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
         * Constructor for MyClassLoader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        public MyClassLoader()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public String debug_byteArrayToString(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (b == null) return "null";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        StringBuffer buf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        buf.append("byte[");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        buf.append(b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        buf.append("] (");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        for (int i = 0; i < b.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            buf.append(b[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            buf.append(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        buf.deleteCharAt(buf.length()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        buf.append(")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        return buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
}