001// Copyright 2007 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.internal.services;
016
017import java.lang.annotation.Annotation;
018import java.lang.reflect.Type;
019
020import org.apache.tapestry5.PropertyConduit;
021import org.apache.tapestry5.PropertyConduit2;
022import org.apache.tapestry5.ioc.services.TypeCoercer;
023
024public class CoercingPropertyConduitWrapper implements PropertyConduit2
025{
026    private final PropertyConduit conduit;
027
028    private final TypeCoercer coercer;
029
030    public CoercingPropertyConduitWrapper(final PropertyConduit conduit, final TypeCoercer coercer)
031    {
032        this.conduit = conduit;
033        this.coercer = coercer;
034    }
035
036    public Object get(Object instance)
037    {
038        return conduit.get(instance);
039    }
040
041    public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
042    {
043        return conduit.getAnnotation(annotationClass);
044    }
045
046    public Class getPropertyType()
047    {
048        return conduit.getPropertyType();
049    }
050    
051    public Type getPropertyGenericType()
052    {
053        if (conduit instanceof PropertyConduit2) {
054                return ((PropertyConduit2) conduit).getPropertyGenericType();
055        }
056        return conduit.getPropertyType();
057    }
058
059    @SuppressWarnings("unchecked")
060    public void set(Object instance, Object value)
061    {
062        Object coerced = coercer.coerce(value, getPropertyType());
063
064        conduit.set(instance, coerced);
065    }
066
067}