/** * {@inheritDoc} */ protected ElementMatcher<FieldDescription> nonNullable(ElementMatcher<FieldDescription> matcher) { return not(matcher); } }
/** * Matches an iterable by assuring that no element of the iterable collection matches the provided matcher. * * @param matcher The matcher to apply to each element. * @param <T> The type of the matched object. * @return A matcher that matches an iterable if no element matches the provided matcher. */ public static <T> ElementMatcher.Junction<Iterable<? extends T>> whereNone(ElementMatcher<? super T> matcher) { return not(whereAny(matcher)); }
/** * Matches a module if it exists, i.e. not {@code null}. * * @param <T> The type of the matched object. * @return A matcher that validates a module's existence. */ public static <T extends JavaModule> ElementMatcher.Junction<T> supportsModules() { return not(new NullMatcher<T>()); } }
/** * {@inheritDoc} */ public MethodList<?> getInstrumentedMethods() { return new MethodList.Explicit<MethodDescription>(new ArrayList<MethodDescription>(implementations.keySet())).filter(not(isTypeInitializer())); }
/** * {@inheritDoc} */ public MethodList<?> getInstrumentedMethods() { return new MethodList.Explicit<MethodDescription>(new ArrayList<MethodDescription>(implementations.keySet())).filter(not(isTypeInitializer())); }
/** * {@inheritDoc} */ @SuppressWarnings("unchecked") public ElementMatcher<? super MethodDescription> resolve(TypeDescription typeDescription) { // Casting is required by some Java 6 compilers. return (ElementMatcher<? super MethodDescription>) (inverted ? not(isDeclaredBy(typeDescription)) : isDeclaredBy(typeDescription)); } }
/** * <p> * Returns the only class loader that was appended if exactly one class loader was appended or a multiple parent class loader as * a parent of all supplied class loader and with the bootstrap class loader as an implicit parent. If no class loader was appended, * or if only the supplied parent to this method was included, this class loader is returned, * </p> * <p> * <b>Important</b>: Byte Buddy does not provide any access control for the creation of the class loader. It is the responsibility * of the user of this builder to provide such privileges. * </p> * * @param parent The class loader's contractual parent which is accessible via {@link ClassLoader#getParent()}. If this class loader * is also included in the appended class loaders, it is not * @return A suitable class loader. */ public ClassLoader build(ClassLoader parent) { return classLoaders.isEmpty() || (classLoaders.size() == 1 && classLoaders.contains(parent)) ? parent : filter(not(is(parent))).doBuild(parent); }
/** * Creates a matcher that matches none of the given objects by the {@link java.lang.Object#equals(Object)} method. * None of the values must be {@code null}. * * @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 none of the given objects. */ public static <T> ElementMatcher.Junction<T> noneOf(Iterable<?> values) { ElementMatcher.Junction<T> matcher = null; for (Object value : values) { matcher = matcher == null ? ElementMatchers.<T>not(is(value)) : matcher.and(not(is(value))); } return matcher == null ? ElementMatchers.<T>any() : matcher; }
/** {@inheritDoc} */ public DynamicType.Builder<?> transform(TypeDescription typeDescription, ByteBuddy byteBuddy, ClassFileLocator classFileLocator, MethodNameTransformer methodNameTransformer) { return byteBuddy.redefine(typeDescription, classFileLocator).ignoreAlso(not(isDeclaredBy(typeDescription))); } },
/** * {@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 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))); }
/** * {@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 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)); } }
/** * 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 Linked compile(TypeDefinition typeDefinition, TypeDescription viewPoint) { LinkedHashMap<MethodDescription.SignatureToken, Node> nodes = new LinkedHashMap<MethodDescription.SignatureToken, Node>(); for (MethodDescription methodDescription : typeDefinition.getDeclaredMethods().filter(isVirtual().and(not(isBridge())).and(isVisibleTo(viewPoint)))) { nodes.put(methodDescription.asSignatureToken(), new Node.Simple(methodDescription)); } return new Linked.Delegation(new MethodGraph.Simple(nodes), Empty.INSTANCE, Collections.<TypeDescription, MethodGraph>emptyMap()); } }
/** * {@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); }
/** * {@inheritDoc} */ public FieldDescription resolve(TypeDescription targetType, ByteCodeElement target, TypeList.Generic parameters, TypeDescription.Generic result) { if (parameters.isEmpty()) { throw new IllegalStateException("Cannot substitute parameterless instruction with " + target); } else if (parameters.get(0).isPrimitive() || parameters.get(0).isArray()) { throw new IllegalStateException("Cannot access field on primitive or array type for " + target); } TypeDefinition current = parameters.get(0); do { FieldList<?> fields = current.getDeclaredFields().filter(not(isStatic()).<FieldDescription>and(isVisibleTo(instrumentedType)).and(matcher)); if (fields.size() == 1) { return fields.getOnly(); } else if (fields.size() > 1) { throw new IllegalStateException("Ambiguous field location of " + fields); } current = current.getSuperClass(); } while (current != null); throw new IllegalStateException("Cannot locate field matching " + matcher + " on " + targetType); } }
/** * 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))))); }
/** * {@inheritDoc} */ public ByteCodeAppender appender(Target implementationTarget) { if (implementationTarget.getInstrumentedType().isInterface()) { throw new IllegalStateException("Cannot implement meaningful equals method for " + implementationTarget.getInstrumentedType()); } List<FieldDescription.InDefinedShape> fields = new ArrayList<FieldDescription.InDefinedShape>(implementationTarget.getInstrumentedType() .getDeclaredFields() .filter(not(isStatic().or(ignored)))); Collections.sort(fields, comparator); return new Appender(implementationTarget.getInstrumentedType(), new StackManipulation.Compound( superClassCheck.resolve(implementationTarget.getInstrumentedType()), MethodVariableAccess.loadThis(), MethodVariableAccess.REFERENCE.loadFrom(1), ConditionalReturn.onIdentity().returningTrue(), typeCompatibilityCheck.resolve(implementationTarget.getInstrumentedType()) ), fields, nonNullable); }