Skip to content

Commit

Permalink
[*] Optimize ReflectUtils, Callback transform bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
i36lib committed Dec 13, 2024
1 parent ae8a68f commit 5b0ef82
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 155 deletions.
135 changes: 48 additions & 87 deletions src/main/java/io/github/artlibs/autotrace4j/context/ReflectUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,77 +15,55 @@ public final class ReflectUtils {
private ReflectUtils() {}

/**
* get public member from class or super class
* @param className class
* Get [public] methods from the object's class, or it's super class.
* @param obj object or class
* @param methodName method
* @param parameterTypes arg types
* @return method wrapper
*/
public static MethodWrapper getMethodWrapper(String className, String methodName, Class<?>... parameterTypes) {
return getMethodWrapper(className, methodName, false, parameterTypes);
public static MethodWrapper getMethod(Object obj, String methodName, Class<?>... parameterTypes) {
return MethodWrapper.of(obj, methodName, false, parameterTypes);
}

/**
* get public member from class or super class or get declared member form class
* @param className class
* @param methodName method
* @param declared declared method or not
* @param parameterTypes arg types
* @return MethodWrapper
*/
public static MethodWrapper getMethodWrapper(
String className, String methodName, boolean declared, Class<?>... parameterTypes
) {
Class<?> clazz = null;
try {
clazz = Class.forName(className, true, ClassLoader.getSystemClassLoader());
} catch (Exception e) {
e.printStackTrace();
}
return MethodWrapper.of(clazz, methodName, declared, parameterTypes);
}

/**
* get public member from object's class or super class
* @param obj instance
* Get [declared] methods of the object's class.
* @param obj object or class
* @param methodName method
* @param parameterTypes arg types
* @return method wrapper
*/
public static MethodWrapper getMethodWrapper(Object obj, String methodName, Class<?>... parameterTypes) {
return getMethodWrapper(obj, methodName, false, parameterTypes);
public static MethodWrapper getDeclaredMethod(Object obj, String methodName, Class<?>... parameterTypes) {
return MethodWrapper.of(obj, methodName, true, parameterTypes);
}

/**
* get public member from object's class or super class or get declared field form class
* @param obj instance
* @param methodName method
* @param declared declared method or not
* @param parameterTypes arg types
* @return method wrapper
* Get [public] fields from the object/class, or it's super/class.
* @param obj object or class
* @param fieldName field
* @return field
*/
public static MethodWrapper getMethodWrapper(Object obj, String methodName, boolean declared, Class<?>... parameterTypes) {
return MethodWrapper.of(obj, methodName, declared, parameterTypes);
public static Field getField(Object obj, String fieldName) {
return getField(obj, fieldName, false);
}

/**
* get public field from object's class or super class
* @param obj instance
* Get [declared] fields from the object or class.
* @param obj object or class
* @param fieldName field
* @return field
*/
public static Field getField(Object obj, String fieldName) {
return getField(obj, fieldName, false);
public static Field getDeclaredField(Object obj, String fieldName) {
return getField(obj, fieldName, true);
}

/**
* get public field from object's class or super class or get declared field form class
* @param obj instance
* Get fields from the object/class (or it's super/class).
* @param obj object or class
* @param fieldName field
* @param declared declared method or not
* @return field
*/
public static Field getField(Object obj, String fieldName, boolean declared) {
private static Field getField(Object obj, String fieldName, boolean declared) {
if (Objects.isNull(obj)) {
return null;
}
Expand All @@ -107,36 +85,38 @@ public static Field getField(Object obj, String fieldName, boolean declared) {
}

/**
* get field value
* @param obj instance
* Get [public] field value from the object/class, or it's super/class.
* @param obj object or class
* @param fieldName field
* @return result
* @param <T> field type
*/
@SuppressWarnings("unchecked")
public static <T> T getFieldValue(Object obj, String fieldName) {
Field field = getField(obj, fieldName, false);
return getFieldValue(obj, fieldName, false);
}

try {
return Objects.isNull(field) ? null : (T) field.get(obj);
} catch (Exception e) {
e.printStackTrace();
return null;
}
/**
* Get [declared] field value from the object or class.
* @param obj object or class
* @param fieldName field
* @return result
* @param <T> field type
*/
public static <T> T getDeclaredFieldValue(Object obj, String fieldName) {
return getFieldValue(obj, fieldName, true);
}

/**
* get field value
* @param obj instance
* Get field value from the object/class (or it's super/class).
* @param obj object or class
* @param fieldName field
* @param declared declared method or not
* @return result
* @param <T> field type
*/
@SuppressWarnings("unchecked")
public static <T> T getFieldValue(Object obj, String fieldName, boolean declared) {
private static <T> T getFieldValue(Object obj, String fieldName, boolean declared) {
Field field = getField(obj, fieldName, declared);

try {
return Objects.isNull(field) ? null : (T) field.get(obj);
} catch (Exception e) {
Expand All @@ -146,52 +126,33 @@ public static <T> T getFieldValue(Object obj, String fieldName, boolean declared
}

/**
* set field value
* @param className class
* Set [public] field value for the object/class (or it's super/class).
* @param obj object or class
* @param fieldName field
* @param value field value
*/
public static void setFieldValue(String className, String fieldName, Object value) {
setFieldValue(className, fieldName, value, false);
}

/**
* set field value
* @param className class
* @param fieldName field
* @param value field value
* @param declared declared method or not
*/
public static void setFieldValue(String className, String fieldName, Object value, boolean declared) {
Class<?> clazz;
try {
clazz = Class.forName(className, true, ClassLoader.getSystemClassLoader());
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
return;
}
setFieldValue(clazz, fieldName, value, declared);
public static void setFieldValue(Object obj, String fieldName, Object value) {
setFieldValue(obj, fieldName, value, false);
}

/**
* set field value
* @param obj instance
* Set [declared] field value for the object (or class).
* @param obj object or class
* @param fieldName field
* @param value field value
*/
public static void setFieldValue(Object obj, String fieldName, Object value) {
setFieldValue(obj, fieldName, value, false);
public static void setDeclaredFieldValue(Object obj, String fieldName, Object value) {
setFieldValue(obj, fieldName, value, true);
}

/**
* set field value
* @param obj instance
* Set field value for the object/class, or it's super/class.
* @param obj object or class
* @param fieldName field
* @param value field value
* @param declared declared method or not
*/
public static void setFieldValue(Object obj, String fieldName, Object value, boolean declared) {
private static void setFieldValue(Object obj, String fieldName, Object value, boolean declared) {
Field field = getField(obj, fieldName, declared);
if (Objects.isNull(field)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,20 +189,15 @@ public abstract static class AbsAnonymousInterface extends AbsConstructor {
* {@inheritDoc}
*/
@Override
protected void onMethodEnter(Object obj, Object[] allArgs, Method originMethod) throws Exception {
String traceId = ReflectUtils.getFieldValue(obj, TraceContext.TRACE_KEY);
protected void onMethodEnter(Object thiz, Object[] allArgs, Method originMethod) throws Exception {
String traceId = ReflectUtils.getDeclaredFieldValue(thiz, TraceContext.TRACE_KEY);
if (Objects.nonNull(traceId)) {
TraceContext.setTraceId(traceId);
TraceContext.setSpanId(TraceContext.generate());
}

String spanId = ReflectUtils.getFieldValue(obj, TraceContext.SPAN_KEY);
String spanId = ReflectUtils.getDeclaredFieldValue(thiz, TraceContext.SPAN_KEY);
if (Objects.nonNull(spanId)) {
TraceContext.setSpanId(spanId);
}

String parentSpanId = ReflectUtils.getFieldValue(obj, TraceContext.PARENT_SPAN_KEY);
if (Objects.nonNull(parentSpanId)) {
TraceContext.setParentSpanId(parentSpanId);
TraceContext.setParentSpanId(spanId);
}
}
}
Expand Down Expand Up @@ -257,13 +252,13 @@ protected void onMethodEnter(Object thiz, Object[] allArgs, Method originMethod)
if (isHttp) {
// first we take it from the req attributes
String traceId = ReflectUtils
.getMethodWrapper(allArgs[0], Constants.GET_ATTRIBUTE, String.class)
.getMethod(allArgs[0], Constants.GET_ATTRIBUTE, String.class)
.invoke(TraceContext.ATO_TRACE_ID);
String parentSpanId = ReflectUtils
.getMethodWrapper(allArgs[0], Constants.GET_ATTRIBUTE, String.class)
.getMethod(allArgs[0], Constants.GET_ATTRIBUTE, String.class)
.invoke(TraceContext.ATO_PARENT_SPAN_ID);
String spanId = ReflectUtils
.getMethodWrapper(allArgs[0], Constants.GET_ATTRIBUTE, String.class)
.getMethod(allArgs[0], Constants.GET_ATTRIBUTE, String.class)
.invoke(TraceContext.ATO_SPAN_ID);

if (Objects.nonNull(traceId)) {
Expand All @@ -272,10 +267,10 @@ protected void onMethodEnter(Object thiz, Object[] allArgs, Method originMethod)
TraceContext.setSpanId(spanId);
} else {
traceId = ReflectUtils
.getMethodWrapper(allArgs[0], Constants.GET_HEADER, String.class)
.getMethod(allArgs[0], Constants.GET_HEADER, String.class)
.invoke(TraceContext.ATO_TRACE_ID);
parentSpanId = ReflectUtils
.getMethodWrapper(allArgs[0], Constants.GET_HEADER, String.class)
.getMethod(allArgs[0], Constants.GET_HEADER, String.class)
.invoke(TraceContext.ATO_SPAN_ID);

if (Objects.isNull(traceId)) {
Expand All @@ -288,22 +283,22 @@ protected void onMethodEnter(Object thiz, Object[] allArgs, Method originMethod)
TraceContext.setParentSpanId(parentSpanId);

ReflectUtils
.getMethodWrapper(allArgs[0], Constants.SET_ATTRIBUTE, String.class, Object.class)
.getMethod(allArgs[0], Constants.SET_ATTRIBUTE, String.class, Object.class)
.invoke(TraceContext.ATO_TRACE_ID, traceId);
ReflectUtils
.getMethodWrapper(allArgs[0], Constants.SET_ATTRIBUTE, String.class, Object.class)
.getMethod(allArgs[0], Constants.SET_ATTRIBUTE, String.class, Object.class)
.invoke(TraceContext.ATO_PARENT_SPAN_ID, parentSpanId);
ReflectUtils
.getMethodWrapper(allArgs[0], Constants.SET_ATTRIBUTE, String.class, Object.class)
.getMethod(allArgs[0], Constants.SET_ATTRIBUTE, String.class, Object.class)
.invoke(TraceContext.ATO_SPAN_ID, spanId);
ReflectUtils
.getMethodWrapper(allArgs[1], Constants.SET_HEADER, String.class, String.class)
.getMethod(allArgs[1], Constants.SET_HEADER, String.class, String.class)
.invoke(TraceContext.ATO_TRACE_ID, traceId);
ReflectUtils
.getMethodWrapper(allArgs[1], Constants.SET_HEADER, String.class, String.class)
.getMethod(allArgs[1], Constants.SET_HEADER, String.class, String.class)
.invoke(TraceContext.ATO_PARENT_SPAN_ID, parentSpanId);
ReflectUtils
.getMethodWrapper(allArgs[1], Constants.SET_HEADER, String.class, String.class)
.getMethod(allArgs[1], Constants.SET_HEADER, String.class, String.class)
.invoke(TraceContext.ATO_SPAN_ID, spanId);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected ElementMatcher<? super MethodDescription> methodMatcher() {
protected void onMethodEnter(Object thiz, Object[] allArgs, Method originMethod) throws Exception {
final String traceId = TraceContext.getTraceId();
if (Objects.nonNull(traceId)) {
MethodWrapper setHeaderMethod = ReflectUtils.getMethodWrapper(allArgs[1]
MethodWrapper setHeaderMethod = ReflectUtils.getMethod(allArgs[1]
, Constants.SET_HEADER, String.class, String.class);
setHeaderMethod.invoke(TraceContext.ATO_TRACE_ID, traceId);
final String spanId = TraceContext.getSpanId();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.github.artlibs.autotrace4j.transformer.impl;

import io.github.artlibs.autotrace4j.context.TraceContext;
import io.github.artlibs.autotrace4j.context.MethodWrapper;
import io.github.artlibs.autotrace4j.context.ReflectUtils;
import io.github.artlibs.autotrace4j.context.TraceContext;
import io.github.artlibs.autotrace4j.transformer.abs.AbsDelegateTransformer;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
Expand Down Expand Up @@ -51,7 +51,7 @@ protected ElementMatcher<? super MethodDescription> methodMatcher() {
protected void onMethodEnter(Object thiz, Object[] allArgs, Method originMethod) throws Exception {
String traceId = TraceContext.getTraceId();
if (Objects.nonNull(traceId)) {
MethodWrapper methodWrapper = ReflectUtils.getMethodWrapper(allArgs[1]
MethodWrapper methodWrapper = ReflectUtils.getMethod(allArgs[1]
, "setAttachment", String.class, String.class);
methodWrapper.invoke(TraceContext.ATO_TRACE_ID, traceId);
final String spanId = TraceContext.getSpanId();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.github.artlibs.autotrace4j.transformer.impl;

import io.github.artlibs.autotrace4j.context.TraceContext;
import io.github.artlibs.autotrace4j.context.MethodWrapper;
import io.github.artlibs.autotrace4j.context.ReflectUtils;
import io.github.artlibs.autotrace4j.context.TraceContext;
import io.github.artlibs.autotrace4j.transformer.abs.AbsDelegateTransformer;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
Expand Down Expand Up @@ -49,7 +49,7 @@ protected ElementMatcher<? super MethodDescription> methodMatcher() {
*/
@Override
protected void onMethodEnter(Object thiz, Object[] allArgs, Method originMethod) throws Exception {
MethodWrapper methodWrapper = ReflectUtils.getMethodWrapper(allArgs[1]
MethodWrapper methodWrapper = ReflectUtils.getMethod(allArgs[1]
, "getAttachment", String.class);
final String traceId = methodWrapper.invoke(TraceContext.ATO_TRACE_ID);
if (Objects.nonNull(traceId)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ public static void adviceOnMethodEnter(
if (Objects.isNull(producerRecord) || Objects.isNull(traceId)) {
return;
}
Object headers = ReflectUtils.getMethodWrapper(producerRecord
Object headers = ReflectUtils.getMethod(producerRecord
, "headers").invoke();
if (Objects.isNull(headers)) {
return;
}
MethodWrapper method = ReflectUtils.getMethodWrapper(headers
MethodWrapper method = ReflectUtils.getMethod(headers
, "add", String.class, byte[].class);

method.invoke(TraceContext.TRACE_KEY, traceId.getBytes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@ protected MethodMatcherHolder methodMatchers() {
public static void adviceOnMethodEnter(@Advice.Argument(value = 0) Object recOrRecs) {
Object consumerRecord = recOrRecs;
if (Iterable.class.isAssignableFrom(recOrRecs.getClass())) {
Iterator<?> iterator = ReflectUtils.getMethodWrapper(recOrRecs
Iterator<?> iterator = ReflectUtils.getMethod(recOrRecs
, "iterator").invoke();
consumerRecord = ReflectUtils.getMethodWrapper(iterator
consumerRecord = ReflectUtils.getMethod(iterator
, "next").invoke();
}
Object headers = ReflectUtils.getMethodWrapper(consumerRecord
Object headers = ReflectUtils.getMethod(consumerRecord
, "headers").invoke();
MethodWrapper lastHeader = ReflectUtils.getMethodWrapper(headers
MethodWrapper lastHeader = ReflectUtils.getMethod(headers
, "lastHeader", String.class);

Object traceIdHeader = lastHeader.invoke(TraceContext.TRACE_KEY);
byte[] traceIdByte = ReflectUtils.getMethodWrapper(traceIdHeader
byte[] traceIdByte = ReflectUtils.getMethod(traceIdHeader
, "value").invoke();
Object spanIdHeader = lastHeader.invoke(TraceContext.SPAN_KEY);
byte[] spanIdByte = ReflectUtils.getMethodWrapper(spanIdHeader
byte[] spanIdByte = ReflectUtils.getMethod(spanIdHeader
, "value").invoke();

if (Objects.isNull(traceIdByte)) {
Expand Down
Loading

0 comments on commit 5b0ef82

Please sign in to comment.