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