-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathspoon_smpl.html
1404 lines (821 loc) · 43.1 KB
/
spoon_smpl.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="keywords" content="semantic-patching, patch, patching, semantic, smpl, spoon">
<title>Semantic patching | Spoon</title>
<link rel="stylesheet" type="text/css" href="css/syntax.css">
<link rel="stylesheet" type="text/css" href="css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/modern-business.css">
<link rel="stylesheet" type="text/css" href="css/lavish-bootstrap.css">
<link rel="canonical" href="/spoon_smpl.html">
<link rel="stylesheet" type="text/css" href="css/customstyles.css">
<link rel="stylesheet" type="text/css" href="css/theme-mauve.css">
<link rel="canonical" href="/spoon_smpl.html">
<link rel="alternate" type="application/rss+xml" title="" href="feed.xml" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script src="js/jquery.navgoco.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script src="js/toc.js"></script>
<script src="js/customscripts.js"></script>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
<script>
$(function () {
$('[data-toggle="tooltip"]').tooltip()
})
</script>
</head>
<body>
<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container topnavlinks">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="fa fa-home fa-lg navbar-brand" href="index.html"> <span class="projectTitle"> Spoon</span></a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<!-- entries without drop-downs appear here -->
<!-- conditional logic to control which topnav appears for the audience defined in the configuration file.-->
<li><a href="papers.html">Papers</a></li>
<li><a href="ecosystem.html">Ecosystem</a></li>
<li><a href="about.html">About</a></li>
<li><a href="https://ci.inria.fr/sos/" target="_blank">Jenkins</a></li>
<li><a href="https://github.com/INRIA/spoon" target="_blank">Github</a></li>
<!-- entries with drop-downs appear here -->
<!-- conditional logic to control which topnav appears for the audience defined in the configuration file.-->
<li class="dropdown">
</div>
<!-- /.container -->
</nav>
<!-- Page Content -->
<div class="container">
<div class="col-lg-12"> </div>
<!-- Content Row -->
<div class="row">
<!-- Sidebar Column -->
<div class="col-md-3">
<script>
$(document).ready(function() {
// Initialize navgoco with default options
$("#mysidebar").navgoco({
caretHtml: '',
accordion: true,
openClass: 'active', // open
save: false, // leave false or nav highlighting doesn't work right
cookie: {
name: 'navgoco',
expires: false,
path: '/'
},
slide: {
duration: 400,
easing: 'swing'
}
});
$("#collapseAll").click(function(e) {
e.preventDefault();
$("#mysidebar").navgoco('toggle', false);
});
$("#expandAll").click(function(e) {
e.preventDefault();
$("#mysidebar").navgoco('toggle', true);
});
});
</script>
<ul id="mysidebar" class="nav">
<div class="siteTagline">Spoon</div>
<li><a href="#">Getting started</a>
<ul>
<li><a href="first_analysis_processor.html">Code Analysis</a></li>
</li>
<li><a href="architecture_test.html">Architecture Checking</a></li>
</li>
<li><a href="reflection.html">Runtime Reflection</a></li>
</li>
<li><a href="custom-pretty-printing.html">Custom Java Pretty-Printing</a></li>
</li>
<li><a href="processor.html">Processor for elements</a></li>
</li>
<li><a href="/mvnsites/spoon-core/apidocs" target="_blank">Javadoc</a></li>
</li>
<li><a href="spoon_javadoc.html">Javadoc Analysis</a></li>
</li>
<li><a href="faq.html">FAQ</a></li>
</li>
</ul>
</li>
<li><a href="#">Querying source code elements</a>
<ul>
<li><a href="filter.html">AST Traversing</a></li>
</li>
<li><a href="path.html">AST Paths and Roles</a></li>
</li>
<li><a href="matcher.html">Matcher</a></li>
</li>
<li><a href="pattern.html">Pattern</a></li>
</li>
</ul>
</li>
<li><a href="#">Code Transformation</a>
<ul>
<li><a href="first_transformation.html">Code Transformation</a></li>
</li>
<li><a href="examples.html">Transformation Examples</a></li>
</li>
<li><a href="processor_annotations.html">Transformation with annotations</a></li>
</li>
<li><a href="template_definition.html">Transformation with Templates</a></li>
</li>
<li class="active"><a href="spoon_smpl.html">Semantic patching</a></li>
</li>
<li><a href="pattern.html#generator">Code Generation</a></li>
</li>
<li><a href="testing_quickstart.html">Testing transformations</a></li>
</li>
</ul>
</li>
<li><a href="#">Usage</a>
<ul>
<li><a href="launcher.html">From Java</a></li>
</li>
<li><a href="command_line.html">Command Line</a></li>
</li>
<li><a href="maven.html">From Maven Plugin</a></li>
</li>
<li><a href="gradle.html">From Gradle Plugin</a></li>
</li>
</ul>
</li>
<li><a href="#">Spoon Meta model</a>
<ul>
<li><a href="structural_elements.html">Structural elements</a></li>
</li>
<li><a href="code_elements.html">Code elements</a></li>
</li>
<li><a href="references.html">References</a></li>
</li>
<li><a href="factories.html">Create elements with factories</a></li>
</li>
<li><a href="comments.html">Comments and Positions</a></li>
</li>
</ul>
</li>
</ul>
</div>
<!-- this highlights the active parent class in the navgoco sidebar. this is critical so that the parent expands when you're viewing a page. This must appear below the sidebar code above.-->
<script>$("li.active").parents('li').toggleClass("active");</script>
<!-- Content Column -->
<div class="col-md-9">
<div class="post-header">
<h1 class="post-title-main">Semantic patching</h1>
</div>
<div class="post-content">
<p>The <a href="https://github.com/INRIA/spoon/tree/master/spoon-smpl">spoon-smpl</a> submodule provides a
prototype implementation of a subset of <a href="http://coccinelle.lip6.fr/">SmPL</a> (Semantic Patch Language)
for a subset of Java. SmPL patches can be thought of as traditional plain text patches with enhanced
expressiveness thanks to support for the syntax and semantics of specific programming languages. For
example, a Java SmPL patch can be written to specify a generic transformation that reorders of a series
of method call arguments without having to worry about matching any specific literal variable names
or other literal argument expressions.</p>
<h3 id="installation">Installation</h3>
<p>On a Unix-like system, the following set of commands should be sufficient for getting spoon-smpl up
and running from scratch.</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ git clone -b smpl https://github.com/INRIA/spoon.git
$ cd spoon/spoon-smpl
$ mvn package
$ ./tools/smplcli.sh
usage:
smplcli ACTION [ARG [ARG ..]]
ACTIONs:
patch apply SmPL patch
requires --smpl-file and --java-file
check run model checker
requires --smpl-file and --java-file
checksub run model checker on every subformula
requires --smpl-file and --java-file
rewrite rewrite SmPL input
requires --smpl-file
compile compile SmPL input
requires --smpl-file
ctl compile and print CTL formula
requires --smpl-file
ARGs:
--smpl-file FILENAME
--java-file FILENAME
</code></pre></div></div>
<p>Alternatively, the command line application can be invoked directly as:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ java -cp <classpath> spoon.smpl.CommandlineApplication
</code></pre></div></div>
<h3 id="basic-usage">Basic usage</h3>
<p>The basic use case of spoon-smpl involves at minimum two files: one .java source file and one
semantic patch. For this tutorial, we will use the following two files:</p>
<h4 id="file-1-example-semantic-patch-patchsmpl">File 1: example semantic patch (patch.smpl)</h4>
<div class="language-patch highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="p">@@
type T;
identifier ret;
constant C;
@@</span>
<span class="gd">- T ret = C;
</span>... when != ret
<span class="gd">- return ret;
</span><span class="gi">+ return C;
</span></code></pre></div></div>
<p>This example patch removes local variables only used to return a constant.</p>
<h4 id="file-2-example-java-source-programjava">File 2: example Java source (Program.java)</h4>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Program</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">fn1</span><span class="o">()</span> <span class="o">{</span>
<span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span>
<span class="k">return</span> <span class="n">x</span><span class="o">;</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">fn2</span><span class="o">(</span><span class="kt">boolean</span> <span class="n">print</span><span class="o">)</span> <span class="o">{</span>
<span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">2</span><span class="o">;</span>
<span class="k">if</span> <span class="o">(</span><span class="n">print</span><span class="o">)</span> <span class="o">{</span>
<span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">"hello from fn2"</span><span class="o">);</span>
<span class="o">}</span>
<span class="k">return</span> <span class="n">x</span><span class="o">;</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">fn3</span><span class="o">(</span><span class="kt">boolean</span> <span class="n">print</span><span class="o">)</span> <span class="o">{</span>
<span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">3</span><span class="o">;</span>
<span class="k">if</span> <span class="o">(</span><span class="n">print</span><span class="o">)</span> <span class="o">{</span>
<span class="nc">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">x</span><span class="o">);</span>
<span class="o">}</span>
<span class="k">return</span> <span class="n">x</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p>We then apply the semantic patch to the Java source code as follows (output also shown):</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ ./tools/smplcli.sh patch --smpl-file patch.smpl --java-file Program.java
public class Program {
public int fn1() {
return 1;
}
public int fn2(boolean print) {
if (print) {
java.lang.System.out.println("hello from fn2");
}
return 2;
}
public int fn3(boolean print) {
int x = 3;
if (print) {
java.lang.System.out.println(x);
}
return x;
}
}
</code></pre></div></div>
<h3 id="graphical-interface">Graphical interface</h3>
<p>There is a very simple graphical interface available in <code class="language-plaintext highlighter-rouge">tools/smplgui.py</code>. This tool requires
Python 3 and a suitable <code class="language-plaintext highlighter-rouge">python3-pyqt5</code> package providing Qt5 bindings, in particular the module
<code class="language-plaintext highlighter-rouge">PyQt5.QtGui</code>. Furthermore, the tool currently assumes it is executing on a Unix-like system from
a working directory in which the file <code class="language-plaintext highlighter-rouge">./tools/smplcli.sh</code> is available to run spoon-smpl. As such,
it is recommended to start the tool from the spoon-smpl root folder using the command <code class="language-plaintext highlighter-rouge">$ ./tools/smplgui.py</code>.</p>
<p><img src="/images/smplgui.png" alt="Spoon-smpl graphical interface" /></p>
<p>The tool provides two panes for editing the semantic patch and some Java source code, respectively.
The upper left pane contains the semantic patch, while the upper right pane contains the Java source
code. Finally, the tool provides a number of modes for invoking spoon-smpl using the inputs shown in
the two panes. To change mode one presses the F6 key followed by the key corresponding to the
desired mode, as shown in the image below. To execute the currently selected mode, one presses the
F5 key.</p>
<p><img src="/images/smplgui-modes.png" alt="Spoon-smpl graphical interface modes" /></p>
<p>These modes correspond to the <code class="language-plaintext highlighter-rouge">ACTION</code> alternatives present in <code class="language-plaintext highlighter-rouge">spoon.smpl.CommandLineApplication</code>,
with the addition of the <code class="language-plaintext highlighter-rouge">gentest</code> mode which generates a test case in a special format for the
inputs present in the two upper panes.</p>
<h3 id="batch-processing">Batch processing</h3>
<p>Spoon-smpl provides a batch processing mode in which a single semantic patch is applied to a full
source tree recursively. This mode is implemented in the form of a Spoon <code class="language-plaintext highlighter-rouge">Processor</code> that also
features a <code class="language-plaintext highlighter-rouge">main</code> method. The following example command is intended to be executed in the spoon-smpl
root directory, where a call to <code class="language-plaintext highlighter-rouge">mvn package</code> has placed a full suite of <code class="language-plaintext highlighter-rouge">.jar</code> files in the
<code class="language-plaintext highlighter-rouge">./target</code> sub-directory.</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ java -cp $(for f in target/*.jar; do echo -n $f:; done) spoon.smpl.SmPLProcessor \
--with-diff-command "bash -c \"diff -U5 -u {a} {b}\"" \
--with-smpl-file "path/to/patch.smpl" \
## The following options are passed to spoon.Launcher, more may be added
-i "path/to/target/source" \
-o "path/to/write/output" \
-p spoon.smpl.SmPLProcessor
</code></pre></div></div>
<p>The expression <code class="language-plaintext highlighter-rouge">-cp $(for f in target/*.jar; do echo -n $f:; done)</code> collects and places on the
classpatch all <code class="language-plaintext highlighter-rouge">.jar</code> files found in the <code class="language-plaintext highlighter-rouge">target</code> sub-directory.</p>
<p>The <code class="language-plaintext highlighter-rouge">--with-diff-command</code> option expects a shell-executable command string containing the
placeholder expressions <code class="language-plaintext highlighter-rouge">{a}</code> and <code class="language-plaintext highlighter-rouge">{b}</code>. The placeholders are substituted for the full paths to the
pretty-printed input and the pretty-printed output respectively, for each modified file in the
source tree. For example, in the event that spoon-smpl during batch processing has modified a file
<code class="language-plaintext highlighter-rouge">Program.java</code>, the option used in the example command would result in a command akin to the
following being executed:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>bash -c "diff -U5 -u /tmp/9qKMH/Program.java /tmp/CYd40/Program.java"
</code></pre></div></div>
<h3 id="developing">Developing</h3>
<p>The following code shows the core workflow of spoon-smpl, and is intended to guide developers
towards finding the code for the component(s) of interest:</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kt">boolean</span> <span class="nf">tryApplyPatch</span><span class="o">(</span><span class="nc">String</span> <span class="n">plainTextSmPLCode</span><span class="o">,</span> <span class="nc">CtExecutable</span> <span class="n">patchTargetExe</span><span class="o">)</span> <span class="o">{</span>
<span class="c1">// Parse a plain text SmPL patch</span>
<span class="nc">SmPLRule</span> <span class="n">rule</span> <span class="o">=</span> <span class="nc">SmPLParser</span><span class="o">.</span><span class="na">parse</span><span class="o">(</span><span class="n">plainTextSmPLCode</span><span class="o">);</span>
<span class="c1">// Create the CFG from the executable block</span>
<span class="nc">SmPLMethodCFG</span> <span class="n">cfg</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">SmPLMethodCFG</span><span class="o">(</span><span class="n">patchTargetExe</span><span class="o">);</span>
<span class="c1">// Create the CTL model from the CFG</span>
<span class="nc">CFGModel</span> <span class="n">model</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">CFGModel</span><span class="o">(</span><span class="n">cfg</span><span class="o">);</span>
<span class="c1">// Create the model checker</span>
<span class="nc">ModelChecker</span> <span class="n">checker</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">ModelChecker</span><span class="o">(</span><span class="n">model</span><span class="o">);</span>
<span class="c1">// Run the model checker on the formula that encodes the SmPL patch</span>
<span class="c1">// This uses the visitor pattern</span>
<span class="c1">// We ask the formula tree to accept the model checker visitor</span>
<span class="n">rule</span><span class="o">.</span><span class="na">getFormula</span><span class="o">().</span><span class="na">accept</span><span class="o">(</span><span class="n">checker</span><span class="o">);</span>
<span class="c1">// Fetch the results</span>
<span class="nc">ModelChecker</span><span class="o">.</span><span class="na">ResultSet</span> <span class="n">results</span> <span class="o">=</span> <span class="n">checker</span><span class="o">.</span><span class="na">getResult</span><span class="o">();</span>
<span class="c1">// If we get an empty result, there were no matches</span>
<span class="c1">// If we get no witnesses, there were no transformations to apply</span>
<span class="k">if</span> <span class="o">(</span><span class="n">results</span><span class="o">.</span><span class="na">isEmpty</span><span class="o">()</span> <span class="o">||</span> <span class="n">results</span><span class="o">.</span><span class="na">getAllWitnesses</span><span class="o">().</span><span class="na">isEmpty</span><span class="o">())</span> <span class="o">{</span>
<span class="c1">// Restore metamodel changes applied by SmPLMethodCFG</span>
<span class="n">model</span><span class="o">.</span><span class="na">getCfg</span><span class="o">().</span><span class="na">restoreUnsupportedElements</span><span class="o">();</span>
<span class="k">return</span> <span class="kc">false</span><span class="o">;</span>
<span class="o">}</span>
<span class="c1">// Apply transformations</span>
<span class="nc">Transformer</span><span class="o">.</span><span class="na">transform</span><span class="o">(</span><span class="n">model</span><span class="o">,</span> <span class="n">results</span><span class="o">.</span><span class="na">getAllWitnesses</span><span class="o">());</span>
<span class="c1">// Copy any new methods added by the patch</span>
<span class="k">if</span> <span class="o">(</span><span class="n">rule</span><span class="o">.</span><span class="na">getMethodsAdded</span><span class="o">().</span><span class="na">size</span><span class="o">()</span> <span class="o">></span> <span class="mi">0</span><span class="o">)</span> <span class="o">{</span>
<span class="nc">Transformer</span><span class="o">.</span><span class="na">copyAddedMethods</span><span class="o">(</span><span class="n">model</span><span class="o">,</span> <span class="n">rule</span><span class="o">);</span>
<span class="o">}</span>
<span class="c1">// Restore metamodel changes applied by SmPLMethodCFG</span>
<span class="n">model</span><span class="o">.</span><span class="na">getCfg</span><span class="o">().</span><span class="na">restoreUnsupportedElements</span><span class="o">();</span>
<span class="k">return</span> <span class="kc">true</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></div></div>
<h3 id="scope-of-implementation-as-of-may-2022">Scope of implementation as of May 2022</h3>
<h4 id="smpl-metavariables">SmPL Metavariables</h4>
<p>| Name/Description | Binds | Example |
| ———————– | —————————————— | ——————— |
| Constant | <code class="language-plaintext highlighter-rouge">CtLiteral</code> | <code class="language-plaintext highlighter-rouge">@@ constant C; @@</code> |
| Expression | <code class="language-plaintext highlighter-rouge">CtExpression</code> | <code class="language-plaintext highlighter-rouge">@@ expression E; @@</code> |
| Identifier | <code class="language-plaintext highlighter-rouge">CtVariableReference</code> | <code class="language-plaintext highlighter-rouge">@@ identifier x; @@</code> |
| Type | <code class="language-plaintext highlighter-rouge">CtTypeReference</code> | <code class="language-plaintext highlighter-rouge">@@ type T; @@</code> |
| Typed identifier | <code class="language-plaintext highlighter-rouge">CtVariableReference</code> with correct type | <code class="language-plaintext highlighter-rouge">@@ Integer x; @@</code> |
| Regex<sup id="fnref:1" role="doc-noteref"><a href="#fn:1" class="footnote" rel="footnote">1</a></sup> | Any<sup id="fnref:1:1" role="doc-noteref"><a href="#fn:1" class="footnote" rel="footnote">1</a></sup> | <code class="language-plaintext highlighter-rouge">@@ expression E when matches "regex"; @@</code> |</p>
<p>constraint (e.g identifier) and adds the additional constraint of the string representation (.toString)
of a candidate binding element having to match the given regex.</p>
<h4 id="smpl-patch-syntax">SmPL Patch syntax</h4>
<p>Referenced tests are found at https://github.com/INRIA/spoon/tree/master/spoon-smpl/src/test/resources/endtoend</p>
<table>
<tr>
<td> Description </td>
<td> Example </td>
<td> Test(s) </td>
</tr>
<tr>
<td> Prepend/append to matched context </td>
<td>
```java
+ a();
ctx(); // matched context
+ b();
```
</td>
<td> AppendToContext, AppendContextBranch, PrependToContext, PrependContextBranch </td>
</tr>
<tr>
<td> Delete all matched </td>
<td>
```java
- a();
```
</td>
<td> TypedIdentifierMetavariables1 </td>
</tr>
<tr>
<td> Delete around matched context </td>
<td>
```java
- a();
ctx();
- b();
```
</td>
<td> DeleteStmBeforeBranch, DeleteStmAfterBranch </td>
</tr>
<tr>
<td> Enclose matched context in branch </td>
<td>
```java
+ if (cond) {
ctx();
+ }
```
</td>
<td> EncloseInBranch </td>
</tr>
<tr>
<td> Delete enclosing branch </td>
<td>
```java
- if (cond) {
ctx();
- }
```
</td>
<td> DeleteEnclosingBranch </td>
</tr>
<tr>
<td> Dots (computation path wildcard) </td>
<td>
```java
int v1;
...
- v1 = C;
+ v1 = C + 1;
```
</td>
<td> BasicDots, DotsShortestPath </td>
</tr>
<tr>
<td> Dots "when any" </td>
<td>
```java
int v1;
... when any
- v1 = C;
+ v1 = C + 1;
```
</td>
<td> DotsWhenAny </td>
</tr>
<tr>
<td> Dots "when exists" </td>
<td>
```java
a();
... when exists
- c();
```
</td>
<td> Exceptions/ExistsDotsPatchingCatchBlock </td>
</tr>
<tr>
<td> Dots "when != x" </td>
<td>
```java
- a();
... when != b()
- c();
```
</td>
<td> DotsWhenNeqExpression/* </td>
</tr>
<tr>
<td> "Optional match" dots <... P ...> </td>
<td>
```java
a();
<...
- b(x);
+ log(x);
...>
- c();
```
</td>
<td> DotsWithOptionalMatch/* </td>
</tr>
<tr>
<td> Binding metavariables on method header </td>
<td>
```java
@@ type T; @@
T square(T x) {
- log(...);
}
```
</td>
<td> MatchingMethodHeader/MethodHeaderBinding </td>
</tr>
<tr>
<td> Literal matching in method header + params </td>
<td>
```java
int square(int x) {
```
</td>
<td> MatchingMethodHeader/MethodHeaderLiteralMatch </td>
</tr>
<tr>
<td> Wildcard matching in method header params </td>
<td>
```java
T fn(..., Point pt, ...) {
```
</td>
<td> MatchingMethodHeader/MethodHeaderDots </td>
</tr>
<tr>
<td> Wildcard matching in method arguments </td>
<td>
```java
- f(...);
- f(..., g(..., 1));
```
</td>
<td> MethodCallArgDots/* </td>
</tr>
<tr>
<td> Add full method to class </td>
<td>
```java
+ void b() {
+ System.out.println("Hello, World!");
+ }
void fn() {
a();
+ b();
}
```
</td>
<td> MethodsAddedToClass/* </td>
</tr>
<tr>
<td> Pattern disjunction w/ clause order priority </td>
<td>
```java
(
- a();
|
- b();
|
- c();
)
```
</td>
<td> BasicPatternDisjunction </td>
</tr>
</table>
<h4 id="matchable-code">Matchable code</h4>
<p>Java elements/constructs you can specify in the semantic patch in order to match against real code,
produce new code in the output, or produce transformed code in the output. Not all
aspects/features/attributes are necessarily supported.</p>
<table>
<thead>
<tr>
<th>Description</th>
<th>Examples</th>
</tr>
</thead>
<tbody>
<tr>
<td>Literals</td>
<td><code class="language-plaintext highlighter-rouge">5</code>, <code class="language-plaintext highlighter-rouge">"Hello"</code></td>
</tr>
<tr>
<td>Type names</td>
<td><code class="language-plaintext highlighter-rouge">int</code></td>
</tr>
<tr>
<td>Variable declarations</td>
<td><code class="language-plaintext highlighter-rouge">int x;</code></td>
</tr>
<tr>