2 * Copyright © 2017 Orange, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.transportpce.servicehandler.utils;
12 * Field Injection When Mocking Frameworks Fail.
14 * @author Martial Coulibaly ( martial.coulibaly@gfi.com ) on behalf of Orange
17 public final class InjectField {
19 private InjectField() {
22 public static void inject(final Object injectable, final String fieldname, final Object value) {
24 final java.lang.reflect.Field field = injectable.getClass().getDeclaredField(fieldname);
25 final boolean origionalValue = field.isAccessible();
26 field.setAccessible(true);
27 field.set(injectable, value);
28 field.setAccessible(origionalValue);
29 } catch (final NoSuchFieldException | IllegalAccessException e) {
30 throw new RuntimeException(e.getMessage(), e);