Skip to content

Commit 3f76888

Browse files
authored
Merge pull request #8 from secure-software-engineering/feature/suggest-methods
Implement suggest methods feature
2 parents ae0998e + 61e0a7f commit 3f76888

Some content is hidden

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

49 files changed

+1018
-328
lines changed

swan_assist/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ sourceCompatibility = 1.8
1010

1111
repositories {
1212
mavenCentral()
13+
mavenLocal()
1314
}
1415

1516
dependencies {
1617
compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1'
17-
compile group:'de.upb.cs.swt', name: 'swan_core', version: '1.3.0'
18+
compile group:'de.upb.cs.swt', name: 'swan_core', version: '1.2.1'
1819
compile group: 'ca.mcgill.sable', name: 'soot', version: '3.3.0'
1920
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.5'
2021
compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.5'

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import com.intellij.openapi.actionSystem.CommonDataKeys;
1313
import com.intellij.openapi.project.Project;
1414
import com.intellij.util.messages.MessageBus;
15-
import de.fraunhofer.iem.swan.assist.comm.FileSelectedNotifier;
15+
import de.fraunhofer.iem.swan.assist.comm.ConfigurationFileNotifier;
1616

1717
import javax.swing.*;
1818
import javax.swing.filechooser.FileNameExtensionFilter;
@@ -51,8 +51,8 @@ public void actionPerformed(AnActionEvent e) {
5151
File selectedFile = fileChooser.getSelectedFile();
5252

5353
MessageBus messageBus = project.getMessageBus();
54-
FileSelectedNotifier publisher = messageBus.syncPublisher(FileSelectedNotifier.INITIAL_FILE_NOTIFIER_TOPIC);
55-
publisher.notifyFileChange(selectedFile.getAbsoluteFile().toString());
54+
ConfigurationFileNotifier publisher = messageBus.syncPublisher(ConfigurationFileNotifier.FILE_NOTIFIER_TOPIC);
55+
publisher.loadInitialFile(selectedFile.getAbsoluteFile().toString());
5656
}
5757
}
5858
}

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

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,29 @@
1515
import com.intellij.util.messages.MessageBus;
1616
import de.fraunhofer.iem.swan.assist.comm.SwanNotifier;
1717
import de.fraunhofer.iem.swan.assist.data.JSONFileLoader;
18-
import de.fraunhofer.iem.swan.assist.data.JSONFileParser;
19-
import de.fraunhofer.iem.swan.assist.data.JSONWriter;
20-
import de.fraunhofer.iem.swan.assist.data.MethodWrapper;
21-
import de.fraunhofer.iem.swan.assist.ui.dialog.SwanLauncherDialog;
18+
import de.fraunhofer.iem.swan.assist.data.TrainingFileManager;
19+
import de.fraunhofer.iem.swan.assist.ui.dialog.PluginSettingsDialog;
2220
import de.fraunhofer.iem.swan.assist.util.Constants;
2321
import de.fraunhofer.iem.swan.data.Method;
2422

2523
import java.io.File;
2624
import java.io.IOException;
2725
import java.io.InputStream;
28-
import java.io.InputStreamReader;
29-
import java.util.*;
26+
import java.util.HashMap;
27+
import java.util.HashSet;
28+
import java.util.Properties;
29+
import java.util.Set;
3030

3131
/**
3232
* Action opens dialog for user to set parameters for running SWAN. After which thread is created to run SWAN.
3333
*/
3434
public class LaunchSwanAction extends AnAction {
3535

3636
protected Set<Method> methods = new HashSet<Method>();
37+
3738
/**
3839
* Obtains application parameters from user, exports updated JSON file and starts thread to run SWAN.
40+
*
3941
* @param anActionEvent source event
4042
*/
4143
@Override
@@ -63,42 +65,20 @@ public void actionPerformed(AnActionEvent anActionEvent) {
6365
}
6466

6567
//Launch Dialog
66-
SwanLauncherDialog dialog = new SwanLauncherDialog(project, true);
68+
PluginSettingsDialog dialog = new PluginSettingsDialog(project, true);
6769
dialog.show();
6870

6971
if (dialog.getExitCode() == DialogWrapper.OK_EXIT_CODE) {
7072

7173
HashMap<String, String> swanParameters = dialog.getParameters();
7274

73-
if(JSONFileLoader.isFileSelected()) {
74-
//Merge current list with training methods
75-
HashMap<String, MethodWrapper> methods = JSONFileLoader.getAllMethods();
76-
77-
InputStream stream = getClass().getClassLoader().getResourceAsStream(config.getProperty("train_config_file"));
78-
HashMap<String, MethodWrapper> trainingMethods = new HashMap<>();
79-
80-
if (stream != null) {
81-
82-
JSONFileParser fileParser = new JSONFileParser();
83-
trainingMethods = fileParser.parseJSONFileStream(new InputStreamReader(stream));
84-
}
85-
86-
HashMap<String, MethodWrapper> mergedMethods = new HashMap<>(methods);
87-
mergedMethods.putAll(trainingMethods);
75+
String outputPath = swanParameters.get(Constants.OUTPUT_DIRECTORY) + File.separator + config.getProperty("input_json_suffix");
76+
TrainingFileManager trainingFileManager = new TrainingFileManager(project);
8877

89-
//Export changes to configuration files
90-
JSONWriter exportFile = new JSONWriter();
91-
String newConfigFile = swanParameters.get(Constants.SWAN_OUTPUT_DIR) + File.separator + config.getProperty("input_json_suffix");
92-
try {
93-
exportFile.writeToJsonFile(new ArrayList<>(mergedMethods.values()), newConfigFile);
94-
} catch (IOException e) {
95-
e.printStackTrace();
96-
}
97-
swanParameters.put(Constants.SWAN_CONFIG_FILE, newConfigFile);
98-
}
99-
else{
100-
swanParameters.put(Constants.SWAN_CONFIG_FILE, config.getProperty("swan_default_param_value"));
101-
}
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"));
10282

10383
SwanProcessBuilder processBuilder = new SwanProcessBuilder(project, dialog.getParameters());
10484
processBuilder.start();
@@ -110,6 +90,7 @@ public void actionPerformed(AnActionEvent anActionEvent) {
11090

11191
/**
11292
* Controls whether the action is enabled or disabled
93+
*
11394
* @param event source event
11495
*/
11596
@Override

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ public void run() {
4848

4949
String currentTimestamp = getCurrentTimestamp();
5050

51-
File outputFolder = new File(parameters.get(Constants.SWAN_OUTPUT_DIR));
51+
File outputFolder = new File(parameters.get(Constants.OUTPUT_DIRECTORY));
5252

5353
if(!outputFolder.exists())
5454
outputFolder.mkdir();
5555

56-
File logFile = new File(outputFolder, currentTimestamp + parameters.get(Constants.SWAN_OUTPUT_LOG));
56+
File logFile = new File(outputFolder, currentTimestamp + parameters.get(Constants.OUTPUT_LOG));
5757
try {
5858
logFile.createNewFile();
59-
parameters.replace(Constants.SWAN_OUTPUT_LOG, logFile.getPath());
59+
parameters.replace(Constants.OUTPUT_LOG, logFile.getPath());
6060

6161
FileOutputStream fileOutputStream = new FileOutputStream(logFile.getAbsolutePath());
6262

@@ -66,16 +66,16 @@ public void run() {
6666
e.printStackTrace();
6767
}
6868

69-
Main.main(new String[]{parameters.get(Constants.SWAN_SOURCE_DIR),
70-
parameters.get(Constants.SWAN_TRAIN_DIR),
71-
parameters.get(Constants.SWAN_CONFIG_FILE),
72-
parameters.get(Constants.SWAN_OUTPUT_DIR)});
69+
Main.main(new String[]{parameters.get(Constants.SOURCE_DIRECTORY),
70+
parameters.get(Constants.TRAIN_DIRECTORY),
71+
parameters.get(Constants.CONFIGURATION_FILE),
72+
parameters.get(Constants.OUTPUT_DIRECTORY)});
7373

7474
System.setOut(System.out);
7575

7676
HashMap<String, String> results = new HashMap<String, String>();
77-
results.put(Constants.SWAN_OUTPUT_FILE, parameters.get(Constants.SWAN_OUTPUT_FILE));
78-
results.put(Constants.SWAN_OUTPUT_LOG, parameters.get(Constants.SWAN_OUTPUT_LOG));
77+
results.put(Constants.OUTPUT_FILE, parameters.get(Constants.OUTPUT_FILE));
78+
results.put(Constants.OUTPUT_LOG, parameters.get(Constants.OUTPUT_LOG));
7979

8080
MessageBus messageBus = project.getMessageBus();
8181
SwanNotifier publisher = messageBus.syncPublisher(SwanNotifier.END_SWAN_PROCESS_TOPIC);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ public void actionPerformed(AnActionEvent anActionEvent) {
6868
if (confirmation == JOptionPane.YES_OPTION) {
6969

7070
MessageBus messageBus = project.getMessageBus();
71-
MethodNotifier publisher = messageBus.syncPublisher(MethodNotifier.METHOD_REMOVED_TOPIC);
72-
publisher.afterAction(deleteMethod);
71+
MethodNotifier publisher = messageBus.syncPublisher(MethodNotifier.ADD_UPDATE_DELETE_METHOD);
72+
publisher.removeMethod(deleteMethod);
7373
}
7474
} else {
7575
final Editor editor = anActionEvent.getRequiredData(CommonDataKeys.EDITOR);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ public void actionPerformed(AnActionEvent anActionEvent) {
6666
//Notify Summary Tool window that new method was restored
6767
method.setUpdateOperation(null);
6868
MessageBus messageBus = project.getMessageBus();
69-
MethodNotifier publisher = messageBus.syncPublisher(MethodNotifier.METHOD_UPDATED_ADDED_TOPIC);
70-
publisher.afterAction(method);
69+
MethodNotifier publisher = messageBus.syncPublisher(MethodNotifier.ADD_UPDATE_DELETE_METHOD);
70+
publisher.restoreMethod(method);
7171
}
7272
}
7373
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2018 Fraunhofer IEM, Paderborn, Germany.
3+
*
4+
* Contributors:
5+
* Goran Piskachev (goran.piskachev@iem.fraunhofer.de) - initial implementation
6+
* Oshando Johnson (oshando.johnson@iem.fraunhofer.de ) - plugin integration
7+
******************************************************************************/
8+
9+
package de.fraunhofer.iem.swan.assist.actions.suggest;
10+
11+
import de.fraunhofer.iem.swan.assist.data.MethodWrapper;
12+
import de.fraunhofer.iem.swan.data.Method;
13+
14+
public class MethodPair {
15+
private MethodWrapper m1;
16+
private MethodWrapper m2;
17+
18+
/**
19+
* Initializes MethodPair object.
20+
* @param m1 first method
21+
* @param m2 second method
22+
*/
23+
MethodPair(MethodWrapper m1, MethodWrapper m2) {
24+
this.m1 = m1;
25+
this.m2 = m2;
26+
}
27+
28+
/**
29+
*
30+
* @return first method of pair.
31+
*/
32+
MethodWrapper getMethod1() {
33+
return m1;
34+
}
35+
36+
/**
37+
*
38+
* @return second method of pair.
39+
*/
40+
MethodWrapper getMethod2() {
41+
return m2;
42+
}
43+
}

swan_assist/src/main/java/de/fraunhofer/iem/swan/assist/actions/SuggestAction.java renamed to swan_assist/src/main/java/de/fraunhofer/iem/swan/assist/actions/suggest/SuggestAction.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@
55
* Oshando Johnson (oshando.johnson@iem.fraunhofer.de ) - initial implementation
66
******************************************************************************/
77

8-
package de.fraunhofer.iem.swan.assist.actions;
8+
package de.fraunhofer.iem.swan.assist.actions.suggest;
99

10+
import com.intellij.ide.util.PropertiesComponent;
1011
import com.intellij.openapi.actionSystem.AnAction;
1112
import com.intellij.openapi.actionSystem.AnActionEvent;
13+
import com.intellij.openapi.actionSystem.CommonDataKeys;
14+
import com.intellij.openapi.project.Project;
15+
import com.intellij.util.messages.MessageBus;
16+
import de.fraunhofer.iem.swan.assist.comm.SuggestNotifier;
1217
import de.fraunhofer.iem.swan.assist.data.JSONFileLoader;
13-
import de.fraunhofer.iem.swan.assist.data.MethodWrapper;
14-
import de.fraunhofer.iem.swan.assist.ui.dialog.MethodDialog;
18+
import de.fraunhofer.iem.swan.assist.data.PropertiesManager;
19+
import de.fraunhofer.iem.swan.assist.util.Constants;
1520

16-
import java.util.Arrays;
17-
import java.util.HashMap;
18-
19-
/**
20-
* @author Oshando Johnson on 2019-04-11
21-
*/
2221
public class SuggestAction extends AnAction {
2322

2423
/**
@@ -27,25 +26,25 @@ public class SuggestAction extends AnAction {
2726
*/
2827
@Override
2928
public void actionPerformed(AnActionEvent e) {
30-
// TODO: implement logic to obtain methods from SWAN
31-
32-
HashMap<String,MethodWrapper> suggestedMethods = new HashMap<>();
3329

34-
MethodWrapper m1 = new MethodWrapper("getPassport", Arrays.asList("java.lang.String"), "java.lang.String", "de.fraunhofer.Test");
35-
m1.setStatus(MethodWrapper.MethodStatus.SUGGESTED);
30+
final Project project = e.getRequiredData(CommonDataKeys.PROJECT);
31+
MessageBus messageBus = project.getMessageBus();
3632

37-
MethodWrapper m2 = new MethodWrapper("deleteApplication", Arrays.asList("java.lang.String"), "void", "de.fraunhofer.Test");
38-
m2.setStatus(MethodWrapper.MethodStatus.SUGGESTED);
33+
//Set output directory for plugin, if not set
34+
PropertiesManager.setProjectOutputPath(project);
3935

40-
suggestedMethods.put(m1.getSignature(true),m1);
41-
suggestedMethods.put(m2.getSignature(true),m2);
36+
SuggestThread suggestThread = new SuggestThread(project,
37+
PropertiesComponent.getInstance(project).getValue(Constants.CONFIGURATION_FILE),
38+
PropertiesComponent.getInstance(project).getValue(Constants.SOURCE_DIRECTORY));
39+
suggestThread.start();
4240

43-
MethodDialog dialog = new MethodDialog(suggestedMethods, m1.getSignature(true), e.getProject(), JSONFileLoader.getCategories());
44-
dialog.show();
41+
SuggestNotifier suggestNotifier = messageBus.syncPublisher(SuggestNotifier.SUGGEST_METHOD_TOPIC);
42+
suggestNotifier.startSuggestMethod();
4543
}
4644

4745
/**
4846
* Controls whether the action is enabled or disabled
47+
*
4948
* @param event source event
5049
*/
5150
@Override

0 commit comments

Comments
 (0)