Neo4j in Action Software Metrics Michael Hunger (@mesirii) 1 1
Graphs in Software Technolgoy ๏ UML Diagrams are graphs ๏ dependencies between classes, packages, modules etc are graphs ๏ Software Metrics use dependency analysis ๏ Visualizations ๏ Cyclomatic Complexity, ๏ Fan-in (afferent-coupling) / Fan-out (efferent coupling) etc. 2 2
Code City 3 3
Class Diagram is a Graph 4 4
SonarJ 5 5
But there is more ๏Visualize & query Method, Field dependencies ๏Collaborative filtering (co-usage) ๏Ranking ๏God classes ๏Paths between classes 6 6
Welcome to Class-Graph ๏take a JAR ๏put it under ASM ๏scan it superfast ๏pull everything into Neo4j ๏add categories, indexes ๏Have Fun http://github.com/jexp/class-graph 7 7
Welcome to Class-Graph 8 8
Interactive Hands-On Session ๏Lots of tasks ๏use Cypher to solve them http://neo4j.org/resources/cypher ๏be creative, work together ๏ask ! ๏Server http://bit.ly/innoq-neo4j ๏just the beginning 9 9
Task: Find java.lang.Number and return it 10 10
Task: Find java.lang.Number and return it START o=node:types(name="java.lang.Number")  RETURN o; 10 10
Task: Subclasses of Number? •Return just the name •Order them alphabetically 11 11
Task: Subclasses of Number? START n=node:types(name="java.lang.Number")  MATCH n<-[:SUPER_TYPE]-s RETURN s.name ORDER BY s.name; •Return just the name •Order them alphabetically 11 11
Task: Which Methods does it have / how many •Find the top 5 classes with the most members 12 12
Task: Which Methods does it have / how many START n=node:types(name="java.lang.Number")  MATCH n-[:METHOD_OF|FIELD_OF]->m RETURN m; •Find the top 5 classes with the most members 12 12
Task: Calculate the fan-out of java.lang.StringBuilder •Calculate fan-in •Which class has the highest fan-out •What about package-level? 13 13
Task: Calculate the fan-out of java.lang.StringBuilder START o=node:types(name="j.l.StringBuilder") MATCH o-[:FIELD_OF]->f-[:FIELD_TYPE]->tf, o-[:METHOD_OF]->m-[:PARAM_TYPE]->tp, m-[:RETURN_TYPE]->tr RETURN o,count(distinct tf) + count(distinct tp) + count(distinct tr) as fan_out; •Calculate fan-in •Which class has the highest fan-out •What about package-level? 13 13
Task: Find longest Inheritance Path 14 14
Task: Find longest Inheritance Path start c=node:types(name="java.lang.Object")  match path=p<-[:SUPER_TYPE*]-c  return extract(n in nodes(path) : n.name), length(path) as len order by len desc  limit 5; 14 14
Task: Find the class that used IOException most often 15 15
Task: Find the class that used IOException most often START ex=node:types(name="java.io.IOException" MATCH ex<-[:THROWS]-m<-[:METHOD_OF]-c RETURN c, count(*) ORDER BY count(*) LIMIT 5; 15 15
Task: Which other classes did classes that threw IOException use most often? •What could be a source of IOExceptions 16 16
Task: Which other classes did classes that threw IOException use most often? START ex=node:types(name="java.io.IOException") MATCH ex<-[:THROWS]-m<-[:METHOD_OF]-c,  mbr<-[:METHOD_OF|FIELD_OF]-c, mbr-[:FIELD_TYPE|PARAM_TYPE| RETURN_TYPE|THROWS]->other_type WHERE other_type.name =~ /.+[.].+/ RETURN other_type.name, count(*) ORDER BY count(*) desc LIMIT 10; •What could be a source of IOExceptions 16 16
Task: Find a class you like and add a field with your name and some type 17 17
Task: Find a class you like and add a field with your name and some type START c=node:types(name="void"), t=node:types(name="java.lang.reflect.Proxy")  CREATE c-[:FIELD_OF]->(field {name:“Michael“}) -[:FIELD_TYPE]->t; 17 17
Task: Delete the most annoying class and all its methods, fields and their relationships 18 18
Task: Delete the most annoying class and all its methods, fields and their relationships START c=node:types(name="java.awt.List"), MATCH c-[r1:FIELD_OF|METHOD_OF]->mbr-[r2]-() c-[r]-() DELETE c,mbr,r1,r2,r; 18 18

Class graph neo4j and software metrics

  • 1.
    Neo4j in Action SoftwareMetrics Michael Hunger (@mesirii) 1 1
  • 2.
    Graphs in SoftwareTechnolgoy ๏ UML Diagrams are graphs ๏ dependencies between classes, packages, modules etc are graphs ๏ Software Metrics use dependency analysis ๏ Visualizations ๏ Cyclomatic Complexity, ๏ Fan-in (afferent-coupling) / Fan-out (efferent coupling) etc. 2 2
  • 3.
  • 4.
    Class Diagram isa Graph 4 4
  • 5.
  • 6.
    But there ismore ๏Visualize & query Method, Field dependencies ๏Collaborative filtering (co-usage) ๏Ranking ๏God classes ๏Paths between classes 6 6
  • 7.
    Welcome to Class-Graph ๏takea JAR ๏put it under ASM ๏scan it superfast ๏pull everything into Neo4j ๏add categories, indexes ๏Have Fun http://github.com/jexp/class-graph 7 7
  • 8.
  • 9.
    Interactive Hands-On Session ๏Lotsof tasks ๏use Cypher to solve them http://neo4j.org/resources/cypher ๏be creative, work together ๏ask ! ๏Server http://bit.ly/innoq-neo4j ๏just the beginning 9 9
  • 10.
    Task: Find java.lang.Numberand return it 10 10
  • 11.
    Task: Find java.lang.Numberand return it START o=node:types(name="java.lang.Number")  RETURN o; 10 10
  • 12.
    Task: Subclasses ofNumber? •Return just the name •Order them alphabetically 11 11
  • 13.
    Task: Subclasses ofNumber? START n=node:types(name="java.lang.Number")  MATCH n<-[:SUPER_TYPE]-s RETURN s.name ORDER BY s.name; •Return just the name •Order them alphabetically 11 11
  • 14.
    Task: Which Methodsdoes it have / how many •Find the top 5 classes with the most members 12 12
  • 15.
    Task: Which Methodsdoes it have / how many START n=node:types(name="java.lang.Number")  MATCH n-[:METHOD_OF|FIELD_OF]->m RETURN m; •Find the top 5 classes with the most members 12 12
  • 16.
    Task: Calculate thefan-out of java.lang.StringBuilder •Calculate fan-in •Which class has the highest fan-out •What about package-level? 13 13
  • 17.
    Task: Calculate thefan-out of java.lang.StringBuilder START o=node:types(name="j.l.StringBuilder") MATCH o-[:FIELD_OF]->f-[:FIELD_TYPE]->tf, o-[:METHOD_OF]->m-[:PARAM_TYPE]->tp, m-[:RETURN_TYPE]->tr RETURN o,count(distinct tf) + count(distinct tp) + count(distinct tr) as fan_out; •Calculate fan-in •Which class has the highest fan-out •What about package-level? 13 13
  • 18.
    Task: Find longestInheritance Path 14 14
  • 19.
    Task: Find longestInheritance Path start c=node:types(name="java.lang.Object")  match path=p<-[:SUPER_TYPE*]-c  return extract(n in nodes(path) : n.name), length(path) as len order by len desc  limit 5; 14 14
  • 20.
    Task: Find theclass that used IOException most often 15 15
  • 21.
    Task: Find theclass that used IOException most often START ex=node:types(name="java.io.IOException" MATCH ex<-[:THROWS]-m<-[:METHOD_OF]-c RETURN c, count(*) ORDER BY count(*) LIMIT 5; 15 15
  • 22.
    Task: Which otherclasses did classes that threw IOException use most often? •What could be a source of IOExceptions 16 16
  • 23.
    Task: Which otherclasses did classes that threw IOException use most often? START ex=node:types(name="java.io.IOException") MATCH ex<-[:THROWS]-m<-[:METHOD_OF]-c,  mbr<-[:METHOD_OF|FIELD_OF]-c, mbr-[:FIELD_TYPE|PARAM_TYPE| RETURN_TYPE|THROWS]->other_type WHERE other_type.name =~ /.+[.].+/ RETURN other_type.name, count(*) ORDER BY count(*) desc LIMIT 10; •What could be a source of IOExceptions 16 16
  • 24.
    Task: Find aclass you like and add a field with your name and some type 17 17
  • 25.
    Task: Find aclass you like and add a field with your name and some type START c=node:types(name="void"), t=node:types(name="java.lang.reflect.Proxy")  CREATE c-[:FIELD_OF]->(field {name:“Michael“}) -[:FIELD_TYPE]->t; 17 17
  • 26.
    Task: Delete themost annoying class and all its methods, fields and their relationships 18 18
  • 27.
    Task: Delete themost annoying class and all its methods, fields and their relationships START c=node:types(name="java.awt.List"), MATCH c-[r1:FIELD_OF|METHOD_OF]->mbr-[r2]-() c-[r]-() DELETE c,mbr,r1,r2,r; 18 18