2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (c) 2007 Mockito contributors
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.provider;
26 import java.lang.reflect.Field;
28 public class Whitebox {
29 public static Object getInternalState(Object target, String field) {
30 Class<?> c = target.getClass();
32 Field f = getFieldFromHierarchy(c, field);
33 f.setAccessible(true);
35 } catch (Exception e) {
36 throw new RuntimeException("Unable to get internal state on a private field. Please report to mockito mailing list.", e);
40 public static void setInternalState(Object target, String field, Object value) {
41 Class<?> c = target.getClass();
43 Field f = getFieldFromHierarchy(c, field);
44 f.setAccessible(true);
46 } catch (Exception e) {
47 throw new RuntimeException("Unable to set internal state on a private field. Please report to mockito mailing list.", e);
51 private static Field getFieldFromHierarchy(Class<?> clazz, String field) {
52 Field f = getField(clazz, field);
53 while (f == null && clazz != Object.class) {
54 clazz = clazz.getSuperclass();
55 f = getField(clazz, field);
58 throw new RuntimeException(
59 "You want me to get this field: '" + field +
60 "' on this class: '" + clazz.getSimpleName() +
61 "' but this field is not declared withing hierarchy of this class!");
66 private static Field getField(Class<?> clazz, String field) {
68 return clazz.getDeclaredField(field);
69 } catch (NoSuchFieldException e) {