net.bytebuddy.description.field.FieldDescription Java Examples

The following examples show how to use net.bytebuddy.description.field.FieldDescription. 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: InstrumentedType.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public InstrumentedType.WithFlexibleName subclass(String name, int modifiers, TypeDescription.Generic superClass) {
    return new InstrumentedType.Default(name,
            modifiers,
            superClass,
            Collections.<TypeVariableToken>emptyList(),
            Collections.<Generic>emptyList(),
            Collections.<FieldDescription.Token>emptyList(),
            Collections.<MethodDescription.Token>emptyList(),
            Collections.<RecordComponentDescription.Token>emptyList(),
            Collections.<AnnotationDescription>emptyList(),
            TypeInitializer.None.INSTANCE,
            LoadedTypeInitializer.NoOp.INSTANCE,
            TypeDescription.UNDEFINED,
            MethodDescription.UNDEFINED,
            TypeDescription.UNDEFINED,
            Collections.<TypeDescription>emptyList(),
            false,
            false,
            false,
            TargetType.DESCRIPTION,
            Collections.<TypeDescription>emptyList());
}
 
Example #2
Source File: TypeConstantAdjustmentTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testInstrumentationLegacyClassFileObjectType() throws Exception {
    ClassVisitor classVisitor = TypeConstantAdjustment.INSTANCE.wrap(mock(TypeDescription.class),
            this.classVisitor,
            mock(Implementation.Context.class),
            mock(TypePool.class),
            new FieldList.Empty<FieldDescription.InDefinedShape>(),
            new MethodList.Empty<MethodDescription>(),
            IGNORED,
            IGNORED);
    classVisitor.visit(ClassFileVersion.JAVA_V4.getMinorMajorVersion(), FOOBAR, FOO, BAR, QUX, new String[]{BAZ});
    MethodVisitor methodVisitor = classVisitor.visitMethod(FOOBAR, FOO, BAR, QUX, new String[]{BAZ});
    assertThat(methodVisitor, not(this.methodVisitor));
    methodVisitor.visitLdcInsn(Type.getType(Object.class));
    verify(this.classVisitor).visit(ClassFileVersion.JAVA_V4.getMinorMajorVersion(), FOOBAR, FOO, BAR, QUX, new String[]{BAZ});
    verify(this.classVisitor).visitMethod(FOOBAR, FOO, BAR, QUX, new String[]{BAZ});
    verifyNoMoreInteractions(this.classVisitor);
    verify(this.methodVisitor).visitLdcInsn(Type.getType(Object.class).getClassName());
    verify(this.methodVisitor).visitMethodInsn(Opcodes.INVOKESTATIC,
            Type.getType(Class.class).getInternalName(),
            "forName",
            Type.getType(Class.class.getDeclaredMethod("forName", String.class)).getDescriptor(),
            false);
    verifyNoMoreInteractions(this.methodVisitor);
}
 
Example #3
Source File: PersistentAttributeTransformer.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private static Collection<FieldDescription> collectInheritPersistentFields(
		TypeDefinition managedCtClass,
		ByteBuddyEnhancementContext enhancementContext) {
	if ( managedCtClass == null || managedCtClass.represents( Object.class ) ) {
		return Collections.emptyList();
	}
	TypeDefinition managedCtSuperclass = managedCtClass.getSuperClass();

	if ( !enhancementContext.isMappedSuperclassClass( managedCtSuperclass.asErasure() ) ) {
		return collectInheritPersistentFields( managedCtSuperclass, enhancementContext );
	}
	log.debugf( "Found @MappedSuperclass %s to collectPersistenceFields", managedCtSuperclass );
	List<FieldDescription> persistentFieldList = new ArrayList<FieldDescription>();

	for ( FieldDescription ctField : managedCtSuperclass.getDeclaredFields() ) {
		if ( ctField.getName().startsWith( "$$_hibernate_" ) || "this$0".equals( ctField.getName() ) ) {
			continue;
		}
		if ( !ctField.isStatic() && enhancementContext.isPersistentField( ctField ) ) {
			persistentFieldList.add( ctField );
		}
	}
	persistentFieldList.addAll( collectInheritPersistentFields( managedCtSuperclass, enhancementContext ) );
	return persistentFieldList;
}
 
Example #4
Source File: ByteBuddy.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public InstrumentedType prepare(InstrumentedType instrumentedType) {
    for (RecordComponentDescription.InDefinedShape recordComponent : instrumentedType.getRecordComponents()) {
        instrumentedType = instrumentedType
                .withField(new FieldDescription.Token(recordComponent.getActualName(),
                        Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL,
                        recordComponent.getType(),
                        recordComponent.getDeclaredAnnotations().filter(targetsElement(ElementType.FIELD))))
                .withMethod(new MethodDescription.Token(recordComponent.getActualName(),
                        Opcodes.ACC_PUBLIC,
                        Collections.<TypeVariableToken>emptyList(),
                        recordComponent.getType(),
                        Collections.<ParameterDescription.Token>emptyList(),
                        Collections.<TypeDescription.Generic>emptyList(),
                        recordComponent.getDeclaredAnnotations().filter(targetsElement(ElementType.METHOD)),
                        AnnotationValue.UNDEFINED,
                        TypeDescription.Generic.UNDEFINED));
    }
    return instrumentedType;
}
 
Example #5
Source File: EnhancerImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private List<FieldDescription> collectCollectionFields(TypeDescription managedCtClass) {
	List<FieldDescription> collectionList = new ArrayList<>();

	for ( FieldDescription ctField : managedCtClass.getDeclaredFields() ) {
		// skip static fields and skip fields added by enhancement
		if ( Modifier.isStatic( ctField.getModifiers() ) || ctField.getName().startsWith( "$$_hibernate_" ) ) {
			continue;
		}
		if ( enhancementContext.isPersistentField( ctField ) && !enhancementContext.isMappedCollection( ctField ) ) {
			if ( ctField.getType().asErasure().isAssignableTo( Collection.class ) || ctField.getType().asErasure().isAssignableTo( Map.class ) ) {
				collectionList.add( ctField );
			}
		}
	}

	// HHH-10646 Add fields inherited from @MappedSuperclass
	// HHH-10981 There is no need to do it for @MappedSuperclass
	if ( !enhancementContext.isMappedSuperclassClass( managedCtClass ) ) {
		collectionList.addAll( collectInheritCollectionFields( managedCtClass ) );
	}

	return collectionList;
}
 
Example #6
Source File: TypeProxyCreationTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testAccessorIsValid() throws Exception {
    TypeProxy typeProxy = new TypeProxy(mock(TypeDescription.class),
            mock(Implementation.Target.class),
            mock(TypeProxy.InvocationFactory.class),
            false,
            false);
    TypeProxy.MethodCall methodCall = typeProxy.new MethodCall(mock(MethodAccessorFactory.class));
    TypeDescription instrumentedType = mock(TypeDescription.class);
    FieldList<FieldDescription.InDefinedShape> fieldList = mock(FieldList.class);
    when(fieldList.filter(any(ElementMatcher.class))).thenReturn(fieldList);
    when(fieldList.getOnly()).thenReturn(mock(FieldDescription.InDefinedShape.class));
    when(instrumentedType.getDeclaredFields()).thenReturn(fieldList);
    TypeProxy.MethodCall.Appender appender = methodCall.new Appender(instrumentedType);
    Implementation.SpecialMethodInvocation specialMethodInvocation = mock(Implementation.SpecialMethodInvocation.class);
    when(specialMethodInvocation.isValid()).thenReturn(true);
    StackManipulation stackManipulation = appender.new AccessorMethodInvocation(mock(MethodDescription.class), specialMethodInvocation);
    assertThat(stackManipulation.isValid(), is(true));
    verify(specialMethodInvocation).isValid();
    verifyNoMoreInteractions(specialMethodInvocation);
}
 
Example #7
Source File: FieldValueBinderTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testIllegalAssignmentNonAssignable() throws Exception {
    doReturn(void.class).when(annotation).declaringType();
    when(annotation.value()).thenReturn(FOO);
    when(instrumentedType.getDeclaredFields()).thenReturn(new FieldList.Explicit<FieldDescription.InDefinedShape>(fieldDescription));
    when(fieldDescription.getActualName()).thenReturn(FOO);
    when(fieldDescription.isVisibleTo(instrumentedType)).thenReturn(true);
    when(target.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(stackManipulation.isValid()).thenReturn(false);
    MethodDelegationBinder.ParameterBinding<?> binding = FieldValue.Binder.INSTANCE.bind(annotationDescription,
            source,
            target,
            implementationTarget,
            assigner,
            Assigner.Typing.STATIC);
    assertThat(binding.isValid(), is(false));
}
 
Example #8
Source File: FieldValueBinderTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testIllegalAssignmentNonVisible() throws Exception {
    doReturn(void.class).when(annotation).declaringType();
    when(annotation.value()).thenReturn(FOO);
    when(instrumentedType.getDeclaredFields()).thenReturn((FieldList) new FieldList.Explicit<FieldDescription>(fieldDescription));
    when(fieldDescription.getActualName()).thenReturn(FOO);
    when(fieldDescription.isVisibleTo(instrumentedType)).thenReturn(false);
    when(target.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
    when(stackManipulation.isValid()).thenReturn(true);
    MethodDelegationBinder.ParameterBinding<?> binding = FieldValue.Binder.INSTANCE.bind(annotationDescription,
            source,
            target,
            implementationTarget,
            assigner,
            Assigner.Typing.STATIC);
    assertThat(binding.isValid(), is(false));
}
 
Example #9
Source File: EnhancerImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private Collection<FieldDescription> collectInheritCollectionFields(TypeDefinition managedCtClass) {
	TypeDefinition managedCtSuperclass = managedCtClass.getSuperClass();
	if ( managedCtSuperclass == null || managedCtSuperclass.represents( Object.class ) ) {
		return Collections.emptyList();
	}

	if ( !enhancementContext.isMappedSuperclassClass( managedCtSuperclass.asErasure() ) ) {
		return collectInheritCollectionFields( managedCtSuperclass.asErasure() );
	}
	List<FieldDescription> collectionList = new ArrayList<FieldDescription>();

	for ( FieldDescription ctField : managedCtSuperclass.getDeclaredFields() ) {
		if ( !Modifier.isStatic( ctField.getModifiers() ) ) {
			if ( enhancementContext.isPersistentField( ctField ) && !enhancementContext.isMappedCollection( ctField ) ) {
				if ( ctField.getType().asErasure().isAssignableTo( Collection.class ) || ctField.getType().asErasure().isAssignableTo( Map.class ) ) {
					collectionList.add( ctField );
				}
			}
		}
	}
	collectionList.addAll( collectInheritCollectionFields( managedCtSuperclass ) );
	return collectionList;
}
 
Example #10
Source File: TypeConstantAdjustmentTest.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
@Test
public void testInstrumentationLegacyClassFileArrayType() throws Exception {
    ClassVisitor classVisitor = TypeConstantAdjustment.INSTANCE.wrap(mock(TypeDescription.class),
            this.classVisitor,
            mock(Implementation.Context.class),
            mock(TypePool.class),
            new FieldList.Empty<FieldDescription.InDefinedShape>(),
            new MethodList.Empty<MethodDescription>(),
            IGNORED,
            IGNORED);
    classVisitor.visit(ClassFileVersion.JAVA_V4.getMinorMajorVersion(), FOOBAR, FOO, BAR, QUX, new String[]{BAZ});
    MethodVisitor methodVisitor = classVisitor.visitMethod(FOOBAR, FOO, BAR, QUX, new String[]{BAZ});
    assertThat(methodVisitor, not(this.methodVisitor));
    methodVisitor.visitLdcInsn(Type.getType(Object[].class));
    verify(this.classVisitor).visit(ClassFileVersion.JAVA_V4.getMinorMajorVersion(), FOOBAR, FOO, BAR, QUX, new String[]{BAZ});
    verify(this.classVisitor).visitMethod(FOOBAR, FOO, BAR, QUX, new String[]{BAZ});
    verifyNoMoreInteractions(this.classVisitor);
    verify(this.methodVisitor).visitLdcInsn(Type.getType(Object[].class).getInternalName().replace('/', '.'));
    verify(this.methodVisitor).visitMethodInsn(Opcodes.INVOKESTATIC,
            Type.getType(Class.class).getInternalName(),
            "forName",
            Type.getType(Class.class.getDeclaredMethod("forName", String.class)).getDescriptor(),
            false);
    verifyNoMoreInteractions(this.methodVisitor);
}
 
Example #11
Source File: Implementation.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new default implementation context.
 *
 * @param instrumentedType            The description of the type that is currently subject of creation.
 * @param classFileVersion            The class file version of the created class.
 * @param auxiliaryTypeNamingStrategy The naming strategy for naming an auxiliary type.
 * @param typeInitializer             The type initializer of the created instrumented type.
 * @param auxiliaryClassFileVersion   The class file version to use for auxiliary classes.
 */
protected Default(TypeDescription instrumentedType,
                  ClassFileVersion classFileVersion,
                  AuxiliaryType.NamingStrategy auxiliaryTypeNamingStrategy,
                  TypeInitializer typeInitializer,
                  ClassFileVersion auxiliaryClassFileVersion) {
    super(instrumentedType, classFileVersion);
    this.auxiliaryTypeNamingStrategy = auxiliaryTypeNamingStrategy;
    this.typeInitializer = typeInitializer;
    this.auxiliaryClassFileVersion = auxiliaryClassFileVersion;
    registeredAccessorMethods = new HashMap<SpecialMethodInvocation, DelegationRecord>();
    registeredGetters = new HashMap<FieldDescription, DelegationRecord>();
    registeredSetters = new HashMap<FieldDescription, DelegationRecord>();
    auxiliaryTypes = new HashMap<AuxiliaryType, DynamicType>();
    registeredFieldCacheEntries = new HashMap<FieldCacheEntry, FieldDescription.InDefinedShape>();
    registeredFieldCacheFields = new HashSet<FieldDescription.InDefinedShape>();
    suffix = RandomString.make();
    fieldCacheCanAppendEntries = true;
}
 
Example #12
Source File: PersistentAttributeTransformer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private Implementation fieldReader(FieldDescription enhancedField) {
	if ( !enhancementContext.hasLazyLoadableAttributes( managedCtClass ) || !enhancementContext.isLazyLoadable( enhancedField ) ) {
		if ( enhancedField.getDeclaringType().asErasure().equals( managedCtClass ) ) {
			return FieldAccessor.ofField( enhancedField.getName() ).in( enhancedField.getDeclaringType().asErasure() );
		}
		else {
			return new Implementation.Simple( new FieldMethodReader( managedCtClass, enhancedField ) );
		}
	}
	else {
		return new Implementation.Simple( FieldReaderAppender.of( managedCtClass, enhancedField ) );
	}
}
 
Example #13
Source File: EnhancerImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
static MethodDescription getterOf(FieldDescription persistentField) {
	MethodList<?> methodList = MethodGraph.Compiler.DEFAULT.compile( persistentField.getDeclaringType().asErasure() )
			.listNodes()
			.asMethodList()
			.filter( isGetter(persistentField.getName() ) );
	if ( methodList.size() == 1 ) {
		return methodList.getOnly();
	}
	else {
		return null;
	}
}
 
Example #14
Source File: FieldRegistry.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new entry.
 *
 * @param matcher                       The matcher to identify any field that this definition concerns.
 * @param fieldAttributeAppenderFactory The field attribute appender factory to apply on any matched field.
 * @param defaultValue                  The default value to write to the field or {@code null} if no default value is to be set for the field.
 * @param transformer                   The field transformer to apply to any matched field.
 */
protected Entry(LatentMatcher<? super FieldDescription> matcher,
                FieldAttributeAppender.Factory fieldAttributeAppenderFactory,
                Object defaultValue,
                Transformer<FieldDescription> transformer) {
    this.matcher = matcher;
    this.fieldAttributeAppenderFactory = fieldAttributeAppenderFactory;
    this.defaultValue = defaultValue;
    this.transformer = transformer;
}
 
Example #15
Source File: TransformerForFieldTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testModifierTransformation() throws Exception {
    FieldDescription.Token transformed = new Transformer.ForField.FieldModifierTransformer(ModifierContributor.Resolver.of(modifierContributor))
            .transform(instrumentedType, fieldToken);
    assertThat(transformed.getName(), is(FOO));
    assertThat(transformed.getModifiers(), is((MODIFIERS & ~RANGE) | MASK));
    assertThat(transformed.getType(), is(fieldType));
}
 
Example #16
Source File: PersistentAttributeTransformer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private PersistentAttributeTransformer(
		TypeDescription managedCtClass,
		ByteBuddyEnhancementContext enhancementContext,
		TypePool classPool,
		FieldDescription[] enhancedFields) {
	this.managedCtClass = managedCtClass;
	this.enhancementContext = enhancementContext;
	this.classPool = classPool;
	this.enhancedFields = enhancedFields;
}
 
Example #17
Source File: AsmVisitorWrapper.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public ClassVisitor wrap(TypeDescription instrumentedType,
                         ClassVisitor classVisitor,
                         Implementation.Context implementationContext,
                         TypePool typePool,
                         FieldList<FieldDescription.InDefinedShape> fields,
                         MethodList<?> methods,
                         int writerFlags,
                         int readerFlags) {
    return classVisitor;
}
 
Example #18
Source File: MemberRemoval.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new member removing class visitor.
 *
 * @param classVisitor  The class visitor to delegate to.
 * @param fieldMatcher  The matcher that determines field removal.
 * @param methodMatcher The matcher that determines method removal.
 * @param fields        A mapping of field names and descriptors to their description.
 * @param methods       A mapping of method names and descriptors to their description.
 */
protected MemberRemovingClassVisitor(ClassVisitor classVisitor,
                                     ElementMatcher.Junction<FieldDescription.InDefinedShape> fieldMatcher,
                                     ElementMatcher.Junction<MethodDescription> methodMatcher,
                                     Map<String, FieldDescription.InDefinedShape> fields,
                                     Map<String, MethodDescription> methods) {
    super(OpenedClassReader.ASM_API, classVisitor);
    this.fieldMatcher = fieldMatcher;
    this.methodMatcher = methodMatcher;
    this.fields = fields;
    this.methods = methods;
}
 
Example #19
Source File: BiDirectionalAssociationHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private static TypeDescription.Generic target(FieldDescription persistentField) {
	AnnotationDescription.Loadable<Access> access = persistentField.getDeclaringType().asErasure().getDeclaredAnnotations().ofType( Access.class );
	if ( access != null && access.loadSilent().value() == AccessType.FIELD ) {
		return persistentField.getType();
	}
	else {
		MethodDescription getter = EnhancerImpl.getterOf( persistentField );
		if ( getter == null ) {
			return persistentField.getType();
		}
		else {
			return getter.getReturnType();
		}
	}
}
 
Example #20
Source File: AbstractTypeDescriptionGenericTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testIntermediateRawType() throws Exception {
    TypeDescription.Generic type = describeType(IntermediateRaw.class.getDeclaredField(FOO)).getSuperClass().getSuperClass().getSuperClass();
    FieldDescription fieldDescription = type.getDeclaredFields().filter(named(BAR)).getOnly();
    assertThat(fieldDescription.getType().getSort(), is(TypeDefinition.Sort.NON_GENERIC));
    assertThat(fieldDescription.getType().asErasure(), is((TypeDescription) TypeDescription.ForLoadedType.of(Integer.class)));
}
 
Example #21
Source File: FieldProxyBinderTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testUnresolvedResolverNoApplication() throws Exception {
    FieldProxy.Binder.FieldResolver.Unresolved.INSTANCE.apply(mock(DynamicType.Builder.class),
            mock(FieldDescription.class),
            mock(Assigner.class),
            mock(MethodAccessorFactory.class));
}
 
Example #22
Source File: HashCodeAndEqualsPluginTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testAnnotationComparatorEqualsRightBiggerAnnotations() {
    Comparator<FieldDescription.InDefinedShape> comparator = HashCodeAndEqualsPlugin.AnnotationOrderComparator.INSTANCE;
    FieldDescription.InDefinedShape left = mock(FieldDescription.InDefinedShape.class), right = mock(FieldDescription.InDefinedShape.class);
    when(left.getDeclaredAnnotations()).thenReturn(new AnnotationList.Explicit(AnnotationDescription.Builder.ofType(HashCodeAndEqualsPlugin.Sorted.class)
            .define("value", 0)
            .build()));
    when(right.getDeclaredAnnotations()).thenReturn(new AnnotationList.Explicit(AnnotationDescription.Builder.ofType(HashCodeAndEqualsPlugin.Sorted.class)
            .define("value", 42)
            .build()));
    assertThat(comparator.compare(left, right), is(1));
}
 
Example #23
Source File: InvokeDynamic.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Resolved resolve(TypeDescription instrumentedType, MethodDescription instrumentedMethod, Assigner assigner, Assigner.Typing typing) {
    FieldDescription fieldDescription = instrumentedType.getDeclaredFields().filter(named(name)).getOnly();
    StackManipulation stackManipulation = assigner.assign(fieldDescription.getType(), fieldType.asGenericType(), typing);
    if (!stackManipulation.isValid()) {
        throw new IllegalStateException("Cannot assign " + fieldDescription + " to " + fieldType);
    }
    return new Resolved.Simple(new StackManipulation.Compound(FieldAccess.forField(fieldDescription).read(),
            stackManipulation), fieldDescription.getType().asErasure());
}
 
Example #24
Source File: TypeWriterFieldPoolRecordTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testExplicitFieldEntryProperties() throws Exception {
    TypeWriter.FieldPool.Record record = new TypeWriter.FieldPool.Record.ForExplicitField(fieldAttributeAppender, defaultValue, fieldDescription);
    assertThat(record.getFieldAppender(), is(fieldAttributeAppender));
    assertThat(record.resolveDefault(FieldDescription.NO_DEFAULT_VALUE), is(defaultValue));
    assertThat(record.isImplicit(), is(false));
}
 
Example #25
Source File: JavaConstant.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a method handle for a getter of the given field.
 *
 * @param fieldDescription The field to represent.
 * @return A method handle for a getter of the given field.
 */
public static MethodHandle ofSetter(FieldDescription.InDefinedShape fieldDescription) {
    return new MethodHandle(HandleType.ofSetter(fieldDescription),
            fieldDescription.getDeclaringType().asErasure(),
            fieldDescription.getInternalName(),
            TypeDescription.VOID,
            Collections.singletonList(fieldDescription.getType().asErasure()));
}
 
Example #26
Source File: EqualsMethodOtherTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testStringTypeComparatorBothEnumeration() {
    Comparator<FieldDescription.InDefinedShape> comparator = EqualsMethod.TypePropertyComparator.FOR_ENUMERATION_TYPES;
    FieldDescription.InDefinedShape left = mock(FieldDescription.InDefinedShape.class), right = mock(FieldDescription.InDefinedShape.class);
    TypeDescription.Generic leftType = mock(TypeDescription.Generic.class), rightType = mock(TypeDescription.Generic.class);
    when(left.getType()).thenReturn(leftType);
    when(right.getType()).thenReturn(rightType);
    when(leftType.isEnum()).thenReturn(true);
    when(rightType.isEnum()).thenReturn(true);
    assertThat(comparator.compare(left, right), is(0));
}
 
Example #27
Source File: ByteArrayClassLoaderChildFirstTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
public ClassVisitor wrap(TypeDescription instrumentedType,
                         ClassVisitor classVisitor,
                         Implementation.Context implementationContext,
                         TypePool typePool,
                         FieldList<FieldDescription.InDefinedShape> fields,
                         MethodList<?> methods,
                         int writerFlags,
                         int readerFlags) {
    return new ClassRemapper(OpenedClassReader.ASM_API, classVisitor, new SimpleRemapper(oldName, newName)) {
        /* only anonymous to define usage of Byte Buddy specific API version */
    };
}
 
Example #28
Source File: ElementMatchersTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoneOfFieldDefinedShape() throws Exception {
    Field field = GenericFieldType.class.getDeclaredField(FOO);
    FieldDescription fieldDescription = TypeDescription.ForLoadedType.of(GenericFieldType.Inner.class).getSuperClass()
            .getDeclaredFields().filter(named(FOO)).getOnly();
    assertThat(ElementMatchers.noneOf(field).matches(fieldDescription), is(false));
    assertThat(ElementMatchers.definedField(ElementMatchers.noneOf(fieldDescription.asDefined())).matches(fieldDescription), is(false));
    assertThat(ElementMatchers.noneOf(fieldDescription.asDefined()).matches(fieldDescription.asDefined()), is(false));
    assertThat(ElementMatchers.noneOf(fieldDescription.asDefined()).matches(fieldDescription), is(true));
    assertThat(ElementMatchers.noneOf(fieldDescription).matches(fieldDescription.asDefined()), is(true));
}
 
Example #29
Source File: AsmVisitorWrapperForDeclaredMethodsTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testNonMatchedMethod() throws Exception {
    assertThat(new AsmVisitorWrapper.ForDeclaredMethods()
            .method(matcher, methodVisitorWrapper)
            .wrap(instrumentedType,
                    classVisitor,
                    implementationContext,
                    typePool,
                    new FieldList.Empty<FieldDescription.InDefinedShape>(),
                    new MethodList.Explicit<MethodDescription>(foo, bar),
                    FLAGS,
                    FLAGS * 2)
            .visitMethod(MODIFIERS, FOO, QUX, BAZ, new String[]{QUX + BAZ}), is(methodVisitor));
    verifyZeroInteractions(matcher);
}
 
Example #30
Source File: AnnotationValueFilterDefaultTest.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
@Test
public void testFactory() throws Exception {
    assertThat(AnnotationValueFilter.Default.SKIP_DEFAULTS.on(mock(FieldDescription.class)),
            is((AnnotationValueFilter) AnnotationValueFilter.Default.SKIP_DEFAULTS));
    assertThat(AnnotationValueFilter.Default.SKIP_DEFAULTS.on(mock(MethodDescription.class)),
            is((AnnotationValueFilter) AnnotationValueFilter.Default.SKIP_DEFAULTS));
    assertThat(AnnotationValueFilter.Default.SKIP_DEFAULTS.on(mock(TypeDescription.class)),
            is((AnnotationValueFilter) AnnotationValueFilter.Default.SKIP_DEFAULTS));
    assertThat(AnnotationValueFilter.Default.APPEND_DEFAULTS.on(mock(FieldDescription.class)),
            is((AnnotationValueFilter) AnnotationValueFilter.Default.APPEND_DEFAULTS));
    assertThat(AnnotationValueFilter.Default.APPEND_DEFAULTS.on(mock(MethodDescription.class)),
            is((AnnotationValueFilter) AnnotationValueFilter.Default.APPEND_DEFAULTS));
    assertThat(AnnotationValueFilter.Default.APPEND_DEFAULTS.on(mock(TypeDescription.class)),
            is((AnnotationValueFilter) AnnotationValueFilter.Default.APPEND_DEFAULTS));
}