-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClassSourceProducer.java
68 lines (55 loc) · 2.2 KB
/
ClassSourceProducer.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
package nl.rug.jbi.jsm.metrics;
import com.google.common.base.Supplier;
import com.google.common.collect.Lists;
import nl.rug.jbi.jsm.util.CompositeBCELClassLoader;
import nl.rug.jbi.jsm.bcel.JavaClassDefinition;
import nl.rug.jbi.jsm.core.calculator.MetricScope;
import nl.rug.jbi.jsm.core.calculator.MetricState;
import nl.rug.jbi.jsm.core.calculator.ProducerMetric;
import nl.rug.jbi.jsm.core.event.Subscribe;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import static com.google.common.base.Preconditions.checkState;
/**
* Producer for {@link nl.rug.jbi.jsm.metrics.ClassSource}
*
* @author David van Leusen
* @since 2014-05-29
*/
public class ClassSourceProducer extends ProducerMetric {
private final static Supplier<String> MISSING_SOURCE = new Supplier<String>() {
@Override
public String get() {
return "UNKNOWN";
}
};
private static AtomicReference<CompositeBCELClassLoader> CLOADER = new AtomicReference<CompositeBCELClassLoader>(null);
public ClassSourceProducer() {
super(MetricScope.CLASS, MetricScope.CLASS);
}
public static void setCBCL(final CompositeBCELClassLoader cLoader) {
CLOADER = new AtomicReference<CompositeBCELClassLoader>(cLoader);
}
@Subscribe
public void onClass(final MetricState state, final JavaClassDefinition ignored) {
final CompositeBCELClassLoader CBCL = CLOADER.get();
if (CBCL != null) {
state.setValue("source", CBCL.getSource(state.getIdentifier()));
}
}
@Override
public List<Produce> getProduce(final Map<String, MetricState> states, final int invalidMembers) {
checkState(invalidMembers == 0, "Calculation of this producer should never fail.");
final List<Produce> ret = Lists.newLinkedList();
for (final Map.Entry<String, MetricState> entry : states.entrySet()) {
final String source = entry.getValue().getValue("source", MISSING_SOURCE);
ret.add(new Produce(entry.getKey(), new ClassSource(entry.getKey(), source)));
}
return ret;
}
@Override
public Class getProducedClass() {
return ClassSource.class;
}
}