下面列出了javax.persistence.Embedded#net.bytebuddy.description.modifier.Visibility 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testAnnotationDefinition() throws Exception {
Class<? extends Annotation> type = new ByteBuddy()
.makeAnnotation()
.defineMethod(FOO, int.class, Visibility.PUBLIC)
.withoutCode()
.defineMethod(BAR, String.class, Visibility.PUBLIC)
.defaultValue(FOO, String.class)
.defineMethod(QUX, SimpleEnum.class, Visibility.PUBLIC)
.defaultValue(SimpleEnum.FIRST, SimpleEnum.class)
.make()
.load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
assertThat(type.getDeclaredMethods().length, is(3));
assertThat(type.getDeclaredMethod(FOO), notNullValue(Method.class));
assertThat(type.getDeclaredMethod(BAR).getDefaultValue(), is((Object) FOO));
assertThat(type.getDeclaredMethod(QUX).getDefaultValue(), is((Object) SimpleEnum.FIRST));
assertThat(type.getDeclaredConstructors().length, is(0));
assertThat(Annotation.class.isAssignableFrom(type), is(true));
assertThat(type, not(CoreMatchers.<Class<?>>is(Annotation.class)));
assertThat(type.isInterface(), is(true));
assertThat(type.isAnnotation(), is(true));
}
@Test
@JavaVersionRule.Enforce(7)
public void testFrameInconsistentParameterTrivial() throws Exception {
Class<?> type = new ByteBuddy()
.subclass(Object.class)
.defineMethod(FOO, String.class, Visibility.PUBLIC, Ownership.STATIC)
.withParameters(Void.class)
.intercept(new InconsistentParameterReferenceMethod())
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER_PERSISTENT)
.getLoaded();
assertThat(type.getDeclaredMethod(FOO, Void.class).invoke(null, (Object) null), is((Object) BAR));
new ByteBuddy()
.redefine(type)
.visit(Advice.to(trivial).on(named(FOO)))
.make();
}
@Test
public void testInstanceAdapterWithMethodCache() throws Exception {
DynamicType.Loaded<Bar> loaded = new ByteBuddy()
.subclass(Bar.class)
.defineField(QUX, InvocationHandler.class, Visibility.PUBLIC)
.method(isDeclaredBy(Bar.class))
.intercept(InvocationHandlerAdapter.toField(QUX))
.make()
.load(Bar.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0));
assertThat(loaded.getLoaded().getDeclaredMethods().length, is(1));
assertThat(loaded.getLoaded().getDeclaredFields().length, is(2));
Field field = loaded.getLoaded().getDeclaredField(QUX);
assertThat(field.getModifiers(), is(Modifier.PUBLIC));
field.setAccessible(true);
Bar instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
Foo foo = new Foo();
field.set(instance, foo);
assertThat(instance.bar(FOO), is((Object) instance));
assertThat(foo.methods.size(), is(1));
assertThat(instance.bar(FOO), is((Object) instance));
assertThat(foo.methods.size(), is(2));
assertThat(foo.methods.get(0), sameInstance(foo.methods.get(1)));
instance.assertZeroCalls();
}
@Test
@JavaVersionRule.Enforce(7)
public void testFrameDropImplicitTrivialCopying() throws Exception {
Class<?> type = new ByteBuddy()
.subclass(Object.class)
.defineMethod(FOO, String.class, Visibility.PUBLIC)
.intercept(new DropImplicitMethod())
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER_PERSISTENT)
.getLoaded();
assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is((Object) BAR));
new ByteBuddy()
.redefine(type)
.visit(Advice.to(copying).on(named(FOO)))
.make();
}
@Test
public void testTypeOrderForEnumerationTypedFields() throws Exception {
DynamicType.Loaded<?> loaded = new ByteBuddy()
.subclass(Object.class)
.defineField(FOO, Object.class, Visibility.PUBLIC)
.defineField(BAR, RetentionPolicy.class, Visibility.PUBLIC)
.method(isEquals())
.intercept(EqualsMethod.isolated().withNonNullableFields(any()).withEnumerationTypedFieldsFirst())
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER);
assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0));
assertThat(loaded.getLoaded().getDeclaredMethods().length, is(1));
assertThat(loaded.getLoaded().getDeclaredFields().length, is(2));
Object left = loaded.getLoaded().getDeclaredConstructor().newInstance(), right = loaded.getLoaded().getDeclaredConstructor().newInstance();
left.getClass().getDeclaredField(BAR).set(left, RetentionPolicy.RUNTIME);
right.getClass().getDeclaredField(BAR).set(right, RetentionPolicy.CLASS);
assertThat(left, not(right));
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public BasicProxyFactoryImpl(Class superClass, Class[] interfaces, ByteBuddyState byteBuddyState) {
if ( superClass == null && ( interfaces == null || interfaces.length < 1 ) ) {
throw new AssertionFailure( "attempting to build proxy without any superclass or interfaces" );
}
final Class<?> superClassOrMainInterface = superClass != null ? superClass : interfaces[0];
final TypeCache.SimpleKey cacheKey = getCacheKey( superClass, interfaces );
this.proxyClass = byteBuddyState.loadBasicProxy( superClassOrMainInterface, cacheKey, byteBuddy -> byteBuddy
.with( new NamingStrategy.SuffixingRandom( PROXY_NAMING_SUFFIX, new NamingStrategy.SuffixingRandom.BaseNameResolver.ForFixedValue( superClassOrMainInterface.getName() ) ) )
.subclass( superClass == null ? Object.class : superClass, ConstructorStrategy.Default.DEFAULT_CONSTRUCTOR )
.implement( interfaces == null ? NO_INTERFACES : interfaces )
.defineField( ProxyConfiguration.INTERCEPTOR_FIELD_NAME, ProxyConfiguration.Interceptor.class, Visibility.PRIVATE )
.method( byteBuddyState.getProxyDefinitionHelpers().getVirtualNotFinalizerFilter() )
.intercept( byteBuddyState.getProxyDefinitionHelpers().getDelegateToInterceptorDispatcherMethodDelegation() )
.implement( ProxyConfiguration.class )
.intercept( byteBuddyState.getProxyDefinitionHelpers().getInterceptorFieldAccessor() )
);
this.interceptor = new PassThroughInterceptor( proxyClass.getName() );
}
@Test
@JavaVersionRule.Enforce(7)
public void testFrameDropImplicitTrivialDiscarding() throws Exception {
Class<?> type = new ByteBuddy()
.subclass(Object.class)
.defineMethod(FOO, String.class, Visibility.PUBLIC)
.intercept(new DropImplicitMethod())
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER_PERSISTENT)
.getLoaded();
assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is((Object) BAR));
new ByteBuddy()
.redefine(type)
.visit(Advice.to(discarding).on(named(FOO)))
.make();
}
static <T> Class<? extends T> createProxyClass(Class<T> beanClass) {
try {
return new ByteBuddy()
.subclass(beanClass, ConstructorStrategy.Default.NO_CONSTRUCTORS)
.defineField(MethodCaptor.FIELD_NAME, MethodCaptor.class, Visibility.PRIVATE)
.method(isMethod()
.and(takesArguments(0))
.and(not(isDeclaredBy(Object.class))))
.intercept(MethodDelegation.to(MethodCaptor.class))
.make()
.load(PropertyUtils.class.getClassLoader())
.getLoaded();
} catch (IllegalAccessError e) {
throw new ReflectionRuntimeException("Failed to create proxy on " + beanClass, e);
}
}
@Test
public void testGenericClassMultipleEvolution() throws Exception {
TypeDescription typeDescription = TypeDescription.ForLoadedType.of(GenericClassBase.Intermediate.Inner.class);
MethodGraph.Linked methodGraph = MethodGraph.Compiler.Default.forJavaHierarchy().compile(typeDescription);
assertThat(methodGraph.listNodes().size(), is(TypeDescription.OBJECT.getDeclaredMethods().filter(isVirtual()).size() + 1));
MethodDescription.SignatureToken token = typeDescription.getDeclaredMethods().filter(isMethod().and(ElementMatchers.not(isBridge()))).getOnly().asSignatureToken();
MethodGraph.Node node = methodGraph.locate(token);
MethodDescription.SignatureToken firstBridgeToken = typeDescription.getSuperClass().getDeclaredMethods()
.filter(isMethod().and(ElementMatchers.not(isBridge()))).getOnly().asDefined().asSignatureToken();
MethodDescription.SignatureToken secondBridgeToken = typeDescription.getSuperClass().getSuperClass().getDeclaredMethods()
.filter(isMethod()).getOnly().asDefined().asSignatureToken();
assertThat(node, is(methodGraph.locate(firstBridgeToken)));
assertThat(node, is(methodGraph.locate(secondBridgeToken)));
assertThat(node.getSort(), is(MethodGraph.Node.Sort.RESOLVED));
assertThat(node.getMethodTypes().size(), is(3));
assertThat(node.getMethodTypes().contains(token.asTypeToken()), is(true));
assertThat(node.getMethodTypes().contains(firstBridgeToken.asTypeToken()), is(true));
assertThat(node.getMethodTypes().contains(secondBridgeToken.asTypeToken()), is(true));
assertThat(node.getVisibility(), is(Visibility.PUBLIC));
}
@Test
public void testEqualToSelfIdentity() throws Exception {
DynamicType.Loaded<?> loaded = new ByteBuddy()
.subclass(Object.class)
.defineField(FOO, Object.class, Visibility.PUBLIC)
.method(isEquals())
.intercept(EqualsMethod.isolated())
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER);
assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0));
assertThat(loaded.getLoaded().getDeclaredMethods().length, is(1));
assertThat(loaded.getLoaded().getDeclaredFields().length, is(1));
Object instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
loaded.getLoaded().getDeclaredField(FOO).set(instance, new NonEqualsBase());
assertThat(instance, is(instance));
}
@Before
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
when(methodDescription.getInternalName()).thenReturn(FOO);
when(methodDescription.getDescriptor()).thenReturn(BAR);
when(methodDescription.getGenericSignature()).thenReturn(QUX);
when(methodDescription.getExceptionTypes()).thenReturn(exceptionTypes);
when(methodDescription.getActualModifiers(anyBoolean(), any(Visibility.class))).thenReturn(MODIFIERS);
when(exceptionTypes.asErasures()).thenReturn(rawExceptionTypes);
when(rawExceptionTypes.toInternalNames()).thenReturn(new String[]{BAZ});
when(classVisitor.visitMethod(MODIFIERS, FOO, BAR, QUX, new String[]{BAZ})).thenReturn(methodVisitor);
when(methodDescription.getParameters()).thenReturn((ParameterList) new ParameterList.Explicit<ParameterDescription>(parameterDescription));
when(parameterDescription.getName()).thenReturn(FOO);
when(parameterDescription.getModifiers()).thenReturn(MODIFIERS);
when(methodVisitor.visitAnnotationDefault()).thenReturn(annotationVisitor);
when(byteCodeAppender.apply(methodVisitor, implementationContext, methodDescription))
.thenReturn(new ByteCodeAppender.Size(ONE, TWO));
when(otherAppender.apply(methodVisitor, implementationContext, methodDescription))
.thenReturn(new ByteCodeAppender.Size(ONE * MULTIPLIER, TWO * MULTIPLIER));
when(annotationValueFilterFactory.on(methodDescription)).thenReturn(annotationValueFilter);
when(methodDescription.getVisibility()).thenReturn(Visibility.PUBLIC);
}
@Test
public void testGenericWithReturnTypeInterfaceMultipleEvolution() throws Exception {
TypeDescription typeDescription = TypeDescription.ForLoadedType.of(GenericWithReturnTypeInterfaceBase.Intermediate.Inner.class);
MethodGraph.Linked methodGraph = MethodGraph.Compiler.Default.forJavaHierarchy().compile(typeDescription);
assertThat(methodGraph.listNodes().size(), is(1));
MethodDescription.SignatureToken token = typeDescription.getDeclaredMethods().filter(ElementMatchers.not(isBridge())).getOnly().asSignatureToken();
MethodGraph.Node node = methodGraph.locate(token);
MethodDescription.SignatureToken firstBridgeToken = typeDescription.getInterfaces().getOnly()
.getDeclaredMethods().filter(ElementMatchers.not(isBridge())).getOnly().asDefined().asSignatureToken();
MethodDescription.SignatureToken secondBridgeToken = typeDescription.getInterfaces().getOnly().getInterfaces().getOnly()
.getDeclaredMethods().getOnly().asDefined().asSignatureToken();
assertThat(node, is(methodGraph.locate(firstBridgeToken)));
assertThat(node, is(methodGraph.locate(secondBridgeToken)));
assertThat(node.getSort(), is(MethodGraph.Node.Sort.RESOLVED));
assertThat(node.getMethodTypes().size(), is(3));
assertThat(node.getMethodTypes().contains(token.asTypeToken()), is(true));
assertThat(node.getMethodTypes().contains(firstBridgeToken.asTypeToken()), is(true));
assertThat(node.getMethodTypes().contains(secondBridgeToken.asTypeToken()), is(true));
assertThat(node.getVisibility(), is(Visibility.PUBLIC));
}
@Test
public void testEqualsFalse() throws Exception {
DynamicType.Loaded<?> loaded = new ByteBuddy()
.subclass(Object.class)
.defineField(FOO, type, Visibility.PUBLIC)
.method(isEquals())
.intercept(EqualsMethod.isolated())
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER);
assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0));
assertThat(loaded.getLoaded().getDeclaredMethods().length, is(1));
assertThat(loaded.getLoaded().getDeclaredFields().length, is(1));
Object first = loaded.getLoaded().getDeclaredConstructor().newInstance();
Object second = loaded.getLoaded().getDeclaredConstructor().newInstance();
first.getClass().getDeclaredField(FOO).set(first, value);
second.getClass().getDeclaredField(FOO).set(second, alternative);
assertThat(first, not(second));
}
@Test
@JavaVersionRule.Enforce(7)
public void testFrameDropImplicitTrivial() throws Exception {
Class<?> type = new ByteBuddy()
.subclass(Object.class)
.defineMethod(FOO, String.class, Visibility.PUBLIC)
.intercept(new DropImplicitMethod())
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER_PERSISTENT)
.getLoaded();
assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is((Object) BAR));
new ByteBuddy()
.redefine(type)
.visit(Advice.to(trivial).on(named(FOO)))
.make();
}
@Test
public void testTypeOrderForStringTypedFields() throws Exception {
DynamicType.Loaded<?> loaded = new ByteBuddy()
.subclass(Object.class)
.defineField(FOO, Object.class, Visibility.PUBLIC)
.defineField(BAR, String.class, Visibility.PUBLIC)
.method(isEquals())
.intercept(EqualsMethod.isolated().withNonNullableFields(any()).withStringTypedFieldsFirst())
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER);
assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0));
assertThat(loaded.getLoaded().getDeclaredMethods().length, is(1));
assertThat(loaded.getLoaded().getDeclaredFields().length, is(2));
Object left = loaded.getLoaded().getDeclaredConstructor().newInstance(), right = loaded.getLoaded().getDeclaredConstructor().newInstance();
left.getClass().getDeclaredField(BAR).set(left, FOO);
right.getClass().getDeclaredField(BAR).set(right, BAR);
assertThat(left, not(right));
}
@Test
@JavaVersionRule.Enforce(7)
public void testFrameTooShortTrivial() throws Exception {
Class<?> type = new ByteBuddy()
.subclass(Object.class)
.defineMethod(FOO, String.class, Visibility.PUBLIC)
.intercept(new TooShortMethod())
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER_PERSISTENT)
.getLoaded();
assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is((Object) BAR));
new ByteBuddy()
.redefine(type)
.visit(Advice.to(trivial).on(named(FOO)))
.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();
}
@Test
public void testReturnTypeClassMultipleEvolution() throws Exception {
TypeDescription typeDescription = TypeDescription.ForLoadedType.of(ReturnTypeClassBase.Intermediate.Inner.class);
MethodGraph.Linked methodGraph = MethodGraph.Compiler.Default.forJavaHierarchy().compile(typeDescription);
assertThat(methodGraph.listNodes().size(), is(TypeDescription.OBJECT.getDeclaredMethods().filter(isVirtual()).size() + 1));
MethodDescription.SignatureToken token = typeDescription.getDeclaredMethods().filter(isMethod().and(ElementMatchers.not(isBridge()))).getOnly().asSignatureToken();
MethodGraph.Node node = methodGraph.locate(token);
MethodDescription.SignatureToken firstBridgeToken = typeDescription.getSuperClass().getDeclaredMethods()
.filter(isMethod().and(ElementMatchers.not(isBridge()))).getOnly().asDefined().asSignatureToken();
MethodDescription.SignatureToken secondBridgeToken = typeDescription.getSuperClass().getSuperClass().getDeclaredMethods()
.filter(isMethod()).getOnly().asDefined().asSignatureToken();
assertThat(node, is(methodGraph.locate(firstBridgeToken)));
assertThat(node, is(methodGraph.locate(secondBridgeToken)));
assertThat(node.getSort(), is(MethodGraph.Node.Sort.RESOLVED));
assertThat(node.getMethodTypes().size(), is(3));
assertThat(node.getMethodTypes().contains(token.asTypeToken()), is(true));
assertThat(node.getMethodTypes().contains(firstBridgeToken.asTypeToken()), is(true));
assertThat(node.getMethodTypes().contains(secondBridgeToken.asTypeToken()), is(true));
assertThat(node.getVisibility(), is(Visibility.PUBLIC));
}
@Test
public void testSuperMethodNoMatch() throws Exception {
DynamicType.Loaded<?> loaded = new ByteBuddy()
.subclass(NonEqualsBase.class)
.defineField(FOO, Object.class, Visibility.PUBLIC)
.method(isEquals())
.intercept(EqualsMethod.requiringSuperClassEquality())
.make()
.load(NonEqualsBase.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0));
assertThat(loaded.getLoaded().getDeclaredMethods().length, is(1));
assertThat(loaded.getLoaded().getDeclaredFields().length, is(1));
Object left = loaded.getLoaded().getDeclaredConstructor().newInstance(), right = loaded.getLoaded().getDeclaredConstructor().newInstance();
left.getClass().getDeclaredField(FOO).set(left, FOO);
right.getClass().getDeclaredField(FOO).set(right, FOO);
assertThat(left, not(right));
}
@Test(expected = IllegalStateException.class)
@JavaVersionRule.Enforce(7)
public void testFrameTooShort() throws Exception {
Class<?> type = new ByteBuddy()
.subclass(Object.class)
.defineMethod(FOO, String.class, Visibility.PUBLIC)
.intercept(new TooShortMethod())
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER_PERSISTENT)
.getLoaded();
assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is((Object) BAR));
new ByteBuddy()
.redefine(type)
.visit(Advice.to(retaining).on(named(FOO)))
.make();
}
@Test(expected = IllegalStateException.class)
@JavaVersionRule.Enforce(7)
public void testFrameInconsistentThisParameter() throws Exception {
Class<?> type = new ByteBuddy()
.subclass(Object.class)
.defineMethod(FOO, String.class, Visibility.PUBLIC)
.intercept(new InconsistentThisReferenceMethod())
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER_PERSISTENT)
.getLoaded();
assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is((Object) BAR));
new ByteBuddy()
.redefine(type)
.visit(Advice.to(retaining).on(named(FOO)))
.make();
}
@Test
public void testImplementedMethodPrepended() throws Exception {
TypeWriter.MethodPool.Record record = new TypeWriter.MethodPool.Record.ForDefinedMethod.WithBody(methodDescription,
byteCodeAppender,
methodAttributeAppender,
Visibility.PUBLIC)
.prepend(otherAppender);
assertThat(record.getSort(), is(TypeWriter.MethodPool.Record.Sort.IMPLEMENTED));
record.apply(classVisitor, implementationContext, annotationValueFilterFactory);
verify(classVisitor).visitMethod(MODIFIERS, FOO, BAR, QUX, new String[]{BAZ});
verifyNoMoreInteractions(classVisitor);
verify(methodVisitor).visitCode();
verify(methodVisitor).visitMaxs(ONE * MULTIPLIER, TWO * MULTIPLIER);
verify(methodVisitor).visitEnd();
verifyNoMoreInteractions(methodVisitor);
verifyZeroInteractions(implementationContext);
verify(methodAttributeAppender).apply(methodVisitor, methodDescription, annotationValueFilter);
verifyNoMoreInteractions(methodAttributeAppender);
verify(byteCodeAppender).apply(methodVisitor, implementationContext, methodDescription);
verifyNoMoreInteractions(byteCodeAppender);
verify(otherAppender).apply(methodVisitor, implementationContext, methodDescription);
verifyNoMoreInteractions(otherAppender);
}
@Test
public void testGenericReturnTypeClassMultipleEvolution() throws Exception {
TypeDescription typeDescription = TypeDescription.ForLoadedType.of(GenericReturnClassBase.Intermediate.Inner.class);
MethodGraph.Linked methodGraph = MethodGraph.Compiler.Default.forJavaHierarchy().compile(typeDescription);
assertThat(methodGraph.listNodes().size(), is(TypeDescription.OBJECT.getDeclaredMethods().filter(isVirtual()).size() + 1));
MethodDescription.SignatureToken token = typeDescription.getDeclaredMethods().filter(isMethod().and(ElementMatchers.not(isBridge()))).getOnly().asSignatureToken();
MethodGraph.Node node = methodGraph.locate(token);
MethodDescription.SignatureToken firstBridgeToken = typeDescription.getSuperClass().getDeclaredMethods()
.filter(isMethod().and(ElementMatchers.not(isBridge()))).getOnly().asDefined().asSignatureToken();
MethodDescription.SignatureToken secondBridgeToken = typeDescription.getSuperClass().getSuperClass().getDeclaredMethods()
.filter(isMethod()).getOnly().asDefined().asSignatureToken();
assertThat(node, is(methodGraph.locate(firstBridgeToken)));
assertThat(node, is(methodGraph.locate(secondBridgeToken)));
assertThat(node.getSort(), is(MethodGraph.Node.Sort.RESOLVED));
assertThat(node.getMethodTypes().size(), is(3));
assertThat(node.getMethodTypes().contains(token.asTypeToken()), is(true));
assertThat(node.getMethodTypes().contains(firstBridgeToken.asTypeToken()), is(true));
assertThat(node.getMethodTypes().contains(secondBridgeToken.asTypeToken()), is(true));
assertThat(node.getVisibility(), is(Visibility.PUBLIC));
}
@Test
@JavaVersionRule.Enforce(7)
public void testFrameInconsistentParameterTrivialDiscarding() throws Exception {
Class<?> type = new ByteBuddy()
.subclass(Object.class)
.defineMethod(FOO, String.class, Visibility.PUBLIC, Ownership.STATIC)
.withParameters(Void.class)
.intercept(new InconsistentParameterReferenceMethod())
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER_PERSISTENT)
.getLoaded();
assertThat(type.getDeclaredMethod(FOO, Void.class).invoke(null, (Object) null), is((Object) BAR));
new ByteBuddy()
.redefine(type)
.visit(Advice.to(discarding).on(named(FOO)))
.make();
}
@Test
public void testTypeOrderForPrimitiveWrapperTypes() throws Exception {
DynamicType.Loaded<?> loaded = new ByteBuddy()
.subclass(Object.class)
.defineField(FOO, Object.class, Visibility.PUBLIC)
.defineField(BAR, Integer.class, Visibility.PUBLIC)
.method(isEquals())
.intercept(EqualsMethod.isolated().withNonNullableFields(any()).withPrimitiveWrapperTypedFieldsFirst())
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER);
assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0));
assertThat(loaded.getLoaded().getDeclaredMethods().length, is(1));
assertThat(loaded.getLoaded().getDeclaredFields().length, is(2));
Object left = loaded.getLoaded().getDeclaredConstructor().newInstance(), right = loaded.getLoaded().getDeclaredConstructor().newInstance();
left.getClass().getDeclaredField(BAR).set(left, 42);
right.getClass().getDeclaredField(BAR).set(right, 84);
assertThat(left, not(right));
}
@Test
public void testEqualsTrue() throws Exception {
DynamicType.Loaded<?> loaded = new ByteBuddy()
.subclass(Object.class)
.defineField(FOO, type, Visibility.PUBLIC)
.method(isToString())
.intercept(ToStringMethod.prefixedBy(FOO))
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER);
assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0));
assertThat(loaded.getLoaded().getDeclaredMethods().length, is(1));
assertThat(loaded.getLoaded().getDeclaredFields().length, is(1));
Object instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
instance.getClass().getDeclaredField(FOO).set(instance, value);
assertThat(instance.toString(), is(FOO + "{" + FOO + "=" + string + "}"));
}
@Test
public void testIgnoredField() throws Exception {
DynamicType.Loaded<?> loaded = new ByteBuddy()
.subclass(Object.class)
.defineField(FOO, Object.class, Visibility.PUBLIC)
.method(isEquals())
.intercept(EqualsMethod.isolated().withIgnoredFields(named(FOO)))
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER);
assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0));
assertThat(loaded.getLoaded().getDeclaredMethods().length, is(1));
assertThat(loaded.getLoaded().getDeclaredFields().length, is(1));
Object left = loaded.getLoaded().getDeclaredConstructor().newInstance(), right = loaded.getLoaded().getDeclaredConstructor().newInstance();
left.getClass().getDeclaredField(FOO).set(left, FOO);
left.getClass().getDeclaredField(FOO).set(left, BAR);
assertThat(left, is(right));
}
@Test
public void testJsrRetByteCodes() throws Exception {
Class<?> type = new ByteBuddy(ClassFileVersion.JAVA_V4)
.subclass(Object.class)
.defineMethod(FOO, String.class, Visibility.PUBLIC)
.intercept(new JsrRetMethod())
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER_PERSISTENT)
.getLoaded();
assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is((Object) FOO));
Class<?> advised = new ByteBuddy()
.redefine(type)
.visit(Advice.to(JsrAdvice.class).on(named(FOO)))
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
assertThat(advised.getDeclaredMethod(FOO).invoke(advised.getDeclaredConstructor().newInstance()), is((Object) BAR));
}
@Test
public void testAdviceProcessesDeadCode() throws Exception {
Class<?> type = new ByteBuddy(classFileVersion)
.subclass(Object.class)
.defineMethod(FOO, String.class, Visibility.PUBLIC)
.intercept(new DeadStringAppender())
.make()
.load(null, ClassLoadingStrategy.Default.WRAPPER_PERSISTENT)
.getLoaded();
Class<?> redefined = new ByteBuddy()
.redefine(type)
.visit(Advice.to(ExitAdvice.class).on(named(FOO)))
.make()
.load(null, ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
assertThat(redefined.getDeclaredMethod(FOO).invoke(redefined.getDeclaredConstructor().newInstance()), is((Object) FOO));
}
@Test
public void testHashCode() throws Exception {
DynamicType.Loaded<?> loaded = new ByteBuddy()
.subclass(Object.class)
.defineField(FOO, type, Visibility.PUBLIC)
.method(isHashCode())
.intercept(HashCodeMethod.usingOffset(0))
.make()
.load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER);
assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0));
assertThat(loaded.getLoaded().getDeclaredMethods().length, is(1));
assertThat(loaded.getLoaded().getDeclaredFields().length, is(1));
Object instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
instance.getClass().getDeclaredField(FOO).set(instance, value);
assertThat(instance.hashCode(), is(hashOffset));
}