001// Copyright 2006, 2007, 2010, 2011, 2012 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.ioc.internal.services;
016
017import org.apache.tapestry5.ioc.services.*;
018import org.apache.tapestry5.plastic.*;
019
020import java.lang.reflect.Method;
021
022public class PropertyShadowBuilderImpl implements PropertyShadowBuilder
023{
024    private final PropertyAccess propertyAccess;
025
026    private final PlasticProxyFactory proxyFactory;
027
028    public PropertyShadowBuilderImpl(@Builtin
029                                     PlasticProxyFactory proxyFactory,
030
031                                     PropertyAccess propertyAccess)
032    {
033        this.proxyFactory = proxyFactory;
034        this.propertyAccess = propertyAccess;
035    }
036
037    public <T> T build(final Object source, final String propertyName, final Class<T> propertyType)
038    {
039        final Class sourceClass = source.getClass();
040        final PropertyAdapter adapter = propertyAccess.getAdapter(sourceClass).getPropertyAdapter(propertyName);
041
042        // TODO: Perhaps extend ClassPropertyAdapter to do these checks?
043
044        if (adapter == null)
045            throw new RuntimeException(ServiceMessages.noSuchProperty(sourceClass, propertyName));
046
047        if (!adapter.isRead())
048        {
049            throw new RuntimeException(
050                    String.format("Class %s does not provide an accessor ('getter') method for property '%s'.",
051                            source.getClass().getName(), propertyName));
052        }
053
054        if (!propertyType.isAssignableFrom(adapter.getType()))
055            throw new RuntimeException(ServiceMessages.propertyTypeMismatch(propertyName, sourceClass,
056                    adapter.getType(), propertyType));
057
058        ClassInstantiator instantiator = proxyFactory.createProxy(propertyType, new PlasticClassTransformer()
059        {
060            public void transform(PlasticClass plasticClass)
061            {
062                final PlasticField sourceField = plasticClass.introduceField(sourceClass, "source").inject(source);
063
064                PlasticMethod delegateMethod = plasticClass.introducePrivateMethod(propertyType.getName(),
065                        "readProperty", null, null);
066
067                // You don't do this using MethodAdvice, because then we'd have to use reflection to access the read
068                // method.
069
070                delegateMethod.changeImplementation(new InstructionBuilderCallback()
071                {
072                    public void doBuild(InstructionBuilder builder)
073                    {
074                        builder.loadThis().getField(sourceField);
075                        builder.invoke(sourceClass, propertyType, adapter.getReadMethod().getName());
076
077                        // Now add the null check.
078
079                        builder.dupe().when(Condition.NULL, new InstructionBuilderCallback()
080                        {
081                            public void doBuild(InstructionBuilder builder)
082                            {
083                                builder.throwException(
084                                        NullPointerException.class,
085                                        String.format(
086                                                "Unable to delegate method invocation to property '%s' of %s, because the property is null.",
087                                                propertyName, source));
088                            }
089                        });
090
091                        builder.returnResult();
092                    }
093                });
094
095                for (Method m : propertyType.getMethods())
096                {
097                    plasticClass.introduceMethod(m).delegateTo(delegateMethod);
098                }
099
100                plasticClass.addToString(String.format("<Shadow: property %s of %s>", propertyName, source));
101            }
102        });
103
104        return propertyType.cast(instantiator.newInstance());
105    }
106}