Technical debt - fix SH deprecated warnings
[transportpce.git] / servicehandler / src / test / java / org / opendaylight / transportpce / servicehandler / utils / InjectField.java
1 /*
2  * Copyright © 2017 Orange, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.transportpce.servicehandler.utils;
9
10
11 /**
12  * Field Injection When Mocking Frameworks Fail.
13  *
14  * @author Martial Coulibaly ( martial.coulibaly@gfi.com ) on behalf of Orange
15  *
16  */
17 public final class InjectField {
18
19     private InjectField() {
20     }
21
22     public static void inject(final Object injectable, final String fieldname, final Object value) {
23         try {
24             final java.lang.reflect.Field field = injectable.getClass().getDeclaredField(fieldname);
25             final boolean origionalValue = field.canAccess(injectable);
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);
31         }
32     }
33 }