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