View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.rule.properties;
5   
6   import java.lang.reflect.Method;
7   import java.util.Map;
8   
9   import net.sourceforge.pmd.PropertyDescriptorFactory;
10  import net.sourceforge.pmd.lang.rule.properties.factories.BasicPropertyDescriptorFactory;
11  import net.sourceforge.pmd.util.StringUtil;
12  
13  /**
14   * Defines a property type that can specify multiple methods to use as part of a
15   * rule.
16   *
17   * Rule developers can limit the rules to those within designated packages per
18   * the 'legalPackages' argument in the constructor which can be an array of
19   * partial package names, i.e., ["java.lang", "com.mycompany" ].
20   *
21   * @author Brian Remedios
22   */
23  public class MethodMultiProperty extends AbstractMultiPackagedProperty<Method[]> {
24  
25      public static final PropertyDescriptorFactory FACTORY = new BasicPropertyDescriptorFactory<MethodMultiProperty>(
26              Method[].class, PACKAGED_FIELD_TYPES_BY_KEY) {
27  
28          public MethodMultiProperty createWith(Map<String, String> valuesById) {
29              char delimiter = delimiterIn(valuesById);
30              return new MethodMultiProperty(nameIn(valuesById), descriptionIn(valuesById), defaultValueIn(valuesById),
31                      legalPackageNamesIn(valuesById, delimiter), 0f);
32          }
33      };
34  
35      /**
36       * Constructor for MethodProperty.
37       *
38       * @param theName String
39       * @param theDescription String
40       * @param theDefaults Method[]
41       * @param legalPackageNames String[]
42       * @param theUIOrder float
43       * @throws IllegalArgumentException
44       */
45      public MethodMultiProperty(String theName, String theDescription, Method[] theDefaults, String[] legalPackageNames,
46              float theUIOrder) {
47          super(theName, theDescription, theDefaults, legalPackageNames, theUIOrder);
48      }
49  
50      /**
51       * Constructor for MethodProperty.
52       *
53       * @param theName String
54       * @param theDescription String
55       * @param methodDefaults String
56       * @param legalPackageNames String[]
57       * @param theUIOrder float
58       * @throws IllegalArgumentException
59       */
60      public MethodMultiProperty(String theName, String theDescription, String methodDefaults,
61              String[] legalPackageNames, float theUIOrder) {
62          super(theName, theDescription, methodsFrom(methodDefaults), legalPackageNames, theUIOrder);
63      }
64  
65      /**
66       * Constructor for MethodProperty.
67       *
68       * @param theName String
69       * @param theDescription String
70       * @param methodDefaults String
71       * @param otherParams Map<String, String>
72       * @param theUIOrder float
73       * @throws IllegalArgumentException
74       */
75      public MethodMultiProperty(String theName, String theDescription, String methodDefaults,
76              Map<String, String> otherParams, float theUIOrder) {
77          this(theName, theDescription, methodsFrom(methodDefaults), packageNamesIn(otherParams), theUIOrder);
78      }
79  
80      /**
81       * @param methodsStr String
82       * @return Method[]
83       */
84      public static Method[] methodsFrom(String methodsStr) {
85  
86          String[] values = StringUtil.substringsOf(methodsStr, DELIMITER);
87  
88          Method[] methods = new Method[values.length];
89          for (int i = 0; i < methods.length; i++) {
90              methods[i] = MethodProperty.methodFrom(values[i], MethodProperty.CLASS_METHOD_DELIMITER,
91                      MethodProperty.METHOD_ARG_DELIMITER);
92          }
93          return methods;
94      }
95  
96      /**
97       * Return the value as a string that can be easily recognized and parsed
98       * when we see it again.
99       *
100      * @param value Object
101      * @return String
102      */
103     @Override
104     protected String asString(Object value) {
105         return value == null ? "" : MethodProperty.asStringFor((Method) value);
106     }
107 
108     /**
109      * @param item Object
110      * @return String
111      */
112     @Override
113     protected String packageNameOf(Object item) {
114 
115         final Method method = (Method) item;
116         return method.getDeclaringClass().getName() + '.' + method.getName();
117     }
118 
119     /**
120      * @return String
121      */
122     @Override
123     protected String itemTypeName() {
124         return "method";
125     }
126 
127     /**
128      *
129      * @return Class
130      * @see net.sourceforge.pmd.PropertyDescriptor#type()
131      */
132     public Class<Method[]> type() {
133         return Method[].class;
134     }
135 
136     /**
137      * @param valueString String
138      * @return Object
139      * @throws IllegalArgumentException
140      * @see net.sourceforge.pmd.PropertyDescriptor#valueFrom(String)
141      */
142     public Method[] valueFrom(String valueString) throws IllegalArgumentException {
143         return methodsFrom(valueString);
144     }
145 }