net.bytebuddy.description.modifier.Visibility Java Examples

The following examples show how to use net.bytebuddy.description.modifier.Visibility. 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: EqualsMethodOtherTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #2
Source File: MethodCaptor.java    From reflection-util with Apache License 2.0 6 votes vote down vote up
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);
	}
}
 
Example #3
Source File: MethodGraphCompilerDefaultTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #4
Source File: InvocationHandlerAdapterTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #5
Source File: AdviceInconsistentFrameTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #6
Source File: EqualsMethodOtherTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #7
Source File: EqualsMethodOtherTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #8
Source File: MethodGraphCompilerDefaultTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #9
Source File: TypeWriterMethodPoolRecordTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #10
Source File: EqualsMethodTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #11
Source File: AdviceInconsistentFrameTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #12
Source File: EqualsMethodOtherTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #13
Source File: AdviceInconsistentFrameTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #14
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 #15
Source File: MethodGraphCompilerDefaultTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #16
Source File: AdviceInconsistentFrameTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #17
Source File: AdviceInconsistentFrameTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #18
Source File: AdviceInconsistentFrameTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #19
Source File: TypeWriterMethodPoolRecordTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #20
Source File: MethodGraphCompilerDefaultTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #21
Source File: AdviceInconsistentFrameTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #22
Source File: EqualsMethodOtherTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #23
Source File: ToStringMethodTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@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 + "}"));
}
 
Example #24
Source File: EqualsMethodOtherTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #25
Source File: ClassInjectorUsingLookupTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    type = new ByteBuddy()
            .subclass(Object.class)
            .name("net.bytebuddy.test.Foo")
            .defineMethod("lookup", Object.class, Ownership.STATIC, Visibility.PUBLIC)
            .intercept(MethodCall.invoke(Class.forName("java.lang.invoke.MethodHandles").getMethod("lookup")))
            .make()
            .load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER)
            .getLoaded();
}
 
Example #26
Source File: AdviceJsrRetTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #27
Source File: AdviceDeadCodeTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #28
Source File: HashCodeMethodTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #29
Source File: EqualsMethodOtherTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testSuperMethod() throws Exception {
    DynamicType.Loaded<?> loaded = new ByteBuddy()
            .subclass(EqualsBase.class)
            .defineField(FOO, Object.class, Visibility.PUBLIC)
            .method(isEquals())
            .intercept(EqualsMethod.requiringSuperClassEquality())
            .make()
            .load(EqualsBase.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, is(right));
}
 
Example #30
Source File: MethodGraphCompilerDefaultTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenericWithReturnTypeInterfaceSingleEvolution() throws Exception {
    TypeDescription typeDescription = TypeDescription.ForLoadedType.of(GenericWithReturnTypeInterfaceBase.Inner.class);
    MethodGraph.Linked methodGraph = MethodGraph.Compiler.Default.forJavaHierarchy().compile(typeDescription);
    assertThat(methodGraph.listNodes().size(), is(1));
    MethodDescription.SignatureToken token = typeDescription.getDeclaredMethods().filter(isMethod().and(ElementMatchers.not(isBridge()))).getOnly().asSignatureToken();
    MethodGraph.Node node = methodGraph.locate(token);
    MethodDescription.SignatureToken bridgeToken = typeDescription.getInterfaces().getOnly()
            .getDeclaredMethods().filter(isMethod()).getOnly().asDefined().asSignatureToken();
    assertThat(node, is(methodGraph.locate(bridgeToken)));
    assertThat(node.getSort(), is(MethodGraph.Node.Sort.RESOLVED));
    assertThat(node.getMethodTypes().size(), is(2));
    assertThat(node.getMethodTypes().contains(token.asTypeToken()), is(true));
    assertThat(node.getMethodTypes().contains(bridgeToken.asTypeToken()), is(true));
    assertThat(node.getVisibility(), is(Visibility.PUBLIC));
}