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 import net.sourceforge.pmd.util.StringUtil;
12
13
14
15
16
17
18
19
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
35
36
37
38
39
40
41
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
51
52
53
54
55
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
65
66
67
68
69
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
78
79 protected String defaultAsString() {
80 return asString(defaultValue());
81 }
82
83
84
85
86
87
88
89 @Override
90 protected String packageNameOf(Object item) {
91 return ((Class<?>) item).getName();
92 }
93
94
95
96
97
98 public Class<Class> type() {
99 return Class.class;
100 }
101
102
103
104
105 @Override
106 protected String itemTypeName() {
107 return "type";
108 }
109
110
111
112
113
114 @Override
115 protected String asString(Object value) {
116 return value == null ? "" : ((Class<?>) value).getName();
117 }
118
119
120
121
122
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
143
144
145
146 public Class<?> valueFrom(String valueString) {
147 return classFrom(valueString);
148 }
149 }