001// Copyright 2008, 2010, 2011, 2024 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.transform;
016
017import org.apache.tapestry5.SymbolConstants;
018import org.apache.tapestry5.annotations.Property;
019import org.apache.tapestry5.ioc.annotations.Symbol;
020import org.apache.tapestry5.model.MutableComponentModel;
021import org.apache.tapestry5.plastic.MethodAlreadyExistsException;
022import org.apache.tapestry5.plastic.PlasticClass;
023import org.apache.tapestry5.plastic.PlasticField;
024import org.apache.tapestry5.plastic.PropertyAccessType;
025import org.apache.tapestry5.services.transform.ComponentClassTransformWorker2;
026import org.apache.tapestry5.services.transform.TransformationSupport;
027
028/**
029 * Provides the getter and setter methods. The methods are added as "existing", meaning that field access to them will
030 * be transformed as necessary by other annotations. This worker needs to be scheduled before any worker that might
031 * delete a field.
032 * 
033 * @see org.apache.tapestry5.annotations.Property
034 */
035public class PropertyWorker implements ComponentClassTransformWorker2
036{
037    
038    final private boolean multipleClassloaders;
039
040    public PropertyWorker(@Symbol(SymbolConstants.MULTIPLE_CLASSLOADERS) final boolean multipleClassloaders)
041    {
042        super();
043        this.multipleClassloaders = multipleClassloaders;
044    }
045
046    public void transform(PlasticClass plasticClass, TransformationSupport support, MutableComponentModel model)
047    {
048        for (PlasticField field : plasticClass.getFieldsWithAnnotation(Property.class))
049        {
050            try
051            {
052                createAccessorsForField(field);
053            }
054            catch (MethodAlreadyExistsException e)
055            {
056                // Method was already created somewhere else, so
057                // nothing to do here 
058                if (!multipleClassloaders)
059                {
060                    throw e;
061                }
062            }
063        }
064    }
065
066    private void createAccessorsForField(PlasticField field)
067    {
068        PropertyAccessType accessType = toType(field);
069
070        field.createAccessors(accessType);
071    }
072
073    private PropertyAccessType toType(PlasticField field)
074    {
075        Property annotation = field.getAnnotation(Property.class);
076
077        boolean read = annotation.read();
078        boolean write = annotation.write();
079
080        if (read && write)
081            return PropertyAccessType.READ_WRITE;
082
083        if (read)
084            return PropertyAccessType.READ_ONLY;
085
086        if (write)
087            return PropertyAccessType.WRITE_ONLY;
088
089        throw new IllegalArgumentException(String.format(
090                "@Property annotation on %s.%s should have either read() or write() enabled.", field.getPlasticClass()
091                        .getClassName(), field.getName()));
092    }
093}