01: package net.sourceforge.pmd.properties;
02:
03: import java.lang.reflect.Method;
04:
05: /**
06: * @author Brian Remedios
07: */
08: public class MethodProperty extends AbstractPMDProperty {
09:
10: /**
11: * Constructor for MethodProperty.
12: * @param theName String
13: * @param theDescription String
14: * @param theDefault Object
15: * @param theUIOrder float
16: */
17: public MethodProperty(String theName, String theDescription,
18: Object theDefault, float theUIOrder) {
19: super (theName, theDescription, theDefault, theUIOrder);
20: }
21:
22: /**
23: * Method type.
24: * @return Class
25: * @see net.sourceforge.pmd.PropertyDescriptor#type()
26: */
27: public Class<Method> type() {
28: return Method.class;
29: }
30:
31: /**
32: * Method valueFrom.
33: * @param propertyString String
34: * @return Object
35: * @throws IllegalArgumentException
36: * @see net.sourceforge.pmd.PropertyDescriptor#valueFrom(String)
37: */
38: public Object valueFrom(String propertyString)
39: throws IllegalArgumentException {
40:
41: Class<?> cls = classIn(propertyString);
42: String methodName = methodNameIn(propertyString);
43: Class[] parameterTypes = parameterTypesIn(propertyString);
44:
45: try {
46: return cls.getMethod(methodName, parameterTypes);
47: } catch (Exception e) {
48: throw new IllegalArgumentException("invalid method: "
49: + propertyString);
50: }
51: }
52:
53: private Class<?> classIn(String propertyString)
54: throws IllegalArgumentException {
55:
56: int dotPos = propertyString.lastIndexOf('.');
57: String className = propertyString.substring(0, dotPos);
58:
59: try {
60: return Class.forName(className);
61: } catch (Exception ex) {
62: throw new IllegalArgumentException("class not found: "
63: + className);
64: }
65: }
66:
67: private String methodNameIn(String propertyString)
68: throws IllegalArgumentException {
69:
70: int dotPos = propertyString.lastIndexOf('.');
71: return propertyString.substring(dotPos);
72: }
73:
74: private Class[] parameterTypesIn(String propertyString) {
75: return null;
76: }
77: }
|