-
Notifications
You must be signed in to change notification settings - Fork 23
/
getstarted.html
286 lines (231 loc) · 21.6 KB
/
getstarted.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
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!--link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css"-->
<link rel="stylesheet" href="/gfx/bootstrap.min.css">
<link rel="stylesheet" href="/gfx/main.css">
<link rel="stylesheet" href="/gfx/code.css">
<title>Get started!</title>
</head>
<body class="page">
<!-- Google Tag Manager -->
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-PMJSKV"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-PMJSKV');</script>
<!-- End Google Tag Manager -->
<header>
<div class="container">
<a href="/">Immutables</a> ←
<h1>Get started! <iframe src="https://ghbtns.com/github-btn.html?user=immutables&repo=immutables&type=star&count=true&size=large" frameborder="0" scrolling="0" width="160px" height="30px"></iframe>
</h1>
</div>
</header>
<aside id="toc"></aside>
<section class="documentation">
<h2 id="prerequisites">Prerequisites</h2>
<p>Java 7 or higher is required to run the <em>Immutables</em> annotation processor.</p>
<p>Add the required dependencies for basic immutable object generation:</p>
<ul>
<li><a href="https://search.maven.org/artifact/org.immutables/value/2.10.1/jar">org.immutables:value:2.10.1</a>
<ul>
<li>Compile-only annotation processing tool. All in one artifact: annotations, processor with properly repackaged embedded dependencies.</li>
</ul>
</li>
</ul>
<p>Snippet of Maven dependencies:</p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><dependency></span>
<span class="nt"><groupId></span>org.immutables<span class="nt"></groupId></span>
<span class="nt"><artifactId></span>value<span class="nt"></artifactId></span>
<span class="nt"><version></span>2.10.1<span class="nt"></version></span>
<span class="nt"><scope></span>provided<span class="nt"></scope></span>
<span class="nt"></dependency></span>
</code></pre></div></div>
<p>In Maven, the dependency can be declared in the “provided” scope, or made “optional”. The artifact is not required at runtime; it is compile-only dependency.</p>
<p>If you are using multiple dependencies like <code class="language-plaintext highlighter-rouge">org.immutables:serial</code> you can import the bill of materials (BoM) in your dependency management and go without specifying the version of the dependency individually:</p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><dependencyManagement></span>
<span class="nt"><dependencies></span>
<span class="nt"><dependency></span>
<span class="nt"><groupId></span>org.immutables<span class="nt"></groupId></span>
<span class="nt"><artifactId></span>bom<span class="nt"></artifactId></span>
<span class="nt"><version></span>2.10.1<span class="nt"></version></span>
<span class="nt"><scope></span>import<span class="nt"></scope></span>
<span class="nt"><type></span>pom<span class="nt"></type></span>
<span class="nt"></dependency></span>
<span class="nt"></dependencies></span>
<span class="nt"></dependencyManagement></span>
</code></pre></div></div>
<p>The <em>Immutables</em> annotation processor runs under any Java build tool that uses <code class="language-plaintext highlighter-rouge">javac</code> as compiler backend (assuming that annotation processing is not disabled in build tool configuration).
The <em>Eclipse JDT compiler</em> (ECJ) also supports this annotation processor. See <a href="/apt.html">Using annotation processor in IDE</a>.</p>
<h2 id="create-immutable-object">Create immutable object</h2>
<p>Assuming that required dependencies were added, create an abstract class with abstract accessor methods.
You can do the same by annotating with interfaces or even annotations (<code class="language-plaintext highlighter-rouge">@interface</code>):</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">package</span> <span class="nn">info.sample</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">java.util.List</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">java.util.Set</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.immutables.value.Value</span><span class="o">;</span>
<span class="nd">@Value</span><span class="o">.</span><span class="na">Immutable</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="kd">class</span> <span class="nc">FoobarValue</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="kt">int</span> <span class="nf">foo</span><span class="o">();</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="nc">String</span> <span class="nf">bar</span><span class="o">();</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="nc">List</span><span class="o"><</span><span class="nc">Integer</span><span class="o">></span> <span class="nf">buz</span><span class="o">();</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="nc">Set</span><span class="o"><</span><span class="nc">Long</span><span class="o">></span> <span class="nf">crux</span><span class="o">();</span>
<span class="o">}</span>
</code></pre></div></div>
<p>It is now possible to generate and then use the generated immutable implementation:</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">package</span> <span class="nn">info.sample</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">java.util.List</span><span class="o">;</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">FoobarValueMain</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="nc">String</span><span class="o">...</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
<span class="nc">FoobarValue</span> <span class="n">value</span> <span class="o">=</span> <span class="nc">ImmutableFoobarValue</span><span class="o">.</span><span class="na">builder</span><span class="o">()</span>
<span class="o">.</span><span class="na">foo</span><span class="o">(</span><span class="mi">2</span><span class="o">)</span>
<span class="o">.</span><span class="na">bar</span><span class="o">(</span><span class="s">"Bar"</span><span class="o">)</span>
<span class="o">.</span><span class="na">addBuz</span><span class="o">(</span><span class="mi">1</span><span class="o">,</span> <span class="mi">3</span><span class="o">,</span> <span class="mi">4</span><span class="o">)</span>
<span class="o">.</span><span class="na">build</span><span class="o">();</span> <span class="c1">// FoobarValue{foo=2, bar=Bar, buz=[1, 3, 4], crux={}}</span>
<span class="kt">int</span> <span class="n">foo</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="na">foo</span><span class="o">();</span> <span class="c1">// 2</span>
<span class="nc">List</span><span class="o"><</span><span class="nc">Integer</span><span class="o">></span> <span class="n">buz</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="na">buz</span><span class="o">();</span> <span class="c1">// ImmutableList.of(1, 3, 4)</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p>Congratulations! You’re done!
See the sample <a href="/generated.html">generated code</a> for an example of what kind of code is being generated by the processor.</p>
<p>Even basic immutable class generation has a lot more tricks to show. Check out the <a href="/immutable.html">guide</a>!</p>
<p><a href="/immutable.html" class="btn btn-default btn-lg">Read guide…</a></p>
<p><a name="android"></a></p>
<h3 id="configuration-for-android">Configuration for Android</h3>
<p>To comfortably use <em>Immutables</em> for Android development, the following steps are required to set up a working build configuration:</p>
<h4 id="using-apt-plugin">Using apt plugin</h4>
<p>An annotation processor plugin should be configured for the Android build.
Although <em>Immutables</em> annotation processing works with <code class="language-plaintext highlighter-rouge">javac</code> without specific a configuration, a plugin is required to ensure that output directories, dependency scopes and other miscellaneous details are being set up correctly. We’ve found that the <code class="language-plaintext highlighter-rouge">android-apt</code> plugin works well for this, including interaction with Android Studio.</p>
<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nx">buildscript</span> <span class="p">{</span>
<span class="nx">repositories</span> <span class="p">{</span>
<span class="nf">jcenter</span><span class="p">()</span>
<span class="p">}</span>
<span class="nx">dependencies</span> <span class="p">{</span>
<span class="nx">classpath</span> <span class="dl">"</span><span class="s2">com.android.tools.build:gradle:1.2.3</span><span class="dl">"</span>
<span class="c1">// add dependency to plugin</span>
<span class="nx">classpath</span> <span class="dl">"</span><span class="s2">com.neenbedankt.gradle.plugins:android-apt:1.6</span><span class="dl">"</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="c1">// ...</span>
</code></pre></div></div>
<p>Then, use <code class="language-plaintext highlighter-rouge">apply plugin: 'android-apt'</code> in modules to apply the plugin.</p>
<h4 id="adding-immutables-dependencies">Adding Immutables dependencies</h4>
<p>The <em>Immutables</em> annotation processor should be added to the special <code class="language-plaintext highlighter-rouge">apt</code> scope (declared by the <code class="language-plaintext highlighter-rouge">android-apt</code> plugin) and to the compile-time-only <code class="language-plaintext highlighter-rouge">provided</code> scope (declared by the Android Gradle plugins).</p>
<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nx">apply</span> <span class="nx">plugin</span><span class="p">:</span> <span class="dl">"</span><span class="s2">com.android.application</span><span class="dl">"</span>
<span class="nx">apply</span> <span class="nx">plugin</span><span class="p">:</span> <span class="dl">"</span><span class="s2">android-apt</span><span class="dl">"</span>
<span class="c1">// ...</span>
<span class="nx">dependencies</span> <span class="p">{</span>
<span class="nx">compile</span> <span class="nf">fileTree</span><span class="p">(</span><span class="nx">dir</span><span class="p">:</span> <span class="dl">"</span><span class="s2">libs</span><span class="dl">"</span><span class="p">,</span> <span class="nx">include</span><span class="p">:</span> <span class="p">[</span><span class="dl">"</span><span class="s2">*.jar</span><span class="dl">"</span><span class="p">])</span>
<span class="nx">apt</span> <span class="dl">"</span><span class="s2">org.immutables:value:2.10.1</span><span class="dl">"</span> <span class="c1">// <-- for annotation processor</span>
<span class="nx">provided</span> <span class="dl">"</span><span class="s2">org.immutables:value:2.10.1</span><span class="dl">"</span> <span class="c1">// <-- for annotation API</span>
<span class="p">}</span>
</code></pre></div></div>
<p>Other compile-only dependencies applicable to Android should be added in the <code class="language-plaintext highlighter-rouge">provided</code> scope. Jar files with annotations such as <a href="https://search.maven.org/artifact/org.immutables/builder/2.10.1/jar">org.immutables:builder:2.10.1</a> (see <a href="factory.html">Factory Builders</a>) or <a href="https://search.maven.org/artifact/org.immutables/gson/2.10.1/jar">org.immutables:gson:2.10.1</a> (see <a href="json.html#gson">GSON support</a>) don’t have to be propagated to an Android application, especially if there are no runtime classes needed. For example, the <code class="language-plaintext highlighter-rouge">gson</code> module provides optional runtime classes which are not suitable for Android apps.</p>
<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nx">dependencies</span> <span class="p">{</span>
<span class="c1">// ...</span>
<span class="nx">apt</span> <span class="dl">"</span><span class="s2">org.immutables:value:2.10.1</span><span class="dl">"</span> <span class="c1">// for annotation processor</span>
<span class="nx">provided</span> <span class="dl">"</span><span class="s2">org.immutables:value-annotations:2.10.1</span><span class="dl">"</span> <span class="c1">// for annotations</span>
<span class="nx">provided</span> <span class="dl">"</span><span class="s2">org.immutables:builder:2.10.1</span><span class="dl">"</span> <span class="c1">// for annotations</span>
<span class="nx">provided</span> <span class="dl">"</span><span class="s2">org.immutables:gson:2.10.1</span><span class="dl">"</span> <span class="c1">// for annotations</span>
<span class="p">}</span>
</code></pre></div></div>
<p>Since version <code class="language-plaintext highlighter-rouge">2.0.19</code>, for some combined annotation and runtime artifacts there are separate annotation-only artifacts available.
For example, the <code class="language-plaintext highlighter-rouge">value</code> and <code class="language-plaintext highlighter-rouge">gson</code> modules have additional artifacts with <code class="language-plaintext highlighter-rouge">annotations</code> classifiers. This allows for the reduction of dependencies and
avoids lint warnings about using certain types that are not available on Android (despite not being
required at runtime). The above example with the alternative dependencies could be rewritten as:</p>
<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nx">dependencies</span> <span class="p">{</span>
<span class="c1">// ...</span>
<span class="nx">apt</span> <span class="dl">"</span><span class="s2">org.immutables:value:2.10.1</span><span class="dl">"</span> <span class="c1">// for annotation processor</span>
<span class="nx">provided</span> <span class="dl">"</span><span class="s2">org.immutables:value:2.10.1:annotations</span><span class="dl">"</span> <span class="c1">// annotation-only artifact</span>
<span class="nx">provided</span> <span class="dl">"</span><span class="s2">org.immutables:builder:2.10.1</span><span class="dl">"</span> <span class="c1">// there are only annotations anyway</span>
<span class="nx">provided</span> <span class="dl">"</span><span class="s2">org.immutables:gson:2.10.1:annotations</span><span class="dl">"</span> <span class="c1">// annotation-only artifact</span>
<span class="p">}</span>
</code></pre></div></div>
<p>Since version 2.7 in addition to <code class="language-plaintext highlighter-rouge">org.immutables:value:annotations</code> artifact (with <code class="language-plaintext highlighter-rouge">annotations</code> classifier), also available equivalent <code class="language-plaintext highlighter-rouge">org.immutables:value-annotations</code> annotation-only artifact. It will be easier for some tools to consume artifacts without classifiers and including source jars.</p>
<p><a href="/immutable.html" class="btn btn-default btn-lg">Read guide…</a></p>
<h2 id="troubleshooting">Troubleshooting</h2>
<p>Immutables itself and the surrounding ecosystem of build tools, compilers and even JVM may contain bugs. Please, try to upgrade to the latest stable version of tools you use if possible and report any issues found.</p>
<p>There’s known issue with the interaction between the incremental compilation feature of <code class="language-plaintext highlighter-rouge">javac</code> and annotation processing.
Build tools like Maven are also affected by this bug. Typically, commands such as <code class="language-plaintext highlighter-rouge">mvn clean compile</code> will resolve any such problems by a forcing full build.
Disabling incremental compilation is also an option:</p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code>
<span class="nt"><plugin></span>
<span class="nt"><groupId></span>org.apache.maven.plugins<span class="nt"></groupId></span>
<span class="nt"><artifactId></span>maven-compiler-plugin<span class="nt"></artifactId></span>
<span class="nt"><version></span>3.3<span class="nt"></version></span>
<span class="nt"><configuration></span>
<span class="nt"><compilerVersion></span>1.8<span class="nt"></compilerVersion></span>
<span class="nt"><source></span>1.8<span class="nt"></source></span>
<span class="nt"><target></span>1.8<span class="nt"></target></span>
<span class="c"><!-- Prevents an endPosTable exception during compilation --></span>
<span class="nt"><useIncrementalCompilation></span>false<span class="nt"></useIncrementalCompilation></span>
<span class="nt"></configuration></span>
<span class="nt"></plugin></span>
</code></pre></div></div>
<p>Search phrase: <a href="https://www.google.com/search?q=java.lang.IllegalStateException%3A+endPosTable+already+set">“java.lang.IllegalStateException: endPosTable already set”</a></p>
<p>Sometimes there are too many compilation errors due to generated files not found, that may hinder real annotation processing errors when max error limit is reached. The limit can be configured for javac as <code class="language-plaintext highlighter-rouge">-Xmaxerrs 1000000</code>. For Maven it could be set as <code class="language-plaintext highlighter-rouge">...
<compilerArguments><Xmaxerrs>1000000</Xmaxerrs></compilerArguments></configuration></code> for <code class="language-plaintext highlighter-rouge">maven-compiler-plugin</code>.</p>
<p>In case of some spurious errors Some internal annotation processing errors may be not clearly reported for Maven, you may rerun build with <code class="language-plaintext highlighter-rouge">-X</code> debug output and, possibly, quite verbose compiler configuration:</p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><compilerArgs></span>
<span class="nt"><arg></span>-verbose<span class="nt"></arg></span>
<span class="nt"><arg></span>-XprintRounds<span class="nt"></arg></span>
<span class="nt"><arg></span>-XprintProcessorInfo<span class="nt"></arg></span>
<span class="nt"><arg></span>-Xlint<span class="nt"></arg></span>
<span class="nt"><arg></span>-J-verbose<span class="nt"></arg></span>
<span class="nt"></compilerArgs></span>
</code></pre></div></div>
</section>
<footer class="jumbotron">
<div class="container">
<h2 id="guides">Guides</h2>
<ul>
<li><a href="/getstarted.html">Get started!</a></li>
<li><a href="/intro.html">Inception</a></li>
<li><a href="/immutable.html">Immutable objects</a></li>
<li><a href="/factory.html">Factory builders</a></li>
<li><a href="/functional.html">Functions and Predicates (for Java 7)</a></li>
<li><a href="/style.html">Style customization</a></li>
<li><a href="/json.html">JSON serialization</a></li>
<li><a href="/criteria.html">Criteria</a></li>
<li><a href="/mongo.html">MongoDB repositories</a></li>
<li><a href="/dynamodb.html">DynamoDB integration</a></li>
<li><a href="/encoding.html">Encoding: Customizing attributes and builders (experimental)</a></li>
<li><a href="/apt.html">Using annotation processor in IDE</a></li>
</ul>
<h2 id="get-involved">Get involved</h2>
<ul>
<li>Clone source repository, contribute bug reports and fixes on <a href="https://github.com/immutables/immutables">GitHub immutables/immutables</a></li>
<li>Issue reports, questions and feedback is welcome on issue tracker <a href="https://github.com/immutables/immutables/issues">GitHub immutables/immutables/issues</a></li>
<li>News and announcements on twitter <a href="https://twitter.com/ImmutablesOrg">@ImmutablesOrg</a></li>
</ul>
<p><a href="/license.html">Apache License 2.0</a></p>
<!--<div><h2>Posts</h2>
<ul>
</ul>
</div>-->
</div>
</footer>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script defer src="/gfx/jquery.toc.min.js"></script>
<script>
$(function() {
$('#toc').toc({
container: '.documentation',
selectors: 'h1,h2,h3,h4',
anchorName: function(i, heading, prefix) {
heading = $(heading).text();
if (heading.trim) heading = heading.trim();
return heading.toLowerCase().replace(/ /g, '-').replace(/[^a-z^\-]+/g, '');
},
})
})
</script>
</body>
</html>