1
2
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
14
15
16
17
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
35
36
37
38
39
40
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
49
50
51
52
53
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
62
63
64
65
66
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
74
75 protected String defaultAsString() {
76 return asString(defaultValue());
77 }
78
79
80
81
82
83
84 @Override
85 protected String packageNameOf(Object item) {
86 return ((Class<?>) item).getName();
87 }
88
89
90
91
92
93 public Class<Class> type() {
94 return Class.class;
95 }
96
97
98
99
100 @Override
101 protected String itemTypeName() {
102 return "type";
103 }
104
105
106
107
108
109 @Override
110 protected String asString(Object value) {
111 return value == null ? "" : ((Class<?>) value).getName();
112 }
113
114
115
116
117
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
135
136
137
138 public Class<?> valueFrom(String valueString) {
139 return classFrom(valueString);
140 }
141 }