Repository navigation
Expand file tree
/
Copy pathgetstarted.html
More file actions
250 lines (213 loc) · 18.3 KB
/
Copy pathgetstarted.html
File metadata and controls
250 lines (213 loc) · 18.3 KB
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
<!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! <a class="github-button" href="https://github.com/immutables/immutables" data-style="mega" data-count-href="/immutables/immutables/stargazers" data-count-api="/repos/immutables/immutables#stargazers_count" data-count-aria-label="# stargazers on GitHub" aria-label="Star immutables/immutables on GitHub">stars</a></h1>
</div>
</header>
<aside id="toc"></aside>
<section class="documentation">
<h2>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="http://search.maven.org/#artifactdetails%7Corg.immutables%7Cvalue%7C2.6.3%7Cjar">org.immutables:value:2.6.3</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="highlight"><pre><code class="language-xml" data-lang="xml"><span></span><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.6.3<span class="nt"></version></span>
<span class="nt"><scope></span>provided<span class="nt"></scope></span>
<span class="nt"></dependency></span>
</code></pre></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>The <em>Immutables</em> annotation processor runs under any Java build tool that uses <code>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>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>@interface</code>):</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span></span><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.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="n">String</span> <span class="nf">bar</span><span class="o">();</span>
<span class="kd">public</span> <span class="kd">abstract</span> <span class="n">List</span><span class="o"><</span><span class="n">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="n">Set</span><span class="o"><</span><span class="n">Long</span><span class="o">></span> <span class="nf">crux</span><span class="o">();</span>
<span class="o">}</span>
</code></pre></div>
<p>It is now possible to generate and then use the generated immutable implementation:</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span></span><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="n">String</span><span class="o">...</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
<span class="n">FoobarValue</span> <span class="n">value</span> <span class="o">=</span> <span class="n">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="n">List</span><span class="o"><</span><span class="n">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>
<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>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>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>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>android-apt</code> plugin works well for this, including interaction with Android Studio.</p>
<div class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span></span><span class="nx">buildscript</span> <span class="p">{</span>
<span class="nx">repositories</span> <span class="p">{</span>
<span class="nx">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="s2">"com.android.tools.build:gradle:1.2.3"</span>
<span class="c1">// add dependency to plugin</span>
<span class="nx">classpath</span> <span class="s2">"com.neenbedankt.gradle.plugins:android-apt:1.6"</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="c1">// ...</span>
</code></pre></div>
<p>Then, use <code>apply plugin: 'android-apt'</code> in modules to apply the plugin.</p>
<h4>Adding Immutables dependencies</h4>
<p>The <em>Immutables</em> annotation processor should be added to the special <code>apt</code> scope (declared by the <code>android-apt</code> plugin) and to the compile-time-only <code>provided</code> scope (declared by the Android Gradle plugins).</p>
<div class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span></span><span class="nx">apply</span> <span class="nx">plugin</span><span class="o">:</span> <span class="s2">"com.android.application"</span>
<span class="nx">apply</span> <span class="nx">plugin</span><span class="o">:</span> <span class="s2">"android-apt"</span>
<span class="c1">// ...</span>
<span class="nx">dependencies</span> <span class="p">{</span>
<span class="nx">compile</span> <span class="nx">fileTree</span><span class="p">(</span><span class="nx">dir</span><span class="o">:</span> <span class="s2">"libs"</span><span class="p">,</span> <span class="nx">include</span><span class="o">:</span> <span class="p">[</span><span class="s2">"*.jar"</span><span class="p">])</span>
<span class="nx">apt</span> <span class="s2">"org.immutables:value:2.6.3"</span> <span class="c1">// <-- for annotation processor</span>
<span class="nx">provided</span> <span class="s2">"org.immutables:value:2.6.3"</span> <span class="c1">// <-- for annotation API</span>
<span class="p">}</span>
</code></pre></div>
<p>Other compile-only dependencies applicable to Android should be added in the <code>provided</code> scope. Jar files with annotations such as <a href="http://search.maven.org/#artifactdetails%7Corg.immutables%7Cbuilder%7C2.6.3%7Cjar">org.immutables:builder:2.6.3</a> (see <a href="factory.html">Factory Builders</a>) or <a href="http://search.maven.org/#artifactdetails%7Corg.immutables%7Cgson%7C2.6.3%7Cjar">org.immutables:gson:2.6.3</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>gson</code> module provides optional runtime classes which are not suitable for Android apps.</p>
<div class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span></span><span class="nx">dependencies</span> <span class="p">{</span>
<span class="c1">// ...</span>
<span class="nx">apt</span> <span class="s2">"org.immutables:value:2.6.3"</span> <span class="c1">// for annotation processor</span>
<span class="nx">provided</span> <span class="s2">"org.immutables:value:2.6.3"</span> <span class="c1">// for annotations</span>
<span class="nx">provided</span> <span class="s2">"org.immutables:builder:2.6.3"</span> <span class="c1">// for annotations</span>
<span class="nx">provided</span> <span class="s2">"org.immutables:gson:2.6.3"</span> <span class="c1">// for annotations</span>
<span class="p">}</span>
</code></pre></div>
<p>Since version <code>2.0.19</code>, for some combined annotation and runtime artifacts there are separate annotation-only artifacts available.
For example, the <code>value</code> and <code>gson</code> modules have additional artifacts with <code>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="highlight"><pre><code class="language-javascript" data-lang="javascript"><span></span><span class="nx">dependencies</span> <span class="p">{</span>
<span class="c1">// ...</span>
<span class="nx">apt</span> <span class="s2">"org.immutables:value:2.6.3"</span> <span class="c1">// for annotation processor</span>
<span class="nx">provided</span> <span class="s2">"org.immutables:value:2.6.3:annotations"</span> <span class="c1">// annotation-only artifact</span>
<span class="nx">provided</span> <span class="s2">"org.immutables:builder:2.6.3"</span> <span class="c1">// there are only annotations anyway</span>
<span class="nx">provided</span> <span class="s2">"org.immutables:gson:2.6.3:annotations"</span> <span class="c1">// annotation-only artifact</span>
<span class="p">}</span>
</code></pre></div>
<p><a href="/immutable.html" class="btn btn-default btn-lg">Read guide...</a></p>
<h2>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>javac</code> and annotation processing.
Build tools like Maven are also affected by this bug. Typically, commands such as <code>mvn clean compile</code> will resolve any such problems by a forcing full build.
Disabling incremental compilation is also an option:</p>
<div class="highlight"><pre><code class="language-xml" data-lang="xml"><span></span><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>
<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>-Xmaxerrs 1000000</code>. For Maven it could be set as <code>...
<compilerArguments><Xmaxerrs>1000000</Xmaxerrs></compilerArguments></configuration></code> for <code>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>-X</code> debug output and, possibly, quite verbose compiler configuration:</p>
<div class="highlight"><pre><code class="language-xml" data-lang="xml"><span></span><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>
</section>
<footer class="jumbotron">
<div class="container">
<h2>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="/mongo.html">MongoDB repositories</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>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>News and announcements on twitter <a href="https://twitter.com/ImmutablesOrg">@ImmutablesOrg</a></li>
<li>Ask questions or give feedback and ideas using mailing group <a href="https://groups.google.com/forum/#!forum/immutables">immutables at googlegroups.com</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>
<script async defer id="github-bjs" src="https://buttons.github.io/buttons.js"></script>
</body>
</html>