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