Skip to content

Commit f6f0adf

Browse files
cdupuisPhillip Webb
authored andcommitted
Allow instrumentation to be used with Attach API
Update InstrumentationSavingAgent to allow it to be used with the Attach API Issue: SPR-11083
1 parent 41cdbd2 commit f6f0adf

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,8 @@ project("spring-instrument") {
327327
jar {
328328
manifest.attributes["Premain-Class"] =
329329
"org.springframework.instrument.InstrumentationSavingAgent"
330+
manifest.attributes["Agent-Class"] =
331+
"org.springframework.instrument.InstrumentationSavingAgent"
330332
manifest.attributes["Can-Redefine-Classes"] = "true"
331333
manifest.attributes["Can-Retransform-Classes"] = "true"
332334
manifest.attributes["Can-Set-Native-Method-Prefix"] = "false"

spring-instrument/src/main/java/org/springframework/instrument/InstrumentationSavingAgent.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,6 +39,13 @@ public static void premain(String agentArgs, Instrumentation inst) {
3939
instrumentation = inst;
4040
}
4141

42+
/**
43+
* Save the {@link Instrumentation} interface exposed by the JVM.
44+
* This method is required to dynamically load this Agent with the Attach API.
45+
*/
46+
public static void agentmain(String agentArgs, Instrumentation inst) {
47+
instrumentation = inst;
48+
}
4249

4350
/**
4451
* Return the {@link Instrumentation} interface exposed by the JVM.
@@ -48,8 +55,9 @@ public static void premain(String agentArgs, Instrumentation inst) {
4855
* {@link org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver#getInstrumentation()}
4956
* instead - which will work without the agent class in the classpath as well.
5057
* @return the {@code Instrumentation} instance previously saved when
51-
* the {@link #premain} method was called by the JVM; will be {@code null}
52-
* if this class was not used as Java agent when this JVM was started.
58+
* the {@link #premain} or {@link #agentmain} methods was called by the JVM;
59+
* will be {@code null} if this class was not used as Java agent when this
60+
* JVM was started or it wasn't installed as agent using the Attach API.
5361
* @see org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver#getInstrumentation()
5462
*/
5563
public static Instrumentation getInstrumentation() {

0 commit comments

Comments
 (0)