Search in sources :

Example 1 with ClassPropertyAdapter

use of org.apache.tapestry5.commons.services.ClassPropertyAdapter in project tapestry-5 by apache.

the class ComponentDefaultProviderImplTest method no_matching_property_for_default.

@Test
public void no_matching_property_for_default() {
    String parameterName = "myparam";
    String id = "mycomponentid";
    ComponentResources resources = mockComponentResources();
    Component container = mockComponent();
    PropertyAccess access = mockPropertyAccess();
    ClassPropertyAdapter classPropertyAdapter = mockClassPropertyAdapter();
    BindingSource bindingSource = mockBindingSource();
    train_getId(resources, id);
    train_getContainer(resources, container);
    train_getAdapter(access, container, classPropertyAdapter);
    train_getPropertyAdapter(classPropertyAdapter, id, null);
    replay();
    ComponentDefaultProvider source = new ComponentDefaultProviderImpl(access, bindingSource, null, null, null);
    assertNull(source.defaultBinding(parameterName, resources));
    verify();
}
Also used : BindingSource(org.apache.tapestry5.services.BindingSource) ClassPropertyAdapter(org.apache.tapestry5.commons.services.ClassPropertyAdapter) Component(org.apache.tapestry5.runtime.Component) ComponentResources(org.apache.tapestry5.ComponentResources) PropertyAccess(org.apache.tapestry5.commons.services.PropertyAccess) ComponentDefaultProvider(org.apache.tapestry5.services.ComponentDefaultProvider) Test(org.testng.annotations.Test)

Example 2 with ClassPropertyAdapter

use of org.apache.tapestry5.commons.services.ClassPropertyAdapter in project tapestry-5 by apache.

the class PropertyAccessImpl method getAdapter.

@Override
public ClassPropertyAdapter getAdapter(Class forClass) {
    ClassPropertyAdapter result = adapters.get(forClass);
    if (result == null) {
        result = buildAdapter(forClass);
        adapters.put(forClass, result);
    }
    return result;
}
Also used : ClassPropertyAdapter(org.apache.tapestry5.commons.services.ClassPropertyAdapter)

Example 3 with ClassPropertyAdapter

use of org.apache.tapestry5.commons.services.ClassPropertyAdapter in project tapestry-5 by apache.

the class ExceptionAnalyzerImpl method extractData.

private ExceptionData extractData(Throwable t) {
    Map<String, Object> properties = CollectionFactory.newMap();
    ClassPropertyAdapter adapter = propertyAccess.getAdapter(t);
    Throwable cause = null;
    for (String name : adapter.getPropertyNames()) {
        PropertyAdapter pa = adapter.getPropertyAdapter(name);
        if (!pa.isRead())
            continue;
        if (cause == null && Throwable.class.isAssignableFrom(pa.getType())) {
            // Ignore the property, but track it as the cause.
            Throwable nestedException = (Throwable) pa.get(t);
            // Handle the case where an exception is its own cause (avoid endless loop!)
            if (t != nestedException)
                cause = nestedException;
            continue;
        }
        if (throwableProperties.contains(name))
            continue;
        Object value = pa.get(t);
        if (value == null)
            continue;
        // An interesting property, let's save it for the analysis.
        properties.put(name, value);
    }
    // Provide the stack trace only at the deepest exception.
    List<StackTraceElement> stackTrace = Collections.emptyList();
    if (cause == null)
        stackTrace = Arrays.asList(t.getStackTrace());
    ExceptionInfo info = new ExceptionInfoImpl(t, properties, stackTrace);
    return new ExceptionData(info, cause);
}
Also used : ExceptionInfo(org.apache.tapestry5.ioc.services.ExceptionInfo)

Example 4 with ClassPropertyAdapter

use of org.apache.tapestry5.commons.services.ClassPropertyAdapter in project tapestry-5 by apache.

the class BeanModelSourceImpl method orderProperties.

/**
 * @param classAdapter  defines the bean that contains the properties
 * @param propertyNames the initial set of property names, which will be rebuilt in the correct order
 */
private void orderProperties(ClassPropertyAdapter classAdapter, List<String> propertyNames) {
    List<PropertyOrder> properties = CollectionFactory.newList();
    for (String name : propertyNames) {
        PropertyAdapter pa = classAdapter.getPropertyAdapter(name);
        Method readMethod = pa.getReadMethod();
        Location location = readMethod == null ? null : proxyFactory.getMethodLocation(readMethod);
        int line = location == null ? -1 : location.getLine();
        properties.add(new PropertyOrder(name, computeDepth(pa), line));
    }
    Collections.sort(properties);
    propertyNames.clear();
    for (PropertyOrder po : properties) {
        propertyNames.add(po.propertyName);
    }
}
Also used : ClassPropertyAdapter(org.apache.tapestry5.commons.services.ClassPropertyAdapter) PropertyAdapter(org.apache.tapestry5.commons.services.PropertyAdapter) Method(java.lang.reflect.Method) Location(org.apache.tapestry5.commons.Location)

Example 5 with ClassPropertyAdapter

use of org.apache.tapestry5.commons.services.ClassPropertyAdapter in project tapestry-5 by apache.

the class BeanModelSourceImpl method create.

public <T> BeanModel<T> create(Class<T> beanClass, boolean filterReadOnlyProperties, Messages messages) {
    assert beanClass != null;
    assert messages != null;
    ClassPropertyAdapter adapter = propertyAccess.getAdapter(beanClass);
    BeanModel<T> model = new BeanModelImpl<T>(beanClass, propertyConduitSource, typeCoercer, messages, locator);
    for (final String propertyName : adapter.getPropertyNames()) {
        PropertyAdapter pa = adapter.getPropertyAdapter(propertyName);
        if (!pa.isRead()) {
            continue;
        }
        if (isStaticFieldProperty(pa)) {
            continue;
        }
        if (pa.getAnnotation(NonVisual.class) != null) {
            continue;
        }
        if (filterReadOnlyProperties && !pa.isUpdate()) {
            continue;
        }
        final String dataType = dataTypeAnalyzer.identifyDataType(pa);
        if (dataType == null) {
            continue;
        }
        model.add(propertyName).dataType(dataType);
    }
    // First, order the properties based on the location of the getter method
    // within the class.
    List<String> propertyNames = model.getPropertyNames();
    orderProperties(adapter, propertyNames);
    model.reorder(propertyNames.toArray(new String[propertyNames.size()]));
    // Next, check for an annotation with specific ordering information.
    ReorderProperties reorderAnnotation = beanClass.getAnnotation(ReorderProperties.class);
    if (reorderAnnotation != null) {
        BeanModelUtils.reorder(model, reorderAnnotation.value());
    }
    return model;
}
Also used : BeanModelImpl(org.apache.tapestry5.beanmodel.internal.beanmodel.BeanModelImpl) NonVisual(org.apache.tapestry5.beaneditor.NonVisual) ReorderProperties(org.apache.tapestry5.beaneditor.ReorderProperties) ClassPropertyAdapter(org.apache.tapestry5.commons.services.ClassPropertyAdapter) ClassPropertyAdapter(org.apache.tapestry5.commons.services.ClassPropertyAdapter) PropertyAdapter(org.apache.tapestry5.commons.services.PropertyAdapter)

Aggregations

ClassPropertyAdapter (org.apache.tapestry5.commons.services.ClassPropertyAdapter)7 PropertyAdapter (org.apache.tapestry5.commons.services.PropertyAdapter)4 ComponentResources (org.apache.tapestry5.ComponentResources)2 PropertyAccess (org.apache.tapestry5.commons.services.PropertyAccess)2 Component (org.apache.tapestry5.runtime.Component)2 BindingSource (org.apache.tapestry5.services.BindingSource)2 ComponentDefaultProvider (org.apache.tapestry5.services.ComponentDefaultProvider)2 Test (org.testng.annotations.Test)2 Method (java.lang.reflect.Method)1 Binding (org.apache.tapestry5.Binding)1 ValueEncoder (org.apache.tapestry5.ValueEncoder)1 NonVisual (org.apache.tapestry5.beaneditor.NonVisual)1 ReorderProperties (org.apache.tapestry5.beaneditor.ReorderProperties)1 BeanModelImpl (org.apache.tapestry5.beanmodel.internal.beanmodel.BeanModelImpl)1 Location (org.apache.tapestry5.commons.Location)1 ExceptionInfo (org.apache.tapestry5.ioc.services.ExceptionInfo)1