01: // Copyright 2006, 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // 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:
15: package org.apache.tapestry.internal.services;
16:
17: import static org.apache.tapestry.services.TransformConstants.CONTAINING_PAGE_DID_DETACH_SIGNATURE;
18: import static org.apache.tapestry.services.TransformConstants.CONTAINING_PAGE_DID_LOAD_SIGNATURE;
19:
20: import java.lang.reflect.Modifier;
21: import java.util.List;
22:
23: import org.apache.tapestry.model.MutableComponentModel;
24: import org.apache.tapestry.services.ClassTransformation;
25: import org.apache.tapestry.services.ComponentClassTransformWorker;
26:
27: /**
28: * Designed to be just about the last worker in the pipeline. Its job is to add cleanup code that
29: * restores transient fields back to their initial (null) value. Fields that have been previously
30: * {@link ClassTransformation#claimField(String, Object) claimed} are ignored, as are fields that
31: * are final.
32: */
33: public final class UnclaimedFieldWorker implements
34: ComponentClassTransformWorker {
35:
36: public void transform(ClassTransformation transformation,
37: MutableComponentModel model) {
38: List<String> fieldNames = transformation.findUnclaimedFields();
39:
40: for (String fieldName : fieldNames) {
41: transformField(fieldName, transformation);
42: }
43: }
44:
45: private void transformField(String fieldName,
46: ClassTransformation transformation) {
47: int modifiers = transformation.getFieldModifiers(fieldName);
48:
49: if (Modifier.isFinal(modifiers))
50: return;
51:
52: String type = transformation.getFieldType(fieldName);
53:
54: String defaultFieldName = transformation.addField(
55: Modifier.PRIVATE, type, fieldName + "_default");
56:
57: transformation.extendMethod(CONTAINING_PAGE_DID_LOAD_SIGNATURE,
58: defaultFieldName + " = " + fieldName + ";");
59:
60: // At the end of the request, we want to move the default value back over the
61: // active field value. This will most often be null.
62:
63: transformation.extendMethod(
64: CONTAINING_PAGE_DID_DETACH_SIGNATURE, fieldName + " = "
65: + defaultFieldName + ";");
66: }
67: }
|