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.util.Map;
    7
    8   import net.sourceforge.pmd.PropertyDescriptorFactory;
    9   import net.sourceforge.pmd.lang.rule.properties.factories.BasicPropertyDescriptorFactory;
    10  import net.sourceforge.pmd.util.StringUtil;
    11
    12  /**
    13   * Defines a property that supports multiple class types, even for primitive values!
    14   *
    15   * TODO - untested for array types
    16   *
    17   * @author Brian Remedios
    18   */
    19  public class TypeMultiProperty extends AbstractMultiPackagedProperty<Class[]> {
    20
    21
    22  	public static final PropertyDescriptorFactory FACTORY = new BasicPropertyDescriptorFactory<TypeMultiProperty>(Class[].class, packagedFieldTypesByKey) {
    23
    24  		public TypeMultiProperty createWith(Map<String, String> valuesById) {
    25  			return new TypeMultiProperty(
    26  					nameIn(valuesById),
    27  					descriptionIn(valuesById),
    28  					defaultValueIn(valuesById),
    29  					legalPackageNamesIn(valuesById),
    30  					0f);
    31  		}
    32  	};
    33
    34      /**
    35       * Constructor for TypeProperty.
    36       * @param theName String
    37       * @param theDescription String
    38       * @param theDefaults Class[]
    39       * @param legalPackageNames String[]
    40       * @param theUIOrder float
    41       * @throws IllegalArgumentException
    42       */
    43      public TypeMultiProperty(String theName, String theDescription, Class<?>[] theDefaults, String[] legalPackageNames, float theUIOrder) {
    44          super(theName, theDescription, theDefaults, legalPackageNames, theUIOrder);
    45
    46      }
    47
    48      /**
    49       * Constructor for TypeProperty.
    50       * @param theName String
    51       * @param theDescription String
    52       * @param theTypeDefaults String
    53       * @param legalPackageNames String[]
    54       * @param theUIOrder float
    55       * @throws IllegalArgumentException
    56       */
    57      public TypeMultiProperty(String theName, String theDescription, String theTypeDefaults, String[] legalPackageNames, float theUIOrder) {
    58          this(theName, theDescription, typesFrom(theTypeDefaults), legalPackageNames, theUIOrder);
    59
    60      }
    61
    62      /**
    63       * Constructor for TypeProperty.
    64       * @param theName String
    65       * @param theDescription String
    66       * @param theTypeDefaults String
    67       * @param otherParams Map<String, String>
    68       * @param theUIOrder float
    69       * @throws IllegalArgumentException
    70       */
    71      public TypeMultiProperty(String theName, String theDescription, String theTypeDefaults, Map<String, String> otherParams, float theUIOrder) {
    72          this(theName, theDescription, typesFrom(theTypeDefaults), packageNamesIn(otherParams), theUIOrder);
    73      }
    74
    75      /**
    76       * @param classesStr String
    77       * @return Class[]
    78       */
    79      public static Class[] typesFrom(String classesStr) {
    80          String[] values = StringUtil.substringsOf(classesStr, DELIMITER);
    81
    82          Class<?>[] classes = new Class<?>[values.length];
    83          for (int i = 0; i < values.length; i++) {
    84              classes[i] = TypeProperty.classFrom(values[i]);
    85          }
    86          return classes;
    87      }
    88
    89      /**
    90       * @param item Object
    91       * @return String
    92       */
    93      @Override
    94      protected String packageNameOf(Object item) {
    95          return ((Class<?>) item).getName();
    96      }
    97
    98      /**
    99       * @return Class
    100      * @see net.sourceforge.pmd.PropertyDescriptor#type()
    101      */
    102     public Class<Class[]> type() {
    103         return Class[].class;
    104     }
    105
    106     /**
    107      * @return String
    108      */
    109     @Override
    110     protected String itemTypeName() {
    111         return "type";
    112     }
    113
    114     /**
    115      * @param value Object
    116      * @return String
    117      */
    118     @Override
    119     protected String asString(Object value) {
    120         return value == null ? "" : ((Class<?>) value).getName();
    121     }
    122
    123     /**
    124      * @param valueString String
    125      * @return Object
    126      * @see net.sourceforge.pmd.PropertyDescriptor#valueFrom(String)
    127      */
    128     public Class<?>[] valueFrom(String valueString) {
    129         return typesFrom(valueString);
    130     }
    131 }