KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > core > util > ProxyValidator


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.core.util;
10
11 import java.lang.reflect.Method JavaDoc;
12 import java.text.MessageFormat JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Set JavaDoc;
17
18 /**
19  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
20  * @version $Revision: 1.2 $
21  */

22 public class ProxyValidator
23 {
24
25    private static final Set JavaDoc acceptedClasses = new HashSet JavaDoc();
26    static
27    {
28       acceptedClasses.add(String JavaDoc.class);
29       acceptedClasses.add(int.class);
30       acceptedClasses.add(boolean.class);
31       acceptedClasses.add(String JavaDoc[].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    // 0 == method
53
// 1 == method name
54
private static final String JavaDoc[] 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 JavaDoc method;
75       private String JavaDoc desc;
76       public Error(int code, Method JavaDoc method)
77       {
78          this.code = code;
79          this.method = method;
80          desc = MessageFormat.format(DESCRIPTIONS[code], new Object JavaDoc[]{method, method.getName()});
81       }
82       public int getCode()
83       {
84          return code;
85       }
86       public Method JavaDoc getMethod()
87       {
88          return method;
89       }
90       public String JavaDoc getDescription()
91       {
92          return desc;
93       }
94
95       public String JavaDoc toString()
96       {
97          return desc;
98       }
99    }
100
101    public static Error JavaDoc[] validate(Class JavaDoc itf)
102    {
103       List JavaDoc errors = new ArrayList JavaDoc();
104       Method JavaDoc[] methods = itf.getMethods();
105       Set JavaDoc getters = new HashSet JavaDoc();
106       Set JavaDoc setters = new HashSet JavaDoc();
107       for (int i = 0;i < methods.length;i++)
108       {
109          Method JavaDoc method = methods[i];
110          String JavaDoc methodName = method.getName();
111          if (methodName.startsWith("get"))
112          {
113             //
114
if (methodName.substring(3).length() == 0)
115             {
116                errors.add(new Error JavaDoc(GETTER_INVALID_NAME, method));
117             }
118             if (getters.contains(methodName.substring(3)))
119             {
120                errors.add(new Error JavaDoc(GETTER_DUPLICATE_NAME, method));
121             }
122             if (!acceptedClasses.contains(method.getReturnType()))
123             {
124                errors.add(new Error JavaDoc(GETTER_INVALID_RETURN_TYPE, method));
125             }
126             if (method.getParameterTypes().length == 0)
127             {
128                errors.add(new Error JavaDoc(GETTER_NO_ARGUMENT, method));
129             }
130             else if (method.getParameterTypes().length > 1)
131             {
132                errors.add(new Error JavaDoc(GETTER_TOO_MANY_ARGUMENTS, method));
133             }
134             else if (!method.getReturnType().equals(method.getParameterTypes()[0]))
135             {
136                errors.add(new Error JavaDoc(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 JavaDoc(SETTER_NO_ARGUMENT, method));
145             }
146             else if (method.getParameterTypes().length > 1)
147             {
148                errors.add(new Error JavaDoc(SETTER_TOO_MANY_ARGUMENTS, method));
149             }
150             else if (!acceptedClasses.contains(method.getParameterTypes()[0]))
151             {
152                errors.add(new Error JavaDoc(SETTER_INVALID_ARGUMENT_TYPE, method));
153             }
154             if (methodName.substring(3).length() == 0)
155             {
156                errors.add(new Error JavaDoc(SETTER_INVALID_NAME, method));
157             }
158             if (setters.contains(methodName.substring(3)))
159             {
160                errors.add(new Error JavaDoc(SETTER_DUPLICATE_NAME, method));
161             }
162             if (!method.getReturnType().equals(void.class))
163             {
164                errors.add(new Error JavaDoc(SETTER_RETURN_TYPE_IS_NOT_VOID, method));
165             }
166             setters.add(methodName.substring(3));
167          }
168          else
169          {
170             // Invalid
171
errors.add(new Error JavaDoc(METHOD_NOT_ACCESSOR, method));
172          }
173       }
174
175       return (Error JavaDoc[])errors.toArray(new Error JavaDoc[errors.size()]);
176    }
177 }
178
Popular Tags