/** * Matches {@link MethodDescription}s that define a given type erasure as a parameter at the given index. * * @param index The index of the parameter. * @param type The erasure of the type the matched method is expected to define as a parameter type. * @param <T> The type of the matched object. * @return An element matcher that matches a given argument type for a method description. */ public static <T extends MethodDescription> ElementMatcher.Junction<T> takesArgument(int index, TypeDescription type) { return takesArgument(index, is(type)); }
/** * Ctor. */ public SmtInvokeStringBuilderAppend() { super( new TypeDescription.ForLoadedType(StringBuilder.class), new ConjunctionMatcher<>( ElementMatchers.named("append"), ElementMatchers.takesArguments(1), ElementMatchers.takesArgument(0, Object.class), ElementMatchers.returns(StringBuilder.class) ) ); } }
/** * Returns a byte buddy matcher matching any method contained in methodSignatures. */ public static ElementMatcher<MethodDescription> matchAnyMethodIn(Set<MethodSignature> methodSignatures) { ElementMatcher.Junction<MethodDescription> methodMatcher = ElementMatchers.none(); for (MethodSignature methodSignature : methodSignatures) { ElementMatcher.Junction<MethodDescription> junction = ElementMatchers .named(methodSignature.getMethodName()) .and(not(isAbstract())) .and(takesArguments(methodSignature.getParameterTypes().size())); for (int i = 0; i < methodSignature.getParameterTypes().size(); i++) { junction = junction.and(takesArgument(i, named(methodSignature.getParameterTypes().get(i)))); } methodMatcher = methodMatcher.or(junction); } return methodMatcher; }
/** * Matches {@link MethodDescription}s that define a given generic type as a parameter at the given index. * * @param index The index of the parameter. * @param type The erasure of the type the matched method is expected to define as a parameter type. * @param <T> The type of the matched object. * @return An element matcher that matches a given argument type for a method description. */ public static <T extends MethodDescription> ElementMatcher.Junction<T> takesArgument(int index, Class<?> type) { return takesArgument(index, TypeDescription.ForLoadedType.of(type)); }
List<WildcardMatcher> argumentMatchers = methodMatcher.getArgumentMatchers(); for (int i = 0; i < argumentMatchers.size(); i++) { matcher = matcher.and(takesArgument(i, matches(argumentMatchers.get(i))));
.subclass(ExecutingTransformer.class) .name(ExecutingTransformer.class.getName() + "$ByteBuddy$ModuleSupport") .method(named("transform").and(takesArgument(0, JavaType.MODULE.load()))) .intercept(MethodCall.invoke(ExecutingTransformer.class.getDeclaredMethod("transform", Object.class,
@Override public final StackManipulationToken stackManipulationToken() { return new SmtCombined( new SmtStatic( new TextConstant(", ") ), new SmtArray( new TypeDescription.ForLoadedType(CharSequence.class), List.of(type) .flatMap(TypeDescription::getDeclaredFields) .filter(f -> !f.isStatic()) .<StackManipulationToken>map(SmtAtomFieldToString::new) .prepend(new SmtAtomTypeToString(type)) .toJavaArray(i -> new StackManipulationToken[i]) ), new SmtInvokeMethod( new TypeDescription.ForLoadedType(String.class), new ConjunctionMatcher<>( ElementMatchers.named("join"), ElementMatchers.takesArguments(2), ElementMatchers.takesArgument(0, CharSequence.class), ElementMatchers.takesArgument(1, CharSequence[].class), ElementMatchers.returns(String.class) ) ) ); } }