2 * Copyright (c) 2019 PANTHEON.tech, s.r.o. 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.mdsal.binding.dom.codec.impl;
10 import java.lang.reflect.Field;
11 import java.lang.reflect.Method;
12 import net.bytebuddy.description.field.FieldDescription.ForLoadedField;
13 import net.bytebuddy.description.method.MethodDescription.ForLoadedMethod;
14 import net.bytebuddy.description.type.TypeDescription;
15 import net.bytebuddy.implementation.bytecode.StackManipulation;
16 import net.bytebuddy.implementation.bytecode.member.FieldAccess;
17 import net.bytebuddy.implementation.bytecode.member.FieldAccess.Defined;
18 import net.bytebuddy.implementation.bytecode.member.MethodInvocation;
19 import net.bytebuddy.matcher.ElementMatchers;
21 final class ByteBuddyUtils {
22 private ByteBuddyUtils() {
26 static StackManipulation invokeMethod(final Method method) {
27 return MethodInvocation.invoke(describe(method));
30 static StackManipulation invokeMethod(final Class<?> clazz, final String name, final Class<?>... args) {
31 return MethodInvocation.invoke(describe(clazz, name, args));
34 static StackManipulation getField(final Field field) {
35 return FieldAccess.forField(new ForLoadedField(field).asDefined()).read();
38 static StackManipulation getField(final TypeDescription instrumentedType, final String fieldName) {
39 return fieldAccess(instrumentedType, fieldName).read();
42 static StackManipulation putField(final TypeDescription instrumentedType, final String fieldName) {
43 return fieldAccess(instrumentedType, fieldName).write();
46 private static ForLoadedMethod describe(final Method method) {
47 return new ForLoadedMethod(method);
50 private static ForLoadedMethod describe(final Class<?> clazz, final String name, final Class<?>... args) {
51 return describe(getMethod(clazz, name, args));
54 private static Defined fieldAccess(final TypeDescription instrumentedType, final String fieldName) {
55 return FieldAccess.forField(instrumentedType.getDeclaredFields().filter(ElementMatchers.named(fieldName))
59 private static Method getMethod(final Class<?> clazz, final String name, final Class<?>... args) {
61 return clazz.getDeclaredMethod(name, args);
62 } catch (NoSuchMethodException e) {
63 throw new IllegalStateException(e);