net.bytebuddy.dynamic.scaffold.TypeValidation Java Examples

The following examples show how to use net.bytebuddy.dynamic.scaffold.TypeValidation. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ByteBuddyState.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
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();
}
 
Example #2
Source File: KanelaAgentBuilder.java    From kanela with Apache License 2.0 6 votes vote down vote up
private AgentBuilder newAgentBuilder() {
        val byteBuddy = new ByteBuddy()
            .with(TypeValidation.of(config.isDebugMode()))
            .with(MethodGraph.Compiler.ForDeclaredMethods.INSTANCE);

        AgentBuilder agentBuilder = new AgentBuilder.Default(byteBuddy)
                .with(poolStrategyCache);


        agentBuilder = withRetransformationForRuntime(agentBuilder);
        agentBuilder = withBootstrapAttaching(agentBuilder);
        agentBuilder = withIgnore(agentBuilder);

        return agentBuilder
                .with(DefaultInstrumentationListener.instance())
                .with(additionalListeners());
}
 
Example #3
Source File: ElasticApmAgent.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
private static AgentBuilder initAgentBuilder(ElasticApmTracer tracer, Instrumentation instrumentation,
                                             Iterable<ElasticApmInstrumentation> instrumentations, Logger logger,
                                             AgentBuilder.DescriptionStrategy descriptionStrategy, boolean premain) {
    final CoreConfiguration coreConfiguration = tracer.getConfig(CoreConfiguration.class);
    ElasticApmAgent.instrumentation = instrumentation;
    final ByteBuddy byteBuddy = new ByteBuddy()
        .with(TypeValidation.of(logger.isDebugEnabled()))
        .with(FailSafeDeclaredMethodsCompiler.INSTANCE);
    AgentBuilder agentBuilder = getAgentBuilder(byteBuddy, coreConfiguration, logger, descriptionStrategy, premain);
    int numberOfAdvices = 0;
    for (final ElasticApmInstrumentation advice : instrumentations) {
        if (isIncluded(advice, coreConfiguration)) {
            numberOfAdvices++;
            agentBuilder = applyAdvice(tracer, agentBuilder, advice, new ElementMatcher.Junction.Conjunction<>(advice.getTypeMatcher(), not(isInterface())));
        }
    }
    logger.debug("Applied {} advices", numberOfAdvices);
    return agentBuilder;
}
 
Example #4
Source File: ByteBuddyTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore("Requires preview feature enabling")
public void testRecordWithMember() throws Exception {
    Class<?> type = new ByteBuddy()
            .with(TypeValidation.DISABLED)
            .with(ClassFileVersion.JAVA_V14.asPreviewVersion())
            .makeRecord()
            .defineRecordComponent("foo", String.class)
            .make()
            .load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
            .getLoaded();
    assertThat((Boolean) Class.class.getMethod("isRecord").invoke(type), is(true));
    Object record = type.getConstructor(String.class).newInstance("bar");
    assertThat(type.getMethod("foo").invoke(record), is((Object) "bar"));
    assertThat(type.getMethod("hashCode").invoke(record), is((Object) "bar".hashCode()));
    assertThat(type.getMethod("equals", Object.class).invoke(record, new Object()), is((Object) false));
    assertThat(type.getMethod("equals", Object.class).invoke(record, record), is((Object) true));
    assertThat(type.getMethod("toString").invoke(record), is((Object) (type.getSimpleName() + "[foo=bar]")));
}
 
Example #5
Source File: ByteBuddyTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore("Requires preview feature enabling")
public void testRecordWithoutMember() throws Exception {
    Class<?> type = new ByteBuddy()
            .with(TypeValidation.DISABLED)
            .with(ClassFileVersion.JAVA_V14.asPreviewVersion())
            .makeRecord()
            .make()
            .load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
            .getLoaded();
    assertThat((Boolean) Class.class.getMethod("isRecord").invoke(type), is(true));
    Object record = type.getConstructor().newInstance();
    assertThat(type.getMethod("hashCode").invoke(record), is((Object) 0));
    assertThat(type.getMethod("equals", Object.class).invoke(record, new Object()), is((Object) false));
    assertThat(type.getMethod("equals", Object.class).invoke(record, record), is((Object) true));
    assertThat(type.getMethod("toString").invoke(record), is((Object) (type.getSimpleName() + "[]")));
}
 
Example #6
Source File: BufferAlignmentAgent.java    From agrona with Apache License 2.0 6 votes vote down vote up
private static synchronized void agent(final boolean shouldRedefine, final Instrumentation instrumentation)
{
    BufferAlignmentAgent.instrumentation = instrumentation;
    // all Int methods, and all String method other than
    // XXXStringWithoutLengthXXX or getStringXXX(int, int)
    final Junction<MethodDescription> intVerifierMatcher = nameContains("Int")
        .or(nameMatches(".*String[^W].*").and(not(ElementMatchers.takesArguments(int.class, int.class))));

    alignmentTransformer = new AgentBuilder.Default(new ByteBuddy().with(TypeValidation.DISABLED))
        .with(new AgentBuilderListener())
        .disableClassFormatChanges()
        .with(shouldRedefine ?
            AgentBuilder.RedefinitionStrategy.RETRANSFORMATION : AgentBuilder.RedefinitionStrategy.DISABLED)
        .type(isSubTypeOf(DirectBuffer.class).and(not(isInterface())))
        .transform((builder, typeDescription, classLoader, module) -> builder
            .visit(to(LongVerifier.class).on(nameContains("Long")))
            .visit(to(DoubleVerifier.class).on(nameContains("Double")))
            .visit(to(IntVerifier.class).on(intVerifierMatcher))
            .visit(to(FloatVerifier.class).on(nameContains("Float")))
            .visit(to(ShortVerifier.class).on(nameContains("Short")))
            .visit(to(CharVerifier.class).on(nameContains("Char"))))
        .installOn(instrumentation);
}
 
Example #7
Source File: MethodCallProxy.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@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();
}
 
Example #8
Source File: ClassByExtensionBenchmark.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Performs a benchmark of a class extension using Byte Buddy. This benchmark also uses the annotation-based approach
 * but creates delegation methods which do not require the creation of additional classes. This benchmark reuses a
 * precomputed delegator.
 *
 * @return The created instance, in order to avoid JIT removal.
 * @throws Exception If the invocation causes an exception.
 */
@Benchmark
public ExampleClass benchmarkByteBuddyWithAccessorAndReusedDelegator() throws Exception {
    return new ByteBuddy()
            .with(TypeValidation.DISABLED)
            .ignore(none())
            .subclass(baseClass)
            .method(isDeclaredBy(baseClass)).intercept(accessInterceptor)
            .make()
            .load(newClassLoader(), ClassLoadingStrategy.Default.INJECTION)
            .getLoaded()
            .getDeclaredConstructor()
            .newInstance();
}
 
Example #9
Source File: TrivialClassCreationBenchmark.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Performs a benchmark for a trivial class creation using Byte Buddy.
 *
 * @return The created instance, in order to avoid JIT removal.
 */
@Benchmark
public Class<?> benchmarkByteBuddy() {
    return new ByteBuddy()
            .with(TypeValidation.DISABLED)
            .ignore(any())
            .subclass(baseClass)
            .make()
            .load(newClassLoader(), ClassLoadingStrategy.Default.INJECTION)
            .getLoaded();
}
 
Example #10
Source File: ClassByImplementationBenchmark.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Performs a benchmark of an interface implementation using Byte Buddy.
 *
 * @return The created instance, in order to avoid JIT removal.
 * @throws java.lang.Exception If the reflective invocation causes an exception.
 */
@Benchmark
public ExampleInterface benchmarkByteBuddy() throws Exception {
    return new ByteBuddy()
            .with(TypeValidation.DISABLED)
            .ignore(none())
            .subclass(baseClass)
            .method(isDeclaredBy(baseClass)).intercept(StubMethod.INSTANCE)
            .make()
            .load(newClassLoader(), ClassLoadingStrategy.Default.INJECTION)
            .getLoaded()
            .getDeclaredConstructor()
            .newInstance();
}
 
Example #11
Source File: ClassByImplementationBenchmark.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Performs a benchmark of an interface implementation using Byte Buddy. This benchmark uses a type pool to compare against
 * usage of the reflection API.
 *
 * @return The created instance, in order to avoid JIT removal.
 * @throws java.lang.Exception If the reflective invocation causes an exception.
 */
@Benchmark
public ExampleInterface benchmarkByteBuddyWithTypePool() throws Exception {
    return (ExampleInterface) new ByteBuddy()
            .with(TypeValidation.DISABLED)
            .ignore(none())
            .subclass(baseClassDescription)
            .method(isDeclaredBy(baseClassDescription)).intercept(StubMethod.INSTANCE)
            .make()
            .load(newClassLoader(), ClassLoadingStrategy.Default.INJECTION)
            .getLoaded()
            .getDeclaredConstructor()
            .newInstance();
}
 
Example #12
Source File: ClassByExtensionBenchmark.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Performs a benchmark of a class extension using Byte Buddy. This benchmark creates proxy classes for the invocation
 * of super methods which requires the creation of auxiliary classes.
 *
 * @return The created instance, in order to avoid JIT removal.
 * @throws java.lang.Exception If the invocation causes an exception.
 */
@Benchmark
public ExampleClass benchmarkByteBuddyWithProxy() throws Exception {
    return new ByteBuddy()
            .with(TypeValidation.DISABLED)
            .ignore(none())
            .subclass(baseClass)
            .method(isDeclaredBy(baseClass)).intercept(MethodDelegation.to(ByteBuddyProxyInterceptor.class))
            .make()
            .load(newClassLoader(), ClassLoadingStrategy.Default.INJECTION)
            .getLoaded()
            .getDeclaredConstructor()
            .newInstance();
}
 
Example #13
Source File: ClassByExtensionBenchmark.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Performs a benchmark of a class extension using Byte Buddy. This benchmark also uses the annotation-based approach
 * but creates delegation methods which do not require the creation of additional classes. This benchmark reuses a
 * precomputed delegator.
 *
 * @return The created instance, in order to avoid JIT removal.
 * @throws Exception If the invocation causes an exception.
 */
@Benchmark
public ExampleClass benchmarkByteBuddyWithProxyAndReusedDelegator() throws Exception {
    return new ByteBuddy()
            .with(TypeValidation.DISABLED)
            .ignore(none())
            .subclass(baseClass)
            .method(isDeclaredBy(baseClass)).intercept(proxyInterceptor)
            .make()
            .load(newClassLoader(), ClassLoadingStrategy.Default.INJECTION)
            .getLoaded()
            .getDeclaredConstructor()
            .newInstance();
}
 
Example #14
Source File: ClassByExtensionBenchmark.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Performs a benchmark of a class extension using Byte Buddy. This benchmark creates proxy classes for the invocation
 * of super methods which requires the creation of auxiliary classes. This benchmark uses a type pool to compare against
 * usage of the reflection API.
 *
 * @return The created instance, in order to avoid JIT removal.
 * @throws java.lang.Exception If the invocation causes an exception.
 */
@Benchmark
public ExampleClass benchmarkByteBuddyWithProxyWithTypePool() throws Exception {
    return (ExampleClass) new ByteBuddy()
            .with(TypeValidation.DISABLED)
            .ignore(none())
            .subclass(baseClassDescription)
            .method(isDeclaredBy(baseClassDescription)).intercept(MethodDelegation.to(proxyClassDescription))
            .make()
            .load(newClassLoader(), ClassLoadingStrategy.Default.INJECTION)
            .getLoaded()
            .getDeclaredConstructor()
            .newInstance();
}
 
Example #15
Source File: ClassByExtensionBenchmark.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Performs a benchmark of a class extension using Byte Buddy. This benchmark also uses the annotation-based approach
 * but creates delegation methods which do not require the creation of additional classes. This benchmark reuses a
 * precomputed delegator. This benchmark uses a type pool to compare against usage of the reflection API.
 *
 * @return The created instance, in order to avoid JIT removal.
 * @throws Exception If the invocation causes an exception.
 */
@Benchmark
public ExampleClass benchmarkByteBuddyWithProxyAndReusedDelegatorWithTypePool() throws Exception {
    return (ExampleClass) new ByteBuddy()
            .with(TypeValidation.DISABLED)
            .ignore(none())
            .subclass(baseClassDescription)
            .method(isDeclaredBy(baseClassDescription)).intercept(proxyInterceptorDescription)
            .make()
            .load(newClassLoader(), ClassLoadingStrategy.Default.INJECTION)
            .getLoaded()
            .getDeclaredConstructor()
            .newInstance();
}
 
Example #16
Source File: ClassByExtensionBenchmark.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Performs a benchmark of a class extension using Byte Buddy. This benchmark also uses the annotation-based approach
 * but creates delegation methods which do not require the creation of additional classes.
 *
 * @return The created instance, in order to avoid JIT removal.
 * @throws Exception If the invocation causes an exception.
 */
@Benchmark
public ExampleClass benchmarkByteBuddyWithAccessor() throws Exception {
    return new ByteBuddy()
            .with(TypeValidation.DISABLED)
            .ignore(none())
            .subclass(baseClass)
            .method(isDeclaredBy(baseClass)).intercept(MethodDelegation.to(ByteBuddyAccessInterceptor.class))
            .make()
            .load(newClassLoader(), ClassLoadingStrategy.Default.INJECTION)
            .getLoaded()
            .getDeclaredConstructor()
            .newInstance();
}
 
Example #17
Source File: MethodDelegationOtherTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testDelegationToInvisibleFieldTypeThrowsException() throws Exception {
    new ByteBuddy()
            .with(TypeValidation.DISABLED)
            .subclass(Object.class)
            .defineField("foo", Foo.class)
            .method(isToString())
            .intercept(MethodDelegation.toField("foo"))
            .make();
}
 
Example #18
Source File: ClassByExtensionBenchmark.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Performs a benchmark of a class extension using Byte Buddy. This benchmark also uses the annotation-based approach
 * but creates delegation methods which do not require the creation of additional classes. This benchmark uses a type
 * pool to compare against usage of the reflection API.
 *
 * @return The created instance, in order to avoid JIT removal.
 * @throws Exception If the invocation causes an exception.
 */
@Benchmark
public ExampleClass benchmarkByteBuddyWithAccessorWithTypePool() throws Exception {
    return (ExampleClass) new ByteBuddy()
            .with(TypeValidation.DISABLED)
            .ignore(none())
            .subclass(baseClassDescription)
            .method(isDeclaredBy(baseClassDescription)).intercept(MethodDelegation.to(accessClassDescription))
            .make()
            .load(newClassLoader(), ClassLoadingStrategy.Default.INJECTION)
            .getLoaded()
            .getDeclaredConstructor()
            .newInstance();
}
 
Example #19
Source File: ClassByExtensionBenchmark.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Performs a benchmark of a class extension using Byte Buddy. This benchmark also uses the annotation-based approach
 * but creates delegation methods which do not require the creation of additional classes. This benchmark reuses a
 * precomputed delegator. This benchmark uses a type pool to compare against usage of the reflection API.
 *
 * @return The created instance, in order to avoid JIT removal.
 * @throws Exception If the invocation causes an exception.
 */
@Benchmark
public ExampleClass benchmarkByteBuddyWithAccessorAndReusedDelegatorWithTypePool() throws Exception {
    return (ExampleClass) new ByteBuddy()
            .with(TypeValidation.DISABLED)
            .ignore(none())
            .subclass(baseClassDescription)
            .method(isDeclaredBy(baseClassDescription)).intercept(accessInterceptorDescription)
            .make()
            .load(newClassLoader(), ClassLoadingStrategy.Default.INJECTION)
            .getLoaded()
            .getDeclaredConstructor()
            .newInstance();
}
 
Example #20
Source File: ClassByExtensionBenchmark.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Performs a benchmark of a class extension using Byte Buddy. This benchmark uses delegation but completes with a
 * hard-coded super method call.
 *
 * @return The created instance, in order to avoid JIT removal.
 * @throws Exception If the invocation causes an exception.
 */
@Benchmark
public ExampleClass benchmarkByteBuddyWithPrefix() throws Exception {
    return new ByteBuddy()
            .with(TypeValidation.DISABLED)
            .ignore(none())
            .subclass(baseClass)
            .method(isDeclaredBy(baseClass)).intercept(MethodDelegation.to(ByteBuddyPrefixInterceptor.class).andThen(SuperMethodCall.INSTANCE))
            .make()
            .load(newClassLoader(), ClassLoadingStrategy.Default.INJECTION)
            .getLoaded()
            .getDeclaredConstructor()
            .newInstance();
}
 
Example #21
Source File: ClassByExtensionBenchmark.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Performs a benchmark of a class extension using Byte Buddy. This benchmark uses delegation but completes with a
 * hard-coded super method call. This benchmark reuses a precomputed delegator.
 *
 * @return The created instance, in order to avoid JIT removal.
 * @throws Exception If the invocation causes an exception.
 */
@Benchmark
public ExampleClass benchmarkByteBuddyWithPrefixAndReusedDelegator() throws Exception {
    return new ByteBuddy()
            .with(TypeValidation.DISABLED)
            .ignore(none())
            .subclass(baseClass)
            .method(isDeclaredBy(baseClass)).intercept(prefixInterceptor.andThen(SuperMethodCall.INSTANCE))
            .make()
            .load(newClassLoader(), ClassLoadingStrategy.Default.INJECTION)
            .getLoaded()
            .getDeclaredConstructor()
            .newInstance();
}
 
Example #22
Source File: ClassByExtensionBenchmark.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Performs a benchmark of a class extension using Byte Buddy. This benchmark uses delegation but completes with a
 * hard-coded super method call. This benchmark uses a type pool to compare against usage of the reflection API.
 *
 * @return The created instance, in order to avoid JIT removal.
 * @throws Exception If the invocation causes an exception.
 */
@Benchmark
public ExampleClass benchmarkByteBuddyWithPrefixWithTypePool() throws Exception {
    return (ExampleClass) new ByteBuddy()
            .with(TypeValidation.DISABLED)
            .ignore(none())
            .subclass(baseClassDescription)
            .method(isDeclaredBy(baseClassDescription)).intercept(MethodDelegation.to(prefixClassDescription).andThen(SuperMethodCall.INSTANCE))
            .make()
            .load(newClassLoader(), ClassLoadingStrategy.Default.INJECTION)
            .getLoaded()
            .getDeclaredConstructor()
            .newInstance();
}
 
Example #23
Source File: ClassByExtensionBenchmark.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Performs a benchmark of a class extension using Byte Buddy. This benchmark uses delegation but completes with a
 * hard-coded super method call. This benchmark reuses a precomputed delegator. This benchmark uses a type pool to
 * compare against usage of the reflection API.
 *
 * @return The created instance, in order to avoid JIT removal.
 * @throws Exception If the invocation causes an exception.
 */
@Benchmark
public ExampleClass benchmarkByteBuddyWithPrefixAndReusedDelegatorWithTypePool() throws Exception {
    return (ExampleClass) new ByteBuddy()
            .with(TypeValidation.DISABLED)
            .ignore(none())
            .subclass(baseClassDescription)
            .method(isDeclaredBy(baseClassDescription)).intercept(prefixInterceptorDescription.andThen(SuperMethodCall.INSTANCE))
            .make()
            .load(newClassLoader(), ClassLoadingStrategy.Default.INJECTION)
            .getLoaded()
            .getDeclaredConstructor()
            .newInstance();
}
 
Example #24
Source File: ClassByExtensionBenchmark.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Performs a benchmark of a class extension using Byte Buddy. This benchmark uses a specialized interception
 * strategy which is easier to inline by the compiler.
 *
 * @return The created instance, in order to avoid JIT removal.
 * @throws java.lang.Exception If the invocation causes an exception.
 */
@Benchmark
public ExampleClass benchmarkByteBuddySpecialized() throws Exception {
    return new ByteBuddy()
            .with(TypeValidation.DISABLED)
            .ignore(none())
            .subclass(baseClass)
            .method(isDeclaredBy(baseClass)).intercept(SuperMethodCall.INSTANCE)
            .make()
            .load(newClassLoader(), ClassLoadingStrategy.Default.INJECTION)
            .getLoaded()
            .getDeclaredConstructor()
            .newInstance();
}
 
Example #25
Source File: TypePoolDefaultWithLazyResolutionTypeDescriptionTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenericResolutionIsLazyForSimpleCreation() throws Exception {
    ClassFileLocator classFileLocator = spy(ClassFileLocator.ForClassLoader.ofSystemLoader());
    new ByteBuddy()
            .with(TypeValidation.DISABLED)
            .with(MethodGraph.Empty.INSTANCE)
            .with(InstrumentedType.Factory.Default.FROZEN)
            .redefine(describe(GenericType.class, classFileLocator, new TypePool.CacheProvider.Simple()), classFileLocator)
            .make();
    verify(classFileLocator, times(2)).locate(GenericType.class.getName());
    verifyNoMoreInteractions(classFileLocator);
}
 
Example #26
Source File: TypePoolDefaultWithLazyResolutionTypeDescriptionTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testNonGenericResolutionIsLazyForSimpleCreation() throws Exception {
    ClassFileLocator classFileLocator = spy(ClassFileLocator.ForClassLoader.ofSystemLoader());
    new ByteBuddy()
            .with(TypeValidation.DISABLED)
            .with(MethodGraph.Empty.INSTANCE)
            .with(InstrumentedType.Factory.Default.FROZEN)
            .redefine(describe(NonGenericType.class, classFileLocator, new TypePool.CacheProvider.Simple()), classFileLocator)
            .make();
    verify(classFileLocator, times(2)).locate(NonGenericType.class.getName());
    verifyNoMoreInteractions(classFileLocator);
}
 
Example #27
Source File: TypePoolDefaultWithLazyResolutionTypeDescriptionTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testNonGenericResolutionIsLazyForSimpleCreationNonFrozen() throws Exception {
    ClassFileLocator classFileLocator = spy(ClassFileLocator.ForClassLoader.ofSystemLoader());
    new ByteBuddy()
            .with(TypeValidation.DISABLED)
            .with(MethodGraph.Empty.INSTANCE)
            .with(InstrumentedType.Factory.Default.MODIFIABLE)
            .redefine(describe(NonGenericType.class, classFileLocator, new TypePool.CacheProvider.Simple()), classFileLocator)
            .make();
    verify(classFileLocator, times(2)).locate(NonGenericType.class.getName());
    verifyNoMoreInteractions(classFileLocator);
}
 
Example #28
Source File: DecoratingDynamicTypeBuilder.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new decorating dynamic type builder.
 *
 * @param instrumentedType             The instrumented type to decorate.
 * @param typeAttributeAppender        The type attribute appender to apply onto the instrumented type.
 * @param asmVisitorWrapper            The ASM visitor wrapper to apply onto the class writer.
 * @param classFileVersion             The class file version to define auxiliary types in.
 * @param auxiliaryTypeNamingStrategy  The naming strategy for auxiliary types to apply.
 * @param annotationValueFilterFactory The annotation value filter factory to apply.
 * @param annotationRetention          The annotation retention to apply.
 * @param implementationContextFactory The implementation context factory to apply.
 * @param methodGraphCompiler          The method graph compiler to use.
 * @param typeValidation               Determines if a type should be explicitly validated.
 * @param classWriterStrategy          The class writer strategy to use.
 * @param ignoredMethods               A matcher for identifying methods that should be excluded from instrumentation.
 * @param auxiliaryTypes               A list of explicitly required auxiliary types.
 * @param classFileLocator             The class file locator for locating the original type's class file.
 */
protected DecoratingDynamicTypeBuilder(TypeDescription instrumentedType,
                                       TypeAttributeAppender typeAttributeAppender,
                                       AsmVisitorWrapper asmVisitorWrapper,
                                       ClassFileVersion classFileVersion,
                                       AuxiliaryType.NamingStrategy auxiliaryTypeNamingStrategy,
                                       AnnotationValueFilter.Factory annotationValueFilterFactory,
                                       AnnotationRetention annotationRetention,
                                       Implementation.Context.Factory implementationContextFactory,
                                       MethodGraph.Compiler methodGraphCompiler,
                                       TypeValidation typeValidation,
                                       ClassWriterStrategy classWriterStrategy,
                                       LatentMatcher<? super MethodDescription> ignoredMethods,
                                       List<DynamicType> auxiliaryTypes,
                                       ClassFileLocator classFileLocator) {
    this.instrumentedType = instrumentedType;
    this.typeAttributeAppender = typeAttributeAppender;
    this.asmVisitorWrapper = asmVisitorWrapper;
    this.classFileVersion = classFileVersion;
    this.auxiliaryTypeNamingStrategy = auxiliaryTypeNamingStrategy;
    this.annotationValueFilterFactory = annotationValueFilterFactory;
    this.annotationRetention = annotationRetention;
    this.implementationContextFactory = implementationContextFactory;
    this.methodGraphCompiler = methodGraphCompiler;
    this.typeValidation = typeValidation;
    this.classWriterStrategy = classWriterStrategy;
    this.ignoredMethods = ignoredMethods;
    this.auxiliaryTypes = auxiliaryTypes;
    this.classFileLocator = classFileLocator;
}
 
Example #29
Source File: DecoratingDynamicTypeBuilder.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new decorating dynamic type builder.
 *
 * @param instrumentedType             The instrumented type to decorate.
 * @param classFileVersion             The class file version to define auxiliary types in.
 * @param auxiliaryTypeNamingStrategy  The naming strategy for auxiliary types to apply.
 * @param annotationValueFilterFactory The annotation value filter factory to apply.
 * @param annotationRetention          The annotation retention to apply.
 * @param implementationContextFactory The implementation context factory to apply.
 * @param methodGraphCompiler          The method graph compiler to use.
 * @param typeValidation               Determines if a type should be explicitly validated.
 * @param classWriterStrategy          The class writer strategy to use.
 * @param ignoredMethods               A matcher for identifying methods that should be excluded from instrumentation.
 * @param classFileLocator             The class file locator for locating the original type's class file.
 */
public DecoratingDynamicTypeBuilder(TypeDescription instrumentedType,
                                    ClassFileVersion classFileVersion,
                                    AuxiliaryType.NamingStrategy auxiliaryTypeNamingStrategy,
                                    AnnotationValueFilter.Factory annotationValueFilterFactory,
                                    AnnotationRetention annotationRetention,
                                    Implementation.Context.Factory implementationContextFactory,
                                    MethodGraph.Compiler methodGraphCompiler,
                                    TypeValidation typeValidation,
                                    ClassWriterStrategy classWriterStrategy,
                                    LatentMatcher<? super MethodDescription> ignoredMethods,
                                    ClassFileLocator classFileLocator) {
    this(instrumentedType,
            annotationRetention.isEnabled()
                    ? new TypeAttributeAppender.ForInstrumentedType.Differentiating(instrumentedType)
                    : TypeAttributeAppender.ForInstrumentedType.INSTANCE,
            AsmVisitorWrapper.NoOp.INSTANCE,
            classFileVersion,
            auxiliaryTypeNamingStrategy,
            annotationValueFilterFactory,
            annotationRetention,
            implementationContextFactory,
            methodGraphCompiler,
            typeValidation,
            classWriterStrategy,
            ignoredMethods,
            Collections.<DynamicType>emptyList(),
            classFileLocator);
}
 
Example #30
Source File: TypeProxy.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@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();
}