001 /**
002 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
003 */
004 package net.sourceforge.pmd.lang.rule.properties;
005
006 import java.lang.reflect.Method;
007
008 import net.sourceforge.pmd.util.StringUtil;
009
010 /**
011 * Defines a property type that can specify multiple methods to use as part of a rule.
012 *
013 * Rule developers can limit the rules to those within designated packages per the
014 * 'legalPackages' argument in the constructor which can be an array of partial
015 * package names, i.e., ["java.lang", "com.mycompany" ].
016 *
017 * @author Brian Remedios
018 */
019 public class MethodMultiProperty extends AbstractMultiPackagedProperty<Method[]> {
020
021 /**
022 * Constructor for MethodProperty.
023 *
024 * @param theName String
025 * @param theDescription String
026 * @param theDefaults Method[]
027 * @param legalPackageNames String[]
028 * @param theUIOrder float
029 * @throws IllegalArgumentException
030 */
031 public MethodMultiProperty(String theName, String theDescription, Method[] theDefaults, String[] legalPackageNames, float theUIOrder) {
032 super(theName, theDescription, theDefaults, legalPackageNames, theUIOrder);
033 }
034
035 /**
036 * Constructor for MethodProperty.
037 *
038 * @param theName String
039 * @param theDescription String
040 * @param methodDefaults String
041 * @param legalPackageNames String[]
042 * @param theUIOrder float
043 * @throws IllegalArgumentException
044 */
045 public MethodMultiProperty(String theName, String theDescription, String methodDefaults, String[] legalPackageNames, float theUIOrder) {
046 super(theName, theDescription, methodsFrom(methodDefaults), legalPackageNames, theUIOrder);
047 }
048
049 /**
050 * @param methodsStr String
051 * @return Method[]
052 */
053 public static Method[] methodsFrom(String methodsStr) {
054
055 String[] values = StringUtil.substringsOf(methodsStr, DELIMITER);
056
057 Method[] methods = new Method[values.length];
058 for (int i = 0; i < methods.length; i++) {
059 methods[i] = MethodProperty.methodFrom(values[i], MethodProperty.CLASS_METHOD_DELIMITER, MethodProperty.METHOD_ARG_DELIMITER);
060 }
061 return methods;
062 }
063
064 /**
065 * Return the value as a string that can be easily recognized and parsed
066 * when we see it again.
067 *
068 * @param value Object
069 * @return String
070 */
071 @Override
072 protected String asString(Object value) {
073 return value == null ? "" : MethodProperty.asStringFor((Method) value);
074 }
075
076 /**
077 * @param item Object
078 * @return String
079 */
080 @Override
081 protected String packageNameOf(Object item) {
082
083 final Method method = (Method) item;
084 return method.getDeclaringClass().getName() + '.' + method.getName();
085 }
086
087 /**
088 * @return String
089 */
090 @Override
091 protected String itemTypeName() {
092 return "method";
093 }
094
095 /**
096 *
097 * @return Class
098 * @see net.sourceforge.pmd.PropertyDescriptor#type()
099 */
100 public Class<Method[]> type() {
101 return Method[].class;
102 }
103
104 /**
105 * @param valueString String
106 * @return Object
107 * @throws IllegalArgumentException
108 * @see net.sourceforge.pmd.PropertyDescriptor#valueFrom(String)
109 */
110 public Method[] valueFrom(String valueString) throws IllegalArgumentException {
111 return methodsFrom(valueString);
112 }
113 }
|