@@ -5,105 +5,99 @@ import Weaver from "@specs-feup/lara/api/weaver/Weaver.js";
55import { arrayFromArgs } from "@specs-feup/lara/api/lara/core/LaraCore.js" ;
66
77function saveFile ( ) {
8- const outputFolder = Io . mkdir ( "./mutatedFilesTest/" ) ;
9- Io . deleteFolderContents ( outputFolder ) ;
8+ const outputFolder = Io . mkdir ( "./mutatedFilesTest/" ) ;
9+ Io . deleteFolderContents ( outputFolder ) ;
1010
11- // Write modified code
12- Weaver . writeCode ( outputFolder ) ;
11+ // Write modified code
12+ Weaver . writeCode ( outputFolder ) ;
1313
14- // Print contents
15- for ( const mutatedFile of Io . getFiles ( outputFolder , "*.java" ) ) {
16- console . log ( "<File '" + mutatedFile . getName ( ) + "'>" ) ;
17- console . log ( Io . readFile ( mutatedFile ) ) ;
18- }
14+ // Print contents
15+ for ( const mutatedFile of Io . getFiles ( outputFolder , "*.java" ) ) {
16+ console . log ( "<File '" + mutatedFile . getName ( ) + "'>" ) ;
17+ console . log ( Io . readFile ( mutatedFile ) ) ;
18+ }
1919
20- Io . deleteFolder ( outputFolder ) ;
20+ Io . deleteFolder ( outputFolder ) ;
2121}
2222
2323/**
2424 * @param {$joinPoint } $joinPoint - A join point to use as startpoint to search for constructor calls to replace with null.
2525 */
2626class ConstructorCallMutator extends Mutator {
27- constructor ( $joinPoint ) {
28- super ( ) ;
29-
30- if ( $joinPoint === undefined ) {
31- $joinPoint = Query . root ( ) ;
32- }
33-
34- // Instance variables
35- this . $joinPoint = $joinPoint ;
36- this . extraArgs = arrayFromArgs ( arguments , 1 ) ;
37-
38- this . toMutate = [ ] ;
39- this . totalMutations = - 1 ;
40- this . currentIndex = 0 ;
41-
42- this . $referenceParent = undefined ;
43- this . $originalParent = undefined ;
44-
45- // Checks
46- if ( this . extraArgs . length != 0 )
47- throw new Error (
48- "Expected only 1 argument but received " +
49- ( this . extraArgs . length + 1 )
50- ) ;
51-
52- for ( const $ref of Query . searchFrom ( this . $joinpoint , "reference" )
53- . get ( )
54- . reverse ( ) ) {
55- // Check it is a constructor call reference
56- if (
57- $ref . name === "<init>" &&
58- $ref . type === "Executable" &&
59- $ref . parent . srcCode !== "super()"
60- )
61- this . toMutate . push ( $ref ) ;
62- }
63-
64- this . totalMutations = this . toMutate . length ;
65- if ( this . totalMutations == 0 )
66- console . log ( "Found no suitable code to mutate" ) ;
67- }
27+ constructor ( $joinPoint ) {
28+ super ( ) ;
6829
69- hasMutations ( ) {
70- return this . currentIndex < this . totalMutations ;
30+ if ( $joinPoint === undefined ) {
31+ $joinPoint = Query . root ( ) ;
7132 }
7233
73- mutatePrivate ( ) {
74- this . $referenceParent = this . toMutate [ this . currentIndex ++ ] . parent ;
75-
76- this . $originalParent = this . $referenceParent . copy ( ) ;
77- this . $referenceParent = this . $referenceParent . insertReplace ( "null" ) ;
78-
79- console . log ( "/*--------------------------------------*/" ) ;
80- console . log (
81- "Mutating operator n." +
82- this . currentIndex +
83- ": " +
84- this . $originalParent +
85- " to " +
86- this . $referenceParent
87- ) ;
88- console . log ( "/*--------------------------------------*/" ) ;
34+ // Instance variables
35+ this . $joinPoint = $joinPoint ;
36+ this . extraArgs = arrayFromArgs ( arguments , 1 ) ;
37+
38+ this . toMutate = [ ] ;
39+ this . totalMutations = - 1 ;
40+ this . currentIndex = 0 ;
41+
42+ this . $reference = undefined ;
43+ this . $original = undefined ;
44+
45+ // Checks
46+ if ( this . extraArgs . length != 0 )
47+ throw new Error (
48+ "Expected only 1 argument but received " + ( this . extraArgs . length + 1 )
49+ ) ;
50+
51+ for ( const $ref of Query . searchFrom ( this . $joinpoint , "new" )
52+ . get ( )
53+ . reverse ( ) ) {
54+ // TODO: Not sure if this check is still needed
55+ if ( $ref . name === "<init>" && $ref . parent . srcCode !== "super()" ) {
56+ this . toMutate . push ( $ref ) ;
57+ }
8958 }
9059
91- restorePrivate ( ) {
92- this . $referenceParent = this . $referenceParent . insertReplace (
93- this . $originalParent
94- ) ;
95- this . $originalParent = undefined ;
96- this . $referenceParent = undefined ;
97- }
60+ this . totalMutations = this . toMutate . length ;
61+ if ( this . totalMutations == 0 )
62+ console . log ( "Found no suitable code to mutate" ) ;
63+ }
64+
65+ hasMutations ( ) {
66+ return this . currentIndex < this . totalMutations ;
67+ }
68+
69+ mutatePrivate ( ) {
70+ this . $reference = this . toMutate [ this . currentIndex ++ ] ;
71+
72+ this . $original = this . $reference . copy ( ) ;
73+ this . $reference = this . $reference . insertReplace ( "null" ) ;
74+
75+ console . log ( "/*--------------------------------------*/" ) ;
76+ console . log (
77+ "Mutating operator n." +
78+ this . currentIndex +
79+ ": " +
80+ this . $original +
81+ " to " +
82+ this . $reference
83+ ) ;
84+ console . log ( "/*--------------------------------------*/" ) ;
85+ }
86+
87+ restorePrivate ( ) {
88+ this . $reference = this . $reference . insertReplace ( this . $original ) ;
89+ this . $original = undefined ;
90+ this . $reference = undefined ;
91+ }
9892}
9993
10094const mutator = new ConstructorCallMutator ( Query . root ( ) ) ;
10195
10296while ( mutator . hasMutations ( ) ) {
103- // Mutate
104- mutator . mutate ( ) ;
97+ // Mutate
98+ mutator . mutate ( ) ;
10599
106- saveFile ( ) ;
107- // Restore operator
108- mutator . restore ( ) ;
100+ saveFile ( ) ;
101+ // Restore operator
102+ mutator . restore ( ) ;
109103}
0 commit comments