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