Bump versions to 14.0.0-SNAPSHOT
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / ByteBuddyUtils.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o. 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.mdsal.binding.dom.codec.impl;
9
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;
20
21 final class ByteBuddyUtils {
22     private ByteBuddyUtils() {
23         // Hidden on purpose
24     }
25
26     static StackManipulation invokeMethod(final Method method) {
27         return MethodInvocation.invoke(describe(method));
28     }
29
30     static StackManipulation invokeMethod(final Class<?> clazz, final String name, final Class<?>... args) {
31         return MethodInvocation.invoke(describe(clazz, name, args));
32     }
33
34     static StackManipulation getField(final Field field) {
35         return FieldAccess.forField(new ForLoadedField(field).asDefined()).read();
36     }
37
38     static StackManipulation getField(final TypeDescription instrumentedType, final String fieldName) {
39         return fieldAccess(instrumentedType, fieldName).read();
40     }
41
42     static StackManipulation putField(final TypeDescription instrumentedType, final String fieldName) {
43         return fieldAccess(instrumentedType, fieldName).write();
44     }
45
46     private static ForLoadedMethod describe(final Method method) {
47         return new ForLoadedMethod(method);
48     }
49
50     private static ForLoadedMethod describe(final Class<?> clazz, final String name, final Class<?>... args) {
51         return describe(getMethod(clazz, name, args));
52     }
53
54     private static Defined fieldAccess(final TypeDescription instrumentedType, final String fieldName) {
55         return FieldAccess.forField(instrumentedType.getDeclaredFields().filter(ElementMatchers.named(fieldName))
56             .getOnly());
57     }
58
59     private static Method getMethod(final Class<?> clazz, final String name, final Class<?>... args) {
60         try {
61             return clazz.getDeclaredMethod(name, args);
62         } catch (NoSuchMethodException e) {
63             throw new IllegalStateException(e);
64         }
65     }
66 }