-
Notifications
You must be signed in to change notification settings - Fork 2
/
launcher.html
867 lines (356 loc) · 36.9 KB
/
launcher.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
<!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="usage, usage, java">
<title>Using Spoon as a Java library | 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="/launcher.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="/launcher.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><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 class="active"><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">Using Spoon as a Java library</h1>
</div>
<div class="post-content">
<h3 id="the-launcher-class">The Launcher class</h3>
<p>The Spoon <code class="language-plaintext highlighter-rouge">Launcher</code> (<a href="http://spoon.gforge.inria.fr/mvnsites/spoon-core/apidocs/spoon/Launcher.html">JavaDoc</a>) is used to create the AST model of a project. It can be as short as:</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">CtClass</span> <span class="n">l</span> <span class="o">=</span> <span class="nc">Launcher</span><span class="o">.</span><span class="na">parseClass</span><span class="o">(</span><span class="s">"class A { void m() { System.out.println(\"yeah\");} }"</span><span class="o">);</span>
</code></pre></div></div>
<p>Or with a plain object:</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">Launcher</span> <span class="n">launcher</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">Launcher</span><span class="o">();</span>
<span class="c1">// path can be a folder or a file</span>
<span class="c1">// addInputResource can be called several times</span>
<span class="n">launcher</span><span class="o">.</span><span class="na">addInputResource</span><span class="o">(</span><span class="s">"<path_to_source>"</span><span class="o">);</span>
<span class="c1">// the compliance level should be set to the java version targeted by the input resources, e.g. Java 17</span>
<span class="n">launcher</span><span class="o">.</span><span class="na">getEnvironment</span><span class="o">().</span><span class="na">setComplianceLevel</span><span class="o">(</span><span class="mi">17</span><span class="o">);</span>
<span class="n">launcher</span><span class="o">.</span><span class="na">buildModel</span><span class="o">();</span>
<span class="nc">CtModel</span> <span class="n">model</span> <span class="o">=</span> <span class="n">launcher</span><span class="o">.</span><span class="na">getModel</span><span class="o">();</span>
</code></pre></div></div>
<p>The current default value for the compliance level is 8.
It might cause unexpected issues to have a compliance level lower than the Java version of the input resources.</p>
<h3 id="pretty-printing-modes">Pretty-printing modes</h3>
<p>See <a href="http://spoon.gforge.inria.fr/custom-pretty-printing.html">http://spoon.gforge.inria.fr/custom-pretty-printing.html</a>.</p>
<h3 id="the-mavenlauncher-class">The MavenLauncher class</h3>
<p>The Spoon <code class="language-plaintext highlighter-rouge">MavenLauncher</code> (<a href="http://spoon.gforge.inria.fr/mvnsites/spoon-core/apidocs/spoon/MavenLauncher.html">JavaDoc</a>) is used to create the AST model of a Maven project.
It automatically infers the list of source folders and the dependencies from the <code class="language-plaintext highlighter-rouge">pom.xml</code> file.
This launcher handles multi-module Maven projects.</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// the second parameter can be APP_SOURCE / TEST_SOURCE / ALL_SOURCE</span>
<span class="nc">MavenLauncher</span> <span class="n">launcher</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">MavenLauncher</span><span class="o">(</span><span class="s">"<path_to_maven_project>"</span><span class="o">,</span> <span class="nc">MavenLauncher</span><span class="o">.</span><span class="na">SOURCE_TYPE</span><span class="o">.</span><span class="na">APP_SOURCE</span><span class="o">);</span>
<span class="n">launcher</span><span class="o">.</span><span class="na">buildModel</span><span class="o">();</span>
<span class="nc">CtModel</span> <span class="n">model</span> <span class="o">=</span> <span class="n">launcher</span><span class="o">.</span><span class="na">getModel</span><span class="o">();</span>
<span class="c1">// list all packages of the model</span>
<span class="k">for</span><span class="o">(</span><span class="nc">CtPackage</span> <span class="n">p</span> <span class="o">:</span> <span class="n">model</span><span class="o">.</span><span class="na">getAllPackages</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">"package: "</span> <span class="o">+</span> <span class="n">p</span><span class="o">.</span><span class="na">getQualifiedName</span><span class="o">());</span>
<span class="o">}</span>
<span class="c1">// list all classes of the model</span>
<span class="k">for</span><span class="o">(</span><span class="nc">CtType</span><span class="o"><?></span> <span class="n">s</span> <span class="o">:</span> <span class="n">model</span><span class="o">.</span><span class="na">getAllTypes</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">"class: "</span> <span class="o">+</span> <span class="n">s</span><span class="o">.</span><span class="na">getQualifiedName</span><span class="o">());</span>
<span class="o">}</span>
</code></pre></div></div>
<p>Note that by default, <code class="language-plaintext highlighter-rouge">MavenLauncher</code> relies on an existing local Maven binary to build the project’s classpath. But a constructor allowing the user to skip this step and to provide a custom classpath is available.</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">MavenLauncher</span> <span class="n">launcher</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">MavenLauncher</span><span class="o">(</span><span class="s">"<path_to_maven_project>"</span><span class="o">,</span>
<span class="nc">MavenLauncher</span><span class="o">.</span><span class="na">SOURCE_TYPE</span><span class="o">.</span><span class="na">APP_SOURCE</span><span class="o">,</span>
<span class="k">new</span> <span class="nc">String</span><span class="o">[]</span> <span class="o">{</span>
<span class="s">"/home/user/.m2/repository/org/my/jar/1.0/org-my-jar-1.0.jar"</span>
<span class="o">}</span>
<span class="o">);</span>
<span class="n">launcher</span><span class="o">.</span><span class="na">buildModel</span><span class="o">();</span>
<span class="nc">CtModel</span> <span class="n">model</span> <span class="o">=</span> <span class="n">launcher</span><span class="o">.</span><span class="na">getModel</span><span class="o">();</span>
</code></pre></div></div>
<p>To avoid invoking Maven over and over to build a classpath that has not changed, it is stored in the file <code class="language-plaintext highlighter-rouge">spoon.classpath.tmp</code> (or depending on the scope <code class="language-plaintext highlighter-rouge">spoon.classpath-app.tmp</code> or <code class="language-plaintext highlighter-rouge">spoon.classpath-test.tmp</code>) in the same folder as the <code class="language-plaintext highlighter-rouge">pom.xml</code>. This classpath will be refreshed if the file is deleted or if it has not been modified for 1h.</p>
<h3 id="analyzing-bytecode-with-jarlauncher">Analyzing bytecode with JarLauncher</h3>
<p>There are two ways to analyze bytecode with Spoon:</p>
<ul>
<li>Bytecode resources can be added to the classpath (some information will be extracted through reflection).</li>
<li>A decompiler may be used, and then, the analyzes will be performed on the decompiled sources.</li>
</ul>
<p>The Spoon <code class="language-plaintext highlighter-rouge">JarLauncher</code> (<a href="https://github.com/INRIA/spoon/blob/master/spoon-decompiler/src/main/java/spoon/JarLauncher.java">JavaDoc</a>) is used to create the AST model from a jar.
It automatically decompiles class files contained in the jar and analyzes them.
If a pom file corresponding to the jar is provided, it will be used to build the classpath containing all dependencies.</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">//More constructors are available, check the JavaDOc for more information.</span>
<span class="nc">JarLauncher</span> <span class="n">launcher</span> <span class="o">=</span> <span class="nc">JarLauncher</span><span class="o">(</span><span class="s">"<path_to_jar>"</span><span class="o">,</span> <span class="s">"<path_to_output_src_dir>"</span><span class="o">,</span> <span class="s">"<path_to_pom>"</span><span class="o">);</span>
<span class="n">launcher</span><span class="o">.</span><span class="na">buildModel</span><span class="o">();</span>
<span class="nc">CtModel</span> <span class="n">model</span> <span class="o">=</span> <span class="n">launcher</span><span class="o">.</span><span class="na">getModel</span><span class="o">();</span>
</code></pre></div></div>
<p>Note that the default decompiler <a href="http://www.benf.org/other/cfr/">CFR</a> can be changed by providing an instance implementing <code class="language-plaintext highlighter-rouge">spoon.decompiler.Decompiler</code> as a parameter.</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">JarLauncher</span> <span class="n">launcher</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">JarLauncher</span><span class="o">(</span><span class="s">"<path_to_jar>"</span><span class="o">,</span> <span class="s">"<path_to_output_src_dir>"</span><span class="o">,</span> <span class="s">"<path_to_pom>"</span><span class="o">,</span>
<span class="k">new</span> <span class="nf">Decompiler</span><span class="o">()</span> <span class="o">{</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">decompile</span><span class="o">(</span><span class="nc">String</span> <span class="n">inputPath</span><span class="o">,</span> <span class="nc">String</span> <span class="n">outputPath</span><span class="o">,</span> <span class="nc">String</span><span class="o">[]</span> <span class="n">classpath</span><span class="o">)</span> <span class="o">{</span>
<span class="c1">//Custom decompiler call</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">);</span>
</code></pre></div></div>
<p>Spoon provides two out of the shelf decompilers, CFR by default, and Fernflower. You can use the latter like this:</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">JarLauncher</span> <span class="n">launcher</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">JarLauncher</span><span class="o">(</span>
<span class="s">"<path_to_jar>"</span><span class="o">,</span>
<span class="s">"<path_to_output_src_dir>"</span><span class="o">,</span>
<span class="s">"<path_to_pom>"</span><span class="o">,</span>
<span class="k">new</span> <span class="nf">FernflowerDecompiler</span><span class="o">(</span><span class="k">new</span> <span class="nc">File</span><span class="o">(</span><span class="s">"<path_to_output_src_dir>/src/main/java"</span><span class="o">))</span>
<span class="o">);</span>
</code></pre></div></div>
<p>Optionally, the classic launcher can be used with <code class="language-plaintext highlighter-rouge">DecompiledResource</code> like this:</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">Launcher</span> <span class="n">launcher</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">Launcher</span><span class="o">();</span>
<span class="n">launcher</span><span class="o">.</span><span class="na">addInputResource</span><span class="o">(</span>
<span class="k">new</span> <span class="nf">DecompiledResource</span><span class="o">(</span><span class="n">baseDir</span><span class="o">.</span><span class="na">getAbsolutePath</span><span class="o">(),</span> <span class="k">new</span> <span class="nc">String</span><span class="o">[]{},</span> <span class="k">new</span> <span class="nc">CFRDecompiler</span><span class="o">(),</span> <span class="n">pathToDecompiledRoot</span><span class="o">.</span><span class="na">getPath</span><span class="o">())</span>
<span class="o">);</span>
</code></pre></div></div>
<p><strong>Warning</strong> The <code class="language-plaintext highlighter-rouge">JarLauncher</code> feature (and all features relying on decompilation) are not included in <code class="language-plaintext highlighter-rouge">spoon-core</code> but in <code class="language-plaintext highlighter-rouge">spoon-decompiler</code>. If you want to use them you should declare a dependency to <code class="language-plaintext highlighter-rouge">spoon-decompiler</code>.</p>
<h3 id="resolution-of-elements-and-classpath">Resolution of elements and classpath</h3>
<p>Spoon analyzes source code. However, this source code may refer to libraries (as a field, parameter, or method return type). There are two cases:</p>
<ul>
<li>Full classpath: all dependencies are in the JVM classpath or are given to the launcher with <code class="language-plaintext highlighter-rouge">launcher.getEnvironment().setSourceClasspath("<classpath_project>");</code> (optional).</li>
<li>No classpath: some dependencies are unknown and <code class="language-plaintext highlighter-rouge">launcher.getEnvironment().setNoClasspath(true)</code> is set.</li>
</ul>
<p>This has a direct impact on Spoon references.
When you consider a reference object (say, a <code class="language-plaintext highlighter-rouge">CtTypeReference</code>), there are three cases:</p>
<ul>
<li>Case 1 (code available as source code): the reference points to a code element for which the source code is present. In this case, <code class="language-plaintext highlighter-rouge">reference.getDeclaration()</code> returns this code element (e.g. <code class="language-plaintext highlighter-rouge">TypeReference.getDeclaration</code> returns the <code class="language-plaintext highlighter-rouge">CtType</code> representing the given Java file). <code class="language-plaintext highlighter-rouge">reference.getTypeDeclaration()</code> is identical to <code class="language-plaintext highlighter-rouge">reference.getDeclaration()</code>.</li>
<li>Case 2 (code available as binary in the classpath): the reference points to a code element for which the source code is NOT present, but for which the binary class is in the classpath (either the JVM classpath or the <code class="language-plaintext highlighter-rouge">--source-classpath</code> argument). In this case, <code class="language-plaintext highlighter-rouge">reference.getDeclaration()</code> returns <code class="language-plaintext highlighter-rouge">null</code> and <code class="language-plaintext highlighter-rouge">reference.getTypeDeclaration</code> returns a partial <code class="language-plaintext highlighter-rouge">CtType</code> built using runtime reflection. Those objects built using runtime reflection are called shadow objects, and you can identify them with method <code class="language-plaintext highlighter-rouge">isShadow</code>. (This also holds for <code class="language-plaintext highlighter-rouge">getFieldDeclaration</code> and <code class="language-plaintext highlighter-rouge">getExecutableDeclaration</code>).</li>
<li>Case 3 (code not available, aka noclasspath): the reference points to a code element for which the source code is NOT present, but for which the binary class is NOT in the classpath. This is called in Spoon the noclasspath mode. In this case, both <code class="language-plaintext highlighter-rouge">reference.getDeclaration()</code> and <code class="language-plaintext highlighter-rouge">reference.getTypeDeclaration()</code> return <code class="language-plaintext highlighter-rouge">null</code>. (This also holds for <code class="language-plaintext highlighter-rouge">getFieldDeclaration</code> and <code class="language-plaintext highlighter-rouge">getExecutableDeclaration</code>).</li>
</ul>
<h3 id="default-and-custom-classloaders">Default and custom classloaders</h3>
<p>Spoon uses a <code class="language-plaintext highlighter-rouge">URLClassLoader</code> by default to load binaries before parsing them. A <code class="language-plaintext highlighter-rouge">URLClassLoader</code> will load a class with the parent classloader first. This means that if there is a name conflict between the JVM classpath and the manual classpath then the former will be chosen. To give priority to the manual classpath, a child-first classloader can be used. Classloaders are set with <code class="language-plaintext highlighter-rouge">launcher.getEnvironment().setInputClassLoader(customClassloader)</code>.</p>
<h2 id="declaring-the-dependency-to-spoon">Declaring the dependency to Spoon</h2>
<h3 id="maven">Maven</h3>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><dependency></span>
<span class="nt"><groupId></span>fr.inria.gforge.spoon<span class="nt"></groupId></span>
<span class="nt"><artifactId></span>spoon-core<span class="nt"></artifactId></span>
<span class="nt"><version></version></span>
<span class="nt"></dependency></span>
</code></pre></div></div>
<h3 id="gradle">Gradle</h3>
<div class="language-groovy highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">compile</span> <span class="s1">'fr.inria.gforge.spoon:spoon-core:'</span>
</code></pre></div></div>
<h2 id="advanced-launchers">Advanced launchers</h2>
<h3 id="incremental-launcher">Incremental Launcher</h3>
<p><code class="language-plaintext highlighter-rouge">IncrementalLauncher</code> (<a href="http://spoon.gforge.inria.fr/mvnsites/spoon-core/apidocs/spoon/IncrementalLauncher.html">JavaDoc</a>) allows cached ASTs and compiled classes. Any Spoon analysis can then be restarted from where it stopped instead of restarting from scratch.</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">final</span> <span class="nc">File</span> <span class="n">cache</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">File</span><span class="o">(</span><span class="s">"<path_to_cache>"</span><span class="o">);</span>
<span class="nc">Set</span><span class="o"><</span><span class="nc">File</span><span class="o">></span> <span class="n">inputResources</span> <span class="o">=</span> <span class="nc">Collections</span><span class="o">.</span><span class="na">singleton</span><span class="o">(</span><span class="k">new</span> <span class="nc">File</span><span class="o">(</span><span class="s">"<path_to_sources>"</span><span class="o">));</span>
<span class="nc">Set</span><span class="o"><</span><span class="nc">String</span><span class="o">></span> <span class="n">sourceClasspath</span> <span class="o">=</span> <span class="nc">Collections</span><span class="o">.</span><span class="na">emptySet</span><span class="o">();</span> <span class="c1">// Empty classpath</span>
<span class="c1">//Start build from cache</span>
<span class="nc">IncrementalLauncher</span> <span class="n">launcher</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">IncrementalLauncher</span><span class="o">(</span><span class="n">inputResources</span><span class="o">,</span> <span class="n">sourceClasspath</span><span class="o">,</span> <span class="n">cache</span><span class="o">);</span>
<span class="k">if</span> <span class="o">(</span><span class="n">launcher</span><span class="o">.</span><span class="na">changesPresent</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">"There are changes since last save to cache."</span><span class="o">);</span>
<span class="o">}</span>
<span class="nc">CtModel</span> <span class="n">newModel</span> <span class="o">=</span> <span class="n">launcher</span><span class="o">.</span><span class="na">buildModel</span><span class="o">();</span>
<span class="c1">//Model is now up to date</span>
<span class="n">launcher</span><span class="o">.</span><span class="na">saveCache</span><span class="o">();</span>
<span class="c1">//Cache is now up to date</span>
</code></pre></div></div>
<h3 id="fluent-launcherapi">Fluent LauncherAPI</h3>
<p><code class="language-plaintext highlighter-rouge">FluentLauncher</code> (<a href="http://spoon.gforge.inria.fr/mvnsites/spoon-core/apidocs/spoon/FluentLauncher.html">JavaDoc</a>) allows setting most options directly for simple and fluent launcher usage.</p>
<p>For the classic launcher it’s simply:</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">CtModel</span> <span class="n">model</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">FluentLauncher</span><span class="o">()</span>
<span class="o">.</span><span class="na">inputResource</span><span class="o">(</span><span class="s">"<path_to_sources>"</span><span class="o">)</span>
<span class="o">.</span><span class="na">noClasspath</span><span class="o">(</span><span class="kc">true</span><span class="o">)</span>
<span class="o">.</span><span class="na">outputDirectory</span><span class="o">(</span><span class="s">"<path_to_outputdir>"</span><span class="o">)</span>
<span class="o">.</span><span class="na">processor</span><span class="o">(....)</span>
<span class="o">.</span><span class="na">buildModel</span><span class="o">();</span>
</code></pre></div></div>
<p>If you want to use other launchers like the <code class="language-plaintext highlighter-rouge">MavenLauncher</code>:</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nc">MavenLauncher</span> <span class="n">launcher</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">MavenLauncher</span><span class="o">(....);</span>
<span class="nc">CtModel</span> <span class="n">model</span> <span class="o">=</span> <span class="k">new</span> <span class="nc">FluentLauncher</span><span class="o">(</span><span class="n">launcher</span><span class="o">)</span>
<span class="o">.</span><span class="na">processor</span><span class="o">(....)</span>
<span class="o">.</span><span class="na">encoding</span><span class="o">(...)</span>
<span class="o">.</span><span class="na">buildModel</span><span class="o">();</span>
</code></pre></div></div>
</div>
<hr class="shaded"/>
<footer>
<div class="row">
<div class="col-lg-12 footer">
<a href="https://github.com/INRIA/spoon/issues/new">Contact / Feedback / Question / Bug Report</a><br />
</div>
</div>
</footer>
</div><!-- /.row -->
</div> <!-- /.container -->
</body>
<!-- the google_analytics_id gets auto inserted from the config file -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-24273081-1', 'auto');
ga('send', 'pageview');
</script>
</html>