001 // Copyright 2008, 2010, 2011 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 015 package org.apache.tapestry5.internal.transform; 016 017 import org.apache.tapestry5.annotations.Property; 018 import org.apache.tapestry5.model.MutableComponentModel; 019 import org.apache.tapestry5.plastic.PlasticClass; 020 import org.apache.tapestry5.plastic.PlasticField; 021 import org.apache.tapestry5.plastic.PropertyAccessType; 022 import org.apache.tapestry5.services.transform.ComponentClassTransformWorker2; 023 import org.apache.tapestry5.services.transform.TransformationSupport; 024 025 /** 026 * Provides the getter and setter methods. The methods are added as "existing", meaning that field access to them will 027 * be transformed as necessary by other annotations. This worker needs to be scheduled before any worker that might 028 * delete a field. 029 * 030 * @see org.apache.tapestry5.annotations.Property 031 */ 032 public class PropertyWorker implements ComponentClassTransformWorker2 033 { 034 035 public void transform(PlasticClass plasticClass, TransformationSupport support, MutableComponentModel model) 036 { 037 for (PlasticField field : plasticClass.getFieldsWithAnnotation(Property.class)) 038 { 039 createAccessorsForField(field); 040 } 041 } 042 043 private void createAccessorsForField(PlasticField field) 044 { 045 PropertyAccessType accessType = toType(field); 046 047 field.createAccessors(accessType); 048 } 049 050 private PropertyAccessType toType(PlasticField field) 051 { 052 Property annotation = field.getAnnotation(Property.class); 053 054 boolean read = annotation.read(); 055 boolean write = annotation.write(); 056 057 if (read && write) 058 return PropertyAccessType.READ_WRITE; 059 060 if (read) 061 return PropertyAccessType.READ_ONLY; 062 063 if (write) 064 return PropertyAccessType.WRITE_ONLY; 065 066 throw new IllegalArgumentException(String.format( 067 "@Property annotation on %s.%s should have either read() or write() enabled.", field.getPlasticClass() 068 .getClassName(), field.getName())); 069 } 070 }