KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > enhancer > AccessorVisitor


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19 package org.apache.cayenne.enhancer;
20
21 import java.util.regex.Matcher JavaDoc;
22 import java.util.regex.Pattern JavaDoc;
23
24 import org.objectweb.asm.ClassAdapter;
25 import org.objectweb.asm.ClassVisitor;
26 import org.objectweb.asm.MethodVisitor;
27 import org.objectweb.asm.Type;
28
29 /**
30  * An enhancer that adds interceptor code to the getters and setters.
31  *
32  * @since 3.0
33  * @author Andrus Adamchik
34  */

35 public abstract class AccessorVisitor extends ClassAdapter {
36
37     // duplicated from JpaClassDescriptor.
38
private static final Pattern JavaDoc GETTER_PATTERN = Pattern
39             .compile("^(is|get)([A-Z])(.*)$");
40
41     private static final Pattern JavaDoc SETTER_PATTERN = Pattern.compile("^set([A-Z])(.*)$");
42
43     private EnhancementHelper helper;
44
45     public static String JavaDoc propertyNameForGetter(String JavaDoc getterName) {
46         Matcher JavaDoc getMatch = GETTER_PATTERN.matcher(getterName);
47         if (getMatch.matches()) {
48             return getMatch.group(2).toLowerCase() + getMatch.group(3);
49         }
50
51         return null;
52     }
53
54     public static String JavaDoc propertyNameForSetter(String JavaDoc setterName) {
55         Matcher JavaDoc setMatch = SETTER_PATTERN.matcher(setterName);
56
57         if (setMatch.matches()) {
58             return setMatch.group(1).toLowerCase() + setMatch.group(2);
59         }
60
61         return null;
62     }
63
64     public AccessorVisitor(ClassVisitor cw) {
65         super(cw);
66         this.helper = new EnhancementHelper(this);
67     }
68
69     protected abstract boolean isEnhancedProperty(String JavaDoc property);
70
71     protected abstract boolean isLazyFaulted(String JavaDoc property);
72
73     @Override JavaDoc
74     public void visit(
75             int version,
76             int access,
77             String JavaDoc name,
78             String JavaDoc signature,
79             String JavaDoc superName,
80             String JavaDoc[] interfaces) {
81
82         helper.reset(name);
83         super.visit(version, access, name, signature, superName, interfaces);
84     }
85
86     protected MethodVisitor visitGetter(
87             MethodVisitor mv,
88             String JavaDoc property,
89             Type propertyType) {
90
91         if (isEnhancedProperty(property)) {
92             if (isLazyFaulted(property)) {
93                 // inject fault flag field
94
helper.createField(Boolean.TYPE, "faultResolved_" + property, true);
95                 return new GetterVisitor(mv, helper, property, true);
96             }
97             else {
98                 return new GetterVisitor(mv, helper, property, false);
99             }
100         }
101
102         return mv;
103     }
104
105     protected MethodVisitor visitSetter(
106             MethodVisitor mv,
107             String JavaDoc property,
108             Type propertyType) {
109
110         if (isEnhancedProperty(property)) {
111             return new SetterVisitor(mv, helper, property, propertyType);
112         }
113
114         return mv;
115     }
116
117     @Override JavaDoc
118     public MethodVisitor visitMethod(
119             int access,
120             String JavaDoc name,
121             String JavaDoc desc,
122             String JavaDoc signature,
123             String JavaDoc[] exceptions) {
124
125         MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
126
127         // TODO: andrus, 10/8/2006 - what other signature checks do we need to do?
128

129         Type returnType = Type.getReturnType(desc);
130         Type[] args = Type.getArgumentTypes(desc);
131
132         // possible setter
133
if ("V".equals(returnType.getDescriptor())) {
134             if (args.length == 1) {
135                 String JavaDoc setProperty = AccessorVisitor.propertyNameForSetter(name);
136                 if (setProperty != null) {
137                     return visitSetter(mv, setProperty, args[0]);
138                 }
139             }
140         }
141         // possible getter
142
else if (args.length == 0) {
143             String JavaDoc getProperty = AccessorVisitor.propertyNameForGetter(name);
144             if (getProperty != null) {
145                 return visitGetter(mv, getProperty, returnType);
146             }
147         }
148
149         return mv;
150     }
151 }
152
Popular Tags