/** * Performs the actual installation of the Byte Buddy agent. * * @param accessor An available accessor for accessing the Java attachment API. * @throws Exception If the installation is not possible. */ private static void doInstall(AttachmentProvider.Accessor accessor) throws Exception { Class<?> virtualMachine = accessor.getVirtualMachineType(); Object virtualMachineInstance = virtualMachine .getDeclaredMethod(ATTACH_METHOD_NAME, String.class) .invoke(STATIC_MEMBER, accessor.getProcessId()); File agentJar = File.createTempFile(AGENT_FILE_NAME, JAR_FILE_EXTENSION); try { saveAgentJar(agentJar); virtualMachine .getDeclaredMethod(LOAD_AGENT_METHOD_NAME, String.class, String.class) .invoke(virtualMachineInstance, agentJar.getAbsolutePath(), WITHOUT_ARGUMENTS); } finally { try { virtualMachine.getDeclaredMethod(DETACH_METHOD_NAME).invoke(virtualMachineInstance); } finally { if (!agentJar.delete()) { Logger.getAnonymousLogger().info("Cannot delete temporary file: " + agentJar); } } } }