You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
privateInvokercreateInvoker() {
// getMethod() return java.lang.reflect.Method MethodCall.WithoutSpecifiedTargetwithoutSpecifiedTarget = MethodCall.invoke(getMethod());
MethodCallmethodCall;
// method is static or notif(!isStatic){
methodCall = withoutSpecifiedTarget.onArgument(0);
} else {
methodCall = withoutSpecifiedTarget;
}
// 参数数量对齐Type[] types = getParamTypes();
if(types.length > 0){
int[] indexes = newint[types.length];
for (inti = 0; i < types.length; i++) {
indexes[i] = i + 1;
}
methodCall = methodCall.withArgument(indexes);
}
Implementation.Composablecomposable;
// I have some question on this my interface(Invoker) is (Object,Object[])// but the target method ( result of getMethod() ) params is dynamic how can I make Object[] to (arg0,arg1,gar2,arg3...) like this// I need a new Assigner or use other way// 有需要传递参数那就添加参数类型转换器// isVoid is said the method return type is void or notif(!isStatic || !isVoid){
Assignerassigner = newCusPrimitiveTypeAwareAssigner(ReferenceTypeAwareAssigner.INSTANCE);
composable = methodCall.withAssigner(assigner, Assigner.Typing.DYNAMIC);
} else {
composable = methodCall;
}
Implementationimplementation;
if(isVoid){
implementation = composable.andThen(FixedValue.nullValue());
} else {
implementation = composable;
}
DynamicType.Unloaded<Invoker> invokerUnloaded = newByteBuddy()
.subclass(Invoker.class)
.method(named("invoke"))
.intercept(implementation)
.make();
DynamicType.Loaded<Invoker> load = invokerUnloaded.load(this.getClass().getClassLoader());
try {
Invokerinstance = load.getLoaded().newInstance();
this.invokerClassBytes = invokerUnloaded.getBytes();
returninstance;
} catch (Exceptione) {
thrownewHutoolException(e);
}
}
this is CusPrimitiveTypeAwareAssigner code just ignore void
@HashCodeAndEqualsPlugin.EnhancepublicclassCusPrimitiveTypeAwareAssignerimplementsAssigner {
/** * Another assigner that is aware of assigning reference types. This assigner is queried for assigning * non-primitive types or for assigning a boxed type to another non-primitive type. */privatefinalAssignerreferenceTypeAwareAssigner;
/** * Creates a new assigner with the given delegate. * * @param referenceTypeAwareAssigner A chained assigner that is queried for assignments not involving primitive * types. */publicCusPrimitiveTypeAwareAssigner(AssignerreferenceTypeAwareAssigner) {
this.referenceTypeAwareAssigner = referenceTypeAwareAssigner;
}
/** * {@inheritDoc} */publicStackManipulationassign(TypeDescription.Genericsource, TypeDescription.Generictarget, Assigner.Typingtyping) {
// void类型不进行转换if(source.represents(void.class) || target.represents(void.class)){
returnreferenceTypeAwareAssigner.assign(source, target, typing);
}
if (source.isPrimitive() && target.isPrimitive()) {
returnPrimitiveWideningDelegate.forPrimitive(source).widenTo(target);
} elseif (source.isPrimitive() /* && !target.isPrimitive() */) {
returnPrimitiveBoxingDelegate.forPrimitive(source).assignBoxedTo(target, referenceTypeAwareAssigner, typing);
} elseif (/* !source.isPrimitive() && */target.isPrimitive()) {
returnPrimitiveUnboxingDelegate.forReferenceType(source).assignUnboxedTo(target, referenceTypeAwareAssigner, typing);
} else/* !source.isPrimitive() && !target.isPrimitive()) */ {
returnreferenceTypeAwareAssigner.assign(source, target, typing);
}
}
}
The text was updated successfully, but these errors were encountered:
You will need to wrap the arguments into an array explicitly. Varargs are an instruction to the Java compiler, not to the runtime, so it will not work to just assign.
MethodCall can already assign an array of all arguments using withArgumentArray. Is this what you are looking for?
i want to use byte-buddy to implements a Invoker can be used like jdk reflect method.invoke
I want to make Object[] -> arg0,arg1,arg2,arg3... dynamic bind to a Method
maybe I use a bad way , maybe you can give me some examples
this is a interface Invoker
this is invokerCreate method this is a method
this is CusPrimitiveTypeAwareAssigner code just ignore void
The text was updated successfully, but these errors were encountered: