1 19 package org.apache.cayenne.enhancer; 20 21 import java.util.regex.Matcher ; 22 import java.util.regex.Pattern ; 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 35 public abstract class AccessorVisitor extends ClassAdapter { 36 37 private static final Pattern GETTER_PATTERN = Pattern 39 .compile("^(is|get)([A-Z])(.*)$"); 40 41 private static final Pattern SETTER_PATTERN = Pattern.compile("^set([A-Z])(.*)$"); 42 43 private EnhancementHelper helper; 44 45 public static String propertyNameForGetter(String getterName) { 46 Matcher 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 propertyNameForSetter(String setterName) { 55 Matcher 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 property); 70 71 protected abstract boolean isLazyFaulted(String property); 72 73 @Override 74 public void visit( 75 int version, 76 int access, 77 String name, 78 String signature, 79 String superName, 80 String [] 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 property, 89 Type propertyType) { 90 91 if (isEnhancedProperty(property)) { 92 if (isLazyFaulted(property)) { 93 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 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 118 public MethodVisitor visitMethod( 119 int access, 120 String name, 121 String desc, 122 String signature, 123 String [] exceptions) { 124 125 MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions); 126 127 129 Type returnType = Type.getReturnType(desc); 130 Type[] args = Type.getArgumentTypes(desc); 131 132 if ("V".equals(returnType.getDescriptor())) { 134 if (args.length == 1) { 135 String setProperty = AccessorVisitor.propertyNameForSetter(name); 136 if (setProperty != null) { 137 return visitSetter(mv, setProperty, args[0]); 138 } 139 } 140 } 141 else if (args.length == 0) { 143 String 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 |