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
|
/*
* Copyright © 2009 Reinier Zwitserloot and Roel Spilker.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package lombok.installer;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.jar.JarFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
* Represents an Eclipse installation.
* An instance can figure out if an Eclipse installation has been lombok-ified, and can
* install and uninstall lombok from the Eclipse installation.
*/
final class EclipseLocation {
private static final String OS_NEWLINE;
static {
String os = System.getProperty("os.name", "");
if ( "Mac OS".equals(os) ) OS_NEWLINE = "\r";
else if ( os.toLowerCase().contains("windows") ) OS_NEWLINE = "\r\n";
else OS_NEWLINE = "\n";
}
private final File path;
private volatile boolean hasLombok;
/** Toggling the 'selected' checkbox in the GUI is tracked via this boolean */
boolean selected = true;
/**
* Thrown when creating a new EclipseLocation with a path object that doesn't, in fact,
* point at an Eclipse installation.
*/
final class NotAnEclipseException extends Exception {
private static final long serialVersionUID = 1L;
public NotAnEclipseException(String message, Throwable cause) {
super(message, cause);
}
/**
* Renders a message dialog with information about what went wrong.
*/
void showDialog(JFrame appWindow) {
JOptionPane.showMessageDialog(appWindow, getMessage(), "Cannot configure Eclipse installation", JOptionPane.WARNING_MESSAGE);
}
}
/**
* Create a new EclipseLocation by pointing at either the directory contain the Eclipse executable, or the executable itself.
*
* @throws NotAnEclipseException If this isn't an Eclipse executable or a directory with an Eclipse executable.
*/
EclipseLocation(String path) throws NotAnEclipseException {
if ( path == null ) throw new NullPointerException("path");
File p = new File(path);
if ( !p.exists() ) throw new NotAnEclipseException("File does not exist: " + path, null);
final String execName = EclipseFinder.getEclipseExecutableName();
if ( p.isDirectory() ) {
for ( File f : p.listFiles() ) {
if ( f.getName().equalsIgnoreCase(execName) ) {
p = f;
break;
}
}
}
if ( !p.exists() || !p.getName().equalsIgnoreCase(execName) ) {
throw new NotAnEclipseException("This path does not appear to contain an Eclipse installation: " + p, null);
}
this.path = p;
try {
this.hasLombok = checkForLombok();
} catch ( IOException e ) {
throw new NotAnEclipseException(
"I can't read the configuration file of the Eclipse installed at " + this.path.getAbsolutePath() + "\n" +
"You may need to run this installer with root privileges if you want to modify that Eclipse.", e);
}
}
@Override public int hashCode() {
return path.hashCode();
}
@Override public boolean equals(Object o) {
if ( !(o instanceof EclipseLocation) ) return false;
return ((EclipseLocation)o).path.equals(path);
}
/**
* Returns the absolute path to the Eclipse executable.
*
* Executables: "eclipse.exe" (Windows), "Eclipse.app" (Mac OS X), "eclipse" (Linux and other unixes).
*/
String getPath() {
return path.getAbsolutePath();
}
/**
* @return true if the Eclipse installation has been instrumented with lombok.
*/
boolean hasLombok() {
return hasLombok;
}
/**
* Returns the various directories that can contain the 'eclipse.ini' file.
* Returns multiple directories because there are a few different ways Eclipse is packaged.
*/
private List<File> getTargetDirs() {
return Arrays.asList(path.getParentFile(), new File(new File(path, "Contents"), "MacOS"));
}
private boolean checkForLombok() throws IOException {
for ( File targetDir : getTargetDirs() ) {
if ( checkForLombok0(targetDir) ) return true;
}
return false;
}
private final Pattern JAVA_AGENT_LINE_MATCHER = Pattern.compile(
"^\\-javaagent\\:.*lombok.*\\.jar$", Pattern.CASE_INSENSITIVE);
private final Pattern BOOTCLASSPATH_LINE_MATCHER = Pattern.compile(
"^\\-Xbootclasspath\\/a\\:(.*lombok.*\\.jar.*)$", Pattern.CASE_INSENSITIVE);
private boolean checkForLombok0(File dir) throws IOException {
File iniFile = new File(dir, "eclipse.ini");
if ( !iniFile.exists() ) return false;
FileInputStream fis = new FileInputStream(iniFile);
try {
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line;
while ( (line = br.readLine()) != null ) {
if ( JAVA_AGENT_LINE_MATCHER.matcher(line.trim()).matches() ) return true;
}
return false;
} finally {
fis.close();
}
}
/** Thrown when uninstalling lombok fails. */
class UninstallException extends Exception {
private static final long serialVersionUID = 1L;
public UninstallException(String message, Throwable cause) {
super(message, cause);
}
}
/**
* Uninstalls lombok from this location.
* It's a no-op if lombok wasn't there in the first place,
* and it will remove a half-succeeded lombok installation as well.
*
* @throws UninstallException If there's an obvious I/O problem that is preventing installation.
* bugs in the uninstall code will probably throw other exceptions; this is intentional.
*/
void uninstall() throws UninstallException {
for ( File dir : getTargetDirs() ) {
File lombokJar = new File(dir, "lombok.jar");
if ( lombokJar.exists() ) {
if ( !lombokJar.delete() ) throw new UninstallException(
"Can't delete " + lombokJar.getAbsolutePath() +
" - perhaps the installer does not have the access rights to do so.",
null);
}
File agentJar = new File(dir, "lombok.eclipse.agent.jar");
if ( agentJar.exists() ) {
if ( !agentJar.delete() ) throw new UninstallException(
"Can't delete " + agentJar.getAbsolutePath() +
" - perhaps the installer does not have the access rights to do so.",
null);
}
File iniFile = new File(dir, "eclipse.ini");
StringBuilder newContents = new StringBuilder();
if ( iniFile.exists() ) {
try {
FileInputStream fis = new FileInputStream(iniFile);
try {
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line;
while ( (line = br.readLine()) != null ) {
if ( JAVA_AGENT_LINE_MATCHER.matcher(line).matches() ) continue;
Matcher m = BOOTCLASSPATH_LINE_MATCHER.matcher(line);
if ( m.matches() ) {
StringBuilder elemBuilder = new StringBuilder();
elemBuilder.append("-Xbootclasspath/a:");
boolean first = true;
for ( String elem : m.group(1).split(Pattern.quote(File.pathSeparator)) ) {
if ( elem.toLowerCase().endsWith("lombok.jar") ) continue;
if ( elem.toLowerCase().endsWith("lombok.eclipse.agent.jar") ) continue;
if ( first ) first = false;
else elemBuilder.append(File.pathSeparator);
elemBuilder.append(elem);
}
if ( !first ) newContents.append(elemBuilder.toString()).append(OS_NEWLINE);
continue;
}
newContents.append(line).append(OS_NEWLINE);
}
} finally {
fis.close();
}
FileOutputStream fos = new FileOutputStream(iniFile);
try {
fos.write(newContents.toString().getBytes());
} finally {
fos.close();
}
} catch ( IOException e ) {
throw new UninstallException("Cannot uninstall lombok from " + path.getAbsolutePath() +
" probably because this installer does not have the access rights to do so.", e);
}
}
}
}
/** Thrown when installing lombok fails. */
class InstallException extends Exception {
private static final long serialVersionUID = 1L;
public InstallException(String message, Throwable cause) {
super(message, cause);
}
}
/**
* Install lombok into the Eclipse at this location.
* If lombok is already there, it is overwritten neatly (upgrade mode).
*
* @throws InstallException If there's an obvious I/O problem that is preventing installation.
* bugs in the install code will probably throw other exceptions; this is intentional.
*/
void install() throws InstallException {
List<File> failedDirs = new ArrayList<File>();
boolean installSucceeded = false;
for ( File dir : getTargetDirs() ) {
File iniFile = new File(dir, "eclipse.ini");
StringBuilder newContents = new StringBuilder();
if ( !iniFile.exists() ) failedDirs.add(dir);
else {
//If 'installSucceeded' is true here, something very weird is going on, but instrumenting all of them
//is no less bad than aborting, and this situation should be rare to the point of non-existence.
File lombokJar = new File(iniFile.getParentFile(), "lombok.jar");
File agentJar = new File(iniFile.getParentFile(), "lombok.eclipse.agent.jar");
File ourJar = EclipseFinder.findOurJar();
byte[] b = new byte[524288];
boolean readSucceeded = false;
try {
JarFile jar = new JarFile(ourJar);
try {
ZipEntry entry = jar.getEntry("lombok.eclipse.agent.jar");
InputStream in = jar.getInputStream(entry);
FileOutputStream out = new FileOutputStream(agentJar);
try {
while ( true ) {
int r = in.read(b);
if ( r == -1 ) break;
readSucceeded = true;
out.write(b, 0, r);
}
} finally {
out.close();
}
} finally {
jar.close();
}
FileOutputStream out = new FileOutputStream(lombokJar);
InputStream in = new FileInputStream(ourJar);
try {
while ( true ) {
int r = in.read(b);
if ( r == -1 ) break;
out.write(b, 0, r);
}
} finally {
out.close();
}
} catch ( IOException e ) {
try {
lombokJar.delete();
agentJar.delete();
} catch ( Throwable ignore ) {}
if ( !readSucceeded ) throw new InstallException("I can't read my own jar file. I think you've found a bug in this installer! I suggest you restart it " +
"and use the 'what do I do' link, to manually install lombok. And tell us about this. Thanks!", e);
throw new InstallException("I can't write to your Eclipse directory, probably because this installer does not have the access rights.", e);
}
try {
FileInputStream fis = new FileInputStream(iniFile);
try {
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line;
while ( (line = br.readLine()) != null ) {
if ( JAVA_AGENT_LINE_MATCHER.matcher(line).matches() ) continue;
Matcher m = BOOTCLASSPATH_LINE_MATCHER.matcher(line);
if ( m.matches() ) {
StringBuilder elemBuilder = new StringBuilder();
elemBuilder.append("-Xbootclasspath/a:");
boolean first = true;
for ( String elem : m.group(1).split(Pattern.quote(File.pathSeparator)) ) {
if ( elem.toLowerCase().endsWith("lombok.jar") ) continue;
if ( elem.toLowerCase().endsWith("lombok.eclipse.agent.jar") ) continue;
if ( first ) first = false;
else elemBuilder.append(File.pathSeparator);
elemBuilder.append(elem);
}
if ( !first ) newContents.append(elemBuilder.toString()).append(OS_NEWLINE);
continue;
}
newContents.append(line).append(OS_NEWLINE);
}
} finally {
fis.close();
}
newContents.append("-javaagent:lombok.eclipse.agent.jar").append(OS_NEWLINE);
newContents.append("-Xbootclasspath/a:lombok.jar" + File.pathSeparator + "lombok.eclipse.agent.jar").append(OS_NEWLINE);
FileOutputStream fos = new FileOutputStream(iniFile);
try {
fos.write(newContents.toString().getBytes());
} finally {
fos.close();
}
installSucceeded = true;
} catch ( IOException e ) {
throw new InstallException("Cannot install lombok at " + path.getAbsolutePath() +
" probably because this installer does not have the access rights to do so.", e);
} finally {
if ( !installSucceeded ) try {
lombokJar.delete();
agentJar.delete();
} catch ( Throwable ignore ) {}
}
}
}
if ( !installSucceeded ) {
throw new InstallException("I can't find the eclipse.ini file. Is this a real Eclipse installation?", null);
}
for ( File dir : failedDirs ) {
//If we're updating the old installation might have worked by putting the lombok jars in a different place.
//We'll delete these old files.
try {
new File(dir, "lombok.jar").delete();
new File(dir, "lombok.eclipse.agent.jar").delete();
} catch ( Throwable ignore ) {}
}
}
}
|