View Javadoc

1   //Copyright 2005 The Apache Software Foundation
2   //
3   // Licensed under the Apache License, Version 2.0 (the "License");
4   // you may not use this file except in compliance with the License.
5   // You may obtain a copy of the License at
6   //
7   //     http://www.apache.org/licenses/LICENSE-2.0
8   //
9   // Unless required by applicable law or agreed to in writing, software
10  // distributed under the License is distributed on an "AS IS" BASIS,
11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  // See the License for the specific language governing permissions and
13  // limitations under the License.
14  package net.sf.tacos.annotations.enhance;
15  
16  import java.lang.reflect.Modifier;
17  
18  import org.apache.hivemind.ApplicationRuntimeException;
19  import org.apache.hivemind.Location;
20  import org.apache.hivemind.service.BodyBuilder;
21  import org.apache.hivemind.service.MethodSignature;
22  import org.apache.hivemind.util.Defense;
23  import org.apache.tapestry.enhance.EnhancementOperation;
24  import org.apache.tapestry.enhance.InjectEnhancementWorker;
25  import org.apache.tapestry.spec.InjectSpecification;
26  
27  /**
28   * @author Patrick Moore
29   */
30  public class InjectParameterFlagWorker implements InjectEnhancementWorker {
31  
32      public void performEnhancement(EnhancementOperation op,
33              InjectSpecification spec) {
34          injectParameterFlag(op, spec.getObject(), spec.getProperty(), spec.getLocation());
35      }
36      void injectParameterFlag(EnhancementOperation op, String parameterName, String propertyName,
37              Location location)
38      {
39          Defense.notNull(op, "op");
40          Defense.notNull(parameterName, "parameterName");
41          Defense.notNull(propertyName, "propertyName");
42  
43          Class propertyType = op.getPropertyType(propertyName);
44  
45          // null means no property at all; it's just in the XML
46          // which is ok. Otherwise, make sure it is exactly boolean.
47  
48          if (propertyType != null && propertyType != boolean.class)
49              throw new ApplicationRuntimeException("Property " + propertyName + " must be boolean",
50                      location, null);
51  
52          op.claimReadonlyProperty(propertyName);
53          
54          BodyBuilder builder = new BodyBuilder();
55          builder.begin();
56          builder.add("return isParameterBound(");
57          builder.addQuoted(parameterName);
58          builder.addln(");");
59          builder.end();
60  
61          String methodName = op.getAccessorMethodName(propertyName);
62  
63          MethodSignature sig = new MethodSignature(boolean.class, methodName, null, null);
64  
65          op.addMethod(Modifier.PUBLIC, sig, builder.toString(), location);
66      }
67  
68  }