jdk/src/java.instrument/share/classes/sun/instrument/InstrumentationImpl.java
changeset 36511 9d0388c6b336
parent 25859 3317bb8137f4
child 37807 78e5cb21ca13
equal deleted inserted replaced
36510:043f1af70518 36511:9d0388c6b336
     1 /*
     1 /*
     2  * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    25 
    25 
    26 
    26 
    27 package sun.instrument;
    27 package sun.instrument;
    28 
    28 
    29 import java.lang.reflect.Method;
    29 import java.lang.reflect.Method;
       
    30 import java.lang.reflect.Module;
    30 import java.lang.reflect.AccessibleObject;
    31 import java.lang.reflect.AccessibleObject;
    31 
    32 
    32 import java.lang.instrument.ClassFileTransformer;
    33 import java.lang.instrument.ClassFileTransformer;
    33 import java.lang.instrument.ClassDefinition;
    34 import java.lang.instrument.ClassDefinition;
    34 import java.lang.instrument.Instrumentation;
    35 import java.lang.instrument.Instrumentation;
    35 
    36 
    36 import java.security.AccessController;
    37 import java.security.AccessController;
    37 import java.security.PrivilegedAction;
    38 import java.security.PrivilegedAction;
    38 import java.security.ProtectionDomain;
    39 import java.security.ProtectionDomain;
    39 
    40 
       
    41 import java.util.Objects;
    40 import java.util.jar.JarFile;
    42 import java.util.jar.JarFile;
    41 
    43 
    42 /*
    44 /*
    43  * Copyright 2003 Wily Technology, Inc.
    45  * Copyright 2003 Wily Technology, Inc.
    44  */
    46  */
   223         mgr.setNativeMethodPrefix(transformer, prefix);
   225         mgr.setNativeMethodPrefix(transformer, prefix);
   224         String[] prefixes = mgr.getNativeMethodPrefixes();
   226         String[] prefixes = mgr.getNativeMethodPrefixes();
   225         setNativeMethodPrefixes(mNativeAgent, prefixes, mgr.isRetransformable());
   227         setNativeMethodPrefixes(mNativeAgent, prefixes, mgr.isRetransformable());
   226     }
   228     }
   227 
   229 
       
   230     @Override
       
   231     public void addModuleReads(Module module, Module other) {
       
   232         Objects.requireNonNull(module);
       
   233         Objects.requireNonNull(other);
       
   234         jdk.internal.module.Modules.addReads(module, other);
       
   235     }
       
   236 
       
   237 
   228     private TransformerManager
   238     private TransformerManager
   229     findTransformerManager(ClassFileTransformer transformer) {
   239     findTransformerManager(ClassFileTransformer transformer) {
   230         if (mTransformerManager.includesTransformer(transformer)) {
   240         if (mTransformerManager.includesTransformer(transformer)) {
   231             return mTransformerManager;
   241             return mTransformerManager;
   232         }
   242         }
   385         if (twoArgAgent) {
   395         if (twoArgAgent) {
   386             m.invoke(null, new Object[] { optionsString, this });
   396             m.invoke(null, new Object[] { optionsString, this });
   387         } else {
   397         } else {
   388             m.invoke(null, new Object[] { optionsString });
   398             m.invoke(null, new Object[] { optionsString });
   389         }
   399         }
   390 
       
   391         // don't let others access a non-public premain method
       
   392         setAccessible(m, false);
       
   393     }
   400     }
   394 
   401 
   395     // WARNING: the native code knows the name & signature of this method
   402     // WARNING: the native code knows the name & signature of this method
   396     private void
   403     private void
   397     loadClassAndCallPremain(    String  classname,
   404     loadClassAndCallPremain(    String  classname,
   412     }
   419     }
   413 
   420 
   414     // WARNING: the native code knows the name & signature of this method
   421     // WARNING: the native code knows the name & signature of this method
   415     private byte[]
   422     private byte[]
   416     transform(  ClassLoader         loader,
   423     transform(  ClassLoader         loader,
       
   424                 Module              module,
   417                 String              classname,
   425                 String              classname,
   418                 Class<?>            classBeingRedefined,
   426                 Class<?>            classBeingRedefined,
   419                 ProtectionDomain    protectionDomain,
   427                 ProtectionDomain    protectionDomain,
   420                 byte[]              classfileBuffer,
   428                 byte[]              classfileBuffer,
   421                 boolean             isRetransformer) {
   429                 boolean             isRetransformer) {
   422         TransformerManager mgr = isRetransformer?
   430         TransformerManager mgr = isRetransformer?
   423                                         mRetransfomableTransformerManager :
   431                                         mRetransfomableTransformerManager :
   424                                         mTransformerManager;
   432                                         mTransformerManager;
       
   433         // module is null when not a class load or when loading a class in an
       
   434         // unnamed module and this is the first type to be loaded in the package.
       
   435         if (module == null) {
       
   436             if (classBeingRedefined != null) {
       
   437                 module = classBeingRedefined.getModule();
       
   438             } else {
       
   439                 module = loader.getUnnamedModule();
       
   440             }
       
   441         }
   425         if (mgr == null) {
   442         if (mgr == null) {
   426             return null; // no manager, no transform
   443             return null; // no manager, no transform
   427         } else {
   444         } else {
   428             return mgr.transform(   loader,
   445             return mgr.transform(   module,
   429                                     classname,
   446                                     classname,
   430                                     classBeingRedefined,
   447                                     classBeingRedefined,
   431                                     protectionDomain,
   448                                     protectionDomain,
   432                                     classfileBuffer);
   449                                     classfileBuffer);
   433         }
   450         }