forked from szarnekow/eclipse.jdt.ui
/
SerialVersionHashOperation.java
440 lines (392 loc) · 15.3 KB
/
SerialVersionHashOperation.java
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
/*******************************************************************************
* Copyright (c) 2000, 2011 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.internal.ui.text.correction;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Comparator;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.filebuffers.FileBuffers;
import org.eclipse.core.filebuffers.ITextFileBuffer;
import org.eclipse.core.filebuffers.ITextFileBufferManager;
import org.eclipse.core.filebuffers.LocationKind;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.ui.PlatformUI;
import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IRegion;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.ToolFactory;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.ParameterizedType;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.core.util.IClassFileReader;
import org.eclipse.jdt.core.util.IFieldInfo;
import org.eclipse.jdt.core.util.IInnerClassesAttribute;
import org.eclipse.jdt.core.util.IInnerClassesAttributeEntry;
import org.eclipse.jdt.core.util.IMethodInfo;
import org.eclipse.jdt.internal.corext.fix.AbstractSerialVersionOperation;
import org.eclipse.jdt.internal.corext.fix.LinkedProposalModel;
import org.eclipse.jdt.internal.corext.util.Messages;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jdt.internal.ui.JavaPlugin;
/**
* Proposal for a hashed serial version id.
*
* @since 3.1
*/
public final class SerialVersionHashOperation extends AbstractSerialVersionOperation {
private static final String STATIC_CLASS_INITIALIZER= "<clinit>"; //$NON-NLS-1$
public static Long calculateSerialVersionId(ITypeBinding typeBinding, final IProgressMonitor monitor) throws CoreException, IOException {
try {
IFile classfileResource= getClassfile(typeBinding);
if (classfileResource == null)
return null;
InputStream contents= classfileResource.getContents();
try {
IClassFileReader cfReader= ToolFactory.createDefaultClassFileReader(contents, IClassFileReader.ALL);
if (cfReader != null) {
return calculateSerialVersionId(cfReader);
}
} finally {
contents.close();
}
return null;
} finally {
if (monitor != null)
monitor.done();
}
}
private static String getClassName(char[] name) {
return new String(name).replace('/', '.');
}
private static Long calculateSerialVersionId(IClassFileReader cfReader) throws IOException {
// implementing algorithm specified on http://download.oracle.com/javase/6/docs/platform/serialization/spec/class.html#4100
ByteArrayOutputStream os= new ByteArrayOutputStream();
DataOutputStream doos= new DataOutputStream(os);
doos.writeUTF(getClassName(cfReader.getClassName())); // class name
int mod= getClassModifiers(cfReader);
// System.out.println(Integer.toHexString(mod) + ' ' + Flags.toString(mod));
int classModifiers= mod & (Flags.AccPublic | Flags.AccFinal | Flags.AccInterface | Flags.AccAbstract);
doos.writeInt(classModifiers); // class modifiers
char[][] interfaces= getSortedInterfacesNames(cfReader);
for (int i= 0; i < interfaces.length; i++) {
doos.writeUTF(getClassName(interfaces[i]));
}
IFieldInfo[] sortedFields= getSortedFields(cfReader);
for (int i= 0; i < sortedFields.length; i++) {
IFieldInfo curr= sortedFields[i];
int flags= curr.getAccessFlags();
if (!Flags.isPrivate(flags) || (!Flags.isStatic(flags) && !Flags.isTransient(flags))) {
doos.writeUTF(new String(curr.getName()));
doos.writeInt(flags & (Flags.AccPublic | Flags.AccPrivate | Flags.AccProtected | Flags.AccStatic | Flags.AccFinal | Flags.AccVolatile | Flags.AccTransient)); // field modifiers
doos.writeUTF(new String(curr.getDescriptor()));
}
}
if (hasStaticClassInitializer(cfReader)) {
doos.writeUTF(STATIC_CLASS_INITIALIZER);
doos.writeInt(Flags.AccStatic);
doos.writeUTF("()V"); //$NON-NLS-1$
}
IMethodInfo[] sortedMethods= getSortedMethods(cfReader);
for (int i= 0; i < sortedMethods.length; i++) {
IMethodInfo curr= sortedMethods[i];
int flags= curr.getAccessFlags();
if (!Flags.isPrivate(flags) && !curr.isClinit()) {
doos.writeUTF(new String(curr.getName()));
doos.writeInt(flags & (Flags.AccPublic | Flags.AccPrivate | Flags.AccProtected | Flags.AccStatic | Flags.AccFinal | Flags.AccSynchronized | Flags.AccNative | Flags.AccAbstract | Flags.AccStrictfp)); // method modifiers
doos.writeUTF(getClassName(curr.getDescriptor()));
}
}
doos.flush();
return computeHash(os.toByteArray());
}
private static int getClassModifiers(IClassFileReader cfReader) {
IInnerClassesAttribute innerClassesAttribute= cfReader.getInnerClassesAttribute();
if (innerClassesAttribute != null) {
IInnerClassesAttributeEntry[] entries = innerClassesAttribute.getInnerClassAttributesEntries();
for (int i= 0; i < entries.length; i++) {
IInnerClassesAttributeEntry entry = entries[i];
char[] innerClassName = entry.getInnerClassName();
if (innerClassName != null) {
if (CharOperation.equals(cfReader.getClassName(), innerClassName)) {
return entry.getAccessFlags();
}
}
}
}
return cfReader.getAccessFlags();
}
// private static void print(byte[] bytes) {
// StringBuffer buf= new StringBuffer();
// for (int i= 0; i < bytes.length; i++) {
// char c= (char) bytes[i];
// if (!Character.isISOControl(c)) {
// buf.append(c).append(' ');
// } else {
// buf.append(Integer.toHexString(c)).append(' ');
// }
// }
// System.out.println(buf.toString());
// System.out.println();
// }
private static Long computeHash(byte[] bytes) {
try {
byte[] sha= MessageDigest.getInstance("SHA-1").digest(bytes); //$NON-NLS-1$
if (sha.length >= 8) {
long hash= 0;
for (int i= 7; i >= 0; i--) {
hash= (hash << 8) | (sha[i] & 0xFF);
}
return new Long(hash);
}
} catch (NoSuchAlgorithmException e) {
JavaPlugin.log(e);
}
return null;
}
private static char[][] getSortedInterfacesNames(IClassFileReader cfReader) {
char[][] interfaceNames= cfReader.getInterfaceNames();
Arrays.sort(interfaceNames, new Comparator<char[]>() {
public int compare(char[] o1, char[] o2) {
return CharOperation.compareTo(o1, o2);
}
});
return interfaceNames;
}
private static IFieldInfo[] getSortedFields(IClassFileReader cfReader) {
IFieldInfo[] allFields= cfReader.getFieldInfos();
Arrays.sort(allFields, new Comparator<IFieldInfo>() {
public int compare(IFieldInfo o1, IFieldInfo o2) {
return CharOperation.compareTo(o1.getName(), o2.getName());
}
});
return allFields;
}
private static boolean hasStaticClassInitializer(IClassFileReader cfReader) {
IMethodInfo[] methodInfos= cfReader.getMethodInfos();
for (int i= 0; i < methodInfos.length; i++) {
if (methodInfos[i].isClinit()) {
return true;
}
}
return false;
}
private static IMethodInfo[] getSortedMethods(IClassFileReader cfReader) {
IMethodInfo[] allMethods= cfReader.getMethodInfos();
Arrays.sort(allMethods, new Comparator<IMethodInfo>() {
public int compare(IMethodInfo mi1, IMethodInfo mi2) {
if (mi1.isConstructor() != mi2.isConstructor()) {
return mi1.isConstructor() ? -1 : 1;
} else if (mi1.isConstructor()) {
return 0;
}
int res= CharOperation.compareTo(mi1.getName(), mi2.getName());
if (res != 0) {
return res;
}
return CharOperation.compareTo(mi1.getDescriptor(), mi2.getDescriptor());
}
});
return allMethods;
}
private static IFile getClassfile(ITypeBinding typeBinding) throws CoreException {
// bug 191943
IType type= (IType) typeBinding.getJavaElement();
if (type == null || type.getCompilationUnit() == null) {
return null;
}
IRegion region= JavaCore.newRegion();
region.add(type.getCompilationUnit());
String name= typeBinding.getBinaryName();
if (name != null) {
int packStart= name.lastIndexOf('.');
if (packStart != -1) {
name= name.substring(packStart + 1);
}
} else {
throw new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, CorrectionMessages.SerialVersionHashOperation_error_classnotfound));
}
name += ".class"; //$NON-NLS-1$
IResource[] classFiles= JavaCore.getGeneratedResources(region, false);
for (int i= 0; i < classFiles.length; i++) {
IResource resource= classFiles[i];
if (resource.getType() == IResource.FILE && resource.getName().equals(name)) {
return (IFile) resource;
}
}
throw new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, CorrectionMessages.SerialVersionHashOperation_error_classnotfound));
}
/**
* Displays an appropriate error message for a specific problem.
*
* @param message
* The message to display
*/
private static void displayErrorMessage(final String message) {
final Display display= PlatformUI.getWorkbench().getDisplay();
if (display != null && !display.isDisposed()) {
display.asyncExec(new Runnable() {
public final void run() {
if (!display.isDisposed()) {
final Shell shell= display.getActiveShell();
if (shell != null && !shell.isDisposed())
MessageDialog.openError(shell, CorrectionMessages.SerialVersionHashOperation_dialog_error_caption, Messages.format(CorrectionMessages.SerialVersionHashOperation_dialog_error_message, message));
}
}
});
}
}
/**
* Displays an appropriate error message for a specific problem.
*
* @param throwable
* the throwable object to display
*/
private static void displayErrorMessage(final Throwable throwable) {
displayErrorMessage(throwable.getLocalizedMessage());
}
/**
* Displays a dialog with a question as message.
*
* @param title
* The title to display
* @param message
* The message to display
* @return returns the result of the dialog
*/
private static boolean displayYesNoMessage(final String title, final String message) {
final boolean[] result= { true};
final Display display= PlatformUI.getWorkbench().getDisplay();
if (display != null && !display.isDisposed()) {
display.syncExec(new Runnable() {
public final void run() {
if (!display.isDisposed()) {
final Shell shell= display.getActiveShell();
if (shell != null && !shell.isDisposed())
result[0]= MessageDialog.openQuestion(shell, title, message);
}
}
});
}
return result[0];
}
private final ICompilationUnit fCompilationUnit;
public SerialVersionHashOperation(ICompilationUnit unit, ASTNode[] nodes) {
super(unit, nodes);
fCompilationUnit= unit;
}
/**
* {@inheritDoc}
*/
@Override
protected boolean addInitializer(final VariableDeclarationFragment fragment, final ASTNode declarationNode) {
Assert.isNotNull(fragment);
try {
PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {
public final void run(final IProgressMonitor monitor) throws InterruptedException {
Assert.isNotNull(monitor);
String id= computeId(declarationNode, monitor);
fragment.setInitializer(fragment.getAST().newNumberLiteral(id));
}
});
} catch (InvocationTargetException exception) {
JavaPlugin.log(exception);
} catch (InterruptedException exception) {
// Do nothing
}
return true;
}
/**
* {@inheritDoc}
*/
@Override
protected void addLinkedPositions(ASTRewrite rewrite, VariableDeclarationFragment fragment, LinkedProposalModel positionGroups) {
//Do nothing
}
private String computeId(final ASTNode declarationNode, final IProgressMonitor monitor) throws InterruptedException {
Assert.isNotNull(monitor);
long serialVersionID= SERIAL_VALUE;
try {
monitor.beginTask(CorrectionMessages.SerialVersionHashOperation_computing_id, 200);
final IJavaProject project= fCompilationUnit.getJavaProject();
final IPath path= fCompilationUnit.getResource().getFullPath();
ITextFileBufferManager bufferManager= FileBuffers.getTextFileBufferManager();
try {
bufferManager.connect(path, LocationKind.IFILE, new SubProgressMonitor(monitor, 10));
if (monitor.isCanceled())
throw new InterruptedException();
final ITextFileBuffer buffer= bufferManager.getTextFileBuffer(path, LocationKind.IFILE);
if (buffer.isDirty() && buffer.isStateValidated() && buffer.isCommitable() && displayYesNoMessage(CorrectionMessages.SerialVersionHashOperation_save_caption, CorrectionMessages.SerialVersionHashOperation_save_message))
buffer.commit(new SubProgressMonitor(monitor, 20), true);
else
monitor.worked(20);
if (monitor.isCanceled())
throw new InterruptedException();
} finally {
bufferManager.disconnect(path, LocationKind.IFILE, new SubProgressMonitor(monitor, 10));
}
project.getProject().build(IncrementalProjectBuilder.INCREMENTAL_BUILD, new SubProgressMonitor(monitor, 60));
if (monitor.isCanceled())
throw new InterruptedException();
ITypeBinding typeBinding= getTypeBinding(declarationNode);
if (typeBinding != null) {
Long id= calculateSerialVersionId(typeBinding, new SubProgressMonitor(monitor, 100));
if (id != null)
serialVersionID= id.longValue();
}
} catch (CoreException exception) {
displayErrorMessage(exception);
} catch (IOException exception) {
displayErrorMessage(exception);
} finally {
monitor.done();
}
return serialVersionID + LONG_SUFFIX;
}
private static ITypeBinding getTypeBinding(final ASTNode parent) {
if (parent instanceof AbstractTypeDeclaration) {
final AbstractTypeDeclaration declaration= (AbstractTypeDeclaration) parent;
return declaration.resolveBinding();
} else if (parent instanceof AnonymousClassDeclaration) {
final AnonymousClassDeclaration declaration= (AnonymousClassDeclaration) parent;
return declaration.resolveBinding();
} else if (parent instanceof ParameterizedType) {
final ParameterizedType type= (ParameterizedType) parent;
return type.resolveBinding();
}
return null;
}
}