Skip to content

Commit 8d6f8a1

Browse files
authored
Merge pull request #30 from secure-software-engineering/develop
New plugin features and UI improvements
2 parents 112cf74 + 8c215cf commit 8d6f8a1

File tree

154 files changed

+3245
-1228
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

154 files changed

+3245
-1228
lines changed

swan_assist/build.gradle

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
plugins {
2-
id 'org.jetbrains.intellij' version '0.4.8'
32
id 'java'
3+
id 'org.jetbrains.intellij' version '0.4.15'
44
}
55

66
group 'de.fraunhofer'
7-
version '1.1'
7+
version '1.3'
88

99
sourceCompatibility = 1.8
1010

@@ -15,10 +15,11 @@ repositories {
1515

1616
dependencies {
1717
compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1'
18-
compile group:'de.upb.cs.swt', name: 'swan_core', version: '1.4.0'
18+
compile group:'de.upb.cs.swt', name: 'swan_core', version: '1.5.0'
1919
compile group: 'ca.mcgill.sable', name: 'soot', version: '3.3.0'
20-
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.5'
21-
compile group: 'org.slf4j', name: 'slf4j-simple', version : '1.7.5'
20+
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.29'
21+
compile group: 'org.slf4j', name:'slf4j-simple', version: '1.7.29'
22+
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'
2223
}
2324

2425
intellij {
@@ -30,4 +31,8 @@ patchPluginXml {
3031
"<br>- suggest methods" +
3132
"<br>- filter list" +
3233
"<br>- import and export configuration file"
34+
}
35+
36+
runIde {
37+
jvmArgs '-Xmx1024m'
3338
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package de.fraunhofer.iem.swan.assist.actions;
2+
3+
import com.intellij.openapi.actionSystem.AnAction;
4+
import com.intellij.openapi.actionSystem.AnActionEvent;
5+
import com.intellij.openapi.application.ApplicationManager;
6+
import com.intellij.util.messages.MessageBus;
7+
import de.fraunhofer.iem.swan.assist.comm.ExpandNotifier;
8+
import de.fraunhofer.iem.swan.assist.data.JSONFileLoader;
9+
import de.fraunhofer.iem.swan.assist.ui.MethodListTree;
10+
import icons.PluginIcons;
11+
import org.jetbrains.annotations.NotNull;
12+
13+
/**
14+
* @author Oshando Johnson on 2020-01-07
15+
*/
16+
public class ExpandAllAction extends AnAction {
17+
18+
@Override
19+
public void actionPerformed(@NotNull AnActionEvent e) {
20+
21+
MessageBus messageBus = ApplicationManager.getApplication().getMessageBus();
22+
ExpandNotifier publisher = messageBus.syncPublisher(ExpandNotifier.EXPAND_COLLAPSE_LIST);
23+
publisher.expandTree(true);
24+
25+
}
26+
27+
/**
28+
* Controls whether the action is enabled or disabled
29+
* @param event source event
30+
*/
31+
@Override
32+
public void update(@NotNull AnActionEvent event) {
33+
34+
//Disable/Enable action button
35+
if (JSONFileLoader.isFileSelected())
36+
event.getPresentation().setEnabled(true);
37+
else
38+
event.getPresentation().setEnabled(false);
39+
40+
41+
//Disable/Enable action button
42+
if (MethodListTree.TREE_EXPANDED){
43+
event.getPresentation().setIcon(PluginIcons.COLLAPSE);
44+
event.getPresentation().setText("Collapse Tree");
45+
} else{
46+
event.getPresentation().setIcon(PluginIcons.EXPAND);
47+
event.getPresentation().setText("Expand Tree");
48+
}
49+
}
50+
51+
}

swan_assist/src/main/java/de/fraunhofer/iem/swan/assist/actions/ExportAction.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,21 @@
77

88
package de.fraunhofer.iem.swan.assist.actions;
99

10+
import com.intellij.notification.Notification;
11+
import com.intellij.notification.NotificationType;
12+
import com.intellij.notification.Notifications;
1013
import com.intellij.openapi.actionSystem.AnAction;
1114
import com.intellij.openapi.actionSystem.AnActionEvent;
15+
import com.intellij.openapi.actionSystem.CommonDataKeys;
16+
import com.intellij.openapi.project.Project;
1217
import de.fraunhofer.iem.swan.assist.data.JSONFileLoader;
1318
import de.fraunhofer.iem.swan.assist.data.JSONWriter;
19+
import de.fraunhofer.iem.swan.assist.util.Constants;
20+
import org.jetbrains.annotations.NotNull;
1421

1522
import javax.swing.*;
1623
import javax.swing.filechooser.FileNameExtensionFilter;
24+
import javax.swing.filechooser.FileSystemView;
1725
import java.io.File;
1826
import java.io.IOException;
1927

@@ -28,14 +36,23 @@ public class ExportAction extends AnAction {
2836
* @param anActionEvent source event
2937
*/
3038
@Override
31-
public void actionPerformed(AnActionEvent anActionEvent) {
39+
public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
3240

3341
final String FILE_EXTENSION = ".json";
3442
String filePath = "";
3543

36-
JFileChooser fileChooser = new JFileChooser();
44+
final Project project = anActionEvent.getRequiredData(CommonDataKeys.PROJECT);
45+
46+
File projectPath;
47+
48+
if (project.getBasePath() == null)
49+
projectPath = FileSystemView.getFileSystemView().getDefaultDirectory();
50+
else
51+
projectPath = new File(project.getBasePath());
52+
53+
JFileChooser fileChooser = new JFileChooser(projectPath);
3754
fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
38-
fileChooser.setSelectedFile(new File("projectmethods.json"));
55+
fileChooser.setSelectedFile(new File(project.getName() + "-methods.json"));
3956
fileChooser.setFileFilter(new FileNameExtensionFilter("JSON Files", "json"));
4057

4158
int returnValue = fileChooser.showSaveDialog(null);
@@ -53,6 +70,10 @@ public void actionPerformed(AnActionEvent anActionEvent) {
5370
//TODO deal with exception
5471
try {
5572
exportFile.writeToJsonFile(JSONFileLoader.getMethods(), filePath);
73+
74+
Notifications.Bus.notify(
75+
new Notification(Constants.PLUGIN_GROUP_DISPLAY_ID, "", JSONFileLoader.getMethods().size()+ " methods exported to: "+filePath, NotificationType.INFORMATION));
76+
5677
} catch (IOException e) {
5778
e.printStackTrace();
5879
}
@@ -64,7 +85,7 @@ public void actionPerformed(AnActionEvent anActionEvent) {
6485
* @param event source event
6586
*/
6687
@Override
67-
public void update(AnActionEvent event) {
88+
public void update(@NotNull AnActionEvent event) {
6889

6990
//Disable/Enable action button
7091
if (JSONFileLoader.isFileSelected())

swan_assist/src/main/java/de/fraunhofer/iem/swan/assist/actions/ImportAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
public class ImportAction extends AnAction {
2626

2727
/**
28-
* Allows user to select configuration file that is then loaded.
28+
* Allows user to select configuration file that is then loaded.
2929
* @param e source event
3030
*/
3131
@Override

swan_assist/src/main/java/de/fraunhofer/iem/swan/assist/actions/LaunchSwanAction.java renamed to swan_assist/src/main/java/de/fraunhofer/iem/swan/assist/actions/RunSwanAnalysisAction.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import de.fraunhofer.iem.swan.assist.comm.SwanNotifier;
1717
import de.fraunhofer.iem.swan.assist.data.JSONFileLoader;
1818
import de.fraunhofer.iem.swan.assist.data.TrainingFileManager;
19-
import de.fraunhofer.iem.swan.assist.ui.dialog.PluginSettingsDialog;
19+
import de.fraunhofer.iem.swan.assist.ui.dialog.RunAnalysisDialog;
2020
import de.fraunhofer.iem.swan.assist.util.Constants;
2121
import de.fraunhofer.iem.swan.data.Method;
2222

@@ -31,7 +31,7 @@
3131
/**
3232
* Action opens dialog for user to set parameters for running SWAN. After which thread is created to run SWAN.
3333
*/
34-
public class LaunchSwanAction extends AnAction {
34+
public class RunSwanAnalysisAction extends AnAction {
3535

3636
protected Set<Method> methods = new HashSet<Method>();
3737

@@ -65,28 +65,29 @@ public void actionPerformed(AnActionEvent anActionEvent) {
6565
}
6666

6767
//Launch Dialog
68-
PluginSettingsDialog dialog = new PluginSettingsDialog(project, true);
68+
RunAnalysisDialog dialog = new RunAnalysisDialog(project, true);
6969
dialog.show();
7070

7171
if (dialog.getExitCode() == DialogWrapper.OK_EXIT_CODE) {
7272

7373
HashMap<String, String> swanParameters = dialog.getParameters();
7474

75-
String outputPath = swanParameters.get(Constants.OUTPUT_DIRECTORY) + File.separator + config.getProperty("input_json_suffix");
76-
TrainingFileManager trainingFileManager = new TrainingFileManager(project);
75+
if(!swanParameters.get(Constants.CONFIGURATION_FILE).contentEquals(config.getProperty("swan_default_param_value"))){
76+
String outputPath = swanParameters.get(Constants.OUTPUT_DIRECTORY) + File.separator + config.getProperty("input_json_suffix");
77+
TrainingFileManager trainingFileManager = new TrainingFileManager(project);
7778

78-
if (trainingFileManager.mergeExport(JSONFileLoader.getAllMethods(), outputPath))
79-
swanParameters.put(Constants.CONFIGURATION_FILE, outputPath);
80-
else
81-
swanParameters.put(Constants.CONFIGURATION_FILE, config.getProperty("swan_default_param_value"));
79+
if (trainingFileManager.mergeExport(JSONFileLoader.getAllMethods(), outputPath))
80+
swanParameters.put(Constants.CONFIGURATION_FILE, outputPath);
81+
}
8282

83-
SwanProcessBuilder processBuilder = new SwanProcessBuilder(project, dialog.getParameters());
84-
processBuilder.start();
83+
RunSwanAnalysisImpl processBuilder = new RunSwanAnalysisImpl(project, dialog.getParameters());
84+
processBuilder.run();
8585

8686
SwanNotifier publisher = messageBus.syncPublisher(SwanNotifier.START_SWAN_PROCESS_TOPIC);
8787
publisher.launchSwan(null);
8888
}
89-
}
89+
90+
}
9091

9192
/**
9293
* Controls whether the action is enabled or disabled
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2018 Fraunhofer IEM, Paderborn, Germany.
3+
*
4+
* Contributors:
5+
* Oshando Johnson (oshando.johnson@iem.fraunhofer.de ) - initial implementation
6+
******************************************************************************/
7+
8+
package de.fraunhofer.iem.swan.assist.actions;
9+
10+
import com.intellij.openapi.progress.ProgressIndicator;
11+
import com.intellij.openapi.progress.ProgressManager;
12+
import com.intellij.openapi.progress.Task;
13+
import com.intellij.openapi.project.Project;
14+
import com.intellij.util.messages.MessageBus;
15+
import de.fraunhofer.iem.swan.Main;
16+
import de.fraunhofer.iem.swan.assist.comm.SwanNotifier;
17+
import de.fraunhofer.iem.swan.assist.data.JSONFileParser;
18+
import de.fraunhofer.iem.swan.assist.data.MethodWrapper;
19+
import de.fraunhofer.iem.swan.assist.util.Constants;
20+
import org.apache.commons.io.FileUtils;
21+
import org.jetbrains.annotations.NotNull;
22+
23+
import java.io.File;
24+
import java.io.IOException;
25+
import java.time.LocalDateTime;
26+
import java.time.format.DateTimeFormatter;
27+
import java.util.HashMap;
28+
import java.util.ResourceBundle;
29+
30+
/**
31+
* Creates process to run SWAN on a separate thread.
32+
*/
33+
public class RunSwanAnalysisImpl {
34+
35+
private static HashMap<String, String> parameters;
36+
private Project project;
37+
private long duration;
38+
39+
/**
40+
* Initializes builder.
41+
* @param project Project on which the plugin is being used with
42+
* @param param Parameters that will be used as program arguments
43+
*/
44+
RunSwanAnalysisImpl(Project project, HashMap<String, String> param) {
45+
46+
this.project = project;
47+
parameters = param;
48+
}
49+
50+
/**
51+
* Sets up process to run the application and also send notification to subscribers when finished.
52+
*/
53+
public void run() {
54+
55+
ResourceBundle resource = ResourceBundle.getBundle("dialog_messages");
56+
57+
File outputFolder = new File(parameters.get(Constants.OUTPUT_DIRECTORY));
58+
59+
if(!outputFolder.exists())
60+
outputFolder.mkdir();
61+
62+
/*File logFile = new File(outputFolder, currentTimestamp + parameters.get(Constants.OUTPUT_LOG));
63+
try {
64+
logFile.createNewFile();
65+
parameters.replace(Constants.OUTPUT_LOG, logFile.getPath());
66+
67+
FileOutputStream fileOutputStream = new FileOutputStream(logFile.getAbsolutePath());
68+
69+
System.setOut(new PrintStream(fileOutputStream));
70+
71+
} catch (IOException e) {
72+
e.printStackTrace();
73+
}*/
74+
75+
ProgressManager.getInstance().run(new Task.Backgroundable(project, resource.getString("Messages.Title.Progress")) {
76+
@Override
77+
public void run(@NotNull ProgressIndicator indicator) {
78+
79+
long start = System.currentTimeMillis();
80+
81+
Main.main(new String[]{parameters.get(Constants.SOURCE_DIRECTORY),
82+
parameters.get(Constants.TRAIN_DIRECTORY),
83+
parameters.get(Constants.CONFIGURATION_FILE),
84+
parameters.get(Constants.OUTPUT_DIRECTORY)});
85+
86+
duration = System.currentTimeMillis() - start;
87+
}
88+
89+
@Override
90+
public void onCancel() {
91+
super.onCancel();
92+
}
93+
94+
@Override
95+
public void onSuccess() {
96+
super.onSuccess();
97+
98+
System.out.println("FILE: "+parameters.get(Constants.OUTPUT_FILE));
99+
//Create copy of file
100+
copyFile(new File(parameters.get(Constants.OUTPUT_FILE)));
101+
102+
JSONFileParser parser = new JSONFileParser(parameters.get(Constants.OUTPUT_FILE));
103+
HashMap<String, MethodWrapper> exportedMethods = parser.parseJSONFileMap();
104+
105+
HashMap<String, String> results = new HashMap<String, String>();
106+
results.put(Constants.OUTPUT_FILE, parameters.get(Constants.OUTPUT_FILE));
107+
results.put(Constants.OUTPUT_LOG, parameters.get(Constants.OUTPUT_LOG));
108+
109+
int m = (int) (((duration / 1000) / 60) % 60);
110+
int s = (int) ((duration / 1000) % 60);
111+
112+
results.put(Constants.ANALYSIS_RESULT, exportedMethods.size() + " methods found in "+m+" mins "+s+ " secs");
113+
114+
MessageBus messageBus = project.getMessageBus();
115+
SwanNotifier publisher = messageBus.syncPublisher(SwanNotifier.END_SWAN_PROCESS_TOPIC);
116+
publisher.launchSwan(results);
117+
}
118+
});
119+
}
120+
121+
private void copyFile(File original ){
122+
123+
File copied = new File(
124+
parameters.get(Constants.OUTPUT_DIRECTORY) + File.separator +getCurrentTimestamp()+ "-config.json");
125+
try {
126+
FileUtils.copyFile(original, copied);
127+
} catch (IOException e) {
128+
e.printStackTrace();
129+
}
130+
}
131+
132+
/**
133+
* Get the timestamp in a specified format.
134+
* @return Formatted date
135+
*/
136+
private String getCurrentTimestamp() {
137+
138+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HHmmss");
139+
return LocalDateTime.now().format(formatter);
140+
}
141+
}

0 commit comments

Comments
 (0)