aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.core/src/org/jacoco/core/runtime/URLStreamHandlerRuntime.java
blob: 55f9c874192b7fec7d0c0d1751e557df9104c177 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*******************************************************************************
 * Copyright (c) 2009, 2019 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.core.runtime;

import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.util.Map;

import org.jacoco.core.internal.instr.InstrSupport;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

/**
 * This {@link IRuntime} implementation registers a special
 * {@link URLStreamHandler} to process coverage data. The handler is not
 * actually used for opening a URL, but to get access to the runtime object.
 */
public class URLStreamHandlerRuntime extends AbstractRuntime {

	private static final String PROTOCOLPREFIX = "jacoco-";

	private final String protocol;

	private Map<String, URLStreamHandler> handlers;

	/**
	 * Creates a new runtime.
	 */
	public URLStreamHandlerRuntime() {
		super();
		protocol = PROTOCOLPREFIX + Integer.toHexString(hashCode());
	}

	@Override
	public void startup(final RuntimeData data) throws Exception {
		super.startup(data);
		handlers = getHandlersReference();
		handlers.put(protocol, handler);
	}

	private Map<String, URLStreamHandler> getHandlersReference()
			throws Exception {
		final Field field = URL.class.getDeclaredField("handlers");
		field.setAccessible(true);
		@SuppressWarnings("unchecked")
		final Map<String, URLStreamHandler> map = (Map<String, URLStreamHandler>) field
				.get(null);
		return map;
	}

	public void shutdown() {
		handlers.remove(protocol);
	}

	public int generateDataAccessor(final long classid, final String classname,
			final int probecount, final MethodVisitor mv) {

		// The data accessor performs the following steps:
		//
		// final URL url = new URL(protocol, null, "");
		// final URLConnection connection = url.openConnection();
		// final Object[] args = new Object[3];
		// args[0] = Long.valueOf(classid);
		// args[1] = classname;
		// args[2] = Integer.valueOf(probecount);
		// connection.equals(args);
		// final byte[] probedata = (byte[]) args[0];

		RuntimeData.generateArgumentArray(classid, classname, probecount, mv);
		mv.visitInsn(Opcodes.DUP);

		// Stack[1]: [Ljava/lang/Object;
		// Stack[0]: [Ljava/lang/Object;

		mv.visitTypeInsn(Opcodes.NEW, "java/net/URL");
		mv.visitInsn(Opcodes.DUP);
		mv.visitLdcInsn(protocol);
		mv.visitInsn(Opcodes.ACONST_NULL);
		mv.visitLdcInsn("");
		mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/net/URL", "<init>",
				"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V",
				false);

		// Stack[2]: [Ljava/net/URL;
		// Stack[1]: [Ljava/lang/Object;
		// Stack[0]: [Ljava/lang/Object;

		mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/net/URL",
				"openConnection", "()Ljava/net/URLConnection;", false);

		// Stack[2]: [Ljava/net/URLConnection;
		// Stack[1]: [Ljava/lang/Object;
		// Stack[0]: [Ljava/lang/Object;

		mv.visitInsn(Opcodes.SWAP);

		// Stack[2]: [Ljava/lang/Object;
		// Stack[1]: [Ljava/net/URLConnection;
		// Stack[0]: [Ljava/lang/Object;

		mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "equals",
				"(Ljava/lang/Object;)Z", false);

		// Stack[1]: Z;
		// Stack[0]: [Ljava/lang/Object;

		mv.visitInsn(Opcodes.POP);

		// Stack[0]: [Ljava/lang/Object;

		mv.visitInsn(Opcodes.ICONST_0);
		mv.visitInsn(Opcodes.AALOAD);
		mv.visitTypeInsn(Opcodes.CHECKCAST, InstrSupport.DATAFIELD_DESC);

		return 7;
	}

	private final URLStreamHandler handler = new URLStreamHandler() {
		@Override
		protected URLConnection openConnection(final URL u) throws IOException {
			return connection;
		}
	};

	private final URLConnection connection = new URLConnection(null) {
		@Override
		public void connect() throws IOException {
			throw new AssertionError();
		}

		@Override
		public boolean equals(final Object obj) {
			return data.equals(obj);
		}
	};

}