diff options
author | Marc R. Hoffmann <hoffmann@mountainminds.com> | 2013-01-07 07:21:24 +0100 |
---|---|---|
committer | Marc R. Hoffmann <hoffmann@mountainminds.com> | 2013-01-07 07:21:24 +0100 |
commit | 2a8b5339c422de268cae3a862a85666a9b0ac176 (patch) | |
tree | c1a54ef2ec137a268d30431b2f6648a5d921cc9f /org.jacoco.agent.rt/src | |
parent | ae1034c608eeca9765a43bec34bcb8e5bf23eaff (diff) | |
download | jacoco-2a8b5339c422de268cae3a862a85666a9b0ac176.tar.gz |
Add runtime APIs.
Diffstat (limited to 'org.jacoco.agent.rt/src')
14 files changed, 276 insertions, 15 deletions
diff --git a/org.jacoco.agent.rt/src/com/vladium/emma/rt/RT.java b/org.jacoco.agent.rt/src/com/vladium/emma/rt/RT.java new file mode 100644 index 00000000..2cdb8136 --- /dev/null +++ b/org.jacoco.agent.rt/src/com/vladium/emma/rt/RT.java @@ -0,0 +1,64 @@ +/******************************************************************************* + * Copyright (c) 2009, 2013 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package com.vladium.emma.rt; + +import java.io.File; + +/** + * Compatibility layer for the EMMA runtime which allows to trigger dumps + * through EMMA APIs. + * + * @deprecated Use {@link org.jacoco.agent.rt.IAgent} instead. + */ +@Deprecated +public final class RT { + + private RT() { + } + + /** + * Triggers an execution data dump for the configured output without + * performing a reset. + * + * @param outFile + * ignored + * @param merge + * ignored + * @param stopDataCollection + * ignored + * @throws Exception + * @see org.jacoco.agent.rt.IAgent#dump(boolean) + */ + @SuppressWarnings("unused") + public static void dumpCoverageData(final File outFile, + final boolean merge, final boolean stopDataCollection) + throws Exception { + org.jacoco.agent.rt.RT.getAgent().dump(false); + } + + /** + * Triggers an execution data dump for the configured output without + * performing a reset. + * + * @param outFile + * ignored + * @param stopDataCollection + * ignored + * @throws Exception + * @see org.jacoco.agent.rt.IAgent#dump(boolean) + */ + public static synchronized void dumpCoverageData(final File outFile, + final boolean stopDataCollection) throws Exception { + dumpCoverageData(outFile, true, stopDataCollection); + } + +} diff --git a/org.jacoco.agent.rt/src/com/vladium/emma/rt/package-info.java b/org.jacoco.agent.rt/src/com/vladium/emma/rt/package-info.java new file mode 100644 index 00000000..6fb8b0db --- /dev/null +++ b/org.jacoco.agent.rt/src/com/vladium/emma/rt/package-info.java @@ -0,0 +1,16 @@ +/******************************************************************************* + * Copyright (c) 2009, 2013 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ + +/** + * Compatibility API for EMMA runtime. + */ +package com.vladium.emma.rt;
\ No newline at end of file diff --git a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/IAgent.java b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/IAgent.java new file mode 100644 index 00000000..ac79e65c --- /dev/null +++ b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/IAgent.java @@ -0,0 +1,70 @@ +/******************************************************************************* + * Copyright (c) 2009, 2013 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Evgeny Mandrikov - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.agent.rt; + +import java.io.IOException; + +/** + * Runtime API and MBean agent interface. + */ +public interface IAgent { + + /** + * Returns version of JaCoCo. + * + * @return version of JaCoCo + */ + String getVersion(); + + /** + * Returns current a session identifier. + * + * @return current session identifier + */ + String getSessionId(); + + /** + * Sets a session identifier. + * + * @param id + * new session identifier + */ + void setSessionId(String id); + + /** + * Resets all coverage information. + */ + void reset(); + + /** + * Returns current execution data. + * + * @param reset + * if <code>true</code> the current execution data is cleared + * afterwards + * @return dump of current execution data in JaCoCo binary format + */ + byte[] getExecutionData(boolean reset); + + /** + * Triggers a dump of the current execution data through the configured + * output. + * + * @param reset + * if <code>true</code> the current execution data is cleared + * afterwards + * @throws IOException + * if the output can't write execution data + */ + void dump(boolean reset) throws IOException; + +} diff --git a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/RT.java b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/RT.java new file mode 100644 index 00000000..5c6d2c50 --- /dev/null +++ b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/RT.java @@ -0,0 +1,35 @@ +/******************************************************************************* + * Copyright (c) 2009, 2013 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ +package org.jacoco.agent.rt; + +import org.jacoco.agent.rt.internal.Agent; + +/** + * Entry point to access the JaCoCo agent runtime. + */ +public final class RT { + + private RT() { + } + + /** + * Returns the agent instance of the JaCoCo runtime in this JVM. + * + * @return agent instance + * @throws IllegalStateException + * if no Agent has been started yet + */ + public static IAgent getAgent() throws IllegalStateException { + return Agent.getInstance(); + } + +} diff --git a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/Agent.java b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/Agent.java index b45f29d3..f726e0ca 100644 --- a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/Agent.java +++ b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/Agent.java @@ -11,14 +11,19 @@ *******************************************************************************/ package org.jacoco.agent.rt.internal; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; +import org.jacoco.agent.rt.IAgent; import org.jacoco.agent.rt.internal.controller.IAgentController; import org.jacoco.agent.rt.internal.controller.LocalController; import org.jacoco.agent.rt.internal.controller.MBeanController; import org.jacoco.agent.rt.internal.controller.TcpClientController; import org.jacoco.agent.rt.internal.controller.TcpServerController; +import org.jacoco.core.JaCoCo; +import org.jacoco.core.data.ExecutionDataWriter; import org.jacoco.core.runtime.AbstractRuntime; import org.jacoco.core.runtime.AgentOptions; import org.jacoco.core.runtime.AgentOptions.OutputMode; @@ -27,7 +32,7 @@ import org.jacoco.core.runtime.RuntimeData; /** * The agent manages the life cycle of JaCoCo runtime. */ -public class Agent { +public class Agent implements IAgent { private static Agent singleton; @@ -54,6 +59,21 @@ public class Agent { return singleton; } + /** + * Returns a global instance which is already started. If a agent has not + * been initialized before this method will fail. + * + * @return global instance + * @throws IllegalStateException + * if no Agent has been started yet + */ + public static synchronized Agent getInstance() throws IllegalStateException { + if (singleton == null) { + throw new IllegalStateException("JaCoCo agent not started."); + } + return singleton; + } + private final AgentOptions options; private final IExceptionLogger logger; @@ -109,7 +129,7 @@ public class Agent { public void shutdown() { try { if (options.getDumpOnExit()) { - controller.writeExecutionData(); + controller.writeExecutionData(false); } controller.shutdown(); } catch (final Exception e) { @@ -148,4 +168,38 @@ public class Agent { return host + "-" + AbstractRuntime.createRandomId(); } + // === IAgent Implementation === + + public String getVersion() { + return JaCoCo.VERSION; + } + + public String getSessionId() { + return data.getSessionId(); + } + + public void setSessionId(final String id) { + data.setSessionId(id); + } + + public void reset() { + data.reset(); + } + + public byte[] getExecutionData(final boolean reset) { + final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + try { + final ExecutionDataWriter writer = new ExecutionDataWriter(buffer); + data.collect(writer, writer, reset); + } catch (final IOException e) { + // Must not happen with ByteArrayOutputStream + throw new AssertionError(e); + } + return buffer.toByteArray(); + } + + public void dump(final boolean reset) throws IOException { + controller.writeExecutionData(reset); + } + } diff --git a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/ConfigLoader.java b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/ConfigLoader.java index 52c97d61..19dabb86 100644 --- a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/ConfigLoader.java +++ b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/ConfigLoader.java @@ -30,7 +30,7 @@ final class ConfigLoader { final Properties result = new Properties(); // 1. Try to load resource - final InputStream file = RT.class.getResourceAsStream(resource); + final InputStream file = Offline.class.getResourceAsStream(resource); if (file != null) { try { result.load(file); diff --git a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/RT.java b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/Offline.java index 3be8572a..21541a85 100644 --- a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/RT.java +++ b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/Offline.java @@ -20,7 +20,7 @@ import org.jacoco.core.runtime.RuntimeData; * The API for classes instrumented in "offline" mode. The agent configuration * is provided through system properties prefixed with <code>jacoco.</code>. */ -public class RT { +public class Offline { private static final RuntimeData data; private static final String CONFIG_RESOURCE = "/jacoco-agent.properties"; diff --git a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/controller/IAgentController.java b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/controller/IAgentController.java index bee143b8..cd2be817 100644 --- a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/controller/IAgentController.java +++ b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/controller/IAgentController.java @@ -11,6 +11,8 @@ *******************************************************************************/ package org.jacoco.agent.rt.internal.controller; +import java.io.IOException; + import org.jacoco.core.runtime.AgentOptions; import org.jacoco.core.runtime.RuntimeData; @@ -46,9 +48,11 @@ public interface IAgentController { * Write all execution data in the runtime to a location determined by the * agent controller. This method should only be called by the Agent * - * @throws Exception + * @param reset + * if <code>true</code> execution data is cleared afterwards + * @throws IOException * in case writing fails */ - public void writeExecutionData() throws Exception; + public void writeExecutionData(boolean reset) throws IOException; } diff --git a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/controller/LocalController.java b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/controller/LocalController.java index f43bab27..c6ced00c 100644 --- a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/controller/LocalController.java +++ b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/controller/LocalController.java @@ -47,9 +47,9 @@ public class LocalController implements IAgentController { options.getAppend())); } - public void writeExecutionData() throws IOException { + public void writeExecutionData(final boolean reset) throws IOException { final ExecutionDataWriter writer = new ExecutionDataWriter(output); - data.collect(writer, writer, false); + data.collect(writer, writer, reset); } public void shutdown() throws IOException { diff --git a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/controller/MBeanController.java b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/controller/MBeanController.java index a2f16e0d..2b3b9cb0 100644 --- a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/controller/MBeanController.java +++ b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/controller/MBeanController.java @@ -45,7 +45,7 @@ public class MBeanController implements IAgentController, IRuntimeMBean { new ObjectName(OBJECT_NAME)); } - public void writeExecutionData() throws Exception { + public void writeExecutionData(final boolean reset) { // nothing to do } diff --git a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/controller/TcpClientController.java b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/controller/TcpClientController.java index 79514391..72c0b8fc 100644 --- a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/controller/TcpClientController.java +++ b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/controller/TcpClientController.java @@ -68,8 +68,8 @@ public class TcpClientController implements IAgentController { worker.join(); } - public void writeExecutionData() throws IOException { - connection.writeExecutionData(); + public void writeExecutionData(final boolean reset) throws IOException { + connection.writeExecutionData(reset); } /** diff --git a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/controller/TcpConnection.java b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/controller/TcpConnection.java index f8f1e8b8..5c29483e 100644 --- a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/controller/TcpConnection.java +++ b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/controller/TcpConnection.java @@ -73,11 +73,13 @@ class TcpConnection implements IRemoteCommandVisitor { * Dumps the current execution data if the connection is already initialized * and the underlying socket is still open. * + * @param reset + * if <code>true</code> execution data is cleared afterwards * @throws IOException */ - public void writeExecutionData() throws IOException { + public void writeExecutionData(final boolean reset) throws IOException { if (initialized && !socket.isClosed()) { - visitDumpCommand(true, false); + visitDumpCommand(true, reset); } } diff --git a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/controller/TcpServerController.java b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/controller/TcpServerController.java index f9c588de..8d24e58f 100644 --- a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/controller/TcpServerController.java +++ b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/controller/TcpServerController.java @@ -86,9 +86,9 @@ public class TcpServerController implements IAgentController { worker.join(); } - public void writeExecutionData() throws IOException { + public void writeExecutionData(final boolean reset) throws IOException { if (connection != null) { - connection.writeExecutionData(); + connection.writeExecutionData(reset); } } diff --git a/org.jacoco.agent.rt/src/org/jacoco/agent/rt/package-info.java b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/package-info.java new file mode 100644 index 00000000..09d27e0d --- /dev/null +++ b/org.jacoco.agent.rt/src/org/jacoco/agent/rt/package-info.java @@ -0,0 +1,16 @@ +/******************************************************************************* + * Copyright (c) 2009, 2013 Mountainminds GmbH & Co. KG and Contributors + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Marc R. Hoffmann - initial API and implementation + * + *******************************************************************************/ + +/** + * API to access the JaCoCo agent from within the JVM under test. + */ +package org.jacoco.agent.rt;
\ No newline at end of file |