/** * An element matcher that matches any getter for the given property. When given an empty string, any getter named {@code get} * is matched despite that such a getter is not fulfilling the Java bean naming conventions. If a getter's type is {@code boolean} * or {@link Boolean}, {@code is} is also accepted as a prefix. * * @param property The property to match a getter for. * @param <T> The type of the matched object. * @return A matcher that matches any getter method for the supplied property. */ public static <T extends MethodDescription> ElementMatcher.Junction<T> isGetter(String property) { return isGetter().and(property.length() == 0 ? named("get").or(named("is")) : named("get" + Character.toUpperCase(property.charAt(0)) + property.substring(1)).or(named("is" + Character.toUpperCase(property.charAt(0)) + property.substring(1)))); }
/** * {@inheritDoc} */ public Appender appender(Target implementationTarget) { if (implementationTarget.getInstrumentedType().isInterface()) { throw new IllegalStateException("Cannot implement meaningful toString method for " + implementationTarget.getInstrumentedType()); } String prefix = prefixResolver.resolve(implementationTarget.getInstrumentedType()); if (prefix == null) { throw new IllegalStateException("Prefix for toString method cannot be null"); } return new Appender(prefix, start, end, separator, definer, implementationTarget.getInstrumentedType().getDeclaredFields().filter(not(isStatic().or(ignored)))); }
/** * {@inheritDoc} */ public ByteCodeAppender appender(Target implementationTarget) { if (implementationTarget.getInstrumentedType().isInterface()) { throw new IllegalStateException("Cannot implement meaningful hash code method for " + implementationTarget.getInstrumentedType()); } return new Appender(offsetProvider.resolve(implementationTarget.getInstrumentedType()), multiplier, implementationTarget.getInstrumentedType().getDeclaredFields().filter(not(isStatic().or(ignored))), nonNullable); }
/** * Creates a new Byte Buddy instance with a default configuration that is suitable for most use cases. * * @param classFileVersion The class file version to use for types that are not based on an existing class file. */ public ByteBuddy(ClassFileVersion classFileVersion) { this(classFileVersion, new NamingStrategy.SuffixingRandom(BYTE_BUDDY_DEFAULT_PREFIX), new AuxiliaryType.NamingStrategy.SuffixingRandom(BYTE_BUDDY_DEFAULT_SUFFIX), AnnotationValueFilter.Default.APPEND_DEFAULTS, AnnotationRetention.ENABLED, Implementation.Context.Default.Factory.INSTANCE, MethodGraph.Compiler.DEFAULT, InstrumentedType.Factory.Default.MODIFIABLE, TypeValidation.ENABLED, ClassWriterStrategy.Default.CONSTANT_POOL_RETAINING, new LatentMatcher.Resolved<MethodDescription>(isSynthetic().or(isDefaultFinalizer()))); }
/** * Returns a new version of this equals method implementation that ignores the specified fields additionally to any * previously specified fields. * * @param ignored A matcher to specify any fields that should be ignored. * @return A new version of this equals method implementation that also ignores any fields matched by the provided matcher. */ public EqualsMethod withIgnoredFields(ElementMatcher<? super FieldDescription.InDefinedShape> ignored) { return new EqualsMethod(superClassCheck, typeCompatibilityCheck, this.ignored.<FieldDescription.InDefinedShape>or(ignored), nonNullable, comparator); }
/** * {@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; } }
/** * {@inheritDoc} */ public Ignored ignore(ElementMatcher<? super TypeDescription> typeMatcher, ElementMatcher<? super ClassLoader> classLoaderMatcher, ElementMatcher<? super JavaModule> moduleMatcher) { return ignore(new RawMatcher.ForElementMatchers(typeMatcher, classLoaderMatcher, not(supportsModules()).or(moduleMatcher))); }
/** * Matches a {@link ModifierReviewable} that is package-private. * * @param <T> The type of the matched object. * @return A matcher for a package-private modifier reviewable. */ public static <T extends ModifierReviewable.OfByteCodeElement> ElementMatcher.Junction<T> isPackagePrivate() { return not(isPublic().or(isProtected()).or(isPrivate())); }
/** * {@inheritDoc} */ public ElementMatcher<? super MethodDescription> resolve(TypeDescription typeDescription) { return (ElementMatcher<? super MethodDescription>) not(ignoredMethods.resolve(typeDescription)) .and(isVirtual().and(not(isFinal())).or(isDeclaredBy(typeDescription))) .or(isDeclaredBy(typeDescription).and(not(predefinedMethodSignatures))); } }
/** * {@inheritDoc} */ public Identified.Narrowable type(ElementMatcher<? super TypeDescription> typeMatcher, ElementMatcher<? super ClassLoader> classLoaderMatcher, ElementMatcher<? super JavaModule> moduleMatcher) { return type(new RawMatcher.ForElementMatchers(typeMatcher, classLoaderMatcher, not(supportsModules()).or(moduleMatcher))); }
/** * {@inheritDoc} */ public Engine ignore(ElementMatcher<? super TypeDescription> matcher) { return new Default(byteBuddy, typeStrategy, poolStrategy, classFileLocator, listener, errorHandler, ignoredTypeMatcher.<TypeDescription>or(matcher)); }
/** * {@inheritDoc} */ public ElementMatcher<? super MethodDescription> resolve(TypeDescription typeDescription) { // Casting is required by JDK 6. return (ElementMatcher<? super MethodDescription>) isVirtual().and(not(isFinal())) .and(isVisibleTo(typeDescription)) .and(not(ignoredMethods.resolve(typeDescription))) .or(isDeclaredBy(typeDescription)); } }
/** * Returns a new version of this hash code method implementation that ignores the specified fields additionally to any * previously specified fields. * * @param ignored A matcher to specify any fields that should be ignored. * @return A new version of this hash code method implementation that also ignores any fields matched by the provided matcher. */ public HashCodeMethod withIgnoredFields(ElementMatcher<? super FieldDescription.InDefinedShape> ignored) { return new HashCodeMethod(offsetProvider, multiplier, this.ignored.<FieldDescription.InDefinedShape>or(ignored), nonNullable); }
/** * Excludes all matched types from being added as an attribute. * * @param filter A filter for excluding types from the attribute generation. * @return A new type reference adjustment that uses the supplied filter for excluding types. */ public TypeReferenceAdjustment filter(ElementMatcher<? super TypeDescription> filter) { return new TypeReferenceAdjustment(strict, this.filter.<TypeDescription>or(filter)); }
/** * Specifies that any field that matches the specified matcher should be removed. * * @param matcher The matcher that decides upon field removal. * @return A new member removal instance that removes all previously specified members and any fields that match the specified matcher. */ public MemberRemoval stripFields(ElementMatcher<? super FieldDescription.InDefinedShape> matcher) { return new MemberRemoval(fieldMatcher.or(matcher), methodMatcher); }
/** * Returns a new version of this toString method implementation that ignores the specified fields additionally to any * previously specified fields. * * @param ignored A matcher to specify any fields that should be ignored. * @return A new version of this toString method implementation that also ignores any fields matched by the provided matcher. */ public ToStringMethod withIgnoredFields(ElementMatcher<? super FieldDescription.InDefinedShape> ignored) { return new ToStringMethod(prefixResolver, start, end, separator, definer, this.ignored.<FieldDescription.InDefinedShape>or(ignored)); }
/** * Returns a new version of this hash code method implementation that does not apply a {@code null} value check for the specified fields * if they have a reference type additionally to any previously specified fields. * * @param nonNullable A matcher to specify any fields that should not be guarded against {@code null} values. * @return A new version of this hash code method implementation that also does not apply {@code null} value checks to any fields matched by * the provided matcher. */ public HashCodeMethod withNonNullableFields(ElementMatcher<? super FieldDescription.InDefinedShape> nonNullable) { return new HashCodeMethod(offsetProvider, multiplier, ignored, this.nonNullable.<FieldDescription.InDefinedShape>or(nonNullable)); }
/** * Matches any Java bean getter method. * * @param <T> The type of the matched object. * @return A matcher that matches any getter method. */ public static <T extends MethodDescription> ElementMatcher.Junction<T> isGetter() { return takesArguments(0).and(not(returns(TypeDescription.VOID))).and(nameStartsWith("get").or(nameStartsWith("is").and(returnsGeneric(anyOf(boolean.class, Boolean.class))))); }
/** * Returns a new version of this equals method implementation that does not apply a {@code null} value check for the specified fields * if they have a reference type additionally to any previously specified fields. * * @param nonNullable A matcher to specify any fields that should not be guarded against {@code null} values. * @return A new version of this equals method implementation that also does not apply {@code null} value checks to any fields matched by * the provided matcher. */ public EqualsMethod withNonNullableFields(ElementMatcher<? super FieldDescription.InDefinedShape> nonNullable) { return new EqualsMethod(superClassCheck, typeCompatibilityCheck, ignored, this.nonNullable.<FieldDescription.InDefinedShape>or(nonNullable), comparator); }
/** * Specifies that any method or constructor that matches the specified matcher should be removed. * * @param matcher The matcher that decides upon method and constructor removal. * @return A new member removal instance that removes all previously specified members and any method or constructor that matches the specified matcher. */ public MemberRemoval stripInvokables(ElementMatcher<? super MethodDescription> matcher) { return new MemberRemoval(fieldMatcher, methodMatcher.or(matcher)); }