1 9 package org.jboss.portal.core.util; 10 11 import java.lang.reflect.Method ; 12 import java.text.MessageFormat ; 13 import java.util.ArrayList ; 14 import java.util.HashSet ; 15 import java.util.List ; 16 import java.util.Set ; 17 18 22 public class ProxyValidator 23 { 24 25 private static final Set acceptedClasses = new HashSet (); 26 static 27 { 28 acceptedClasses.add(String .class); 29 acceptedClasses.add(int.class); 30 acceptedClasses.add(boolean.class); 31 acceptedClasses.add(String [].class); 32 acceptedClasses.add(int[].class); 33 acceptedClasses.add(boolean[].class); 34 } 35 36 public static final int METHOD_NOT_ACCESSOR = 0; 37 38 public static final int GETTER_INVALID_NAME = 1; 39 public static final int GETTER_DUPLICATE_NAME = 2; 40 public static final int GETTER_INVALID_RETURN_TYPE = 3; 41 public static final int GETTER_NO_ARGUMENT = 4; 42 public static final int GETTER_TOO_MANY_ARGUMENTS = 5; 43 public static final int GETTER_RETURN_TYPE_DOES_NOT_MATCH_ARGUMENT_TYPE = 6; 44 45 public static final int SETTER_DUPLICATE_NAME = 7; 46 public static final int SETTER_INVALID_NAME = 8; 47 public static final int SETTER_NO_ARGUMENT = 9; 48 public static final int SETTER_TOO_MANY_ARGUMENTS = 10; 49 public static final int SETTER_RETURN_TYPE_IS_NOT_VOID =11; 50 public static final int SETTER_INVALID_ARGUMENT_TYPE = 12; 51 52 private static final String [] DESCRIPTIONS = 55 { 56 "Method {0} is not an accessor", 57 "Name {1} is not valid", 58 "Name {1} is duplicated", 59 "Method {0} has an invalid return type", 60 "Method {0} has no argument", 61 "Method {0} has too many arguments", 62 "Method {0} does not have a return type matching the argument type", 63 "Name {1} is duplicated", 64 "Name {1} is not valid", 65 "Method {0} has no argument", 66 "Method {0} has too many arguments", 67 "Method {0} has return type which is not void", 68 "Method {0} has an invalid argument type", 69 }; 70 71 public static class Error 72 { 73 private int code; 74 private Method method; 75 private String desc; 76 public Error(int code, Method method) 77 { 78 this.code = code; 79 this.method = method; 80 desc = MessageFormat.format(DESCRIPTIONS[code], new Object []{method, method.getName()}); 81 } 82 public int getCode() 83 { 84 return code; 85 } 86 public Method getMethod() 87 { 88 return method; 89 } 90 public String getDescription() 91 { 92 return desc; 93 } 94 95 public String toString() 96 { 97 return desc; 98 } 99 } 100 101 public static Error [] validate(Class itf) 102 { 103 List errors = new ArrayList (); 104 Method [] methods = itf.getMethods(); 105 Set getters = new HashSet (); 106 Set setters = new HashSet (); 107 for (int i = 0;i < methods.length;i++) 108 { 109 Method method = methods[i]; 110 String methodName = method.getName(); 111 if (methodName.startsWith("get")) 112 { 113 if (methodName.substring(3).length() == 0) 115 { 116 errors.add(new Error (GETTER_INVALID_NAME, method)); 117 } 118 if (getters.contains(methodName.substring(3))) 119 { 120 errors.add(new Error (GETTER_DUPLICATE_NAME, method)); 121 } 122 if (!acceptedClasses.contains(method.getReturnType())) 123 { 124 errors.add(new Error (GETTER_INVALID_RETURN_TYPE, method)); 125 } 126 if (method.getParameterTypes().length == 0) 127 { 128 errors.add(new Error (GETTER_NO_ARGUMENT, method)); 129 } 130 else if (method.getParameterTypes().length > 1) 131 { 132 errors.add(new Error (GETTER_TOO_MANY_ARGUMENTS, method)); 133 } 134 else if (!method.getReturnType().equals(method.getParameterTypes()[0])) 135 { 136 errors.add(new Error (GETTER_RETURN_TYPE_DOES_NOT_MATCH_ARGUMENT_TYPE, method)); 137 } 138 getters.add(methodName.substring(3)); 139 } 140 else if (methodName.startsWith("set")) 141 { 142 if (method.getParameterTypes().length == 0) 143 { 144 errors.add(new Error (SETTER_NO_ARGUMENT, method)); 145 } 146 else if (method.getParameterTypes().length > 1) 147 { 148 errors.add(new Error (SETTER_TOO_MANY_ARGUMENTS, method)); 149 } 150 else if (!acceptedClasses.contains(method.getParameterTypes()[0])) 151 { 152 errors.add(new Error (SETTER_INVALID_ARGUMENT_TYPE, method)); 153 } 154 if (methodName.substring(3).length() == 0) 155 { 156 errors.add(new Error (SETTER_INVALID_NAME, method)); 157 } 158 if (setters.contains(methodName.substring(3))) 159 { 160 errors.add(new Error (SETTER_DUPLICATE_NAME, method)); 161 } 162 if (!method.getReturnType().equals(void.class)) 163 { 164 errors.add(new Error (SETTER_RETURN_TYPE_IS_NOT_VOID, method)); 165 } 166 setters.add(methodName.substring(3)); 167 } 168 else 169 { 170 errors.add(new Error (METHOD_NOT_ACCESSOR, method)); 172 } 173 } 174 175 return (Error [])errors.toArray(new Error [errors.size()]); 176 } 177 } 178 | Popular Tags |