-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
RendererFactory.java
166 lines (147 loc) · 6.91 KB
/
RendererFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.renderers;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.sourceforge.pmd.properties.PropertyDescriptor;
import net.sourceforge.pmd.util.AssertionUtil;
/**
* This class handles the creation of Renderers.
*
* @see Renderer
*/
public final class RendererFactory {
private static final Logger LOG = LoggerFactory.getLogger(RendererFactory.class);
private static final Map<String, Class<? extends Renderer>> REPORT_FORMAT_TO_RENDERER;
static {
Map<String, Class<? extends Renderer>> map = new TreeMap<>();
map.put(CodeClimateRenderer.NAME, CodeClimateRenderer.class);
map.put(XMLRenderer.NAME, XMLRenderer.class);
map.put(IDEAJRenderer.NAME, IDEAJRenderer.class);
map.put(TextColorRenderer.NAME, TextColorRenderer.class);
map.put(TextRenderer.NAME, TextRenderer.class);
map.put(TextPadRenderer.NAME, TextPadRenderer.class);
map.put(EmacsRenderer.NAME, EmacsRenderer.class);
map.put(CSVRenderer.NAME, CSVRenderer.class);
map.put(HTMLRenderer.NAME, HTMLRenderer.class);
map.put(XSLTRenderer.NAME, XSLTRenderer.class);
map.put(YAHTMLRenderer.NAME, YAHTMLRenderer.class);
map.put(SummaryHTMLRenderer.NAME, SummaryHTMLRenderer.class);
map.put(VBHTMLRenderer.NAME, VBHTMLRenderer.class);
map.put(EmptyRenderer.NAME, EmptyRenderer.class);
map.put(JsonRenderer.NAME, JsonRenderer.class);
map.put(SarifRenderer.NAME, SarifRenderer.class);
REPORT_FORMAT_TO_RENDERER = Collections.unmodifiableMap(map);
}
private RendererFactory() { }
/**
* Retrieves a collection of all supported renderer names.
*
* @return The set of all supported renderer names.
*/
public static Set<String> supportedRenderers() {
return Collections.unmodifiableSet(REPORT_FORMAT_TO_RENDERER.keySet());
}
/**
* Construct an instance of a Renderer based on report format name.
*
* @param reportFormat
* The report format name.
* @param properties
* Initialization properties for the corresponding Renderer.
* @return A Renderer instance.
*/
public static Renderer createRenderer(String reportFormat, Properties properties) {
AssertionUtil.requireParamNotNull("reportFormat", reportFormat);
Class<? extends Renderer> rendererClass = getRendererClass(reportFormat);
Constructor<? extends Renderer> constructor = getRendererConstructor(rendererClass);
Renderer renderer;
try {
if (constructor.getParameterTypes().length > 0) {
LOG.warn(
"The renderer uses a deprecated mechanism to use the properties. Please define the needed properties with this.definePropertyDescriptor(..).");
renderer = constructor.newInstance(properties);
} else {
renderer = constructor.newInstance();
for (PropertyDescriptor<?> prop : renderer.getPropertyDescriptors()) {
String value = properties.getProperty(prop.name());
if (value != null) {
@SuppressWarnings("unchecked")
PropertyDescriptor<Object> prop2 = (PropertyDescriptor<Object>) prop;
Object valueFrom = prop2.serializer().fromString(value);
renderer.setProperty(prop2, valueFrom);
}
}
}
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalArgumentException(
"Unable to construct report renderer class: " + e.getLocalizedMessage(), e);
} catch (InvocationTargetException e) {
throw new IllegalArgumentException(
"Unable to construct report renderer class: " + e.getTargetException().getLocalizedMessage(), e);
}
// Warn about legacy report format usages
if (REPORT_FORMAT_TO_RENDERER.containsKey(reportFormat) && !reportFormat.equals(renderer.getName())) {
LOG.warn("Report format '{}' is deprecated, and has been replaced with '{}'. "
+ "Future versions of PMD will remove support for this deprecated Report format usage.",
reportFormat, renderer.getName());
}
return renderer;
}
@SuppressWarnings("unchecked")
private static Class<? extends Renderer> getRendererClass(String reportFormat) {
AssertionUtil.requireParamNotNull("reportFormat", reportFormat);
Class<? extends Renderer> rendererClass = REPORT_FORMAT_TO_RENDERER.get(reportFormat);
// Look up a custom renderer class
if (rendererClass == null && !"".equals(reportFormat)) {
try {
Class<?> clazz = Class.forName(reportFormat);
if (!Renderer.class.isAssignableFrom(clazz)) {
throw new IllegalArgumentException("Custom report renderer class does not implement the "
+ Renderer.class.getName() + " interface.");
} else {
rendererClass = (Class<? extends Renderer>) clazz;
}
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Can't find the custom format " + reportFormat + ": " + e);
}
}
return rendererClass;
}
private static Constructor<? extends Renderer> getRendererConstructor(Class<? extends Renderer> rendererClass) {
Constructor<? extends Renderer> constructor = null;
// 1) Properties constructor? - deprecated
try {
constructor = rendererClass.getConstructor(Properties.class);
if (!Modifier.isPublic(constructor.getModifiers())) {
constructor = null;
}
} catch (NoSuchMethodException ignored) {
// Ignored, we'll check default constructor next
}
// 2) No-arg constructor?
try {
constructor = rendererClass.getConstructor();
if (!Modifier.isPublic(constructor.getModifiers())) {
constructor = null;
}
} catch (NoSuchMethodException ignored) {
// Ignored, we'll eventually throw an exception, if there is no constructor
}
if (constructor == null) {
throw new IllegalArgumentException(
"Unable to find either a public java.util.Properties or no-arg constructors for Renderer class: "
+ rendererClass.getName());
}
return constructor;
}
}