/** * Creates a new default agent builder that uses a default {@link net.bytebuddy.ByteBuddy} instance for creating classes. */ public Default() { this(new ByteBuddy()); }
/** * Creates a new default plugin engine that rebases types and fails fast and on unresolved types and on live initializers. */ public Default() { this(new ByteBuddy()); }
/** {@inheritDoc} */ public ByteBuddy byteBuddy(ClassFileVersion classFileVersion) { return new ByteBuddy(classFileVersion); }
/** {@inheritDoc} */ public ByteBuddy byteBuddy(ClassFileVersion classFileVersion) { return new ByteBuddy(classFileVersion); }
/** * Configures Byte Buddy to use the specified factory for creating {@link InstrumentedType}s. Doing so, more efficient * representations can be chosen when only certain operations are required. By default, all operations are supported. * * @param instrumentedTypeFactory The factory to use when creating instrumented types. * @return A new Byte Buddy instance that uses the supplied factory for creating instrumented types. */ public ByteBuddy with(InstrumentedType.Factory instrumentedTypeFactory) { return new ByteBuddy(classFileVersion, namingStrategy, auxiliaryTypeNamingStrategy, annotationValueFilterFactory, annotationRetention, implementationContextFactory, methodGraphCompiler, instrumentedTypeFactory, typeValidation, classWriterStrategy, ignoredMethods); }
/** * Creates a new configuration that applies the supplied class writer strategy. By default, the constant pool of redefined and retransformed * classes is retained as most changes are additive and this retention improves performance. * * @param classWriterStrategy The class writer strategy to apply during type creation. * @return A new Byte Buddy instance that applies the supplied class writer strategy. */ public ByteBuddy with(ClassWriterStrategy classWriterStrategy) { return new ByteBuddy(classFileVersion, namingStrategy, auxiliaryTypeNamingStrategy, annotationValueFilterFactory, annotationRetention, implementationContextFactory, methodGraphCompiler, instrumentedTypeFactory, typeValidation, classWriterStrategy, ignoredMethods); }
/** * Creates a new configuration where auxiliary types are named by applying the given naming strategy. Auxiliary types * are helper types that might be required for implementing certain {@link Implementation}s. By default, Byte Buddy * adds a random suffix to the instrumented type's name when naming its auxiliary types. * * @param auxiliaryTypeNamingStrategy The naming strategy to apply when creating a new auxiliary type. * @return A new Byte Buddy instance that uses the supplied naming strategy for auxiliary types. */ public ByteBuddy with(AuxiliaryType.NamingStrategy auxiliaryTypeNamingStrategy) { return new ByteBuddy(classFileVersion, namingStrategy, auxiliaryTypeNamingStrategy, annotationValueFilterFactory, annotationRetention, implementationContextFactory, methodGraphCompiler, instrumentedTypeFactory, typeValidation, classWriterStrategy, ignoredMethods); }
/** * Creates a new configuration where all class files that are not based on an existing class file are created * using the supplied class file version. When creating a Byte Buddy instance by {@link ByteBuddy#ByteBuddy()}, the class * file version is detected automatically. If the class file version is known before creating a Byte Buddy instance, the * {@link ByteBuddy#ByteBuddy(ClassFileVersion)} constructor should be used. * * @param classFileVersion The class file version to use for types that are not based on an existing class file. * @return A new Byte Buddy instance that uses the supplied class file version. */ public ByteBuddy with(ClassFileVersion classFileVersion) { return new ByteBuddy(classFileVersion, namingStrategy, auxiliaryTypeNamingStrategy, annotationValueFilterFactory, annotationRetention, implementationContextFactory, methodGraphCompiler, instrumentedTypeFactory, typeValidation, classWriterStrategy, ignoredMethods); }
/** * Creates a new configuration that applies the supplied type validation. By default, explicitly type validation is applied * by Byte Buddy but it might be disabled for performance reason or for voluntarily creating illegal types. The Java virtual * machine applies its own type validation where some {@link Error} is thrown if a type is invalid, while Byte Buddy throws * some {@link RuntimeException}. * * @param typeValidation The type validation to apply during type creation. * @return A new Byte Buddy instance that applies the supplied type validation. */ public ByteBuddy with(TypeValidation typeValidation) { return new ByteBuddy(classFileVersion, namingStrategy, auxiliaryTypeNamingStrategy, annotationValueFilterFactory, annotationRetention, implementationContextFactory, methodGraphCompiler, instrumentedTypeFactory, typeValidation, classWriterStrategy, ignoredMethods); }
/** * Creates a new configuration where new types are named by applying the given naming strategy. By default, Byte Buddy * simply retains the name of rebased and redefined types but adds a random suffix to the name of created subclasses or * -interfaces. If a type is defined within the {@code java.*} namespace, Byte Buddy also adds a suffix to the generated * class because this namespace is only available for the bootstrap class loader. * * @param namingStrategy The naming strategy to apply when creating a new dynamic type. * @return A new Byte Buddy instance that uses the supplied naming strategy. */ public ByteBuddy with(NamingStrategy namingStrategy) { return new ByteBuddy(classFileVersion, namingStrategy, auxiliaryTypeNamingStrategy, annotationValueFilterFactory, annotationRetention, implementationContextFactory, methodGraphCompiler, instrumentedTypeFactory, typeValidation, classWriterStrategy, ignoredMethods); }
/** {@inheritDoc} */ public ByteBuddy byteBuddy(ClassFileVersion classFileVersion) { return new ByteBuddy(classFileVersion).with(Implementation.Context.Disabled.Factory.INSTANCE); }
protected SubclassBytecodeGenerator(SubclassLoader loader, Implementation readReplace, ElementMatcher<? super MethodDescription> matcher) { this.loader = loader; this.readReplace = readReplace; this.matcher = matcher; byteBuddy = new ByteBuddy().with(TypeValidation.DISABLED); random = new Random(); handler = ModuleHandler.make(byteBuddy, loader, random); }
/** {@inheritDoc} */ public ByteBuddy byteBuddy(ClassFileVersion classFileVersion) { return new ByteBuddy(classFileVersion) .with(MethodGraph.Compiler.ForDeclaredMethods.INSTANCE) .with(Implementation.Context.Disabled.Factory.INSTANCE); }
ByteBuddyState() { this.byteBuddy = new ByteBuddy().with( TypeValidation.DISABLED ); this.proxyCache = new TypeCache.WithInlineExpunction<TypeCache.SimpleKey>( TypeCache.Sort.WEAK ); this.basicProxyCache = new TypeCache.WithInlineExpunction<TypeCache.SimpleKey>( TypeCache.Sort.WEAK ); if ( System.getSecurityManager() != null ) { this.getDeclaredMethodMemberSubstitution = getDeclaredMethodMemberSubstitution(); this.getMethodMemberSubstitution = getMethodMemberSubstitution(); } else { this.getDeclaredMethodMemberSubstitution = null; this.getMethodMemberSubstitution = null; } this.proxyDefinitionHelpers = new ProxyDefinitionHelpers(); }
/** * {@inheritDoc} */ public DynamicType make(String auxiliaryTypeName, ClassFileVersion classFileVersion, MethodAccessorFactory methodAccessorFactory) { return new ByteBuddy(classFileVersion) .with(TypeValidation.DISABLED) .with(MethodGraph.Empty.INSTANCE) // avoid parsing the graph .subclass(Object.class, ConstructorStrategy.Default.NO_CONSTRUCTORS) .annotateType(eager ? Collections.singletonList(AnnotationDescription.Builder.ofType(SignatureRelevant.class).build()) : Collections.<AnnotationDescription>emptyList()) .name(auxiliaryTypeName) .modifiers(DEFAULT_TYPE_MODIFIER) .make(); } }
/** * {@inheritDoc} */ public DynamicType make(String auxiliaryTypeName, ClassFileVersion classFileVersion, MethodAccessorFactory methodAccessorFactory) { MethodDescription accessorMethod = methodAccessorFactory.registerAccessorFor(specialMethodInvocation, MethodAccessorFactory.AccessType.DEFAULT); LinkedHashMap<String, TypeDescription> parameterFields = extractFields(accessorMethod); DynamicType.Builder<?> builder = new ByteBuddy(classFileVersion) .with(TypeValidation.DISABLED) .with(PrecomputedMethodGraph.INSTANCE) .subclass(Object.class, ConstructorStrategy.Default.NO_CONSTRUCTORS) .name(auxiliaryTypeName) .modifiers(DEFAULT_TYPE_MODIFIER) .implement(Runnable.class, Callable.class).intercept(new MethodCall(accessorMethod, assigner)) .implement(serializableProxy ? new Class<?>[]{Serializable.class} : new Class<?>[0]) .defineConstructor().withParameters(parameterFields.values()) .intercept(ConstructorCall.INSTANCE); for (Map.Entry<String, TypeDescription> field : parameterFields.entrySet()) { builder = builder.defineField(field.getKey(), field.getValue(), Visibility.PRIVATE); } return builder.make(); }
/** * {@inheritDoc} */ public DynamicType make(String auxiliaryTypeName, ClassFileVersion classFileVersion, MethodAccessorFactory methodAccessorFactory) { LinkedHashMap<String, TypeDescription> parameterFields = extractFields(sourceMethod); DynamicType.Builder<?> builder = new ByteBuddy(classFileVersion) .with(TypeValidation.DISABLED) .subclass(forwardingType, ConstructorStrategy.Default.NO_CONSTRUCTORS) .name(auxiliaryTypeName) .modifiers(DEFAULT_TYPE_MODIFIER) .implement(serializableProxy ? new Class<?>[]{Serializable.class} : new Class<?>[0]) .method(ElementMatchers.<MethodDescription>isAbstract().and(isDeclaredBy(forwardingType))) .intercept(new MethodCall(sourceMethod, assigner)) .defineConstructor().withParameters(parameterFields.values()) .intercept(ConstructorCall.INSTANCE); for (Map.Entry<String, TypeDescription> field : parameterFields.entrySet()) { builder = builder.defineField(field.getKey(), field.getValue(), Visibility.PRIVATE); } return builder.make(); }
/** * {@inheritDoc} */ public DynamicType make(String auxiliaryTypeName, ClassFileVersion classFileVersion, MethodAccessorFactory methodAccessorFactory) { return fieldResolver.apply(new ByteBuddy(classFileVersion) .with(TypeValidation.DISABLED) .subclass(fieldResolver.getProxyType(), ConstructorStrategy.Default.NO_CONSTRUCTORS) .name(auxiliaryTypeName) .modifiers(DEFAULT_TYPE_MODIFIER) .implement(serializableProxy ? new Class<?>[]{Serializable.class} : new Class<?>[0]) .defineConstructor().withParameters(fieldDescription.isStatic() ? Collections.<TypeDescription>emptyList() : Collections.singletonList(instrumentedType)) .intercept(fieldDescription.isStatic() ? StaticFieldConstructor.INSTANCE : new InstanceFieldConstructor(instrumentedType)), fieldDescription, assigner, methodAccessorFactory).make(); }
/** * {@inheritDoc} */ public DynamicType make(String auxiliaryTypeName, ClassFileVersion classFileVersion, MethodAccessorFactory methodAccessorFactory) { return new ByteBuddy(classFileVersion) .with(TypeValidation.DISABLED) .subclass(morphingType, ConstructorStrategy.Default.NO_CONSTRUCTORS) .name(auxiliaryTypeName) .modifiers(DEFAULT_TYPE_MODIFIER) .implement(serializableProxy ? new Class<?>[]{Serializable.class} : new Class<?>[0]) .defineConstructor().withParameters(specialMethodInvocation.getMethodDescription().isStatic() ? Collections.<TypeDescription>emptyList() : Collections.singletonList(instrumentedType)) .intercept(specialMethodInvocation.getMethodDescription().isStatic() ? StaticFieldConstructor.INSTANCE : new InstanceFieldConstructor(instrumentedType)) .method(ElementMatchers.<MethodDescription>isAbstract().and(isDeclaredBy(morphingType))) .intercept(new MethodCall(methodAccessorFactory.registerAccessorFor(specialMethodInvocation, MethodAccessorFactory.AccessType.DEFAULT), assigner)) .make(); }
/** * {@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(); }