Search in sources :

Example 1 with Wrapper

use of com.google.common.base.Equivalence.Wrapper in project closure-templates by google.

the class ResolveExpressionTypesVisitor method addTypeSubstitutions.

// Given a map of type subsitutions, add all the entries to the current set of
// active substitutions.
private void addTypeSubstitutions(Map<Wrapper<ExprNode>, SoyType> substitutionsToAdd) {
    for (Map.Entry<Wrapper<ExprNode>, SoyType> entry : substitutionsToAdd.entrySet()) {
        ExprNode expr = entry.getKey().get();
        // Get the existing type
        SoyType previousType = expr.getType();
        for (TypeSubstitution subst = substitutions; subst != null; subst = subst.parent) {
            if (ExprEquivalence.get().equivalent(subst.expression, expr)) {
                previousType = subst.type;
                break;
            }
        }
        // If the new type is different than the current type, then add a new type substitution.
        if (!entry.getValue().equals(previousType)) {
            substitutions = new TypeSubstitution(substitutions, expr, entry.getValue());
        }
    }
}
Also used : AbstractParentExprNode(com.google.template.soy.exprtree.AbstractParentExprNode) ExprNode(com.google.template.soy.exprtree.ExprNode) ParentExprNode(com.google.template.soy.exprtree.ExprNode.ParentExprNode) Wrapper(com.google.common.base.Equivalence.Wrapper) SoyType(com.google.template.soy.types.SoyType) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap)

Example 2 with Wrapper

use of com.google.common.base.Equivalence.Wrapper in project gradle by gradle.

the class ManagedProxyClassGenerator method writeViewMethods.

private void writeViewMethods(ClassVisitor visitor, Type generatedType, Collection<ModelType<?>> viewTypes, StructBindings<?> bindings) {
    Type delegateType;
    StructSchema<?> delegateSchema = bindings.getDelegateSchema();
    if (delegateSchema != null) {
        Class<?> delegateClass = delegateSchema.getType().getRawClass();
        declareDelegateField(visitor, delegateClass);
        delegateType = Type.getType(delegateClass);
    } else {
        delegateType = null;
    }
    Multimap<String, ModelProperty<?>> viewPropertiesByNameBuilder = ArrayListMultimap.create();
    Set<Wrapper<Method>> viewMethods = Sets.newLinkedHashSet();
    for (StructSchema<?> viewSchema : bindings.getImplementedViewSchemas()) {
        for (ModelType<?> viewType : viewTypes) {
            if (viewType.equals(viewSchema.getType())) {
                for (ModelProperty<?> property : viewSchema.getProperties()) {
                    String propertyName = property.getName();
                    viewPropertiesByNameBuilder.put(propertyName, property);
                }
                for (WeaklyTypeReferencingMethod<?, ?> viewMethod : viewSchema.getAllMethods()) {
                    viewMethods.add(SIGNATURE_EQUIVALENCE.wrap(viewMethod.getMethod()));
                }
                break;
            }
        }
    }
    Class<?> viewClass = bindings.getPublicSchema().getType().getConcreteClass();
    for (Collection<ModelProperty<?>> viewProperties : viewPropertiesByNameBuilder.asMap().values()) {
        writeViewPropertyDslMethods(visitor, generatedType, viewProperties, viewClass);
    }
    for (StructMethodBinding methodBinding : bindings.getMethodBindings()) {
        WeaklyTypeReferencingMethod<?, ?> weakViewMethod = methodBinding.getViewMethod();
        Method viewMethod = weakViewMethod.getMethod();
        // Don't generate method if it's not part of the view schema
        Wrapper<Method> methodKey = SIGNATURE_EQUIVALENCE.wrap(viewMethod);
        if (!viewMethods.contains(methodKey)) {
            continue;
        }
        if (methodBinding instanceof DirectMethodBinding) {
            // TODO:LPTR What is with the "metaClass" property here?
            boolean isGetterMethod = methodBinding.getAccessorType() == GET_GETTER || methodBinding.getAccessorType() == IS_GETTER;
            if (isGetterMethod && !Modifier.isFinal(viewMethod.getModifiers()) && !viewMethod.getName().equals("getMetaClass")) {
                writeNonAbstractMethodWrapper(visitor, generatedType, viewClass, viewMethod);
            }
        } else if (methodBinding instanceof BridgeMethodBinding) {
            writeBridgeMethod(visitor, generatedType, viewMethod);
        } else if (methodBinding instanceof DelegateMethodBinding) {
            writeDelegatingMethod(visitor, generatedType, delegateType, viewMethod);
        } else if (methodBinding instanceof ManagedPropertyMethodBinding) {
            ManagedPropertyMethodBinding propertyBinding = (ManagedPropertyMethodBinding) methodBinding;
            ManagedProperty<?> managedProperty = bindings.getManagedProperty(propertyBinding.getPropertyName());
            String propertyName = managedProperty.getName();
            Class<?> propertyClass = managedProperty.getType().getRawClass();
            WeaklyTypeReferencingMethod<?, ?> propertyAccessor = propertyBinding.getViewMethod();
            switch(propertyBinding.getAccessorType()) {
                case GET_GETTER:
                case IS_GETTER:
                    writeGetter(visitor, generatedType, propertyName, propertyClass, propertyAccessor);
                    break;
                case SETTER:
                    writeSetter(visitor, generatedType, propertyName, propertyClass, propertyAccessor);
                    break;
                default:
                    throw new AssertionError();
            }
        } else {
            throw new AssertionError();
        }
    }
}
Also used : Wrapper(com.google.common.base.Equivalence.Wrapper) DirectMethodBinding(org.gradle.model.internal.manage.binding.DirectMethodBinding) Method(java.lang.reflect.Method) WeaklyTypeReferencingMethod(org.gradle.model.internal.method.WeaklyTypeReferencingMethod) BridgeMethodBinding(org.gradle.model.internal.manage.binding.BridgeMethodBinding) ManagedPropertyMethodBinding(org.gradle.model.internal.manage.binding.ManagedPropertyMethodBinding) DelegateMethodBinding(org.gradle.model.internal.manage.binding.DelegateMethodBinding) WeaklyTypeReferencingMethod(org.gradle.model.internal.method.WeaklyTypeReferencingMethod) ManagedProperty(org.gradle.model.internal.manage.binding.ManagedProperty) Type(org.objectweb.asm.Type) ModelType(org.gradle.model.internal.type.ModelType) StructMethodBinding(org.gradle.model.internal.manage.binding.StructMethodBinding) ModelProperty(org.gradle.model.internal.manage.schema.ModelProperty)

Example 3 with Wrapper

use of com.google.common.base.Equivalence.Wrapper 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 4 with Wrapper

use of com.google.common.base.Equivalence.Wrapper 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)

Aggregations

Wrapper (com.google.common.base.Equivalence.Wrapper)4 Method (java.lang.reflect.Method)3 WeaklyTypeReferencingMethod (org.gradle.model.internal.method.WeaklyTypeReferencingMethod)3 Map (java.util.Map)2 PropertyAccessorType (org.gradle.internal.reflect.PropertyAccessorType)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 AbstractParentExprNode (com.google.template.soy.exprtree.AbstractParentExprNode)1 ExprNode (com.google.template.soy.exprtree.ExprNode)1 ParentExprNode (com.google.template.soy.exprtree.ExprNode.ParentExprNode)1 SoyType (com.google.template.soy.types.SoyType)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 BridgeMethodBinding (org.gradle.model.internal.manage.binding.BridgeMethodBinding)1 DelegateMethodBinding (org.gradle.model.internal.manage.binding.DelegateMethodBinding)1 DirectMethodBinding (org.gradle.model.internal.manage.binding.DirectMethodBinding)1 ManagedProperty (org.gradle.model.internal.manage.binding.ManagedProperty)1 ManagedPropertyMethodBinding (org.gradle.model.internal.manage.binding.ManagedPropertyMethodBinding)1 StructMethodBinding (org.gradle.model.internal.manage.binding.StructMethodBinding)1