3333import java .util .stream .Stream ;
3434
3535import org .eclipse .core .resources .IResource ;
36+ import de .cognicrypt .staticanalyzer .sootbridge .SootRunner ;
3637import org .eclipse .core .runtime .CoreException ;
3738import org .eclipse .jdt .core .ICompilationUnit ;
3839import org .eclipse .jdt .core .JavaCore ;
7778import de .cognicrypt .core .Constants ;
7879import de .cognicrypt .utils .CrySLUtils ;
7980import 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