001    //Copyright 2005 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    package net.sf.tacos.annotations.enhance;
015    
016    import java.lang.reflect.Modifier;
017    
018    import org.apache.hivemind.ApplicationRuntimeException;
019    import org.apache.hivemind.Location;
020    import org.apache.hivemind.service.BodyBuilder;
021    import org.apache.hivemind.service.MethodSignature;
022    import org.apache.hivemind.util.Defense;
023    import org.apache.tapestry.enhance.EnhancementOperation;
024    import org.apache.tapestry.enhance.InjectEnhancementWorker;
025    import org.apache.tapestry.spec.InjectSpecification;
026    
027    /**
028     * @author Patrick Moore
029     */
030    public class InjectParameterFlagWorker implements InjectEnhancementWorker {
031    
032        public void performEnhancement(EnhancementOperation op,
033                InjectSpecification spec) {
034            injectParameterFlag(op, spec.getObject(), spec.getProperty(), spec.getLocation());
035        }
036        void injectParameterFlag(EnhancementOperation op, String parameterName, String propertyName,
037                Location location)
038        {
039            Defense.notNull(op, "op");
040            Defense.notNull(parameterName, "parameterName");
041            Defense.notNull(propertyName, "propertyName");
042    
043            Class propertyType = op.getPropertyType(propertyName);
044    
045            // null means no property at all; it's just in the XML
046            // which is ok. Otherwise, make sure it is exactly boolean.
047    
048            if (propertyType != null && propertyType != boolean.class)
049                throw new ApplicationRuntimeException("Property " + propertyName + " must be boolean",
050                        location, null);
051    
052            op.claimReadonlyProperty(propertyName);
053            
054            BodyBuilder builder = new BodyBuilder();
055            builder.begin();
056            builder.add("return isParameterBound(");
057            builder.addQuoted(parameterName);
058            builder.addln(");");
059            builder.end();
060    
061            String methodName = op.getAccessorMethodName(propertyName);
062    
063            MethodSignature sig = new MethodSignature(boolean.class, methodName, null, null);
064    
065            op.addMethod(Modifier.PUBLIC, sig, builder.toString(), location);
066        }
067    
068    }