Search in sources :

Example 1 with PropertyAccessorType

use of org.gradle.internal.reflect.PropertyAccessorType in project gradle by gradle.

the class DefaultStructBindingsStore method collectMethodBindings.

private static <T> Set<StructMethodBinding> collectMethodBindings(StructBindingExtractionContext<T> extractionContext, Map<String, Multimap<PropertyAccessorType, StructMethodBinding>> propertyBindings) {
    Collection<WeaklyTypeReferencingMethod<?, ?>> implementedMethods = collectImplementedMethods(extractionContext.getImplementedSchemas());
    Map<Wrapper<Method>, WeaklyTypeReferencingMethod<?, ?>> publicViewImplMethods = collectPublicViewImplMethods(extractionContext.getPublicSchema());
    Map<Wrapper<Method>, WeaklyTypeReferencingMethod<?, ?>> delegateMethods = collectDelegateMethods(extractionContext.getDelegateSchema());
    ImmutableSet.Builder<StructMethodBinding> methodBindingsBuilder = ImmutableSet.builder();
    for (WeaklyTypeReferencingMethod<?, ?> weakImplementedMethod : implementedMethods) {
        Method implementedMethod = weakImplementedMethod.getMethod();
        PropertyAccessorType accessorType = PropertyAccessorType.of(implementedMethod);
        Wrapper<Method> methodKey = SIGNATURE_EQUIVALENCE.wrap(implementedMethod);
        WeaklyTypeReferencingMethod<?, ?> weakDelegateImplMethod = delegateMethods.get(methodKey);
        WeaklyTypeReferencingMethod<?, ?> weakPublicImplMethod = publicViewImplMethods.get(methodKey);
        if (weakDelegateImplMethod != null && weakPublicImplMethod != null) {
            extractionContext.add(weakImplementedMethod, String.format("it is both implemented by the view '%s' and the delegate type '%s'", extractionContext.getPublicSchema().getType().getDisplayName(), extractionContext.getDelegateSchema().getType().getDisplayName()));
        }
        String propertyName = accessorType == null ? null : accessorType.propertyNameFor(implementedMethod);
        StructMethodBinding binding;
        if (!Modifier.isAbstract(implementedMethod.getModifiers())) {
            binding = new DirectMethodBinding(weakImplementedMethod, accessorType);
        } else if (weakPublicImplMethod != null) {
            binding = new BridgeMethodBinding(weakImplementedMethod, weakPublicImplMethod, accessorType);
        } else if (weakDelegateImplMethod != null) {
            binding = new DelegateMethodBinding(weakImplementedMethod, weakDelegateImplMethod, accessorType);
        } else if (propertyName != null) {
            binding = new ManagedPropertyMethodBinding(weakImplementedMethod, propertyName, accessorType);
        } else {
            handleNoMethodImplementation(extractionContext, weakImplementedMethod);
            continue;
        }
        methodBindingsBuilder.add(binding);
        if (accessorType != null) {
            Multimap<PropertyAccessorType, StructMethodBinding> accessorBindings = propertyBindings.get(propertyName);
            if (accessorBindings == null) {
                accessorBindings = ArrayListMultimap.create();
                propertyBindings.put(propertyName, accessorBindings);
            }
            accessorBindings.put(accessorType, binding);
        }
    }
    return methodBindingsBuilder.build();
}
Also used : Wrapper(com.google.common.base.Equivalence.Wrapper) PropertyAccessorType(org.gradle.internal.reflect.PropertyAccessorType) Method(java.lang.reflect.Method) WeaklyTypeReferencingMethod(org.gradle.model.internal.method.WeaklyTypeReferencingMethod) WeaklyTypeReferencingMethod(org.gradle.model.internal.method.WeaklyTypeReferencingMethod) ImmutableSet(com.google.common.collect.ImmutableSet)

Example 2 with PropertyAccessorType

use of org.gradle.internal.reflect.PropertyAccessorType in project gradle by gradle.

the class ModelPropertyExtractionContext method addAccessor.

public void addAccessor(PropertyAccessorExtractionContext accessor) {
    PropertyAccessorType type = accessor.getAccessorType();
    // TODO:LPTR What happens when the property has multiple accessors in the same role but with different type?
    // if (accessors.containsKey(type)) {
    // throw new IllegalStateException("Accessor already registered: " + type + " " + accessor);
    // }
    accessors.put(type, accessor);
}
Also used : PropertyAccessorType(org.gradle.internal.reflect.PropertyAccessorType)

Example 3 with PropertyAccessorType

use of org.gradle.internal.reflect.PropertyAccessorType in project gradle by gradle.

the class DefaultStructBindingsStore method handleNoMethodImplementation.

private static void handleNoMethodImplementation(StructBindingValidationProblemCollector problems, WeaklyTypeReferencingMethod<?, ?> method) {
    String methodName = method.getName();
    PropertyAccessorType accessorType = PropertyAccessorType.fromName(methodName);
    if (accessorType != null) {
        switch(accessorType) {
            case GET_GETTER:
            case IS_GETTER:
                if (!PropertyAccessorType.takesNoParameter(method.getMethod())) {
                    problems.add(method, "property accessor", "getter method must not take parameters");
                }
                break;
            case SETTER:
                if (!hasVoidReturnType(method.getMethod())) {
                    problems.add(method, "property accessor", "setter method must have void return type");
                }
                if (!takesSingleParameter(method.getMethod())) {
                    problems.add(method, "property accessor", "setter method must take exactly one parameter");
                }
                break;
            default:
                throw new AssertionError();
        }
    } else {
        problems.add(method, "managed type", "it must have an implementation");
    }
}
Also used : PropertyAccessorType(org.gradle.internal.reflect.PropertyAccessorType)

Example 4 with PropertyAccessorType

use of org.gradle.internal.reflect.PropertyAccessorType in project gradle by gradle.

the class StructSchemaExtractionStrategySupport method selectProperties.

private Iterable<ModelPropertyExtractionContext> selectProperties(final ModelSchemaExtractionContext<?> context, CandidateMethods candidateMethods) {
    Map<String, ModelPropertyExtractionContext> propertiesMap = Maps.newTreeMap();
    for (Map.Entry<Wrapper<Method>, Collection<Method>> entry : candidateMethods.allMethods().entrySet()) {
        Method method = entry.getKey().get();
        PropertyAccessorType propertyAccessorType = PropertyAccessorType.of(method);
        Collection<Method> methodsWithEqualSignature = entry.getValue();
        if (propertyAccessorType != null) {
            String propertyName = propertyAccessorType.propertyNameFor(method);
            ModelPropertyExtractionContext propertyContext = propertiesMap.get(propertyName);
            if (propertyContext == null) {
                propertyContext = new ModelPropertyExtractionContext(propertyName);
                propertiesMap.put(propertyName, propertyContext);
            }
            propertyContext.addAccessor(new PropertyAccessorExtractionContext(propertyAccessorType, methodsWithEqualSignature));
        }
    }
    return Collections2.filter(propertiesMap.values(), new Predicate<ModelPropertyExtractionContext>() {

        @Override
        public boolean apply(ModelPropertyExtractionContext property) {
            return property.isReadable();
        }
    });
}
Also used : Wrapper(com.google.common.base.Equivalence.Wrapper) PropertyAccessorType(org.gradle.internal.reflect.PropertyAccessorType) WeaklyTypeReferencingMethod(org.gradle.model.internal.method.WeaklyTypeReferencingMethod) Method(java.lang.reflect.Method) Collection(java.util.Collection) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map)

Example 5 with PropertyAccessorType

use of org.gradle.internal.reflect.PropertyAccessorType in project gradle by gradle.

the class DefaultPropertyMetadataStore method getGetters.

private static List<Getter> getGetters(Class<?> type) {
    Method[] methods = type.getDeclaredMethods();
    List<Getter> getters = Lists.newArrayListWithCapacity(methods.length);
    for (Method method : methods) {
        PropertyAccessorType accessorType = PropertyAccessorType.of(method);
        // We only care about getters
        if (accessorType == null || accessorType == PropertyAccessorType.SETTER) {
            continue;
        }
        // We only care about actual methods the user added
        if (method.isBridge() || GroovyMethods.isObjectMethod(method)) {
            continue;
        }
        getters.add(new Getter(method, accessorType.propertyNameFor(method)));
    }
    Collections.sort(getters);
    return getters;
}
Also used : PropertyAccessorType(org.gradle.internal.reflect.PropertyAccessorType) Method(java.lang.reflect.Method)

Aggregations

PropertyAccessorType (org.gradle.internal.reflect.PropertyAccessorType)5 Method (java.lang.reflect.Method)3 Wrapper (com.google.common.base.Equivalence.Wrapper)2 WeaklyTypeReferencingMethod (org.gradle.model.internal.method.WeaklyTypeReferencingMethod)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Collection (java.util.Collection)1 Map (java.util.Map)1