Skip to content

Commit 661fbd8

Browse files
authored
Merge pull request #401 from eclipse-cognicrypt/feature/extension_points
Task integrator feature for integrating custom tasks into Code generator
2 parents 2828534 + 2df2a2b commit 661fbd8

124 files changed

Lines changed: 970 additions & 13737 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<classpath>
3-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
4-
<attributes>
5-
<attribute name="maven.pomderived" value="true"/>
6-
</attributes>
7-
</classpathentry>
8-
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
9-
<classpathentry kind="src" output="target/classes" path="src/main/java">
10-
<attributes>
11-
<attribute name="optional" value="true"/>
12-
<attribute name="maven.pomderived" value="true"/>
13-
</attributes>
14-
</classpathentry>
15-
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
16-
<attributes>
17-
<attribute name="test" value="true"/>
18-
<attribute name="optional" value="true"/>
19-
<attribute name="maven.pomderived" value="true"/>
20-
</attributes>
21-
</classpathentry>
22-
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
23-
<attributes>
24-
<attribute name="maven.pomderived" value="true"/>
25-
</attributes>
26-
</classpathentry>
27-
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
28-
<attributes>
29-
<attribute name="maven.pomderived" value="true"/>
30-
</attributes>
31-
</classpathentry>
32-
<classpathentry kind="lib" path="lib/saxon9he.jar"/>
33-
<classpathentry kind="lib" path="lib/dom4j-2.0.0.jar"/>
34-
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
35-
<attributes>
36-
<attribute name="test" value="true"/>
37-
<attribute name="maven.pomderived" value="true"/>
38-
</attributes>
39-
</classpathentry>
40-
<classpathentry kind="lib" path="lib/claferchocoig.jar"/>
41-
<classpathentry kind="output" path="target/classes"/>
42-
</classpath>
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
4+
<attributes>
5+
<attribute name="maven.pomderived" value="true"/>
6+
</attributes>
7+
</classpathentry>
8+
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
9+
<classpathentry kind="src" output="target/classes" path="src/main/java">
10+
<attributes>
11+
<attribute name="optional" value="true"/>
12+
<attribute name="maven.pomderived" value="true"/>
13+
</attributes>
14+
</classpathentry>
15+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
16+
<attributes>
17+
<attribute name="test" value="true"/>
18+
<attribute name="optional" value="true"/>
19+
<attribute name="maven.pomderived" value="true"/>
20+
</attributes>
21+
</classpathentry>
22+
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
23+
<attributes>
24+
<attribute name="maven.pomderived" value="true"/>
25+
</attributes>
26+
</classpathentry>
27+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
28+
<attributes>
29+
<attribute name="maven.pomderived" value="true"/>
30+
</attributes>
31+
</classpathentry>
32+
<classpathentry kind="lib" path="lib/saxon9he.jar"/>
33+
<classpathentry kind="lib" path="lib/dom4j-2.0.0.jar"/>
34+
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
35+
<attributes>
36+
<attribute name="test" value="true"/>
37+
<attribute name="maven.pomderived" value="true"/>
38+
</attributes>
39+
</classpathentry>
40+
<classpathentry kind="lib" path="lib/claferchocoig.jar"/>
41+
<classpathentry kind="output" path="target/classes"/>
42+
</classpath>

plugins/de.cognicrypt.codegenerator/META-INF/MANIFEST.MF

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Require-Bundle: org.eclipse.ui,
1717
org.eclipse.ui.workbench,
1818
org.junit,
1919
de.cognicrypt.core;bundle-version="1.0.0",
20+
de.cognicrypt.staticanalyzer;bundle-version="1.0.0",
2021
org.eclipse.ui.ide,
2122
com.google.gson,
2223
org.eclipse.ui.workbench.texteditor,
@@ -29,7 +30,8 @@ Bundle-ClassPath: lib/claferchocoig.jar,
2930
src/main/resources/,
3031
src/main/java/,
3132
lib/dom4j-2.0.0.jar,
32-
lib/saxon9he.jar
33+
lib/saxon9he.jar,
34+
lib/test.jar
3335
Automatic-Module-Name: de.cognicrypt.codegenerator
3436
Bundle-Vendor: TU Darmstadt, Paderborn University
3537
Export-Package: de.cognicrypt.codegenerator,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package de.cognicrypt.codegenerator.crysl.templates.TestCryslTaskInt;
2+
3+
import de.cognicrypt.codegenerator.crysl.CrySLCodeGenerator;
4+
5+
public class TestCryslTask {
6+
7+
public static int sampleTemplateMethod(){
8+
int calories = 900;
9+
int amountOfCoffee = 10;
10+
11+
CrySLCodeGenerator.getInstance().
12+
includeClass("de.coffee.api.Sugar").addParameter(calories, "calories").
13+
includeClass("de.test.api.Coffee").addParameter(amountOfCoffee,"amountOfCoffee").generate();
14+
int returnVal = 10;
15+
return returnVal;
16+
}
17+
18+
}
19+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package de.cognicrypt.codegenerator.crysl.templates.TestCryslTaskString;
2+
3+
import de.cognicrypt.codegenerator.crysl.CrySLCodeGenerator;
4+
5+
public class TestCryslTask {
6+
7+
public static java.lang.String sampleTemplateMethod(){
8+
int calories = 900;
9+
int amountOfCoffee = 10;
10+
11+
CrySLCodeGenerator.getInstance().
12+
includeClass("de.coffee.api.Sugar").addParameter(calories, "calories").
13+
includeClass("de.test.api.Coffee").addParameter(amountOfCoffee,"amountOfCoffee").generate();
14+
java.lang.String returnVal = "return";
15+
return returnVal;
16+
}
17+
18+
}

plugins/de.cognicrypt.codegenerator/src/main/java/de/cognicrypt/codegenerator/generator/CodeGenerator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,9 @@ protected void cleanUpProject(IEditorPart editor) throws CoreException {
345345
UIUtils.closeEditor(editor);
346346
}
347347

348-
final ICompilationUnit openClass = JavaCore.createCompilationUnitFrom(UIUtils.getCurrentlyOpenFile(editor));
349-
organizeImportsActionForAllFilesTouchedDuringGeneration.run(openClass);
350-
faa.runOnMultiple(new ICompilationUnit[] { openClass });
348+
//final ICompilationUnit openClass = JavaCore.createCompilationUnitFrom(UIUtils.getCurrentlyOpenFile(editor));
349+
//organizeImportsActionForAllFilesTouchedDuringGeneration.run(openClass);
350+
//faa.runOnMultiple(new ICompilationUnit[] { openClass });
351351
editor.doSave(null);
352352
}
353353

plugins/de.cognicrypt.codegenerator/src/main/java/de/cognicrypt/codegenerator/generator/CrySLBasedCodeGenerator.java

Lines changed: 60 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.stream.Stream;
3434

3535
import org.eclipse.core.resources.IResource;
36+
import de.cognicrypt.staticanalyzer.sootbridge.SootRunner;
3637
import org.eclipse.core.runtime.CoreException;
3738
import org.eclipse.jdt.core.ICompilationUnit;
3839
import org.eclipse.jdt.core.JavaCore;
@@ -77,6 +78,7 @@
7778
import de.cognicrypt.core.Constants;
7879
import de.cognicrypt.utils.CrySLUtils;
7980
import de.cognicrypt.utils.Utils;
81+
import soot.SootResolver;
8082

8183
/**
8284
*
@@ -184,8 +186,8 @@ public boolean generateCodeTemplates(Configuration chosenConfig, String pathToFo
184186
}
185187
for (CrySLPredicate reqPred : nextRule.getRequiredPredicates()) {
186188
if (reqPred.equals(ensPred) && Utils.isSubType(((CrySLObject) reqPred.getParameters().get(0)).getJavaType(), predType)) {
187-
Optional<Entry<CrySLPredicate, Entry<CrySLRule, CrySLRule>>> matchedPred = predicateConnections.stream()
188-
.filter(e -> e.getKey().equals(ensPred)).findFirst();
189+
Optional<Entry<CrySLPredicate, Entry<CrySLRule, CrySLRule>>> matchedPred = predicateConnections.stream().filter(e -> e.getKey().equals(ensPred))
190+
.findFirst();
189191
if (now && matchedPred.isPresent()) {
190192
int newParNumber = getParameterNumber(curRule, (CrySLObject) ensPred.getParameters().get(0));
191193
Entry<CrySLPredicate, Entry<CrySLRule, CrySLRule>> entry = matchedPred.get();
@@ -231,9 +233,11 @@ public boolean generateCodeTemplates(Configuration chosenConfig, String pathToFo
231233
List<CrySLPredicate> candidates = new ArrayList<CrySLPredicate>();
232234
for (CrySLPredicate reqPred : rule.getPredicates()) {
233235
if (!(reqPred instanceof CrySLCondPredicate)) {
234-
String parType = ((CrySLObject) reqPred.getParameters().get(0)).getJavaType();
235-
if (Utils.isSubType(parType, reqReturnObject.getJavaType()) || Utils.isSubType(reqReturnObject.getJavaType(), parType)) {
236-
candidates.add(reqPred);
236+
if (reqPred.getParameters().size() > 0) {
237+
String parType = ((CrySLObject) reqPred.getParameters().get(0)).getJavaType();
238+
if (Utils.isSubType(parType, reqReturnObject.getJavaType()) || Utils.isSubType(reqReturnObject.getJavaType(), parType)) {
239+
candidates.add(reqPred);
240+
}
237241
}
238242
}
239243
}
@@ -257,18 +261,22 @@ public boolean generateCodeTemplates(Configuration chosenConfig, String pathToFo
257261
if (toBeEnsuredPred == null) {
258262
for (CrySLPredicate reqPred : rule.getPredicates()) {
259263
CrySLObject a = ((CodeGenCrySLRule) rule).getRequiredRetObj();
260-
boolean baseMatch = Utils.isSubType(((CrySLObject) reqPred.getParameters().get(0)).getJavaType(), a.getJavaType());
261-
if (!baseMatch) {
262-
continue;
263-
}
264-
if (toBeEnsuredPred != null) {
265-
Optional<ICrySLPredicateParameter> opt = reqPred.getParameters().subList(1, reqPred.getParameters().size()).stream().filter(e -> rule.getRequiredPars().parallelStream().anyMatch(f -> f.getCrySLVariable().equals(((CrySLObject)e).getVarName()))).findFirst();
266-
if (!opt.isPresent()) {
264+
if (reqPred.getParameters().size() > 0) {
265+
boolean baseMatch = Utils.isSubType(((CrySLObject) reqPred.getParameters().get(0)).getJavaType(), a.getJavaType());
266+
if (!baseMatch) {
267267
continue;
268268
}
269+
if (toBeEnsuredPred != null) {
270+
Optional<ICrySLPredicateParameter> opt = reqPred.getParameters().subList(1, reqPred.getParameters().size()).stream()
271+
.filter(e -> rule.getRequiredPars().parallelStream().anyMatch(f -> f.getCrySLVariable().equals(((CrySLObject) e).getVarName())))
272+
.findFirst();
273+
if (!opt.isPresent()) {
274+
continue;
275+
}
276+
}
277+
toBeEnsuredPred = new SimpleEntry<CrySLPredicate, Entry<CrySLRule, CrySLRule>>(reqPred, new SimpleEntry<CrySLRule, CrySLRule>(rule, null));
278+
// break;
269279
}
270-
toBeEnsuredPred = new SimpleEntry<CrySLPredicate, Entry<CrySLRule, CrySLRule>>(reqPred, new SimpleEntry<CrySLRule, CrySLRule>(rule, null));
271-
// break;
272280
}
273281
}
274282
} else {
@@ -430,7 +438,8 @@ private void generateTemplateUsageBody(Set<GeneratorClass> generatedClasses, Gen
430438
if (!declaredVariables.contains(par)) {
431439
ArrayList<Entry<String, String>> redundantVarList = new ArrayList<>(declaredVariables);
432440
Collections.reverse(redundantVarList);
433-
Optional<Entry<String, String>> o = redundantVarList.parallelStream().filter(e -> !usedVars.contains(e) && Utils.isSubType(e.getValue(), par.getValue())).findFirst();
441+
Optional<Entry<String, String>> o = redundantVarList.parallelStream().filter(e -> !usedVars.contains(e) && Utils.isSubType(e.getValue(), par.getValue()))
442+
.findFirst();
434443
if (o.isPresent()) {
435444
tmplUsage.addStatementToBody(o.get().getKey() + ", ");
436445
usedVars.add(o.get());
@@ -488,7 +497,10 @@ private ArrayList<String> generateMethodInvocations(CodeGenCrySLRule rule, Gener
488497
boolean ensures = false;
489498

490499
List<Entry<String, String>> useMethodParameters = new ArrayList<Entry<String, String>>();
491-
Entry<CrySLPredicate, Entry<CrySLRule, CrySLRule>> pre = new SimpleEntry<>(toBeEnsuredPred.getKey(), toBeEnsuredPred.getValue());
500+
Entry<CrySLPredicate, Entry<CrySLRule, CrySLRule>> pre = null;
501+
if(toBeEnsuredPred != null) {
502+
pre = new SimpleEntry<>(toBeEnsuredPred.getKey(), toBeEnsuredPred.getValue());
503+
}
492504
for (TransitionEdge transition : currentTransitions) {
493505
CrySLMethod method = null;
494506
Set<String> reqMethodNames = new HashSet<>();
@@ -788,13 +800,13 @@ private Entry<String, List<Entry<String, String>>> replaceParameterByValue(CodeG
788800
List<Entry<String, String>> parametersOfUseMethod = new ArrayList<Entry<String, String>>();
789801
List<Entry<String, String>> declaredVariables = useMethod.getDeclaredVariables();
790802
List<CodeGenCrySLObject> reqPars = rule.getRequiredPars();
791-
803+
792804
List<Entry<String, String>> usedVars = new ArrayList<Entry<String, String>>();
793805
reqPars.stream().forEach(e -> usedVars.add(new SimpleEntry<>(e.getVarName(), e.getJavaType())));
794806
if (rule.getRequiredRetObj() != null) {
795807
usedVars.add(new SimpleEntry<String, String>(rule.getRequiredRetObj().getVarName(), rule.getRequiredRetObj().getJavaType()));
796808
}
797-
809+
798810
for (Entry<String, String> parameter : parametersOfCall) {
799811
boolean inTemplate = false;
800812

@@ -814,7 +826,7 @@ private Entry<String, List<Entry<String, String>>> replaceParameterByValue(CodeG
814826
e -> Utils.isSubType(e.getValue().getValue().getClassName(), rule.getClassName()) || Utils.isSubType(rule.getClassName(), e.getValue().getValue().getClassName()))
815827
.findFirst();
816828
if (entry.isPresent()) {
817-
829+
818830
final CrySLObject crySLObject = (CrySLObject) entry.get().getKey().getParameters().get(0);
819831
if (!"this".equals(crySLObject.getVarName())) {
820832
if (usedVars.contains(new SimpleEntry<String, String>(crySLObject.getVarName(), crySLObject.getJavaType())) && (Utils.isSubType(crySLObject.getJavaType(),
@@ -825,9 +837,9 @@ private Entry<String, List<Entry<String, String>>> replaceParameterByValue(CodeG
825837
}
826838
}
827839

828-
List<Entry<String, String>> tmpVariables = new ArrayList<>();
840+
List<Entry<String, String>> tmpVariables = new ArrayList<>();
829841
declaredVariables.stream().filter(e -> !usedVars.contains(e)).filter(e -> !useMethod.getPostCGVars().contains(e)).forEach(e -> tmpVariables.add(e));
830-
Collections.reverse(tmpVariables);
842+
Collections.reverse(tmpVariables);
831843

832844
Optional<Entry<String, String>> typeMatch = tmpVariables.stream()
833845
.filter(e -> (Utils.isSubType(e.getValue(), parameter.getValue()) || Utils.isSubType(parameter.getValue(), e.getValue()))).findFirst();
@@ -858,7 +870,7 @@ private Entry<String, List<Entry<String, String>>> replaceParameterByValue(CodeG
858870

859871
currentInvokedMethod = methodNamdResultAssignment + methodParameter + appendix;
860872
return new SimpleEntry<>(currentInvokedMethod, parametersOfUseMethod);
861-
873+
862874
}
863875

864876
/**
@@ -942,18 +954,21 @@ private String resolveCrySLConstraint(Entry<String, String> parameter, ISLConstr
942954
case ge:
943955
try {
944956
secureInt = String.valueOf(SecureRandom.getInstance("SHA1PRNG").nextInt(2 * value) + value);
945-
} catch (NoSuchAlgorithmException e1) {}
957+
} catch (NoSuchAlgorithmException e1) {
958+
}
946959
break;
947960
case l:
948961
case le:
949962
try {
950963
secureInt = String.valueOf(SecureRandom.getInstance("SHA1PRNG").nextInt(value));
951-
} catch (NoSuchAlgorithmException e) {}
964+
} catch (NoSuchAlgorithmException e) {
965+
}
952966
break;
953967
case neq:
954968
try {
955969
secureInt = String.valueOf(SecureRandom.getInstance("SHA1PRNG").nextInt(value - 1));
956-
} catch (NoSuchAlgorithmException e) {}
970+
} catch (NoSuchAlgorithmException e) {
971+
}
957972
break;
958973
case eq:
959974
default:
@@ -1163,6 +1178,7 @@ private void determineThrownExceptions(String className, String methodName, Clas
11631178
}
11641179
}
11651180

1181+
11661182
private boolean matchMethodParameters(Class<?>[] methodParameters, Class<?>[] classes) {
11671183
for (int i = 0; i < methodParameters.length; i++) {
11681184
if (methodParameters[i].getName().equals("AnyType")) {
@@ -1214,7 +1230,7 @@ public GeneratorClass setUpTemplateClass(String pathToTemplateFile) {
12141230
List<CodeGenCrySLObject> pars = new ArrayList<>();
12151231
Map<SimpleName, CrySLObject> preCGVars = new HashMap<SimpleName, CrySLObject>();
12161232
Map<SimpleName, CrySLObject> postCGVars = new HashMap<SimpleName, CrySLObject>();
1217-
1233+
12181234
List<CodeGenCrySLRule> rules = new ArrayList<CodeGenCrySLRule>();
12191235

12201236
@SuppressWarnings({ "unchecked", "rawtypes" })
@@ -1240,25 +1256,27 @@ public boolean visit(MethodInvocation node) {
12401256
}
12411257
} else if ("includeClass".equals(calledMethodName)) {
12421258
String rule = Utils.filterQuotes(arguments.get(0).toString());
1259+
List<CrySLRule> rulesFromSootRunner = SootRunner.getRules(getDeveloperProject().project);
12431260
String simpleRuleName = rule.substring(rule.lastIndexOf(".") + 1);
1244-
try {
1245-
CrySLRule crySLRule = CrySLUtils.getCrySLRule(simpleRuleName);
1246-
for (CodeGenCrySLObject o : pars) {
1247-
for (TransitionEdge edge : crySLRule.getUsagePattern().getEdges()) {
1248-
for (CrySLMethod method : edge.getLabel()) {
1249-
List<Entry<String, String>> parameters = method.getParameters();
1250-
for (int i = 0; i < parameters.size(); i++) {
1251-
if (parameters.get(i).getKey().equals(o.getCrySLVariable())) {
1252-
o.setMethod(method.getShortMethodName(), i);
1253-
}
1261+
CrySLRule crySLRule = null;//CrySLUtils.getCrySLRule(simpleRuleName);
1262+
for (CrySLRule crySLRuleFromSootRunner : rulesFromSootRunner) {
1263+
if(crySLRuleFromSootRunner.getClassName().contains(simpleRuleName)) {
1264+
crySLRule = crySLRuleFromSootRunner;
1265+
}
1266+
}
1267+
for (CodeGenCrySLObject o : pars) {
1268+
for (TransitionEdge edge : crySLRule.getUsagePattern().getEdges()) {
1269+
for (CrySLMethod method : edge.getLabel()) {
1270+
List<Entry<String, String>> parameters = method.getParameters();
1271+
for (int i = 0; i < parameters.size(); i++) {
1272+
if (parameters.get(i).getKey().equals(o.getCrySLVariable())) {
1273+
o.setMethod(method.getShortMethodName(), i);
12541274
}
12551275
}
12561276
}
12571277
}
1258-
rules.add(new CodeGenCrySLRule(crySLRule, pars, retObj));
1259-
} catch (MalformedURLException e) {
1260-
Activator.getDefault().logError(e);
12611278
}
1279+
rules.add(new CodeGenCrySLRule(crySLRule, pars, retObj));
12621280
retObj = null;
12631281
pars = new ArrayList<>();
12641282

@@ -1284,13 +1302,13 @@ public boolean visit(VariableDeclarationStatement node) {
12841302
int codeGenStmt = -1;
12851303
for (Object s : ((Block) node.getParent()).statements()) {
12861304
Statement statement = (Statement) s;
1287-
if (s instanceof ExpressionStatement && ((ExpressionStatement) s).toString().contains("CrySLCodeGenerator") ) {
1305+
if (s instanceof ExpressionStatement && ((ExpressionStatement) s).toString().contains("CrySLCodeGenerator")) {
12881306
codeGenStmt = statement.getStartPosition();
12891307
break;
1290-
}
1308+
}
12911309
}
12921310

1293-
if(node.getStartPosition() < codeGenStmt) {
1311+
if (node.getStartPosition() < codeGenStmt) {
12941312
preCGVars.put(varName, new CrySLObject(varName.getFullyQualifiedName(), ((VariableDeclarationStatement) node).getType().toString()));
12951313
} else {
12961314
postCGVars.put(varName, new CrySLObject(varName.getFullyQualifiedName(), ((VariableDeclarationStatement) node).getType().toString()));

0 commit comments

Comments
 (0)