/
ApiProblem.java
621 lines (578 loc) · 18.2 KB
/
ApiProblem.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
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
/*******************************************************************************
* Copyright (c) 2008, 2021 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.pde.api.tools.internal.problems;
import org.eclipse.core.runtime.IPath;
import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin;
import org.eclipse.pde.api.tools.internal.provisional.descriptors.IElementDescriptor;
import org.eclipse.pde.api.tools.internal.provisional.problems.IApiProblem;
import org.eclipse.pde.api.tools.internal.util.Util;
/**
* A description of an API problem
*
* @since 1.0.0
*/
public class ApiProblem implements IApiProblem {
/**
* Human readable message for the problem
*/
private String fMessage = null;
/**
* The resource path for this problem
*/
private String fResourcePath = null;
/**
* The qualified type name for this problem
*/
private String fTypeName = null;
/**
* The composite id of the problem. Contains the category, element kind,
* kind, and flags for a specific problem
*/
private int fId = 0;
/**
* The listing of extra argument ids for the problem
*/
private String[] fExtraArgumentIds = null;
/**
* The listing of corresponding arguments for the problem
*/
private Object[] fExtraArguments = null;
/**
* The listing of arguments used to fill localized messages
*/
private String[] fMessageArguments = null;
/**
* The line number the problem occurred on
*/
private int fLineNumber = -1;
/**
* The start of a character selection range
*/
private int fCharStart = -1;
/**
* The end of a character selection range
*/
private int fCharEnd = -1;
/**
* Masks to get the original bits out of the id
*/
public static final int CATEGORY_MASK = 0xF0000000;
public static final int ELEMENT_KIND_MASK = 0x0F000000;
public static final int KIND_MASK = 0x00F00000;
public static final int FLAGS_MASK = 0x000FF000;
public static final int MESSAGE_MASK = 0x00000FFF;
/**
* Constant representing the name of the 'compatibilityStatus' attribute on
* API problem for the compatibility category. Value is:
* <code>compatibilityStatus</code>
*/
public static final String COMPATIBILITY_STATUS = "compatibilityStatus"; //$NON-NLS-1$
/**
* Constructor
*
* @param resource the resource this problem occurs on / in
* @param typeName the qualified type name this problem occurs on / in
* @param messageargs arguments to be passed into a localized message for
* the problem
* @param argumentids the ids of arguments passed into the problem
* @param arguments the arguments that correspond to the listing of ids
* @param linenumber the line this problem occurred on
* @param charstart the char selection start position
* @param charend the char selection end position
* @param severity the severity level of the problem
* @param id the id of the problem
*/
public ApiProblem(String path, String typeName, String[] messageargs, String[] argumentids, Object[] arguments, int linenumber, int charstart, int charend, int id) {
this.fResourcePath = path;
this.fTypeName = typeName;
this.fId = id;
this.fExtraArgumentIds = argumentids;
this.fExtraArguments = arguments;
this.fLineNumber = linenumber;
this.fCharStart = charstart;
this.fCharEnd = charend;
this.fMessageArguments = messageargs;
}
@Override
public int getId() {
return fId;
}
@Override
public int getCategory() {
return (fId & CATEGORY_MASK);
}
@Override
public int getMessageid() {
return (fId & MESSAGE_MASK) >> OFFSET_MESSAGE;
}
@Override
public int getFlags() {
return (fId & FLAGS_MASK) >> OFFSET_FLAGS;
}
@Override
public int getKind() {
return (fId & KIND_MASK) >> OFFSET_KINDS;
}
@Override
public String getMessage() {
if (fMessage == null) {
fMessage = ApiProblemFactory.getLocalizedMessage(this);
}
return fMessage;
}
@Override
public String getResourcePath() {
return fResourcePath;
}
@Override
public int getSeverity() {
if (ApiPlugin.isRunningInFramework()) {
return ApiPlugin.getDefault().getSeverityLevel(ApiProblemFactory.getProblemSeverityId(this), null);
}
return ApiPlugin.SEVERITY_WARNING;
}
@Override
public int getElementKind() {
return (fId & ELEMENT_KIND_MASK) >> OFFSET_ELEMENT;
}
@Override
public int getLineNumber() {
return fLineNumber;
}
@Override
public int getCharStart() {
return fCharStart;
}
@Override
public int getCharEnd() {
return fCharEnd;
}
@Override
public String[] getExtraMarkerAttributeIds() {
if (fExtraArguments == null || fExtraArgumentIds == null) {
return new String[0];
}
if (fExtraArgumentIds.length != fExtraArguments.length) {
return new String[0];
}
return fExtraArgumentIds;
}
@Override
public Object[] getExtraMarkerAttributeValues() {
if (fExtraArguments == null || fExtraArgumentIds == null) {
return new String[0];
}
if (fExtraArgumentIds.length != fExtraArguments.length) {
return new String[0];
}
return fExtraArguments;
}
@Override
public String[] getMessageArguments() {
if (fMessageArguments == null) {
return new String[0];
}
return fMessageArguments;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof IApiProblem problem) {
if (problem.getId() == fId && argumentsEqual(problem.getMessageArguments())) {
String resourcePath = problem.getResourcePath();
if (resourcePath == null) {
if (this.fResourcePath != null) {
return false;
}
} else if (this.fResourcePath == null) {
return false;
} else if (!IPath.fromOSString(resourcePath).equals(IPath.fromOSString(fResourcePath))) {
return false;
}
String typeName = problem.getTypeName();
if (typeName == null) {
if (this.fTypeName != null) {
return false;
}
return true;
} else if (this.fTypeName == null) {
return false;
}
return typeName.equals(this.fTypeName);
}
return false;
}
return super.equals(obj);
}
/**
* Compares the complete list of message arguments
*
* @param arguments
* @return true if all of the arguments are equal, false otherwise
*/
private boolean argumentsEqual(String[] arguments) {
String[] currentArguments = getMessageArguments();
if ((currentArguments == null && arguments != null) || (currentArguments != null && arguments == null)) {
return false;
}
boolean equal = true;
if (currentArguments.length != arguments.length) {
return false;
}
for (int i = 0; i < currentArguments.length; i++) {
equal &= currentArguments[i].equals(arguments[i]);
}
return equal;
}
/**
* Returns the deep hash code of the complete listing of message arguments
*
* @param arguments
* @return the hash code of the message arguments
*/
private int argumentsHashcode(String[] arguments) {
if (fMessageArguments == null) {
return 0;
}
int hashcode = 0;
for (String fMessageArgument : fMessageArguments) {
hashcode += fMessageArgument.hashCode();
}
return hashcode;
}
@Override
public String toString() {
StringBuilder buffer = new StringBuilder();
buffer.append("API problem: "); //$NON-NLS-1$
buffer.append(fResourcePath != null ? fResourcePath : "no path").append("\n"); //$NON-NLS-1$//$NON-NLS-2$
buffer.append("\tseverity: "); //$NON-NLS-1$
buffer.append(Util.getSeverity(getSeverity())).append("\n"); //$NON-NLS-1$
buffer.append("\tcategory: "); //$NON-NLS-1$
buffer.append(ApiProblem.getProblemCategory(getCategory())).append("\n"); //$NON-NLS-1$
buffer.append("\telement kind: "); //$NON-NLS-1$
buffer.append(ApiProblem.getProblemElementKind(getCategory(), getElementKind())).append("\n"); //$NON-NLS-1$
buffer.append("\tkind: "); //$NON-NLS-1$
buffer.append(ApiProblem.getProblemKind(getCategory(), getKind())).append("\n"); //$NON-NLS-1$
buffer.append("\tflags: "); //$NON-NLS-1$
buffer.append(ApiProblem.getProblemFlagsName(getCategory(), getFlags())).append("\n"); //$NON-NLS-1$
buffer.append("\tmessage id: "); //$NON-NLS-1$
buffer.append(getMessageid());
buffer.append("\n\tmessage: "); //$NON-NLS-1$
buffer.append(getMessage());
return buffer.toString();
}
@Override
public int hashCode() {
return getId() + (fResourcePath != null ? fResourcePath.hashCode() : 0) + argumentsHashcode(fMessageArguments) + (fTypeName != null ? fTypeName.hashCode() : 0);
}
@Override
public String getTypeName() {
return this.fTypeName;
}
/**
* Returns the string representation of an element descriptor type or
* <code>null</code> if the kind is unknown
*
* @param kind
* @return the string of the kind or <code>null</code>
*/
public static String getDescriptorKind(int kind) {
switch (kind) {
case IElementDescriptor.PACKAGE: {
return "PACKAGE"; //$NON-NLS-1$
}
case IElementDescriptor.FIELD: {
return "FIELD"; //$NON-NLS-1$
}
case IElementDescriptor.METHOD: {
return "METHOD"; //$NON-NLS-1$
}
case IElementDescriptor.TYPE: {
return "REFERENCE_TYPE"; //$NON-NLS-1$
}
case IElementDescriptor.RESOURCE: {
return "RESOURCE"; //$NON-NLS-1$
}
default: {
return Util.UNKNOWN_ELEMENT_KIND;
}
}
}
/**
* Returns the string representation of the kind of since tab api problem
*
* @param kind
* @return the string for the since tag api problem kind
*/
public static String getTagsProblemKindName(int kind) {
switch (kind) {
case IApiProblem.SINCE_TAG_INVALID:
return "INVALID_SINCE_TAGS"; //$NON-NLS-1$
case IApiProblem.SINCE_TAG_MALFORMED:
return "MALFORMED_SINCE_TAGS"; //$NON-NLS-1$
case IApiProblem.SINCE_TAG_MISSING:
return "MISSING_SINCE_TAGS"; //$NON-NLS-1$
default:
return Util.UNKNOWN_KIND;
}
}
/**
* Returns the string representation of the kind of usage problem for an
* {@link IApiProblem} kind
*
* @param kind
* @return the string for the usage API problem kind
*/
public static String getUsageProblemKindName(int kind) {
switch (kind) {
case IApiProblem.ILLEGAL_EXTEND:
return "ILLEGAL_EXTEND"; //$NON-NLS-1$
case IApiProblem.ILLEGAL_IMPLEMENT:
return "ILLEGAL_IMPLEMENT"; //$NON-NLS-1$
case IApiProblem.ILLEGAL_INSTANTIATE:
return "ILLEGAL_INSTANTIATE"; //$NON-NLS-1$
case IApiProblem.ILLEGAL_REFERENCE:
return "ILLEGAL_REFERENCE"; //$NON-NLS-1$
case IApiProblem.ILLEGAL_OVERRIDE:
return "ILLEGAL_OVERRIDE"; //$NON-NLS-1$
case IApiProblem.API_LEAK:
return "API_LEAK"; //$NON-NLS-1$
case IApiProblem.UNSUPPORTED_TAG_USE:
return "UNSUPPORTED_TAG_USE"; //$NON-NLS-1$
case IApiProblem.UNSUPPORTED_ANNOTATION_USE:
return "UNSUPPORTED_ANNOTATION_USE"; //$NON-NLS-1$
case IApiProblem.INVALID_REFERENCE_IN_SYSTEM_LIBRARIES:
return "INVALID_REFERENCE_IN_SYSTEM_LIBRARIES"; //$NON-NLS-1$
case IApiProblem.UNUSED_PROBLEM_FILTERS:
return "UNUSED_PROBLEM_FILTERS"; //$NON-NLS-1$
default:
return Util.UNKNOWN_KIND;
}
}
/**
* Returns the string representation of the version problem kind.
*
* @param kind
* @return the string of the version API problem kind
*/
public static String getVersionProblemKindName(int kind) {
switch (kind) {
case IApiProblem.MINOR_VERSION_CHANGE:
return "MINOR_VERSION_CHANGE"; //$NON-NLS-1$
case IApiProblem.MAJOR_VERSION_CHANGE:
return "MAJOR_VERSION_CHANGE"; //$NON-NLS-1$
case IApiProblem.MAJOR_VERSION_CHANGE_NO_BREAKAGE:
return "MAJOR_VERSION_CHANGE_NO_BREAKAGE"; //$NON-NLS-1$
case IApiProblem.MINOR_VERSION_CHANGE_NO_NEW_API:
case IApiProblem.MICRO_VERSION_CHANGE_UNNECESSARILY:
case IApiProblem.MINOR_VERSION_CHANGE_UNNECESSARILY:
return "MINOR_VERSION_CHANGE_NO_NEW_API"; //$NON-NLS-1$
case IApiProblem.REEXPORTED_MAJOR_VERSION_CHANGE:
return "REEXPORTED_MAJOR_VERSION_CHANGE"; //$NON-NLS-1$
case IApiProblem.REEXPORTED_REMOVAL_OF_REEXPORT_MAJOR_VERSION_CHANGE:
return "REEXPORTED_REMOVAL_OF_REEXPORT_MAJOR_VERSION_CHANGE"; //$NON-NLS-1$
case IApiProblem.REEXPORTED_MINOR_VERSION_CHANGE:
return "REEXPORTED_MINOR_VERSION_CHANGE"; //$NON-NLS-1$
default:
return Util.UNKNOWN_KIND;
}
}
/**
* Returns the string representation of the API component problem kind
*
* @param kind
* @return the string of the API component problem kind
*/
public static String getApiComponentResolutionProblemKindName(int kind) {
switch (kind) {
case IApiProblem.API_COMPONENT_RESOLUTION:
return "API_COMPONENT_RESOLUTION"; //$NON-NLS-1$
default:
return Util.UNKNOWN_KIND;
}
}
/**
* Returns the string representation of the API baseline problem kind
*
* @param kind
* @return the string of the API baseline problem kind
*/
public static String getApiBaselineProblemKindName(int kind) {
switch (kind) {
case IApiProblem.API_BASELINE_MISSING:
case IApiProblem.API_BASELINE_MISMATCH:
return "API_BASELINE_MISSING"; //$NON-NLS-1$
case IApiProblem.API_PLUGIN_NOT_PRESENT_IN_BASELINE:
return "API_NOT_BASELINE"; //$NON-NLS-1$
default:
return Util.UNKNOWN_KIND;
}
}
/**
* Returns the string representation of the API Use Scan breakage problem
* kind
*
* @param kind
* @return
*/
public static String getApiUseScanProblemKindName(int kind) {
switch (kind) {
case IApiProblem.API_USE_SCAN_TYPE_PROBLEM:
return "API_USE_SCAN_TYPE_PROBLEM"; //$NON-NLS-1$
case IApiProblem.API_USE_SCAN_METHOD_PROBLEM:
return "API_USE_SCAN_METHOD_PROBLEM"; //$NON-NLS-1$
case IApiProblem.API_USE_SCAN_FIELD_PROBLEM:
return "API_USE_SCAN_FIELD_PROBLEM"; //$NON-NLS-1$
default:
return Util.UNKNOWN_KIND;
}
}
/**
* Returns the string representation of the kind of an {@link IApiProblem},
* given its category
*
* @param category
* @param kind
* @return the string of the {@link IApiProblem} kind
*/
public static String getProblemKind(int category, int kind) {
switch (category) {
case IApiProblem.CATEGORY_COMPATIBILITY:
return Util.getDeltaKindName(kind);
case IApiProblem.CATEGORY_SINCETAGS:
return ApiProblem.getTagsProblemKindName(kind);
case IApiProblem.CATEGORY_USAGE:
return ApiProblem.getUsageProblemKindName(kind);
case IApiProblem.CATEGORY_VERSION:
return ApiProblem.getVersionProblemKindName(kind);
case IApiProblem.CATEGORY_API_BASELINE:
return ApiProblem.getApiBaselineProblemKindName(kind);
case IApiProblem.CATEGORY_API_COMPONENT_RESOLUTION:
return ApiProblem.getApiComponentResolutionProblemKindName(kind);
case IApiProblem.CATEGORY_API_USE_SCAN_PROBLEM:
return ApiProblem.getApiUseScanProblemKindName(kind);
default:
return Util.UNKNOWN_KIND;
}
}
/**
* Return the string representation of the flags for a problem
*
* @param category
* @param flags
* @return the string for the problem flags
*/
public static String getProblemFlagsName(int category, int flags) {
switch (category) {
case IApiProblem.CATEGORY_COMPATIBILITY:
return Util.getDeltaFlagsName(flags);
case IApiProblem.CATEGORY_SINCETAGS:
case IApiProblem.CATEGORY_USAGE:
case IApiProblem.CATEGORY_VERSION:
case IApiProblem.CATEGORY_API_BASELINE:
case IApiProblem.CATEGORY_API_COMPONENT_RESOLUTION: {
switch (flags) {
case IApiProblem.LEAK_EXTENDS:
return "LEAK_EXTENDS"; //$NON-NLS-1$
case IApiProblem.LEAK_FIELD:
return "LEAK_FIELD"; //$NON-NLS-1$
case IApiProblem.LEAK_IMPLEMENTS:
return "LEAK_IMPLEMENTS"; //$NON-NLS-1$
case IApiProblem.LEAK_METHOD_PARAMETER:
return "LEAK_METHOD_PARAMETER"; //$NON-NLS-1$
case IApiProblem.LEAK_CONSTRUCTOR_PARAMETER:
return "LEAK_CONSTRUCTOR_PARAMETER"; //$NON-NLS-1$
case IApiProblem.LEAK_RETURN_TYPE:
return "LEAK_RETURN_TYPE"; //$NON-NLS-1$
case IApiProblem.CONSTRUCTOR_METHOD:
return "CONSTRUCTOR_METHOD"; //$NON-NLS-1$
case IApiProblem.NO_FLAGS:
return "NO_FLAGS"; //$NON-NLS-1$
case IApiProblem.INDIRECT_REFERENCE:
return "INDIRECT_REFERENCE"; //$NON-NLS-1$
case IApiProblem.METHOD:
return "METHOD"; //$NON-NLS-1$
case IApiProblem.FIELD:
return "FIELD"; //$NON-NLS-1$
default:
return Util.UNKNOWN_FLAGS;
}
}
case IApiProblem.CATEGORY_API_USE_SCAN_PROBLEM: {
switch (flags) {
case IApiProblem.API_USE_SCAN_DELETED:
return "DELETED"; //$NON-NLS-1$
case IApiProblem.API_USE_SCAN_UNRESOLVED:
return "UNRESOLVED"; //$NON-NLS-1$
default:
return Util.UNKNOWN_FLAGS;
}
}
default:
return Util.UNKNOWN_FLAGS;
}
}
/**
* Returns the string representation of the element kind of an
* {@link IApiProblem}, given its category
*
* @param category
* @param kind
* @return the string of the {@link IApiProblem} element kind
*/
public static String getProblemElementKind(int category, int kind) {
switch (category) {
case IApiProblem.CATEGORY_COMPATIBILITY:
case IApiProblem.CATEGORY_SINCETAGS: {
return Util.getDeltaElementType(kind);
}
case IApiProblem.CATEGORY_USAGE:
case IApiProblem.CATEGORY_VERSION:
case IApiProblem.CATEGORY_API_BASELINE:
case IApiProblem.CATEGORY_API_COMPONENT_RESOLUTION: {
return ApiProblem.getDescriptorKind(kind);
}
case IApiProblem.CATEGORY_API_USE_SCAN_PROBLEM: {
return Util.getApiElementType(kind);
}
default:
return Util.UNKNOWN_KIND;
}
}
/**
* Returns a string representation of the category of an API problem
*
* @param category
* @return the string of the API problem category
*/
public static String getProblemCategory(int category) {
switch (category) {
case IApiProblem.CATEGORY_COMPATIBILITY:
return "COMPATIBILITY"; //$NON-NLS-1$
case IApiProblem.CATEGORY_SINCETAGS:
return "SINCETAGS"; //$NON-NLS-1$
case IApiProblem.CATEGORY_USAGE:
return "USAGE"; //$NON-NLS-1$
case IApiProblem.CATEGORY_VERSION:
return "VERSION"; //$NON-NLS-1$
case IApiProblem.CATEGORY_API_BASELINE:
return "API_BASELINE"; //$NON-NLS-1$
case IApiProblem.CATEGORY_API_COMPONENT_RESOLUTION:
return "API_COMPONENT_RESOLUTION"; //$NON-NLS-1$
case IApiProblem.CATEGORY_API_USE_SCAN_PROBLEM:
return "API_USE_SCAN_PROBLEM"; //$NON-NLS-1$
default:
return "UNKNOWN_CATEGORY"; //$NON-NLS-1$
}
}
}