1 18 package org.apache.beehive.controls.system.jdbc.parser; 19 20 import com.sun.mirror.declaration.ClassDeclaration; 21 import com.sun.mirror.declaration.FieldDeclaration; 22 import com.sun.mirror.declaration.MethodDeclaration; 23 import com.sun.mirror.declaration.ParameterDeclaration; 24 import com.sun.mirror.declaration.TypeDeclaration; 25 import com.sun.mirror.type.DeclaredType; 26 import com.sun.mirror.type.TypeMirror; 27 import com.sun.mirror.util.DeclarationFilter; 28 import org.apache.beehive.controls.api.ControlException; 29 30 import java.util.ArrayList ; 31 import java.util.Collection ; 32 import java.util.HashMap ; 33 34 38 public class ParameterChecker { 39 40 46 public static void checkReflectionParameters(SqlFragmentContainer statement, MethodDeclaration methodDecl) { 47 48 ArrayList <ParameterDeclaration> params = 49 new ArrayList <ParameterDeclaration>(methodDecl.getParameters()); 50 HashMap <String , ParameterDeclaration> paramMap = new HashMap <String , ParameterDeclaration>(); 51 52 if (params.size() > 0 && params.get(0).getSimpleName().equals("arg0")) { 54 return; 55 } 56 57 for (int i = 0; i < params.size(); i++) { 58 paramMap.put(params.get(i).getSimpleName(), params.get(i)); 59 } 60 61 doCheck(statement, paramMap, methodDecl); 62 } 63 64 71 private static void doCheck(SqlFragmentContainer statement, HashMap <String , ParameterDeclaration> paramMap, 72 final MethodDeclaration method) 73 { 74 75 SqlFragment[] fragments = statement.getChildren(); 76 for (SqlFragment fragment : fragments) { 77 78 if (fragment instanceof SqlFragmentContainer) { 80 doCheck((SqlFragmentContainer) fragment, paramMap, method); 81 82 } else if (fragment instanceof ReflectionFragment) { 84 checkReflectionFragment((ReflectionFragment) fragment, paramMap, method); 85 } 86 } 87 } 88 89 97 private static void checkReflectionFragment(ReflectionFragment fragment, 98 HashMap <String , ParameterDeclaration> paramMap, MethodDeclaration method) 99 { 100 101 final String [] paramNameQualifiers = ((ReflectionFragment) fragment).getParameterNameQualifiers(); 102 final String parameterName = ((ReflectionFragment) fragment).getParameterName(); 103 104 if (paramMap.containsKey(paramNameQualifiers[0]) == false) { 105 throw new ControlException(buildMessage(parameterName, method.getSimpleName())); 106 } 107 108 ParameterDeclaration tpd = paramMap.get(paramNameQualifiers[0]); 109 TypeMirror type = tpd.getType(); 110 111 MethodDeclaration getterMethod = null; 112 FieldDeclaration field = null; 113 114 for (int i = 1; i < paramNameQualifiers.length; i++) { 115 116 getterMethod = null; 117 field = null; 118 119 while (type != null) { 121 122 if (type instanceof DeclaredType == false) { 123 throw new ControlException(buildMessage(parameterName, method.getSimpleName())); 124 } 125 126 TypeDeclaration td = ((DeclaredType) type).getDeclaration(); 127 if (td.getQualifiedName().equals("java.util.Map")) { 131 return; 132 } 133 134 Collection <? extends MethodDeclaration> methods = 135 DeclarationFilter.FILTER_PUBLIC.filter(td.getMethods()); 136 for (MethodDeclaration m : methods) { 137 String upperFirst = paramNameQualifiers[i].substring(0,1).toUpperCase(); 138 if (paramNameQualifiers[i].length() > 1) { 139 upperFirst = upperFirst + paramNameQualifiers[i].substring(1); 140 } 141 if (m.getSimpleName().equals("get" + upperFirst) 142 || m.getSimpleName().equals("is" + upperFirst)) { 143 getterMethod = m; 144 } 145 } 146 147 if (getterMethod == null) { 148 Collection <FieldDeclaration> fields = 149 DeclarationFilter.FILTER_PUBLIC.filter(td.getFields()); 150 for (FieldDeclaration fd : fields) { 151 if (fd.getSimpleName().equals(paramNameQualifiers[i])) { 152 field = fd; 153 } 154 } 155 } 156 157 if (getterMethod == null && field == null) { 159 if (td instanceof ClassDeclaration) { 160 type = ((ClassDeclaration) td).getSuperclass(); 161 continue; 162 } 163 } 164 165 break; 166 } 168 if (getterMethod != null) { 170 type = getterMethod.getReturnType(); 171 } else if (field != null) { 172 type = field.getType(); 173 } else { 174 throw new ControlException(buildMessage(parameterName, method.getSimpleName())); 175 } 176 } 177 } 178 179 186 private static String buildMessage(String parameterName, String methodName) { 187 StringBuilder message = new StringBuilder (); 188 message.append("Unable to map the parameter in SQL statement "); 189 message.append(parameterName); 190 message.append(" to a parameter of the method "); 191 message.append(methodName); 192 message.append(". Mapping is accomplished by matching a method parameter name to "); 193 message.append("a value delimited by '{' and '}' in the statement element."); 194 return message.toString(); 195 } 196 } 197 | Popular Tags |