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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.provider;
27 import java.lang.reflect.Field;
29 public class Whitebox {
30 public static Object getInternalState(Object target, String field) {
31 Class<?> c = target.getClass();
33 Field f = getFieldFromHierarchy(c, field);
34 f.setAccessible(true);
36 } catch (Exception e) {
37 throw new RuntimeException("Unable to get internal state on a private field. Please report to mockito mailing list.", e);
41 public static void setInternalState(Object target, String field, Object value) {
42 Class<?> c = target.getClass();
44 Field f = getFieldFromHierarchy(c, field);
45 f.setAccessible(true);
47 } catch (Exception e) {
48 throw new RuntimeException("Unable to set internal state on a private field. Please report to mockito mailing list.", e);
52 private static Field getFieldFromHierarchy(Class<?> clazz, String field) {
53 Field f = getField(clazz, field);
54 while (f == null && clazz != Object.class) {
55 clazz = clazz.getSuperclass();
56 f = getField(clazz, field);
59 throw new RuntimeException(
60 "You want me to get this field: '" + field +
61 "' on this class: '" + clazz.getSimpleName() +
62 "' but this field is not declared withing hierarchy of this class!");
67 private static Field getField(Class<?> clazz, String field) {
69 return clazz.getDeclaredField(field);
70 } catch (NoSuchFieldException e) {