From 334f2173712a4a4c0d434c5f01187a1fc7a4f440 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 14 Jul 2016 15:03:53 +0100 Subject: Extract common code for type descriptions --- .../couscous/util/asm/TypeDescriptions.java | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/main/java/org/zwobble/couscous/util/asm/TypeDescriptions.java (limited to 'src/main/java/org/zwobble/couscous/util') diff --git a/src/main/java/org/zwobble/couscous/util/asm/TypeDescriptions.java b/src/main/java/org/zwobble/couscous/util/asm/TypeDescriptions.java new file mode 100644 index 0000000..e702b97 --- /dev/null +++ b/src/main/java/org/zwobble/couscous/util/asm/TypeDescriptions.java @@ -0,0 +1,45 @@ +package org.zwobble.couscous.util.asm; + +import net.bytebuddy.description.field.FieldDescription; +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.implementation.Implementation; +import net.bytebuddy.matcher.ElementMatchers; + +public class TypeDescriptions { + private TypeDescriptions() { + } + + public static FieldDescription findField(Implementation.Target target, String fieldName) { + return findField(target.getInstrumentedType(), fieldName); + } + + public static FieldDescription findField(TypeDescription typeDescription, String fieldName) { + return typeDescription + .getDeclaredFields() + .filter(ElementMatchers.named(fieldName)) + .getOnly(); + } + + public static MethodDescription findMethod(Class clazz, String name, Class... arguments) { + return findMethod(new TypeDescription.ForLoadedType(clazz), name, arguments); + } + + public static MethodDescription findMethod(TypeDescription typeDescription, String name, Class... arguments) { + return typeDescription + .getDeclaredMethods() + .filter(ElementMatchers.named(name).and(ElementMatchers.takesArguments(arguments))) + .getOnly(); + } + + public static MethodDescription findConstructor(Class clazz, Class... arguments) { + return findConstructor(new TypeDescription.ForLoadedType(clazz), arguments); + } + + public static MethodDescription findConstructor(TypeDescription typeDescription, Class... arguments) { + return typeDescription + .getDeclaredMethods() + .filter(ElementMatchers.isConstructor().and(ElementMatchers.takesArguments(arguments))) + .getOnly(); + } +} -- cgit v1.2.3