Skip to content

Commit

Permalink
Removed strings, now compilation units are used instead across the co…
Browse files Browse the repository at this point in the history
…de base
  • Loading branch information
JuanBesa committed Nov 7, 2017
1 parent c949c65 commit 1ba5ce4
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,157 @@
package org.ucombinator.jaam.visualizer.codeView;

import java.util.Collection;
import java.util.Collections;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.strobel.decompiler.languages.java.ast.*;
import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import org.fxmisc.flowless.VirtualizedScrollPane;
import org.fxmisc.richtext.CodeArea;
import org.fxmisc.richtext.LineNumberFactory;
import org.fxmisc.richtext.model.StyleSpans;
import org.fxmisc.richtext.model.StyleSpansBuilder;

public class CodeAreaGenerator {



public StackPane generateCodeArea()
{
String text = sampleCode;

CodeArea codeArea = new CodeArea();
codeArea.setParagraphGraphicFactory(LineNumberFactory.get(codeArea));

codeArea.richChanges()
.filter(ch -> !ch.getInserted().equals(ch.getRemoved())) // XXX
.subscribe(change -> {
codeArea.setStyleSpans(0, computeHighlighting(codeArea.getText()));
});
codeArea.replaceText(0, 0, sampleCode);

codeArea.setMaxHeight(Double.MAX_VALUE);

VirtualizedScrollPane scrollPane = new VirtualizedScrollPane(codeArea);
scrollPane.setMaxHeight(Double.MAX_VALUE);

StackPane result = new StackPane(scrollPane);

result.setMaxWidth(Double.MAX_VALUE);
result.setMaxHeight(Double.MAX_VALUE);

return result;
}

public void addClass(CompilationUnit unit)
{
AstNodeCollection<TypeDeclaration> types = unit.getTypes();

assert types.size() == 1;

TypeDeclaration typeDeclaration = types.firstOrNullObject();

typeDeclaration.getText();

String className = typeDeclaration.getName();

System.out.println("JUAN " + className);

System.out.println("PACK " + unit.getPackage().getName());

for(AstNode i: typeDeclaration.getChildrenByRole(Roles.TYPE_MEMBER))
{
EntityDeclaration entity = (EntityDeclaration)i;
System.out.println(i.getRole() + " " + i.getClass() + " " + entity.getName() + " " + entity.getEntityType());
}

//String className = getClassName(unit);
//graph.addClass(className, unit.getText());
}

private static final String[] KEYWORDS = new String[] {
"abstract", "assert", "boolean", "break", "byte",
"case", "catch", "char", "class", "const",
"continue", "default", "do", "double", "else",
"enum", "extends", "final", "finally", "float",
"for", "goto", "if", "implements", "import",
"instanceof", "int", "interface", "long", "native",
"new", "package", "private", "protected", "public",
"return", "short", "static", "strictfp", "super",
"switch", "synchronized", "this", "throw", "throws",
"transient", "try", "void", "volatile", "while"
};

private static final String KEYWORD_PATTERN = "\\b(" + String.join("|", KEYWORDS) + ")\\b";
private static final String PAREN_PATTERN = "\\(|\\)";
private static final String BRACE_PATTERN = "\\{|\\}";
private static final String BRACKET_PATTERN = "\\[|\\]";
private static final String SEMICOLON_PATTERN = "\\;";
private static final String STRING_PATTERN = "\"([^\"\\\\]|\\\\.)*\"";
private static final String COMMENT_PATTERN = "//[^\n]*" + "|" + "/\\*(.|\\R)*?\\*/";

private static final Pattern PATTERN = Pattern.compile(
"(?<KEYWORD>" + KEYWORD_PATTERN + ")"
+ "|(?<PAREN>" + PAREN_PATTERN + ")"
+ "|(?<BRACE>" + BRACE_PATTERN + ")"
+ "|(?<BRACKET>" + BRACKET_PATTERN + ")"
+ "|(?<SEMICOLON>" + SEMICOLON_PATTERN + ")"
+ "|(?<STRING>" + STRING_PATTERN + ")"
+ "|(?<COMMENT>" + COMMENT_PATTERN + ")"
);

private static final String sampleCode = String.join("\n", new String[] {
"package com.example;",
"",
"import java.util.*;",
"",
"public class Foo extends Bar implements Baz {",
"",
" /*",
" * multi-line comment",
" */",
" public static void main(String[] args) {",
" // single-line comment",
" for(String arg: args) {",
" if(arg.length() != 0)",
" System.out.println(arg);",
" else",
" System.err.println(\"Warning: empty string as argument\");",
" }",
" }",
"",
"}"
});

private static StyleSpans<Collection<String>> computeHighlighting(String text) {
Matcher matcher = PATTERN.matcher(text);
int lastKwEnd = 0;
StyleSpansBuilder<Collection<String>> spansBuilder
= new StyleSpansBuilder<>();
while(matcher.find()) {
String styleClass =
matcher.group("KEYWORD") != null ? "keyword" :
matcher.group("PAREN") != null ? "paren" :
matcher.group("BRACE") != null ? "brace" :
matcher.group("BRACKET") != null ? "bracket" :
matcher.group("SEMICOLON") != null ? "semicolon" :
matcher.group("STRING") != null ? "string" :
matcher.group("COMMENT") != null ? "comment" :
null; /* never happens */ assert styleClass != null;
spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
spansBuilder.add(Collections.singleton(styleClass), matcher.end() - matcher.start());
lastKwEnd = matcher.end();
}
spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd);
return spansBuilder.create();
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import org.ucombinator.jaam.visualizer.codeView.CodeAreaGenerator;
import org.ucombinator.jaam.visualizer.graph.Graph;
import org.ucombinator.jaam.visualizer.gui.CodeArea;
import org.ucombinator.jaam.visualizer.gui.SearchResults;
Expand All @@ -22,15 +23,16 @@ public class CodeViewController {
@FXML public final VBox root = null; // Initialized by Controllers.loadFXML()
@FXML public final TabPane codeTabs = null; // Initialized by Controllers.loadFXML()

public CodeViewController() throws IOException {
Controllers.loadFXML("/CodeView.fxml", this);

Tab testTab = new Tab("Test", new TextArea());
CodeAreaGenerator codeAreaGenerator;

codeTabs.getTabs().add(testTab);
public CodeViewController(CodeAreaGenerator codeAreaGenerator) throws IOException {
Controllers.loadFXML("/CodeView.fxml", this);

this.codeAreaGenerator = codeAreaGenerator;

Tab testTab = new Tab("Test", this.codeAreaGenerator.generateCodeArea());

codeTabs.getTabs().add(testTab);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.ucombinator.jaam.visualizer.controllers;

import com.strobel.decompiler.languages.java.ast.*;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
Expand All @@ -8,15 +9,15 @@
import javafx.stage.FileChooser;
import javafx.util.Pair;
import org.ucombinator.jaam.serializer.*;
import org.ucombinator.jaam.visualizer.codeView.CodeAreaGenerator;
import org.ucombinator.jaam.visualizer.graph.Graph;
import org.ucombinator.jaam.visualizer.layout.LayoutLoopVertex;
import org.ucombinator.jaam.visualizer.layout.LayoutMethodVertex;
import org.ucombinator.jaam.visualizer.main.Main;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.*;

public class MainPaneController {
@FXML public final Parent root = null; // Initialized by Controllers.loadFXML()
Expand Down Expand Up @@ -61,47 +62,42 @@ public void loadLoopGraphFile(File file) throws IOException {
// Make "Open" dialog remember where we last loaded a file
fileChooser.setInitialDirectory(file.getParentFile());

Graph graph = parseLoopGraph(file);
CodeAreaGenerator codeAreaGenerator = new CodeAreaGenerator();
Graph graph = parseLoopGraph(file, codeAreaGenerator);

System.out.println("--> Create visualization: start...");
MainTabController tabController = new MainTabController(file, graph);
MainTabController tabController = new MainTabController(file, graph, codeAreaGenerator);
System.out.println("<-- Create visualization: Done!");

tabPane.getTabs().add(tabController.tab);
tabPane.getSelectionModel().select(tabController.tab);
}

private static Graph parseLoopGraph(File file) {
private static Graph parseLoopGraph(File file, CodeAreaGenerator codeAreaGenerator) {
Graph graph = new Graph();

ArrayList<LoopEdge> edges = new ArrayList<>();
for (Packet packet : Serializer.readAll(file.getAbsolutePath())) {
if (packet instanceof LoopLoopNode) {
LoopLoopNode node = (LoopLoopNode) packet;
CodeIdentifier identifier = new CodeIdentifier(node.method().getSignature());

graph.addVertex(new LayoutLoopVertex(node.id().id(),
node.method().getSignature(),
node.statementIndex(), identifier.className, identifier.methodName));
node.statementIndex(), node));

} else if (packet instanceof LoopMethodNode) {
LoopMethodNode node = (LoopMethodNode) packet;
CodeIdentifier identifier = new CodeIdentifier(node.method().getSignature());
graph.addVertex(new LayoutMethodVertex(node.id().id(),
node.method().getSignature(),
identifier.className, identifier.methodName));
node.method().getSignature(), node));
} else if (packet instanceof LoopEdge) {
edges.add((LoopEdge) packet);
}
// else if (packet instanceof org.ucombinator.jaam.tools.decompile.DecompiledClass)
// {
// CompilationUnit unit = ((org.ucombinator.jaam.tools.decompile.DecompiledClass) packet).compilationUnit();
// String className = getClassName(unit);
// graph.addClass(className, unit.getText());
// }

// System.out.println("Reading new packet...");
// packet = packetInput.read();
// System.out.println("Packet read!");
else if (packet instanceof org.ucombinator.jaam.tools.decompile.DecompiledClass)
{
CompilationUnit unit = ((org.ucombinator.jaam.tools.decompile.DecompiledClass) packet).compilationUnit();

codeAreaGenerator.addClass(unit);
}
}

// We actually create the edges here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Rectangle;
import org.ucombinator.jaam.visualizer.codeView.CodeAreaGenerator;
import org.ucombinator.jaam.visualizer.graph.Graph;
import org.ucombinator.jaam.visualizer.gui.*;
import org.ucombinator.jaam.visualizer.layout.AbstractLayoutVertex;
Expand Down Expand Up @@ -34,7 +36,7 @@ public enum SearchType {
ID, TAG, INSTRUCTION, METHOD, ALL_LEAVES, ALL_SOURCES, OUT_OPEN, OUT_CLOSED, IN_OPEN, IN_CLOSED, ROOT_PATH
}

public MainTabController(File file, Graph graph) throws IOException {
public MainTabController(File file, Graph graph, CodeAreaGenerator codeAreaGenerator) throws IOException {
Controllers.loadFXML("/MainTabContent.fxml", this);
this.vizPanelController = new VizPanelController(file, graph);
this.centerPane.setCenter(this.vizPanelController.root);
Expand All @@ -43,10 +45,12 @@ public MainTabController(File file, Graph graph) throws IOException {
this.tab.tooltipProperty().set(new Tooltip(file.getAbsolutePath()));
Controllers.put(this.tab, this);

this.codeViewController = new CodeViewController();
this.codeViewController = new CodeViewController(codeAreaGenerator);

this.leftPane.getChildren().add(this.codeViewController.codeTabs);

//this.leftPane.getScene().getStylesheets().add("resources/CodeView.css");

//CodeArea codeArea = new CodeArea();
//codeArea.addSelectHandler(centerPane);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,13 @@ private static AbstractLayoutVertex upgradeVertex(AbstractVertex v)
{
LayoutLoopVertex l = (LayoutLoopVertex)v;
newVertex = new LayoutLoopVertex(l.getId(), l.getLabel(), l.getStatementIndex(),
l.getClassName(), l.getMethodName());
l.getCompilationUnit());
//newVertex = new LayoutLoopVertex(v.getId(), v.getLabel(), 0);
}
else if(v instanceof LayoutMethodVertex)
{
LayoutMethodVertex l = (LayoutMethodVertex)v;
newVertex = new LayoutMethodVertex(l.getId(), l.getLabel(), l.getClassName(), l.getMethodName());
newVertex = new LayoutMethodVertex(l.getId(), l.getLabel(), l.getCompilationUnit());
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.ucombinator.jaam.visualizer.layout;

import javafx.scene.paint.Color;
import org.ucombinator.jaam.serializer.LoopLoopNode;
import org.ucombinator.jaam.visualizer.controllers.VizPanelController;

import java.util.HashSet;
Expand All @@ -10,26 +11,29 @@ public class LayoutLoopVertex extends AbstractLayoutVertex implements Cloneable

private static final Color defaultColor = Color.LIGHTYELLOW;

private int startJimpleIndex;
private String className;
private String methodName;
private int statementIndex;

public LayoutLoopVertex(int id, String label, int statementIndex, String className, String methodName){
private LoopLoopNode compilationUnit;

public LayoutLoopVertex(int id, String label, int statementIndex, LoopLoopNode compilationUnit){
super(id, label, VertexType.LOOP);
this.setDefaultColor();

this.statementIndex = statementIndex;
this.className = className;
this.methodName = methodName;

this.compilationUnit = compilationUnit;
}

public String getMethodName() {
return this.methodName;
return this.compilationUnit.method().getName();
}

public String getClassName() {
return className;
return this.compilationUnit.method().getDeclaringClass().getName();
}

public LoopLoopNode getCompilationUnit() {
return compilationUnit;
}

public String getRightPanelContent() {
Expand All @@ -42,7 +46,7 @@ public String getShortDescription() {

@Override
public boolean searchByMethod(String query, VizPanelController mainPanel) {
boolean found = this.methodName.contains(query);
boolean found = this.getMethodName().contains(query);
if(found) {
this.setHighlighted(found);
mainPanel.getHighlighted().add(this);
Expand Down
Loading

0 comments on commit 1ba5ce4

Please sign in to comment.