Skip to content

Commit 62ef8bc

Browse files
committed
initial commit
0 parents  commit 62ef8bc

22 files changed

+847
-0
lines changed

.classpath

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
10+
<attributes>
11+
<attribute name="maven.pomderived" value="true"/>
12+
</attributes>
13+
</classpathentry>
14+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
15+
<attributes>
16+
<attribute name="optional" value="true"/>
17+
<attribute name="maven.pomderived" value="true"/>
18+
</attributes>
19+
</classpathentry>
20+
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
21+
<attributes>
22+
<attribute name="maven.pomderived" value="true"/>
23+
</attributes>
24+
</classpathentry>
25+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
26+
<attributes>
27+
<attribute name="maven.pomderived" value="true"/>
28+
</attributes>
29+
</classpathentry>
30+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
31+
<attributes>
32+
<attribute name="maven.pomderived" value="true"/>
33+
</attributes>
34+
</classpathentry>
35+
<classpathentry kind="output" path="target/classes"/>
36+
</classpath>

.project

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>dependency-analyzer</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
22+
</natures>
23+
</projectDescription>

.settings/org.eclipse.jdt.core.prefs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
3+
org.eclipse.jdt.core.compiler.compliance=1.8
4+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
5+
org.eclipse.jdt.core.compiler.source=1.8

.settings/org.eclipse.m2e.core.prefs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
activeProfiles=
2+
eclipse.preferences.version=1
3+
resolveWorkspaceProjects=true
4+
version=1

pom.xml

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.abstractdog</groupId>
5+
<artifactId>dependency-visualizer</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
8+
<dependencies>
9+
10+
<dependency>
11+
<groupId>junit</groupId>
12+
<artifactId>junit</artifactId>
13+
<version>4.11</version>
14+
</dependency>
15+
16+
<dependency>
17+
<groupId>net.sf.jung</groupId>
18+
<artifactId>jung-api</artifactId>
19+
<version>2.1.1</version>
20+
</dependency>
21+
22+
<dependency>
23+
<groupId>net.sf.jung</groupId>
24+
<artifactId>jung-graph-impl</artifactId>
25+
<version>2.1.1</version>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>net.sf.jung</groupId>
30+
<artifactId>jung-visualization</artifactId>
31+
<version>2.1.1</version>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>net.sf.jung</groupId>
36+
<artifactId>jung-samples</artifactId>
37+
<version>2.1.1</version>
38+
</dependency>
39+
</dependencies>
40+
41+
<build>
42+
<finalName>${project.artifactId}</finalName>
43+
<plugins>
44+
<plugin>
45+
<groupId>org.apache.maven.plugins</groupId>
46+
<artifactId>maven-compiler-plugin</artifactId>
47+
<configuration>
48+
<source>1.8</source>
49+
<target>1.8</target>
50+
</configuration>
51+
</plugin>
52+
</plugins>
53+
</build>
54+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.abstractdog.dependency.analyzer.eclipse.project;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
6+
public abstract class AbstractProject implements Project {
7+
protected String name;
8+
protected File baseDir;
9+
10+
@Override
11+
public String getName() {
12+
return name;
13+
}
14+
15+
@Override
16+
public File getBaseDir() {
17+
return baseDir;
18+
}
19+
20+
@Override
21+
public String toString() {
22+
return String.format("PROJECT: %s (dir: %s)", getName(), getBaseDir().getPath());
23+
}
24+
25+
26+
@Override
27+
public int hashCode() {
28+
return name.hashCode();
29+
}
30+
31+
@Override
32+
public boolean equals(Object obj) {
33+
try {
34+
return ((Project) obj).getName().equals(name)
35+
&& ((Project) obj).getBaseDir().getCanonicalPath()
36+
.equalsIgnoreCase(getBaseDir().getCanonicalPath());
37+
} catch (IOException e) {
38+
throw new RuntimeException(e);
39+
}
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.abstractdog.dependency.analyzer.eclipse.project;
2+
3+
import java.io.File;
4+
import java.io.Serializable;
5+
import java.util.List;
6+
7+
import com.abstractdog.dependency.analyzer.eclipse.project.classpath.ClassPathEntry;
8+
import com.abstractdog.dependency.analyzer.eclipse.project.classpath.ClassPathParser;
9+
import com.abstractdog.dependency.analyzer.eclipse.project.list.ProjectList;
10+
import com.abstractdog.dependency.analyzer.eclipse.project.projectfile.ProjectFileParser;
11+
12+
public class EclipseProject extends AbstractProject implements Serializable {
13+
private static final long serialVersionUID = 1L;
14+
private ProjectList dependencies;
15+
16+
private EclipseProject(File directory) throws Exception {
17+
this.baseDir = directory;
18+
this.name = parseName(directory);
19+
}
20+
21+
public void parseClassPathDependencies() throws Exception {
22+
dependencies = new ProjectList();
23+
24+
ClassPathParser classPathParser = new ClassPathParser(new File(this.baseDir, ".classpath"));
25+
List<ClassPathEntry> classPathEntries = classPathParser.getClassPathEntries();
26+
27+
for (ClassPathEntry entry : classPathEntries){
28+
if (entry.getKind().equals("src") && entry.getPath() != null
29+
&& entry.getPath().startsWith("/")) { // project names are absoulute to workspace
30+
dependencies.add(EclipseProject.create(new File(".." + entry.getPath()))); // FIXME
31+
}
32+
}
33+
}
34+
35+
private String parseName(File directory) throws Exception {
36+
File projectFile = new File(directory, ".project");
37+
38+
if(!projectFile.exists()){
39+
throw new RuntimeException(String.format("project not found: %s",
40+
directory.getName()));
41+
}
42+
43+
return new ProjectFileParser(projectFile).getName();
44+
}
45+
46+
public static EclipseProject create(File directory) throws Exception {
47+
EclipseProject project = new EclipseProject(directory);
48+
return project;
49+
}
50+
51+
public ProjectList getDependencies(){
52+
return dependencies;
53+
}
54+
55+
@Override
56+
public String toString() {
57+
return String.format("PROJECT {%s, dependencies: %s}", getName(), dependencies);
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.abstractdog.dependency.analyzer.eclipse.project;
2+
3+
import java.io.File;
4+
5+
public interface Project{
6+
String getName();
7+
File getBaseDir();
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.abstractdog.dependency.analyzer.eclipse.project.classpath;
2+
3+
public class ClassPathEntry {
4+
private String kind;
5+
private String output;
6+
private String path;
7+
private String excluding;
8+
9+
private ClassPathEntry() {
10+
}
11+
12+
public static class Builder {
13+
private String kind;
14+
private String output;
15+
private String excluding;
16+
private String path;
17+
18+
public Builder(String kind) {
19+
this.kind = kind;
20+
}
21+
22+
public Builder output (String output){
23+
this.output = output;
24+
return this;
25+
}
26+
27+
public Builder excluding (String excluding){
28+
this.excluding = excluding;
29+
return this;
30+
}
31+
32+
public Builder path (String path){
33+
this.path = path;
34+
return this;
35+
}
36+
37+
public ClassPathEntry build(){
38+
ClassPathEntry entry = new ClassPathEntry();
39+
40+
entry.kind = kind;
41+
entry.output = output;
42+
entry.path = path;
43+
entry.excluding = excluding;
44+
45+
return entry;
46+
}
47+
}
48+
49+
public String getKind() {
50+
return kind;
51+
}
52+
53+
public String getOutput() {
54+
return output;
55+
}
56+
57+
public String getPath() {
58+
return path;
59+
}
60+
61+
public String getExcluding() {
62+
return excluding;
63+
}
64+
65+
@Override
66+
public String toString() {
67+
return String.format("CLASSPATHENTRY {kind:%s, output:%s, path:%s}", kind, output, path);
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.abstractdog.dependency.analyzer.eclipse.project.classpath;
2+
3+
import java.io.File;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
import javax.xml.parsers.DocumentBuilder;
8+
import javax.xml.parsers.DocumentBuilderFactory;
9+
import javax.xml.parsers.ParserConfigurationException;
10+
import javax.xml.xpath.XPathConstants;
11+
import javax.xml.xpath.XPathExpression;
12+
import javax.xml.xpath.XPathExpressionException;
13+
import javax.xml.xpath.XPathFactory;
14+
15+
import org.w3c.dom.Document;
16+
import org.w3c.dom.Node;
17+
import org.w3c.dom.NodeList;
18+
19+
public class ClassPathParser {
20+
private static XPathExpression entryXPath;
21+
private static DocumentBuilder documentBuilder;
22+
23+
private List<ClassPathEntry> classPathEntries;
24+
25+
static {
26+
try {
27+
entryXPath = XPathFactory.newInstance().newXPath()
28+
.compile("/classpath/classpathentry");
29+
} catch (XPathExpressionException e) {
30+
throw new RuntimeException(e);
31+
}
32+
33+
try {
34+
documentBuilder = DocumentBuilderFactory.newInstance()
35+
.newDocumentBuilder();
36+
} catch (ParserConfigurationException e) {
37+
throw new RuntimeException(e);
38+
}
39+
}
40+
41+
private File classPathFile;
42+
43+
public ClassPathParser(File classPathFile) {
44+
this.classPathFile = classPathFile;
45+
}
46+
47+
public List<ClassPathEntry> getClassPathEntries() throws Exception {
48+
if (classPathEntries != null) {
49+
return classPathEntries;
50+
}
51+
52+
classPathEntries = new ArrayList<ClassPathEntry>();
53+
54+
Document doc = documentBuilder.parse(classPathFile);
55+
NodeList nodeList = (NodeList) entryXPath.evaluate(doc,
56+
XPathConstants.NODESET);
57+
58+
for (int i = 0; i < nodeList.getLength(); i++) {
59+
Node node = nodeList.item(i);
60+
61+
ClassPathEntry entry = new ClassPathEntry.Builder(getAttributeValue(node, "kind"))
62+
.path(getAttributeValue(node, "path"))
63+
.output(getAttributeValue(node, "output"))
64+
.excluding(getAttributeValue(node, "excluding"))
65+
.build();
66+
67+
classPathEntries.add(entry);
68+
}
69+
70+
return classPathEntries;
71+
}
72+
73+
private String getAttributeValue(Node node, String name) {
74+
return node.getAttributes().getNamedItem(name) == null ? null : node
75+
.getAttributes().getNamedItem(name).getNodeValue();
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.abstractdog.dependency.analyzer.eclipse.project.graph;
2+
3+
import java.util.List;
4+
5+
import edu.uci.ics.jung.graph.Graph;
6+
7+
public interface GraphBuilder<T> {
8+
Graph<T, String> build(List<T> objects);
9+
}

0 commit comments

Comments
 (0)