aboutsummaryrefslogtreecommitdiffstats
path: root/org.jacoco.cli/src/org/jacoco/cli/internal/commands/Instrument.java
blob: 05c4c7c2d95163c248f53d67c7120b9c076066a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*******************************************************************************
 * Copyright (c) 2009, 2018 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:
 *    John Keeping - initial implementation
 *    Marc R. Hoffmann - rework
 *
 *******************************************************************************/
package org.jacoco.cli.internal.commands;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import org.jacoco.cli.internal.Command;
import org.jacoco.core.instr.Instrumenter;
import org.jacoco.core.runtime.OfflineInstrumentationAccessGenerator;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;

/**
 * The <code>instrument</code> command.
 */
public class Instrument extends Command {

	@Option(name = "--dest", usage = "path to write instrumented Java classes to", metaVar = "<dir>", required = true)
	File dest;

	@Argument(usage = "list of folder or files to instrument recusively", metaVar = "<sourcefiles>")
	List<File> source = new ArrayList<File>();

	private Instrumenter instrumenter;

	@Override
	public String description() {
		return "Off-line instrumentation of Java class files and JAR files.";
	}

	@Override
	public int execute(final PrintWriter out, final PrintWriter err)
			throws IOException {
		final File absoluteDest = dest.getAbsoluteFile();
		instrumenter = new Instrumenter(
				new OfflineInstrumentationAccessGenerator());
		int total = 0;
		for (final File s : source) {
			if (s.isFile()) {
				total += instrument(s, new File(absoluteDest, s.getName()));
			} else {
				total += instrumentRecursive(s, absoluteDest);
			}
		}
		out.printf("[INFO] %s classes instrumented to %s.%n",
				Integer.valueOf(total), absoluteDest);
		return 0;
	}

	private int instrumentRecursive(final File src, final File dest)
			throws IOException {
		int total = 0;
		if (src.isDirectory()) {
			for (final File child : src.listFiles()) {
				total += instrumentRecursive(child,
						new File(dest, child.getName()));
			}
		} else {
			total += instrument(src, dest);
		}
		return total;
	}

	private int instrument(final File src, final File dest) throws IOException {
		dest.getParentFile().mkdirs();
		final InputStream input = new FileInputStream(src);
		try {
			final OutputStream output = new FileOutputStream(dest);
			try {
				return instrumenter.instrumentAll(input, output,
						src.getAbsolutePath());
			} finally {
				output.close();
			}
		} catch (final IOException e) {
			dest.delete();
			throw e;
		} finally {
			input.close();
		}
	}

}