Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import mrmathami.cia.java.JavaCiaException;
import mrmathami.cia.java.tree.dependency.JavaDependencyWeightTable;
import mrmathami.utils.Pair;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -52,7 +55,7 @@ public interface JavaProject {
@Nonnull
JavaProjectSnapshot createSnapshot(@Nonnull String snapshotName,
@Nonnull Map<String, Pair<Path, List<Path>>> javaSources, @Nonnull List<Path> classPaths,
@Nonnull JavaDependencyWeightTable dependencyWeightTable, boolean enableRecovery) throws JavaCiaException;
@Nonnull JavaDependencyWeightTable dependencyWeightTable, boolean enableRecovery, Path configurationPath) throws JavaCiaException, ParserConfigurationException, SAXException, IOException;

/**
* @param comparisonName comparison name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ default JavaXMLNode asXMLNode() {
return this;
}


@Nonnull
String getTextContent();

Expand Down
25 changes: 25 additions & 0 deletions jdt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,31 @@
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<mainClass>mrmathami.cia.java.jdt.Run</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
7 changes: 5 additions & 2 deletions jdt/src/main/java/mrmathami/cia/java/jdt/ProjectBuilders.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
import mrmathami.cia.java.project.JavaProjectSnapshotComparison;
import mrmathami.cia.java.tree.dependency.JavaDependencyWeightTable;
import mrmathami.utils.Pair;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
Expand All @@ -46,8 +49,8 @@ public static JavaProject createProject(@Nonnull String name) {
@Nonnull
public static JavaProjectSnapshot createProjectSnapshot(@Nonnull String snapshotName,
@Nonnull Map<String, Pair<Path, List<Path>>> javaSources, @Nonnull List<Path> classPaths,
@Nonnull JavaDependencyWeightTable dependencyWeightTable, boolean enableRecovery) throws JavaCiaException {
return JavaSnapshotBuilder.build(snapshotName, javaSources, classPaths, dependencyWeightTable, enableRecovery);
@Nonnull JavaDependencyWeightTable dependencyWeightTable, boolean enableRecovery, Path configurationPath) throws JavaCiaException, ParserConfigurationException, SAXException, IOException {
return JavaSnapshotBuilder.build(snapshotName, javaSources, classPaths, dependencyWeightTable, enableRecovery, configurationPath);
}

@Nonnull
Expand Down
78 changes: 78 additions & 0 deletions jdt/src/main/java/mrmathami/cia/java/jdt/Run.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package mrmathami.cia.java.jdt;

import mrmathami.cia.java.JavaCiaException;
import mrmathami.cia.java.project.JavaProjectSnapshot;
import mrmathami.cia.java.tree.dependency.JavaDependency;
import mrmathami.cia.java.tree.dependency.JavaDependencyWeightTable;
import mrmathami.utils.Pair;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Run {
public static final JavaDependencyWeightTable DEPENDENCY_WEIGHT_TABLE = JavaDependencyWeightTable.of(Map.of(
JavaDependency.USE, 1.0,
JavaDependency.MEMBER, 1.0,
JavaDependency.INHERITANCE, 4.0,
JavaDependency.INVOCATION, 4.0,
JavaDependency.OVERRIDE, 1.0
));
//private static final Path configurationPath = Path.of("D:\\project\\MyBatis Collection\\mybatis-XML\\mybatis-example-1\\resources\\SqlMapConfig.xml");
private static Path configurationPath = null;

public static void main(String[] args) throws JavaCiaException, IOException, ParserConfigurationException, SAXException {
Path corePath = null;
String output = "";
if (args.length < 3) {
configurationPath = Path.of("");
corePath = Path.of(args[0]);
output = args[1];
} else if (args.length == 3) {
configurationPath = Path.of(args[0]);
corePath = Path.of(args[1]);
output = args[2];
}
final List<Path> coreFiles = getFileList(new ArrayList<>(), corePath);
final Map<String, Pair<Path, List<Path>>> javaSources = Map.of(
"core", Pair.immutableOf(corePath, coreFiles)
);
final List<Path> classPaths = List.of(
//List classPaths in here
);


long timeStart = System.nanoTime();
final JavaProjectSnapshot projectSnapshot = ProjectBuilders.createProjectSnapshot("before",
javaSources, classPaths, DEPENDENCY_WEIGHT_TABLE, true, configurationPath);
long timeParseA = System.nanoTime();

final String jsonA = projectSnapshot.getRootNode().toJson();

Files.write(Path.of(output + "output.txt"), jsonA.getBytes(StandardCharsets.UTF_8));

System.out.printf("Parse A time: %s\n", (timeParseA - timeStart) / 1000000.0);
}

private static List<Path> getFileList(List<Path> fileList, Path dir) {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
for (Path path : stream) {
if (path.toFile().isDirectory()) {
getFileList(fileList, path);
} else {
fileList.add(path);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return fileList;
}
}
133 changes: 133 additions & 0 deletions jdt/src/main/java/mrmathami/cia/java/jdt/gephi/Printer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package mrmathami.cia.java.jdt.gephi;

import mrmathami.cia.java.jdt.gephi.model.EdgeGephi;
import mrmathami.cia.java.jdt.gephi.model.NodeGephi;
import mrmathami.cia.java.project.JavaNodeWeightTable;
import mrmathami.cia.java.project.JavaProject;
import mrmathami.cia.java.project.JavaProjectSnapshot;
import mrmathami.cia.java.project.JavaProjectSnapshotComparison;
import mrmathami.cia.java.tree.node.JavaNode;
import mrmathami.utils.Pair;
import org.w3c.dom.Node;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Printer {
public String writeGephi(JavaProjectSnapshot projectSnapshot) {
List<? extends JavaNode> nodeList = projectSnapshot.getRootNode().getAllNodes();
HashMap<JavaNode, Set<? extends JavaNode>> dependencyToMap = new HashMap<>();
for (JavaNode node : nodeList) {
Set<? extends JavaNode> dependencyToNodes = node.getDependencyToNodes();
dependencyToMap.put(node, dependencyToNodes);
}
List<NodeGephi> nodeGephiList = new ArrayList<>();
List<EdgeGephi> edgeGephiList = new ArrayList<>();
int count = 0;
for (Map.Entry<JavaNode, Set<? extends JavaNode>> entry : dependencyToMap.entrySet()) {
NodeGephi nodeGephi = new NodeGephi(entry.getKey().getId(), entry.getKey().getNodeName(), NodeGephi.Type.UNCHANGE);
nodeGephiList.add(nodeGephi);
Set<? extends JavaNode> value = entry.getValue();
for (JavaNode javaNode : value) {
EdgeGephi edgeGephi = new EdgeGephi(count, entry.getKey().getId(), javaNode.getId());
count++;
edgeGephiList.add(edgeGephi);
}
}

final String open = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<gexf xmlns=\"http://www.gexf.net/1.3\" version=\"1.3\" xmlns:viz=\"http://www.gexf.net/1.3/viz\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.gexf.net/1.3 http://www.gexf.net/1.3/gexf.xsd\">" +
"\n<graph defaultedgetype=\"directed\" mode=\"static\">\n";

String listNode = writeListNodes(nodeGephiList);
String listEdge = writeListEdges(edgeGephiList);

return open + listNode + listEdge + "\n</graph>\n" + "</gexf>";
}
public String writeGephiComparison(JavaProjectSnapshotComparison comparison) {
final Set<JavaNode> addedNodes = comparison.getAddedNodes();
final Set<Pair<JavaNode, JavaNode>> changedNodes = comparison.getChangedNodes();
final Set<JavaNode> removedNodes = comparison.getRemovedNodes();
final Set<Pair<JavaNode, JavaNode>> unchangedNodes = comparison.getUnchangedNodes();

HashMap<JavaNode, Set<? extends JavaNode>> dependencyToMap = new HashMap<>();
final List<? extends JavaNode> allNodes = comparison.getCurrentSnapshot().getRootNode().getAllNodes();
final JavaNodeWeightTable nodeImpactTable = comparison.getNodeImpactTable();

for (JavaNode node : allNodes) {
final Set<? extends JavaNode> dependencyToNodes = node.getDependencyToNodes();
dependencyToMap.put(node, dependencyToNodes);
}
List<NodeGephi> nodeGephiList = new ArrayList<>();
List<EdgeGephi> edgeGephiList = new ArrayList<>();
int count = 0;
for (Map.Entry<JavaNode, Set<? extends JavaNode>> entry : dependencyToMap.entrySet()) {
NodeGephi nodeGephi = null;
if (addedNodes.contains(entry.getKey())) {
nodeGephi = new NodeGephi(entry.getKey().getId(), entry.getKey().getNodeName() + " weight: " + nodeImpactTable.getWeight(entry.getKey()), NodeGephi.Type.ADD);
}

for (Pair<JavaNode, JavaNode> pair : changedNodes) {
final JavaNode pairB = pair.getB();
if (entry.getKey() == pairB) {
nodeGephi = new NodeGephi(pairB.getId(), pairB.getNodeName() + " weight: " + nodeImpactTable.getWeight(pairB), NodeGephi.Type.CHANGE);
}
}
for (Pair<JavaNode, JavaNode> pair : unchangedNodes) {
final JavaNode pairB = pair.getB();
if (entry.getKey() == pairB) {
nodeGephi = new NodeGephi(pairB.getId(), pairB.getNodeName() + " weight: " + nodeImpactTable.getWeight(pairB), NodeGephi.Type.UNCHANGE);
}
}
nodeGephiList.add(nodeGephi);
final Set<? extends JavaNode> value = entry.getValue();
for (JavaNode javaNode : value) {
EdgeGephi edgeGephi = new EdgeGephi(count, entry.getKey().getId(), javaNode.getId());
count++;
edgeGephiList.add(edgeGephi);
}
}

for (JavaNode node : removedNodes) {
NodeGephi nodeGephi = new NodeGephi(node.getId() + nodeGephiList.size(), node.getNodeName(), NodeGephi.Type.DELETE);
final Set<? extends JavaNode> dependencyToNodes = node.getDependencyToNodes();
for (JavaNode javaNode : dependencyToNodes) {
EdgeGephi edgeGephi = new EdgeGephi(count, node.getId() + nodeGephiList.size(), javaNode.getId());
count++;
edgeGephiList.add(edgeGephi);
}
nodeGephiList.add(nodeGephi);
}

final String open = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<gexf xmlns=\"http://www.gexf.net/1.3\" version=\"1.3\" xmlns:viz=\"http://www.gexf.net/1.3/viz\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.gexf.net/1.3 http://www.gexf.net/1.3/gexf.xsd\">" +
"\n<graph defaultedgetype=\"directed\" mode=\"static\">\n";

String listNode = writeListNodes(nodeGephiList);
String listEdge = writeListEdges(edgeGephiList);

return open + listNode + listEdge + "\n</graph>\n" + "</gexf>";
}

public String writeListNodes(List<NodeGephi> nodeGephiList) {
StringBuilder result = new StringBuilder("\t<nodes>");
for (NodeGephi node : nodeGephiList) {
result.append("\n\t\t").append(node.toString());
}
result.append("\n\t</nodes>");
return result.toString();
}

public String writeListEdges(List<EdgeGephi> edgeGephiList) {
StringBuilder result = new StringBuilder("\n\t<edges>");
for (EdgeGephi node : edgeGephiList) {
result.append("\n\t\t").append(node.toString());
}
result.append("\n\t</edges>");
return result.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package mrmathami.cia.java.jdt.gephi.model;

public class EdgeGephi {
private int id;
private int source;
private int target;

public EdgeGephi(int id, int source, int target) {
this.id = id;
this.source = source;
this.target = target;
}

@Override
public String toString() {
return "<edge " +
"id='" + id + '\'' +
" source='" + source + '\'' +
" target='" + target + '\'' +
">" +
"\n\t\t\t" + "<viz:color r=\"102\" g=\"102\" b=\"102\"></viz:color>" +
"\n\t\t</edge>";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package mrmathami.cia.java.jdt.gephi.model;

public class NodeGephi {
public enum Type {
UNCHANGE(115, 115, 115),//charcoal blue
DELETE(255, 0, 0),//red
CHANGE(255, 255, 0),//yellow
ADD(0, 255, 153);//light green

private final int r;
private final int g;
private final int b;

Type(int r, int g, int b) {
this.r = r;
this.g = g;
this.b = b;
}
}

private final int id;
private final String label;
private final String size = "<viz:size value='8.0'></viz:size>";
private final Type type;

public NodeGephi(int id, String label, Type type) {
this.id = id;
this.label = label;
this.type = type;
}

@Override
public String toString() {
return "<node " +
"id='" + id + '\'' +
" label='" + label + '\'' +
'>' +
"\n\t\t\t" + size +
toStringType(type) +
"\n\t\t</node>";
}

private String toStringType(Type type) {
return "\n\t\t\t<viz:color" +
" r='" + type.r + '\'' +
" g='" + type.g + '\'' +
" b='" + type.b + '\'' +
"></viz:color>";
}
}
7 changes: 5 additions & 2 deletions jdt/src/main/java/mrmathami/cia/java/jdt/project/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
import mrmathami.cia.java.project.JavaProjectSnapshotComparison;
import mrmathami.cia.java.tree.dependency.JavaDependencyWeightTable;
import mrmathami.utils.Pair;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.Serializable;
import java.nio.file.Path;
import java.util.Collections;
Expand Down Expand Up @@ -82,9 +85,9 @@ public List<ProjectSnapshotComparison> getSnapshotComparisons() {
@Override
public JavaProjectSnapshot createSnapshot(@Nonnull String snapshotName,
@Nonnull Map<String, Pair<Path, List<Path>>> javaSources, @Nonnull List<Path> classPaths,
@Nonnull JavaDependencyWeightTable dependencyWeightTable, boolean enableRecovery) throws JavaCiaException {
@Nonnull JavaDependencyWeightTable dependencyWeightTable, boolean enableRecovery, Path configurationPath) throws JavaCiaException, ParserConfigurationException, SAXException, IOException {
final ProjectSnapshot snapshot = JavaSnapshotBuilder.build(
snapshotName, javaSources, classPaths, dependencyWeightTable, enableRecovery);
snapshotName, javaSources, classPaths, dependencyWeightTable, enableRecovery, configurationPath);
snapshots.add(snapshot);
return snapshot;
}
Expand Down
Loading