Java Code Examples for net.bytebuddy.description.field.FieldList#size()

The following examples show how to use net.bytebuddy.description.field.FieldList#size() . 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: PropertyAccessorCollector.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
@Override
protected StackManipulation invocationOperation(
        AnnotatedMember annotatedMember, TypeDefinition beanClassDescription) {

    final String fieldName = annotatedMember.getName();
    @SuppressWarnings("unchecked")
    final FieldList<FieldDescription> matchingFields =
            (FieldList<FieldDescription>) beanClassDescription.getDeclaredFields().filter(named(fieldName));

    if (matchingFields.size() == 1) { //method was declared on class
        return FieldAccess.forField(matchingFields.getOnly()).read();
    }
    if (matchingFields.isEmpty()) { //method was not found on class, try super class
        return invocationOperation(annotatedMember, beanClassDescription.getSuperClass());
    }
    else { //should never happen
        throw new IllegalStateException("Could not find definition of field: " + fieldName);
    }
}
 
Example 2
Source File: PropertyMutatorCollector.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
protected StackManipulation invocationOperation(AnnotatedMember annotatedMember,
        TypeDefinition beanClassDescription) {

    final String fieldName = annotatedMember.getName();
    final FieldList<FieldDescription> matchingFields =
            (FieldList<FieldDescription>) beanClassDescription.getDeclaredFields().filter(named(fieldName));

    if (matchingFields.size() == 1) { //method was declared on class
        return FieldAccess.forField(matchingFields.getOnly()).write();
    }
    if (matchingFields.isEmpty()) { //method was not found on class, try super class
        return invocationOperation(annotatedMember, beanClassDescription.getSuperClass());
    }
    else { //should never happen
        throw new IllegalStateException("Could not find definition of field: " + fieldName);
    }
}
 
Example 3
Source File: MemberSubstitution.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public FieldDescription resolve(TypeDescription targetType, ByteCodeElement target, TypeList.Generic parameters, TypeDescription.Generic result) {
    if (parameters.isEmpty()) {
        throw new IllegalStateException("Cannot substitute parameterless instruction with " + target);
    } else if (parameters.get(0).isPrimitive() || parameters.get(0).isArray()) {
        throw new IllegalStateException("Cannot access field on primitive or array type for " + target);
    }
    TypeDefinition current = parameters.get(0);
    do {
        FieldList<?> fields = current.getDeclaredFields().filter(not(isStatic()).<FieldDescription>and(isVisibleTo(instrumentedType)).and(matcher));
        if (fields.size() == 1) {
            return fields.getOnly();
        } else if (fields.size() > 1) {
            throw new IllegalStateException("Ambiguous field location of " + fields);
        }
        current = current.getSuperClass();
    } while (current != null);
    throw new IllegalStateException("Cannot locate field matching " + matcher + " on " + targetType);
}
 
Example 4
Source File: MethodCallProxy.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Size apply(MethodVisitor methodVisitor, Context implementationContext, MethodDescription instrumentedMethod) {
    FieldList<?> fieldList = instrumentedType.getDeclaredFields();
    StackManipulation[] fieldLoading = new StackManipulation[fieldList.size()];
    int index = 0;
    for (FieldDescription fieldDescription : fieldList) {
        fieldLoading[index] = new StackManipulation.Compound(
                MethodVariableAccess.loadThis(),
                MethodVariableAccess.load(instrumentedMethod.getParameters().get(index)),
                FieldAccess.forField(fieldDescription).write()
        );
        index++;
    }
    StackManipulation.Size stackSize = new StackManipulation.Compound(
            MethodVariableAccess.loadThis(),
            MethodInvocation.invoke(ConstructorCall.INSTANCE.objectTypeDefaultConstructor),
            new StackManipulation.Compound(fieldLoading),
            MethodReturn.VOID
    ).apply(methodVisitor, implementationContext);
    return new Size(stackSize.getMaximalSize(), instrumentedMethod.getStackSize());
}
 
Example 5
Source File: MethodCallProxy.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Size apply(MethodVisitor methodVisitor,
                  Context implementationContext,
                  MethodDescription instrumentedMethod) {
    FieldList<?> fieldList = instrumentedType.getDeclaredFields();
    List<StackManipulation> fieldLoadings = new ArrayList<StackManipulation>(fieldList.size());
    for (FieldDescription fieldDescription : fieldList) {
        fieldLoadings.add(new StackManipulation.Compound(MethodVariableAccess.loadThis(), FieldAccess.forField(fieldDescription).read()));
    }
    StackManipulation.Size stackSize = new StackManipulation.Compound(
            new StackManipulation.Compound(fieldLoadings),
            MethodInvocation.invoke(accessorMethod),
            assigner.assign(accessorMethod.getReturnType(), instrumentedMethod.getReturnType(), Assigner.Typing.DYNAMIC),
            MethodReturn.of(instrumentedMethod.getReturnType())
    ).apply(methodVisitor, implementationContext);
    return new Size(stackSize.getMaximalSize(), instrumentedMethod.getStackSize());
}
 
Example 6
Source File: RedissonLiveObjectService.java    From redisson with Apache License 2.0 5 votes vote down vote up
private <T> void validateClass(Class<T> entityClass) {
    if (entityClass.isAnonymousClass() || entityClass.isLocalClass()) {
        throw new IllegalArgumentException(entityClass.getName() + " is not publically accessable.");
    }
    if (!ClassUtils.isAnnotationPresent(entityClass, REntity.class)) {
        throw new IllegalArgumentException("REntity annotation is missing from class type declaration.");
    }

    FieldList<FieldDescription.InDefinedShape> fields = Introspectior.getFieldsWithAnnotation(entityClass, RIndex.class);
    fields = fields.filter(ElementMatchers.fieldType(ElementMatchers.hasSuperType(
                            ElementMatchers.anyOf(Map.class, Collection.class, RObject.class))));
    for (InDefinedShape field : fields) {
        throw new IllegalArgumentException("RIndex annotation couldn't be defined for field '" + field.getName() + "' with type '" + field.getType() + "'");
    }
    
    FieldList<FieldDescription.InDefinedShape> fieldsWithRIdAnnotation
            = Introspectior.getFieldsWithAnnotation(entityClass, RId.class);
    if (fieldsWithRIdAnnotation.size() == 0) {
        throw new IllegalArgumentException("RId annotation is missing from class field declaration.");
    }
    if (fieldsWithRIdAnnotation.size() > 1) {
        throw new IllegalArgumentException("Only one field with RId annotation is allowed in class field declaration.");
    }
    FieldDescription.InDefinedShape idFieldDescription = fieldsWithRIdAnnotation.getOnly();
    String idFieldName = idFieldDescription.getName();
    Field idField = null;
    try {
        idField = ClassUtils.getDeclaredField(entityClass, idFieldName);
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
    if (ClassUtils.isAnnotationPresent(idField.getType(), REntity.class)) {
        throw new IllegalArgumentException("Field with RId annotation cannot be a type of which class is annotated with REntity.");
    }
    if (idField.getType().isAssignableFrom(RObject.class)) {
        throw new IllegalArgumentException("Field with RId annotation cannot be a type of RObject");
    }
}
 
Example 7
Source File: MethodCall.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public TerminationHandler make(TypeDescription instrumentedType) {
    TypeDefinition current = instrumentedType;
    do {
        FieldList<?> candidates = current.getDeclaredFields().filter(isAccessibleTo(instrumentedType).and(matcher));
        if (candidates.size() == 1) {
            return new FieldSetting(candidates.getOnly());
        } else if (candidates.size() == 2) {
            throw new IllegalStateException(matcher + " is ambigous and resolved: " + candidates);
        }
        current = current.getSuperClass();
    } while (current != null);
    throw new IllegalStateException(matcher + " does not locate any accessible fields for " + instrumentedType);
}
 
Example 8
Source File: FieldLocator.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Resolution locate(String name) {
    FieldList<?> candidates = locate(named(name).and(isVisibleTo(accessingType)));
    return candidates.size() == 1
            ? new Resolution.Simple(candidates.getOnly())
            : Resolution.Illegal.INSTANCE;
}
 
Example 9
Source File: FieldLocator.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Resolution locate(String name, TypeDescription type) {
    FieldList<?> candidates = locate(named(name).and(fieldType(type)).and(isVisibleTo(accessingType)));
    return candidates.size() == 1
            ? new Resolution.Simple(candidates.getOnly())
            : Resolution.Illegal.INSTANCE;
}