soot.jimple.infoflow.methodSummary.taintWrappers.ReportMissingSummaryWrapper Maven / Gradle / Ivy
package soot.jimple.infoflow.methodSummary.taintWrappers;
import java.io.File;
import java.io.IOException;
import java.util.Comparator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import soot.SootClass;
import soot.SootMethod;
import soot.jimple.infoflow.methodSummary.data.provider.IMethodSummaryProvider;
public class ReportMissingSummaryWrapper extends SummaryTaintWrapper {
public ReportMissingSummaryWrapper(IMethodSummaryProvider flows) {
super(flows);
}
ConcurrentHashMap classSummariesMissing = new ConcurrentHashMap<>();
@Override
protected void reportMissingMethod(SootMethod method) {
count(method.getDeclaringClass(), classSummariesMissing);
}
private static void count(T item, Map map) {
AtomicInteger ai = new AtomicInteger();
{
AtomicInteger old = map.putIfAbsent(item, ai);
if (old != null)
ai = old;
}
ai.incrementAndGet();
}
public void writeResults(File file) throws IOException, ParserConfigurationException, TransformerException {
Map sortedClassSummariesMissing = sortMap(classSummariesMissing);
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
// root elements
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("MissingSummaries");
doc.appendChild(rootElement);
Element classes = doc.createElement("Classes");
for (Entry i : sortedClassSummariesMissing.entrySet()) {
Element clazz = doc.createElement("Class");
clazz.setAttribute("Name", i.getKey().getName());
clazz.setAttribute("Count", String.valueOf(i.getValue()));
classes.appendChild(clazz);
}
rootElement.appendChild(classes);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(file);
transformer.transform(source, result);
}
private static Map sortMap(final Map input) {
Map res = new TreeMap<>(new Comparator() {
@Override
public int compare(T o1, T o2) {
return -Integer.compare(input.get(o1).get(), input.get(o2).get());
}
});
for (Entry i : input.entrySet()) {
res.put(i.getKey(), i.getValue().get());
}
return res;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy