aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/couscous/util
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-07-14 15:03:53 +0100
committerMichael Williamson <mike@zwobble.org>2016-07-14 15:03:53 +0100
commit334f2173712a4a4c0d434c5f01187a1fc7a4f440 (patch)
tree52417de806c88707e08706cb3398b025a7a1157b /src/main/java/org/zwobble/couscous/util
parente4d1a3c446825a0fcfa00df4a29e66f81c56b7da (diff)
Extract common code for type descriptions
Diffstat (limited to 'src/main/java/org/zwobble/couscous/util')
-rw-r--r--src/main/java/org/zwobble/couscous/util/asm/TypeDescriptions.java45
1 files changed, 45 insertions, 0 deletions
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();
+ }
+}