View Javadoc

1   package net.sourceforge.pmd.properties;
2   
3   import java.lang.reflect.Method;
4   
5   /**
6    * @author Brian Remedios
7    */
8   public class MethodProperty extends AbstractPMDProperty {
9   	
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, Object theDefault, float theUIOrder) {
18  		super(theName, theDescription, theDefault, theUIOrder);
19  	}
20  
21  	/**
22  	 * Method type.
23  	 * @return Class
24  	 * @see net.sourceforge.pmd.PropertyDescriptor#type()
25  	 */
26  	public Class<Method> type() {
27  		return Method.class;
28  	}
29  
30  	/**
31  	 * Method valueFrom.
32  	 * @param propertyString String
33  	 * @return Object
34  	 * @throws IllegalArgumentException
35  	 * @see net.sourceforge.pmd.PropertyDescriptor#valueFrom(String)
36  	 */
37  	public Object valueFrom(String propertyString) throws IllegalArgumentException {
38  		
39  		Class<?> cls = classIn(propertyString);
40  		String methodName = methodNameIn(propertyString);
41  		Class[] parameterTypes = parameterTypesIn(propertyString);
42  		
43  		try {
44  			return cls.getMethod(methodName, parameterTypes);
45  		} catch (Exception e) {
46  			throw new IllegalArgumentException("invalid method: " + propertyString);
47  		}
48  	}
49  
50  	private Class<?> classIn(String propertyString) throws IllegalArgumentException {
51  		
52  		int dotPos = propertyString.lastIndexOf('.');
53  		String className = propertyString.substring(0, dotPos);
54  		
55  		try {
56  			return Class.forName(className);
57  			} catch (Exception ex) {
58  				throw new IllegalArgumentException("class not found: " + className);
59  			}
60  	}
61  	
62  	private String methodNameIn(String propertyString) throws IllegalArgumentException {
63  		
64  		int dotPos = propertyString.lastIndexOf('.');
65  		return propertyString.substring(dotPos);
66  	}
67  	
68  	private Class[] parameterTypesIn(String propertyString) {
69  		return null;
70  	}
71  }