/** * Creates a new {@code toString} implementation. * * @param prefixResolver A resolver for the prefix of a {@link String} representation. */ protected ToStringMethod(PrefixResolver prefixResolver) { this(prefixResolver, "{", "}", ", ", "=", none()); }
protected ElementMatcher.Junction<TypeDescription> getExtraIncludeTypeMatcher() { return none(); }
protected ElementMatcher.Junction<TypeDescription> getExtraExcludeTypeMatcher() { return none(); }
/** * Matches exactly the extension {@link java.lang.ClassLoader}. The returned matcher is a synonym to * a matcher matching {@code ClassLoader.gerSystemClassLoader().getParent()}. * * @param <T> The type of the matched object. * @return A matcher that only matches the extension class loader. */ public static <T extends ClassLoader> ElementMatcher.Junction<T> isExtensionClassLoader() { ClassLoader classLoader = ClassLoader.getSystemClassLoader().getParent(); return classLoader == null // Check if VM supports the extension class loader. ? ElementMatchers.<T>none() : new EqualityMatcher<T>(classLoader); }
/** * Creates a strict type reference adjustment that throws an exception if a type reference cannot be resolved * in the supplied type pool. * * @return A strict type reference adjustment. */ public static TypeReferenceAdjustment strict() { return new TypeReferenceAdjustment(true, none()); }
/** * Creates a strict type reference adjustment that ignores type references that cannot be resolved * in the supplied type pool. * * @return A relaxed type reference adjustment. */ public static TypeReferenceAdjustment relaxed() { return new TypeReferenceAdjustment(false, none()); }
/** * Creates a factory that only matches field access for given access types. * * @param matcher The matcher to identify fields for substitution. * @param matchFieldRead {@code true} if field read access should be matched. * @param matchFieldWrite {@code true} if field write access should be matched. * @param factory The substitution factory to apply for fields that match the specified criteria. * @return An appropriate replacement factory. */ protected static Replacement.Factory ofField(ElementMatcher<? super FieldDescription.InDefinedShape> matcher, boolean matchFieldRead, boolean matchFieldWrite, Substitution.Factory factory) { return new Factory(matcher, none(), matchFieldRead, matchFieldWrite, false, false, factory); }
/** * Creates a factory that only matches method and constructor invocations for given invocation types. * * @param matcher The matcher to identify methods and constructors for substitution. * @param includeVirtualCalls {@code true} if virtual method calls should be matched. * @param includeSuperCalls {@code true} if super method calls should be matched. * @param factory The substitution factory to apply for methods and constructors that match the specified criteria. * @return An appropriate replacement factory. */ protected static Replacement.Factory ofMethod(ElementMatcher<? super MethodDescription> matcher, boolean includeVirtualCalls, boolean includeSuperCalls, Substitution.Factory factory) { return new Factory(none(), matcher, false, false, includeVirtualCalls, includeSuperCalls, factory); }
/** * Creates a new default plugin engine. * * @param byteBuddy The Byte Buddy instance to use. * @param typeStrategy The type strategy to use. */ protected Default(ByteBuddy byteBuddy, TypeStrategy typeStrategy) { this(byteBuddy, typeStrategy, PoolStrategy.Default.FAST, ClassFileLocator.NoOp.INSTANCE, Listener.NoOp.INSTANCE, new ErrorHandler.Compound(ErrorHandler.Failing.FAIL_FAST, ErrorHandler.Enforcing.ALL_TYPES_RESOLVED, ErrorHandler.Enforcing.NO_LIVE_INITIALIZERS), none()); }
/** * {@inheritDoc} */ public ElementMatcher<? super S> resolve(TypeDescription typeDescription) { ElementMatcher.Junction<S> matcher = none(); for (LatentMatcher<? super S> latentMatcher : matchers) { matcher = matcher.or(latentMatcher.resolve(typeDescription)); } return matcher; } }
private ElementMatcher.Junction<TypeDescription> makeSureClassesAreNotProfiledTwice() { return isSubclass() ? isInsideMonitoredProject() : ElementMatchers.<TypeDescription>none(); }
/** * <p> * Creates a matcher that matches any of the given objects by the {@link java.lang.Object#equals(Object)} method. * None of the values must be {@code null}. * </p> * <p> * <b>Important</b>: This method cannot be used interchangeably with any of the overloaded versions of {@link ElementMatchers#anyOf(Object...)} * which also apply a type conversion. * </p> * * @param values The input values to be compared against. * @param <T> The type of the matched object. * @return A matcher that checks for the equality with any of the given objects. */ public static <T> ElementMatcher.Junction<T> anyOf(Iterable<?> values) { ElementMatcher.Junction<T> matcher = null; for (Object value : values) { matcher = matcher == null ? ElementMatchers.<T>is(value) : matcher.or(is(value)); } return matcher == null ? ElementMatchers.<T>none() : matcher; }
/** * {@inheritDoc} */ public MethodDescription transform(TypeDescription instrumentedType, MethodDescription methodDescription) { return new TransformedMethod(instrumentedType, methodDescription.getDeclaringType(), transformer.transform(instrumentedType, methodDescription.asToken(none())), methodDescription.asDefined()); }
/** * {@inheritDoc} */ public FieldDescription transform(TypeDescription instrumentedType, FieldDescription fieldDescription) { return new TransformedField(instrumentedType, fieldDescription.getDeclaringType(), transformer.transform(instrumentedType, fieldDescription.asToken(none())), fieldDescription.asDefined()); }
/** * {@inheritDoc} */ public DynamicType make(String auxiliaryTypeName, ClassFileVersion classFileVersion, MethodAccessorFactory methodAccessorFactory) { return new ByteBuddy(classFileVersion) .with(TypeValidation.DISABLED) .ignore(ignoreFinalizer ? isFinalizer() : ElementMatchers.<MethodDescription>none()) .subclass(proxiedType) .name(auxiliaryTypeName) .modifiers(DEFAULT_TYPE_MODIFIER) .implement(serializableProxy ? new Class<?>[]{Serializable.class} : new Class<?>[0]) .method(any()).intercept(new MethodCall(methodAccessorFactory)) .defineMethod(REFLECTION_METHOD, TargetType.class, Ownership.STATIC).intercept(SilentConstruction.INSTANCE) .make(); }
/** * {@inheritDoc} */ public DynamicType.Builder<?> apply(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassFileLocator classFileLocator) { Enhance enhance = typeDescription.getDeclaredAnnotations().ofType(Enhance.class).loadSilent(); if (typeDescription.getDeclaredMethods().filter(isToString()).isEmpty()) { builder = builder.method(isToString()).intercept(ToStringMethod.prefixedBy(enhance.prefix().getPrefixResolver()) .withIgnoredFields(enhance.includeSyntheticFields() ? ElementMatchers.<FieldDescription>none() : ElementMatchers.<FieldDescription>isSynthetic()) .withIgnoredFields(isAnnotatedWith(Exclude.class))); } return builder; }
/** * Creates a matcher where only overridable or declared methods are matched unless those are ignored. Methods that * are declared by the target type are only matched if they are not ignored. Declared methods that are not found on the * target type are always matched. * * @param ignoredMethods A method matcher that matches any ignored method. * @param originalType The original type of the instrumentation before adding any user methods. * @return A latent method matcher that identifies any method to instrument for a rebasement or redefinition. */ protected static LatentMatcher<MethodDescription> of(LatentMatcher<? super MethodDescription> ignoredMethods, TypeDescription originalType) { ElementMatcher.Junction<MethodDescription> predefinedMethodSignatures = none(); for (MethodDescription methodDescription : originalType.getDeclaredMethods()) { ElementMatcher.Junction<MethodDescription> signature = methodDescription.isConstructor() ? isConstructor() : ElementMatchers.<MethodDescription>named(methodDescription.getName()); signature = signature.and(returns(methodDescription.getReturnType().asErasure())); signature = signature.and(takesArguments(methodDescription.getParameters().asTypeList().asErasures())); predefinedMethodSignatures = predefinedMethodSignatures.or(signature); } return new InliningImplementationMatcher(ignoredMethods, predefinedMethodSignatures); }