Skip to content

Instantly share code, notes, and snippets.

@adamish
Created December 8, 2016 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamish/0e6f58ead59348f8766376e7bfa8654b to your computer and use it in GitHub Desktop.
Save adamish/0e6f58ead59348f8766376e7bfa8654b to your computer and use it in GitHub Desktop.
Unreferenced spring beans across multiple files
public class UnusedBeans {
private Map<String, Node> defs = new HashMap<String, Node>();
private Set<String> refs = new HashSet<String>();
public void read() throws Exception {
String content = new String(Files.readAllBytes(Paths.get("xml.txt")));
for (String line : content.split("\n")) {
parse("../" + line.trim());
}
for (Entry<String, Node> each : defs.entrySet()) {
if (!refs.contains(each.getKey())) {
Set<String> aliases = new HashSet<>(getAllNames((Element) each.getValue()));
aliases.remove(each.getKey());
boolean referencedByAlias = false;
for (String alias : aliases) {
if (refs.contains(alias)) {
referencedByAlias = true;
}
}
if (!referencedByAlias) {
System.out.println("Not referenced " + each.getKey());
} else {
// System.out.println("Alias unused, but used elsewhere" + each.getKey());
}
}
}
}
private void parse(String trim) throws Exception {
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(trim));
defs.putAll(getBeanDefs(doc));
refs.addAll(getBeansRefs(doc));
}
public static void main(String[] args) throws Exception {
new UnusedBeans().read();
}
private Map<String, Node> getBeanDefs(Document doc) throws Exception {
Map<String, Node> map = new LinkedHashMap<String, Node>();
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList result = (NodeList) xpath.compile("/beans/bean").evaluate(doc, XPathConstants.NODESET);
String defaultLazy = doc.getDocumentElement().getAttribute("default-lazy-init");
if ("false".equals(defaultLazy)) {
return map;
}
for (int i = 0; i < result.getLength(); i++) {
Element element = (Element) result.item(i);
String lazyInit = element.getAttribute("lazy-init");
if ("false".equals(lazyInit)) {
continue;
}
for (String each : getAllNames(element)) {
map.put(each, element);
}
}
return map;
}
private Set<String> getAllNames(Element element) {
String id = element.getAttribute("id");
String name = element.getAttribute("name");
Set<String> names = new HashSet<>();
if (id != null && id.length() > 0) {
names.add(id);
}
if (name != null && name.trim().length() > 0) {
String[] bits = name.split(",");
for (String bit : bits) {
if (bit.length() > 0) {
names.add(bit);
}
}
}
return names;
}
/**
* @param doc
* @return
* @throws Exception
*/
private Set<String> getBeansRefs(Document doc) throws Exception {
Set<String> refs = new HashSet<String>();
XPath xpath = XPathFactory.newInstance().newXPath();
refs.addAll(addAll((NodeList) xpath.compile("//bean[@parent]/@parent").evaluate(doc, XPathConstants.NODESET)));
refs.addAll(addAll((NodeList) xpath.compile("//ref[@bean]/@bean").evaluate(doc, XPathConstants.NODESET)));
refs.addAll(addAll((NodeList) xpath.compile("//*[@ref]/@ref").evaluate(doc, XPathConstants.NODESET)));
refs.addAll(addAll(
(NodeList) xpath.compile("//bean[@factory-bean]/@factory-bean").evaluate(doc, XPathConstants.NODESET)));
return refs;
}
private Set<String> addAll(NodeList list) {
Set<String> refs = new HashSet<String>();
for (int i = 0; i < list.getLength(); i++) {
Attr element = (Attr) list.item(i);
refs.add(element.getValue().trim());
}
return refs;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment