|
1 |
| -/******************************************************************************* |
2 |
| - * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors |
3 |
| - * This program and the accompanying materials are made available under |
4 |
| - * the terms of the Eclipse Public License 2.0 which is available at |
5 |
| - * http://www.eclipse.org/legal/epl-2.0 |
6 |
| - * |
7 |
| - * SPDX-License-Identifier: EPL-2.0 |
8 |
| - * |
9 |
| - * Contributors: |
10 |
| - * Marc R. Hoffmann - initial API and implementation |
11 |
| - * |
12 |
| - *******************************************************************************/ |
13 |
| -package org.jacoco.core.test.validation; |
14 |
| - |
15 |
| -import java.lang.reflect.InvocationTargetException; |
16 |
| - |
17 |
| -/** |
18 |
| - * Executes statements against a given Java object instance. |
19 |
| - */ |
20 |
| -class StatementExecutor implements StatementParser.IStatementVisitor { |
21 |
| - |
22 |
| - private final Object target; |
23 |
| - private final Object[] prefixArgs; |
24 |
| - |
25 |
| - StatementExecutor(Object target, Object... prefixArgs) { |
26 |
| - this.target = target; |
27 |
| - this.prefixArgs = prefixArgs; |
28 |
| - } |
29 |
| - |
30 |
| - public void visitInvocation(String ctx, String name, Object... args) { |
31 |
| - args = concat(prefixArgs, args); |
32 |
| - try { |
33 |
| - target.getClass().getMethod(name, getTypes(args)).invoke(target, |
34 |
| - args); |
35 |
| - } catch (InvocationTargetException e) { |
36 |
| - Throwable te = e.getTargetException(); |
37 |
| - if (te instanceof AssertionError) { |
38 |
| - throw (AssertionError) te; |
39 |
| - } |
40 |
| - throw new RuntimeException("Invocation error (" + ctx + ")", te); |
41 |
| - } catch (Exception e) { |
42 |
| - throw new RuntimeException("Invocation error (" + ctx + ")", e); |
43 |
| - } |
44 |
| - } |
45 |
| - |
46 |
| - private static Object[] concat(Object[] a, Object[] b) { |
47 |
| - final Object[] result = new Object[a.length + b.length]; |
48 |
| - System.arraycopy(a, 0, result, 0, a.length); |
49 |
| - System.arraycopy(b, 0, result, a.length, b.length); |
50 |
| - return result; |
51 |
| - } |
52 |
| - |
53 |
| - private static Class<?>[] getTypes(Object[] instances) { |
54 |
| - final Class<?>[] classes = new Class[instances.length]; |
55 |
| - for (int i = 0; i < instances.length; i++) { |
56 |
| - Class<? extends Object> c = instances[i].getClass(); |
57 |
| - if (c == Integer.class) { |
58 |
| - // We always use primitive int parameters: |
59 |
| - c = Integer.TYPE; |
60 |
| - } |
61 |
| - classes[i] = c; |
62 |
| - } |
63 |
| - return classes; |
64 |
| - } |
65 |
| - |
66 |
| -} |
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2009, 2019 Mountainminds GmbH & Co. KG and Contributors |
| 3 | + * This program and the accompanying materials are made available under |
| 4 | + * the terms of the Eclipse Public License 2.0 which is available at |
| 5 | + * http://www.eclipse.org/legal/epl-2.0 |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: EPL-2.0 |
| 8 | + * |
| 9 | + * Contributors: |
| 10 | + * Marc R. Hoffmann - initial API and implementation |
| 11 | + * |
| 12 | + *******************************************************************************/ |
| 13 | +package org.jacoco.core.test.validation; |
| 14 | + |
| 15 | +import java.lang.reflect.InvocationTargetException; |
| 16 | + |
| 17 | +/** |
| 18 | + * Executes statements against a given Java object instance. |
| 19 | + */ |
| 20 | +class StatementExecutor implements StatementParser.IStatementVisitor { |
| 21 | + |
| 22 | + private final Object target; |
| 23 | + private final Object[] prefixArgs; |
| 24 | + |
| 25 | + StatementExecutor(Object target, Object... prefixArgs) { |
| 26 | + this.target = target; |
| 27 | + this.prefixArgs = prefixArgs; |
| 28 | + } |
| 29 | + |
| 30 | + public void visitInvocation(String ctx, String name, Object... args) { |
| 31 | + args = concat(prefixArgs, args); |
| 32 | + try { |
| 33 | + target.getClass().getMethod(name, getTypes(args)).invoke(target, |
| 34 | + args); |
| 35 | + } catch (InvocationTargetException e) { |
| 36 | + Throwable te = e.getTargetException(); |
| 37 | + if (te instanceof AssertionError) { |
| 38 | + throw (AssertionError) te; |
| 39 | + } |
| 40 | + throw new RuntimeException("Invocation error (" + ctx + ")", te); |
| 41 | + } catch (Exception e) { |
| 42 | + throw new RuntimeException("Invocation error (" + ctx + ")", e); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private static Object[] concat(Object[] a, Object[] b) { |
| 47 | + final Object[] result = new Object[a.length + b.length]; |
| 48 | + System.arraycopy(a, 0, result, 0, a.length); |
| 49 | + System.arraycopy(b, 0, result, a.length, b.length); |
| 50 | + return result; |
| 51 | + } |
| 52 | + |
| 53 | + private static Class<?>[] getTypes(Object[] instances) { |
| 54 | + final Class<?>[] classes = new Class[instances.length]; |
| 55 | + for (int i = 0; i < instances.length; i++) { |
| 56 | + Class<? extends Object> c = instances[i].getClass(); |
| 57 | + if (c == Integer.class) { |
| 58 | + // We always use primitive int parameters: |
| 59 | + c = Integer.TYPE; |
| 60 | + } |
| 61 | + classes[i] = c; |
| 62 | + } |
| 63 | + return classes; |
| 64 | + } |
| 65 | + |
| 66 | +} |
0 commit comments