-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
HighlightVisitorImpl.java
1826 lines (1648 loc) · 88.3 KB
/
HighlightVisitorImpl.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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInsight.daemon.impl.analysis;
import com.intellij.codeHighlighting.Pass;
import com.intellij.codeInsight.daemon.JavaErrorMessages;
import com.intellij.codeInsight.daemon.impl.*;
import com.intellij.codeInsight.daemon.impl.analysis.HighlightUtil.Feature;
import com.intellij.codeInsight.daemon.impl.quickfix.AdjustFunctionContextFix;
import com.intellij.codeInsight.daemon.impl.quickfix.QuickFixAction;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInsight.intention.QuickFixFactory;
import com.intellij.lang.injection.InjectedLanguageManager;
import com.intellij.lang.jvm.JvmModifier;
import com.intellij.lang.jvm.JvmModifiersOwner;
import com.intellij.lang.jvm.actions.JvmElementActionFactories;
import com.intellij.lang.jvm.actions.MemberRequestsKt;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.colors.TextAttributesScheme;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.IndexNotReadyException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.JavaSdkVersion;
import com.intellij.openapi.projectRoots.JavaVersionService;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.TextRange;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
import com.intellij.psi.controlFlow.ControlFlowUtil;
import com.intellij.psi.impl.PsiImplUtil;
import com.intellij.psi.impl.source.javadoc.PsiDocMethodOrFieldRef;
import com.intellij.psi.impl.source.resolve.JavaResolveUtil;
import com.intellij.psi.impl.source.resolve.graphInference.PsiPolyExpressionUtil;
import com.intellij.psi.impl.source.tree.java.PsiReferenceExpressionImpl;
import com.intellij.psi.infos.CandidateInfo;
import com.intellij.psi.infos.MethodCandidateInfo;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.javadoc.PsiDocTagValue;
import com.intellij.psi.util.*;
import com.intellij.util.ObjectUtils;
import com.intellij.util.containers.MostlySingularMultiMap;
import gnu.trove.THashMap;
import gnu.trove.THashSet;
import gnu.trove.TObjectIntHashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
public class HighlightVisitorImpl extends JavaElementVisitor implements HighlightVisitor {
private HighlightInfoHolder myHolder;
private RefCountHolder myRefCountHolder; // can be null during partial file update
private LanguageLevel myLanguageLevel;
private JavaSdkVersion myJavaSdkVersion;
@SuppressWarnings("StatefulEp") private PsiFile myFile;
@SuppressWarnings("StatefulEp") private PsiJavaModule myJavaModule;
// map codeBlock->List of PsiReferenceExpression of uninitialized final variables
private final Map<PsiElement, Collection<PsiReferenceExpression>> myUninitializedVarProblems = new THashMap<>();
// map codeBlock->List of PsiReferenceExpression of extra initialization of final variable
private final Map<PsiElement, Collection<ControlFlowUtil.VariableInfo>> myFinalVarProblems = new THashMap<>();
// value==1: no info if the parameter was reassigned (but the parameter is present in current file), value==2: parameter was reassigned
private final TObjectIntHashMap<PsiParameter> myReassignedParameters = new TObjectIntHashMap<>();
private final Map<String, Pair<PsiImportStaticReferenceElement, PsiClass>> mySingleImportedClasses = new THashMap<>();
private final Map<String, Pair<PsiImportStaticReferenceElement, PsiField>> mySingleImportedFields = new THashMap<>();
private final PsiElementVisitor REGISTER_REFERENCES_VISITOR = new PsiRecursiveElementWalkingVisitor() {
@Override public void visitElement(PsiElement element) {
super.visitElement(element);
for (PsiReference reference : element.getReferences()) {
PsiElement resolved = reference.resolve();
if (resolved instanceof PsiNamedElement) {
myRefCountHolder.registerLocallyReferenced((PsiNamedElement)resolved);
if (resolved instanceof PsiMember) {
myRefCountHolder.registerReference(reference, new CandidateInfo(resolved, PsiSubstitutor.EMPTY));
}
}
}
}
};
private final Map<PsiClass, MostlySingularMultiMap<MethodSignature, PsiMethod>> myDuplicateMethods = new THashMap<>();
private final Set<PsiClass> myOverrideEquivalentMethodsVisitedClasses = new THashSet<>();
private static class Holder {
private static final boolean CHECK_ELEMENT_LEVEL = ApplicationManager.getApplication().isUnitTestMode() || ApplicationManager.getApplication().isInternal();
}
@NotNull
protected PsiResolveHelper getResolveHelper(@NotNull Project project) {
return PsiResolveHelper.SERVICE.getInstance(project);
}
protected HighlightVisitorImpl() {
}
@Deprecated
protected HighlightVisitorImpl(@SuppressWarnings("unused") @NotNull PsiResolveHelper psiResolveHelper) {
}
@NotNull
private MostlySingularMultiMap<MethodSignature, PsiMethod> getDuplicateMethods(@NotNull PsiClass aClass) {
MostlySingularMultiMap<MethodSignature, PsiMethod> signatures = myDuplicateMethods.get(aClass);
if (signatures == null) {
signatures = new MostlySingularMultiMap<>();
for (PsiMethod method : aClass.getMethods()) {
if (method instanceof ExternallyDefinedPsiElement) continue; // ignore aspectj-weaved methods; they are checked elsewhere
MethodSignature signature = method.getSignature(PsiSubstitutor.EMPTY);
signatures.add(signature, method);
}
myDuplicateMethods.put(aClass, signatures);
}
return signatures;
}
@NotNull
@Override
@SuppressWarnings("MethodDoesntCallSuperMethod")
public HighlightVisitorImpl clone() {
return new HighlightVisitorImpl();
}
@Override
public boolean suitableForFile(@NotNull PsiFile file) {
// both PsiJavaFile and PsiCodeFragment must match
return file instanceof PsiImportHolder && !InjectedLanguageManager.getInstance(file.getProject()).isInjectedFragment(file);
}
@Override
public void visit(@NotNull PsiElement element) {
if (Holder.CHECK_ELEMENT_LEVEL) {
((CheckLevelHighlightInfoHolder)myHolder).enterLevel(element);
element.accept(this);
((CheckLevelHighlightInfoHolder)myHolder).enterLevel(null);
}
else {
element.accept(this);
}
}
private void registerReferencesFromInjectedFragments(@NotNull PsiElement element) {
InjectedLanguageManager manager = InjectedLanguageManager.getInstance(myFile.getProject());
manager.enumerateEx(element, myFile, false, (injectedPsi, places) -> injectedPsi.accept(REGISTER_REFERENCES_VISITOR));
}
@Override
public boolean analyze(@NotNull PsiFile file, boolean updateWholeFile, @NotNull HighlightInfoHolder holder, @NotNull Runnable highlight) {
boolean success = true;
try {
prepare(Holder.CHECK_ELEMENT_LEVEL ? new CheckLevelHighlightInfoHolder(file, holder) : holder, file);
if (updateWholeFile) {
ProgressIndicator progress = ProgressManager.getInstance().getProgressIndicator();
if (progress == null) throw new IllegalStateException("Must be run under progress");
RefCountHolder refCountHolder = RefCountHolder.get(file);
myRefCountHolder = refCountHolder;
Project project = file.getProject();
Document document = PsiDocumentManager.getInstance(project).getDocument(file);
TextRange dirtyScope = Optional.ofNullable(document)
.map(doc -> DaemonCodeAnalyzerEx.getInstanceEx(project).getFileStatusMap().getFileDirtyScope(doc, Pass.UPDATE_ALL))
.orElse(file.getTextRange());
success = refCountHolder.analyze(file, dirtyScope, progress, () -> {
highlight.run();
ProgressManager.checkCanceled();
if (document != null) {
new PostHighlightingVisitor(file, document, refCountHolder).collectHighlights(holder, progress);
}
});
}
else {
myRefCountHolder = null;
highlight.run();
}
}
finally {
myUninitializedVarProblems.clear();
myFinalVarProblems.clear();
mySingleImportedClasses.clear();
mySingleImportedFields.clear();
myReassignedParameters.clear();
myRefCountHolder = null;
myJavaModule = null;
myFile = null;
myHolder = null;
myDuplicateMethods.clear();
myOverrideEquivalentMethodsVisitedClasses.clear();
}
return success;
}
protected void prepareToRunAsInspection(@NotNull HighlightInfoHolder holder) {
prepare(holder, holder.getContextFile());
}
private void prepare(@NotNull HighlightInfoHolder holder, @NotNull PsiFile file) {
myHolder = holder;
myFile = file;
myLanguageLevel = PsiUtil.getLanguageLevel(file);
myJavaSdkVersion = ObjectUtils
.notNull(JavaVersionService.getInstance().getJavaSdkVersion(file), JavaSdkVersion.fromLanguageLevel(myLanguageLevel));
myJavaModule = myLanguageLevel.isAtLeast(LanguageLevel.JDK_1_9) ? JavaModuleGraphUtil.findDescriptorByElement(file) : null;
}
@Override
public void visitElement(PsiElement element) {
if (myRefCountHolder != null && myFile instanceof ServerPageFile) {
// in JSP, XmlAttributeValue may contain java references
try {
for (PsiReference reference : element.getReferences()) {
JavaResolveResult result = resolveJavaReference(reference);
if (result != null) {
myRefCountHolder.registerReference(reference, result);
}
}
}
catch (IndexNotReadyException ignored) { }
}
if (!(myFile instanceof ServerPageFile)) {
myHolder.add(DefaultHighlightUtil.checkBadCharacter(element));
}
}
@Nullable
public static JavaResolveResult resolveJavaReference(@NotNull PsiReference reference) {
return reference instanceof PsiJavaReference ? ((PsiJavaReference)reference).advancedResolve(false) : null;
}
@Override
public void visitAnnotation(PsiAnnotation annotation) {
super.visitAnnotation(annotation);
if (!myHolder.hasErrorResults()) myHolder.add(checkFeature(annotation, Feature.ANNOTATIONS));
if (!myHolder.hasErrorResults()) myHolder.add(AnnotationsHighlightUtil.checkApplicability(annotation, myLanguageLevel, myFile));
if (!myHolder.hasErrorResults()) myHolder.add(AnnotationsHighlightUtil.checkAnnotationType(annotation));
if (!myHolder.hasErrorResults()) myHolder.add(AnnotationsHighlightUtil.checkMissingAttributes(annotation));
if (!myHolder.hasErrorResults()) myHolder.add(AnnotationsHighlightUtil.checkTargetAnnotationDuplicates(annotation));
if (!myHolder.hasErrorResults()) myHolder.add(AnnotationsHighlightUtil.checkDuplicateAnnotations(annotation, myLanguageLevel));
if (!myHolder.hasErrorResults()) myHolder.add(AnnotationsHighlightUtil.checkFunctionalInterface(annotation, myLanguageLevel));
if (!myHolder.hasErrorResults()) myHolder.add(AnnotationsHighlightUtil.checkRepeatableAnnotation(annotation));
if (CommonClassNames.JAVA_LANG_OVERRIDE.equals(annotation.getQualifiedName())) {
PsiAnnotationOwner owner = annotation.getOwner();
PsiElement parent = owner instanceof PsiModifierList ? ((PsiModifierList)owner).getParent() : null;
if (parent instanceof PsiMethod) {
myHolder.add(GenericsHighlightUtil.checkOverrideAnnotation((PsiMethod)parent, annotation, myLanguageLevel));
}
}
}
@Override
public void visitAnnotationArrayInitializer(PsiArrayInitializerMemberValue initializer) {
PsiMethod method = null;
PsiElement parent = initializer.getParent();
if (parent instanceof PsiNameValuePair) {
PsiReference reference = parent.getReference();
if (reference != null) {
method = (PsiMethod)reference.resolve();
}
}
else if (PsiUtil.isAnnotationMethod(parent)) {
method = (PsiMethod)parent;
}
if (method != null) {
PsiType type = method.getReturnType();
if (type instanceof PsiArrayType) {
type = ((PsiArrayType)type).getComponentType();
PsiAnnotationMemberValue[] initializers = initializer.getInitializers();
for (PsiAnnotationMemberValue initializer1 : initializers) {
myHolder.add(AnnotationsHighlightUtil.checkMemberValueType(initializer1, type));
}
}
}
}
@Override
public void visitAnnotationMethod(PsiAnnotationMethod method) {
PsiType returnType = method.getReturnType();
PsiAnnotationMemberValue value = method.getDefaultValue();
if (returnType != null && value != null) {
myHolder.add(AnnotationsHighlightUtil.checkMemberValueType(value, returnType));
}
PsiTypeElement typeElement = method.getReturnTypeElement();
if (typeElement != null) {
myHolder.add(AnnotationsHighlightUtil.checkValidAnnotationType(returnType, typeElement));
}
PsiClass aClass = method.getContainingClass();
if (typeElement != null && aClass != null) {
myHolder.add(AnnotationsHighlightUtil.checkCyclicMemberType(typeElement, aClass));
}
myHolder.add(AnnotationsHighlightUtil.checkClashesWithSuperMethods(method));
if (!myHolder.hasErrorResults() && aClass != null) {
myHolder.add(HighlightMethodUtil.checkDuplicateMethod(aClass, method, getDuplicateMethods(aClass)));
}
}
@Override
public void visitArrayInitializerExpression(PsiArrayInitializerExpression expression) {
super.visitArrayInitializerExpression(expression);
if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkArrayInitializerApplicable(expression));
if (!(expression.getParent() instanceof PsiNewExpression)) {
if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkGenericArrayCreation(expression, expression.getType()));
}
}
@Override
public void visitAssignmentExpression(PsiAssignmentExpression assignment) {
if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkAssignmentCompatibleTypes(assignment));
if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkAssignmentOperatorApplicable(assignment));
if (!myHolder.hasErrorResults()) visitExpression(assignment);
}
@Override
public void visitPolyadicExpression(PsiPolyadicExpression expression) {
super.visitPolyadicExpression(expression);
if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkPolyadicOperatorApplicable(expression));
}
@Override
public void visitLambdaExpression(PsiLambdaExpression expression) {
myHolder.add(checkFeature(expression, Feature.LAMBDA_EXPRESSIONS));
final PsiElement parent = PsiUtil.skipParenthesizedExprUp(expression.getParent());
if (toReportFunctionalExpressionProblemOnParent(parent)) return;
if (!myHolder.hasErrorResults() && !LambdaUtil.isValidLambdaContext(parent)) {
myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression)
.descriptionAndTooltip("Lambda expression not expected here").create());
}
if (!myHolder.hasErrorResults()) myHolder.add(LambdaHighlightingUtil.checkConsistentParameterDeclaration(expression));
PsiType functionalInterfaceType = null;
if (!myHolder.hasErrorResults()) {
functionalInterfaceType = expression.getFunctionalInterfaceType();
if (functionalInterfaceType != null) {
final String notFunctionalMessage = LambdaHighlightingUtil.checkInterfaceFunctional(functionalInterfaceType);
if (notFunctionalMessage != null) {
myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression)
.descriptionAndTooltip(notFunctionalMessage).create());
}
else {
checkFunctionalInterfaceTypeAccessible(expression, functionalInterfaceType);
}
}
else if (LambdaUtil.getFunctionalInterfaceType(expression, true) != null) {
myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip("Cannot infer functional interface type").create());
}
}
if (!myHolder.hasErrorResults() && functionalInterfaceType != null) {
String parentInferenceErrorMessage = null;
final PsiCallExpression callExpression = parent instanceof PsiExpressionList && parent.getParent() instanceof PsiCallExpression ?
(PsiCallExpression)parent.getParent() : null;
final JavaResolveResult containingCallResolveResult = callExpression != null ? callExpression.resolveMethodGenerics() : null;
if (containingCallResolveResult instanceof MethodCandidateInfo) {
parentInferenceErrorMessage = ((MethodCandidateInfo)containingCallResolveResult).getInferenceErrorMessage();
}
final Map<PsiElement, String> returnErrors = LambdaUtil.checkReturnTypeCompatible(expression, LambdaUtil.getFunctionalInterfaceReturnType(functionalInterfaceType));
if (parentInferenceErrorMessage != null && (returnErrors == null || !returnErrors.containsValue(parentInferenceErrorMessage))) {
HighlightInfo info =
HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(parentInferenceErrorMessage)
.create();
if (returnErrors != null) {
returnErrors.keySet().forEach(k -> QuickFixAction.registerQuickFixAction(info, AdjustFunctionContextFix.createFix(k)));
}
myHolder.add(info);
}
else if (returnErrors != null) {
for (Map.Entry<PsiElement, String> entry : returnErrors.entrySet()) {
HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR)
.range(entry.getKey())
.descriptionAndTooltip(entry.getValue()).create();
QuickFixAction.registerQuickFixAction(info, AdjustFunctionContextFix.createFix(entry.getKey()));
if (entry.getKey() instanceof PsiExpression) {
PsiExpression expr = (PsiExpression)entry.getKey();
HighlightFixUtil.registerLambdaReturnTypeFixes(info, expression, expr);
}
myHolder.add(info);
}
}
}
if (!myHolder.hasErrorResults() && functionalInterfaceType != null) {
final PsiClassType.ClassResolveResult resolveResult = PsiUtil.resolveGenericsClassInType(functionalInterfaceType);
final PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(resolveResult);
if (interfaceMethod != null) {
final PsiParameter[] parameters = interfaceMethod.getParameterList().getParameters();
myHolder.add(LambdaHighlightingUtil.checkParametersCompatible(expression, parameters, LambdaUtil.getSubstitutor(interfaceMethod, resolveResult)));
}
}
if (!myHolder.hasErrorResults()) {
final PsiElement body = expression.getBody();
if (body instanceof PsiCodeBlock) {
myHolder.add(HighlightControlFlowUtil.checkUnreachableStatement((PsiCodeBlock)body));
}
}
}
@Override
public void visitBreakStatement(PsiBreakStatement statement) {
super.visitBreakStatement(statement);
PsiExpression expression = statement.getExpression();
if (!myHolder.hasErrorResults() && expression == null) {
myHolder.add(HighlightUtil.checkBreakOutsideSwitchOrLoop(statement));
}
if (!myHolder.hasErrorResults() && Feature.ENHANCED_SWITCH.isAvailable(myFile)) {
myHolder.add(HighlightUtil.checkValueBreakExpression(statement, expression));
}
}
@Override
public void visitClass(PsiClass aClass) {
super.visitClass(aClass);
if (aClass instanceof PsiSyntheticClass) return;
if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkInterfaceMultipleInheritance(aClass));
if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkClassSupersAccessibility(aClass));
if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkDuplicateTopLevelClass(aClass));
if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkEnumMustNotBeLocal(aClass));
if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkImplicitThisReferenceBeforeSuper(aClass, myJavaSdkVersion));
if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkClassAndPackageConflict(aClass));
if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkPublicClassInRightFile(aClass));
if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkTypeParameterOverrideEquivalentMethods(aClass, myLanguageLevel));
}
@Override
public void visitClassInitializer(PsiClassInitializer initializer) {
super.visitClassInitializer(initializer);
if (!myHolder.hasErrorResults()) myHolder.add(HighlightControlFlowUtil.checkInitializerCompleteNormally(initializer));
if (!myHolder.hasErrorResults()) myHolder.add(HighlightControlFlowUtil.checkUnreachableStatement(initializer.getBody()));
if (!myHolder.hasErrorResults()) {
myHolder.add(HighlightClassUtil.checkThingNotAllowedInInterface(initializer, initializer.getContainingClass()));
}
}
@Override
public void visitClassObjectAccessExpression(PsiClassObjectAccessExpression expression) {
super.visitClassObjectAccessExpression(expression);
if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkClassObjectAccessExpression(expression));
}
@Override
public void visitComment(PsiComment comment) {
super.visitComment(comment);
if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkUnclosedComment(comment));
if (myRefCountHolder != null && !myHolder.hasErrorResults()) registerReferencesFromInjectedFragments(comment);
}
@Override
public void visitContinueStatement(PsiContinueStatement statement) {
super.visitContinueStatement(statement);
if (!myHolder.hasErrorResults()) {
PsiIdentifier label = statement.getLabelIdentifier();
myHolder.add(label == null ? HighlightUtil.checkContinueOutsideLoop(statement, myLanguageLevel)
: HighlightUtil.checkContinueTarget(statement, label, myLanguageLevel));
}
}
@Override
public void visitJavaToken(PsiJavaToken token) {
super.visitJavaToken(token);
if (!myHolder.hasErrorResults() && token.getTokenType() == JavaTokenType.RAW_STRING_LITERAL) {
myHolder.add(checkFeature(token, Feature.RAW_LITERALS));
}
if (!myHolder.hasErrorResults() && token.getTokenType() == JavaTokenType.RBRACE && token.getParent() instanceof PsiCodeBlock) {
PsiElement gParent = token.getParent().getParent();
PsiCodeBlock codeBlock;
PsiType returnType;
if (gParent instanceof PsiMethod) {
PsiMethod method = (PsiMethod)gParent;
codeBlock = method.getBody();
returnType = method.getReturnType();
}
else if (gParent instanceof PsiLambdaExpression) {
PsiElement body = ((PsiLambdaExpression)gParent).getBody();
if (!(body instanceof PsiCodeBlock)) return;
codeBlock = (PsiCodeBlock)body;
returnType = LambdaUtil.getFunctionalInterfaceReturnType((PsiLambdaExpression)gParent);
}
else {
return;
}
myHolder.add(HighlightControlFlowUtil.checkMissingReturnStatement(codeBlock, returnType));
}
}
@Override
public void visitDocComment(PsiDocComment comment) {
if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkUnclosedComment(comment));
}
@Override
public void visitDocTagValue(PsiDocTagValue value) {
PsiReference reference = value.getReference();
if (reference != null) {
PsiElement element = reference.resolve();
final TextAttributesScheme colorsScheme = myHolder.getColorsScheme();
if (element instanceof PsiMethod) {
PsiElement nameElement = ((PsiDocMethodOrFieldRef)value).getNameElement();
if (nameElement != null) {
myHolder.add(HighlightNamesUtil.highlightMethodName((PsiMethod)element, nameElement, false, colorsScheme));
}
}
else if (element instanceof PsiParameter) {
myHolder.add(HighlightNamesUtil.highlightVariableName((PsiVariable)element, value.getNavigationElement(), colorsScheme));
}
}
}
@Override
public void visitEnumConstant(PsiEnumConstant enumConstant) {
super.visitEnumConstant(enumConstant);
if (!myHolder.hasErrorResults()) GenericsHighlightUtil.checkEnumConstantForConstructorProblems(enumConstant, myHolder, myJavaSdkVersion);
if (!myHolder.hasErrorResults()) registerConstructorCall(enumConstant);
if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkUnhandledExceptions(enumConstant, enumConstant.getNameIdentifier().getTextRange()));
}
@Override
public void visitEnumConstantInitializer(PsiEnumConstantInitializer enumConstantInitializer) {
super.visitEnumConstantInitializer(enumConstantInitializer);
if (!myHolder.hasErrorResults()) {
TextRange textRange = HighlightNamesUtil.getClassDeclarationTextRange(enumConstantInitializer);
myHolder.add(HighlightClassUtil.checkClassMustBeAbstract(enumConstantInitializer, textRange));
}
}
@Override
public void visitExpression(PsiExpression expression) {
ProgressManager.checkCanceled(); // visitLiteralExpression is invoked very often in array initializers
super.visitExpression(expression);
PsiElement parent = expression.getParent();
PsiType type = expression.getType();
if (!myHolder.hasErrorResults() && parent instanceof PsiBreakStatement && !PsiImplUtil.isUnqualifiedReference(expression)) {
myHolder.add(checkFeature(expression, Feature.SWITCH_EXPRESSION));
}
if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkMustBeBoolean(expression, type));
if (!myHolder.hasErrorResults() && expression instanceof PsiArrayAccessExpression) {
myHolder.add(HighlightUtil.checkValidArrayAccessExpression((PsiArrayAccessExpression)expression));
}
if (!myHolder.hasErrorResults() && parent instanceof PsiNewExpression &&
((PsiNewExpression)parent).getQualifier() != expression && ((PsiNewExpression)parent).getArrayInitializer() != expression) {
myHolder.add(HighlightUtil.checkAssignability(PsiType.INT, expression.getType(), expression, expression)); // like in 'new String["s"]'
}
if (!myHolder.hasErrorResults()) myHolder.add(HighlightControlFlowUtil.checkCannotWriteToFinal(expression,myFile));
if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkVariableExpected(expression));
if (!myHolder.hasErrorResults()) myHolder.addAll(HighlightUtil.checkArrayInitializer(expression, type));
if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkTernaryOperatorConditionIsBoolean(expression, type));
if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkAssertOperatorTypes(expression, type));
if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkSynchronizedExpressionType(expression, type, myFile));
if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkConditionalExpressionBranchTypesMatch(expression, type));
if (!myHolder.hasErrorResults() && parent instanceof PsiThrowStatement && ((PsiThrowStatement)parent).getException() == expression && type != null) {
myHolder.add(HighlightUtil.checkMustBeThrowable(type, expression, true));
}
if (!myHolder.hasErrorResults()) myHolder.add(AnnotationsHighlightUtil.checkConstantExpression(expression));
if (!myHolder.hasErrorResults() && parent instanceof PsiForeachStatement && ((PsiForeachStatement)parent).getIteratedValue() == expression) {
myHolder.add(GenericsHighlightUtil.checkForeachExpressionTypeIsIterable(expression));
}
}
@Override
public void visitExpressionList(PsiExpressionList list) {
super.visitExpressionList(list);
PsiElement parent = list.getParent();
if (parent instanceof PsiMethodCallExpression) {
PsiMethodCallExpression expression = (PsiMethodCallExpression)parent;
if (expression.getArgumentList() == list) {
PsiReferenceExpression referenceExpression = expression.getMethodExpression();
JavaResolveResult[] results = resolveOptimised(referenceExpression);
if (results == null) return;
JavaResolveResult result = results.length == 1 ? results[0] : JavaResolveResult.EMPTY;
PsiElement resolved = result.getElement();
if ((!result.isAccessible() || !result.isStaticsScopeCorrect()) &&
!HighlightMethodUtil.isDummyConstructorCall(expression, getResolveHelper(myHolder.getProject()), list, referenceExpression) &&
// this check is for fake expression from JspMethodCallImpl
referenceExpression.getParent() == expression) {
try {
if (PsiTreeUtil.findChildrenOfType(expression.getArgumentList(), PsiLambdaExpression.class).isEmpty()) {
myHolder.add(HighlightMethodUtil.checkAmbiguousMethodCallArguments(referenceExpression, results, list, resolved, result, expression, getResolveHelper(myHolder.getProject()), list));
}
}
catch (IndexNotReadyException ignored) { }
}
}
}
}
@Override
public void visitField(PsiField field) {
super.visitField(field);
if (!myHolder.hasErrorResults()) myHolder.add(HighlightControlFlowUtil.checkFinalFieldInitialized(field));
}
@Override
public void visitForStatement(PsiForStatement statement) {
myHolder.add(HighlightUtil.checkForStatement(statement));
}
@Override
public void visitForeachStatement(PsiForeachStatement statement) {
myHolder.add(checkFeature(statement, Feature.FOR_EACH));
}
@Override
public void visitImportStaticStatement(PsiImportStaticStatement statement) {
myHolder.add(checkFeature(statement, Feature.STATIC_IMPORTS));
if (!myHolder.hasErrorResults()) myHolder.add(ImportsHighlightUtil.checkStaticOnDemandImportResolvesToClass(statement));
}
@Override
public void visitIdentifier(final PsiIdentifier identifier) {
TextAttributesScheme colorsScheme = myHolder.getColorsScheme();
PsiElement parent = identifier.getParent();
if (parent instanceof PsiVariable) {
PsiVariable variable = (PsiVariable)parent;
myHolder.add(HighlightUtil.checkVariableAlreadyDefined(variable));
if (variable.getInitializer() == null) {
final PsiElement child = variable.getLastChild();
if (child instanceof PsiErrorElement && child.getPrevSibling() == identifier) return;
}
boolean isMethodParameter = variable instanceof PsiParameter && ((PsiParameter)variable).getDeclarationScope() instanceof PsiMethod;
if (isMethodParameter) {
myReassignedParameters.put((PsiParameter)variable, 1); // mark param as present in current file
}
else {
// method params are highlighted in visitMethod since we should make sure the method body was visited before
if (HighlightControlFlowUtil.isReassigned(variable, myFinalVarProblems)) {
myHolder.add(HighlightNamesUtil.highlightReassignedVariable(variable, identifier));
}
else {
myHolder.add(HighlightNamesUtil.highlightVariableName(variable, identifier, colorsScheme));
}
}
}
else if (parent instanceof PsiClass) {
PsiClass aClass = (PsiClass)parent;
if (aClass.isAnnotationType()) {
myHolder.add(checkFeature(identifier, Feature.ANNOTATIONS));
}
myHolder.add(HighlightClassUtil.checkClassAlreadyImported(aClass, identifier));
if (!(parent instanceof PsiAnonymousClass) && aClass.getNameIdentifier() == identifier) {
myHolder.add(HighlightNamesUtil.highlightClassName(aClass, identifier, colorsScheme));
}
if (!myHolder.hasErrorResults() && myLanguageLevel.isAtLeast(LanguageLevel.JDK_10)) {
myHolder.add(HighlightClassUtil.checkVarClassConflict(aClass, identifier));
}
if (!myHolder.hasErrorResults() && myLanguageLevel.isAtLeast(LanguageLevel.JDK_1_8)) {
myHolder.add(GenericsHighlightUtil.checkUnrelatedDefaultMethods(aClass, identifier));
}
if (!myHolder.hasErrorResults()) {
myHolder.add(GenericsHighlightUtil.checkUnrelatedConcrete(aClass, identifier));
}
}
else if (parent instanceof PsiMethod) {
PsiMethod method = (PsiMethod)parent;
if (method.isConstructor()) {
myHolder.add(HighlightMethodUtil.checkConstructorName(method));
}
myHolder.add(HighlightNamesUtil.highlightMethodName(method, identifier, true, colorsScheme));
final PsiClass aClass = method.getContainingClass();
if (aClass != null) {
myHolder.add(GenericsHighlightUtil.checkDefaultMethodOverrideEquivalentToObjectNonPrivate(myLanguageLevel, aClass, method, identifier));
}
}
myHolder.add(HighlightUtil.checkUnderscore(identifier, myLanguageLevel));
super.visitIdentifier(identifier);
}
@Override
public void visitImportStatement(final PsiImportStatement statement) {
if (!myHolder.hasErrorResults()) {
myHolder.add(HighlightUtil.checkSingleImportClassConflict(statement, mySingleImportedClasses,myFile));
}
}
@Override
public void visitImportStaticReferenceElement(@NotNull PsiImportStaticReferenceElement ref) {
final String refName = ref.getReferenceName();
final JavaResolveResult[] results = ref.multiResolve(false);
final PsiElement referenceNameElement = ref.getReferenceNameElement();
if (results.length == 0) {
final String description = JavaErrorMessages.message("cannot.resolve.symbol", refName);
assert referenceNameElement != null : ref;
final HighlightInfo info =
HighlightInfo.newHighlightInfo(HighlightInfoType.WRONG_REF).range(referenceNameElement).descriptionAndTooltip(description).create();
QuickFixAction.registerQuickFixAction(info, QuickFixFactory.getInstance().createSetupJDKFix());
myHolder.add(info);
}
else {
final PsiManager manager = ref.getManager();
for (JavaResolveResult result : results) {
final PsiElement element = result.getElement();
String description = null;
if (element instanceof PsiClass) {
final Pair<PsiImportStaticReferenceElement, PsiClass> imported = mySingleImportedClasses.get(refName);
final PsiClass aClass = Pair.getSecond(imported);
if (aClass != null && !manager.areElementsEquivalent(aClass, element)) {
description = imported.first == null
? JavaErrorMessages.message("single.import.class.conflict", refName)
: imported.first.equals(ref)
? JavaErrorMessages.message("class.is.ambiguous.in.single.static.import", refName)
: JavaErrorMessages.message("class.is.already.defined.in.single.static.import", refName);
}
mySingleImportedClasses.put(refName, Pair.create(ref, (PsiClass)element));
}
else if (element instanceof PsiField) {
final Pair<PsiImportStaticReferenceElement, PsiField> imported = mySingleImportedFields.get(refName);
final PsiField field = Pair.getSecond(imported);
if (field != null && !manager.areElementsEquivalent(field, element)) {
description = imported.first.equals(ref)
? JavaErrorMessages.message("field.is.ambiguous.in.single.static.import", refName)
: JavaErrorMessages.message("field.is.already.defined.in.single.static.import", refName);
}
mySingleImportedFields.put(refName, Pair.create(ref, (PsiField)element));
}
if (description != null) {
myHolder.add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(ref).descriptionAndTooltip(description).create());
}
}
}
if (!myHolder.hasErrorResults()) {
PsiElement resolved = results.length >= 1 ? results[0].getElement() : null;
if (results.length > 1) {
for (int i = 1; i < results.length; i++) {
final PsiElement element = results[i].getElement();
if (resolved instanceof PsiMethod && !(element instanceof PsiMethod) ||
resolved instanceof PsiVariable && !(element instanceof PsiVariable) ||
resolved instanceof PsiClass && !(element instanceof PsiClass)) {
resolved = null;
break;
}
}
}
final TextAttributesScheme colorsScheme = myHolder.getColorsScheme();
if (resolved instanceof PsiClass) {
myHolder.add(HighlightNamesUtil.highlightClassName((PsiClass)resolved, ref, colorsScheme));
}
else {
if (referenceNameElement != null) {
if (resolved instanceof PsiVariable) {
myHolder.add(HighlightNamesUtil.highlightVariableName((PsiVariable)resolved, referenceNameElement, colorsScheme));
}
else if (resolved instanceof PsiMethod) {
myHolder.add(HighlightNamesUtil.highlightMethodName((PsiMethod)resolved, referenceNameElement, false, colorsScheme));
}
}
}
}
}
@Override
public void visitInstanceOfExpression(PsiInstanceOfExpression expression) {
super.visitInstanceOfExpression(expression);
if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkInstanceOfApplicable(expression));
if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkInstanceOfGenericType(expression));
}
@Override
public void visitKeyword(PsiKeyword keyword) {
super.visitKeyword(keyword);
PsiElement parent = keyword.getParent();
String text = keyword.getText();
if (parent instanceof PsiModifierList) {
PsiModifierList psiModifierList = (PsiModifierList)parent;
if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkNotAllowedModifier(keyword, psiModifierList));
if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkIllegalModifierCombination(keyword, psiModifierList));
PsiElement pParent = psiModifierList.getParent();
if (PsiModifier.ABSTRACT.equals(text) && pParent instanceof PsiMethod) {
if (!myHolder.hasErrorResults()) {
myHolder.add(HighlightMethodUtil.checkAbstractMethodInConcreteClass((PsiMethod)pParent, keyword));
}
}
}
else if (PsiKeyword.INTERFACE.equals(text) && parent instanceof PsiClass) {
if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkInterfaceCannotBeLocal((PsiClass)parent));
}
if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkStaticDeclarationInInnerClass(keyword));
if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkIllegalVoidType(keyword));
}
@Override
public void visitLabeledStatement(PsiLabeledStatement statement) {
super.visitLabeledStatement(statement);
if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkLabelWithoutStatement(statement));
if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkLabelAlreadyInUse(statement));
}
@Override
public void visitLiteralExpression(PsiLiteralExpression expression) {
super.visitLiteralExpression(expression);
if (myHolder.hasErrorResults()) return;
myHolder.add(HighlightUtil.checkLiteralExpressionParsingError(expression, myLanguageLevel,myFile));
if (myRefCountHolder != null && !myHolder.hasErrorResults()) registerReferencesFromInjectedFragments(expression);
if (myRefCountHolder != null && !myHolder.hasErrorResults()) {
for (PsiReference reference : expression.getReferences()) {
PsiElement resolve = reference.resolve();
if (resolve instanceof PsiMember) {
myRefCountHolder.registerReference(reference, new CandidateInfo(resolve, PsiSubstitutor.EMPTY));
}
}
}
}
@Override
public void visitMethod(PsiMethod method) {
super.visitMethod(method);
if (!myHolder.hasErrorResults()) myHolder.add(HighlightControlFlowUtil.checkUnreachableStatement(method.getBody()));
if (!myHolder.hasErrorResults()) myHolder.add(HighlightMethodUtil.checkConstructorHandleSuperClassExceptions(method));
if (!myHolder.hasErrorResults()) myHolder.add(HighlightMethodUtil.checkRecursiveConstructorInvocation(method));
if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkSafeVarargsAnnotation(method, myLanguageLevel));
PsiClass aClass = method.getContainingClass();
if (!myHolder.hasErrorResults() && method.isConstructor()) {
myHolder.add(HighlightClassUtil.checkThingNotAllowedInInterface(method, aClass));
}
if (!myHolder.hasErrorResults() && method.hasModifierProperty(PsiModifier.DEFAULT)) {
myHolder.add(checkFeature(method, Feature.EXTENSION_METHODS));
}
if (!myHolder.hasErrorResults() && aClass != null && aClass.isInterface() && method.hasModifierProperty(PsiModifier.STATIC)) {
myHolder.add(checkFeature(method, Feature.EXTENSION_METHODS));
}
if (!myHolder.hasErrorResults() && aClass != null) {
myHolder.add(HighlightMethodUtil.checkDuplicateMethod(aClass, method, getDuplicateMethods(aClass)));
}
// method params are highlighted in visitMethod since we should make sure the method body was visited before
PsiParameter[] parameters = method.getParameterList().getParameters();
final TextAttributesScheme colorsScheme = myHolder.getColorsScheme();
for (PsiParameter parameter : parameters) {
int info = myReassignedParameters.get(parameter);
if (info == 0) continue; // out of this file
PsiIdentifier nameIdentifier = parameter.getNameIdentifier();
if (nameIdentifier != null) {
if (info == 2) { // reassigned
myHolder.add(HighlightNamesUtil.highlightReassignedVariable(parameter, nameIdentifier));
}
else {
myHolder.add(HighlightNamesUtil.highlightVariableName(parameter, nameIdentifier, colorsScheme));
}
}
}
}
private void highlightReferencedMethodOrClassName(@NotNull PsiJavaCodeReferenceElement element, @Nullable PsiElement resolved) {
PsiElement parent = element.getParent();
final TextAttributesScheme colorsScheme = myHolder.getColorsScheme();
if (parent instanceof PsiMethodCallExpression) {
PsiMethod method = ((PsiMethodCallExpression)parent).resolveMethod();
PsiElement methodNameElement = element.getReferenceNameElement();
if (method != null && methodNameElement != null&& !(methodNameElement instanceof PsiKeyword)) {
myHolder.add(HighlightNamesUtil.highlightMethodName(method, methodNameElement, false, colorsScheme));
}
}
else if (parent instanceof PsiConstructorCall) {
try {
PsiMethod method = ((PsiConstructorCall)parent).resolveConstructor();
PsiMember methodOrClass = method != null ? method : resolved instanceof PsiClass ? (PsiClass)resolved : null;
if (methodOrClass != null) {
final PsiElement referenceNameElement = element.getReferenceNameElement();
if(referenceNameElement != null) {
// exclude type parameters from the highlighted text range
TextRange range = referenceNameElement.getTextRange();
myHolder.add(HighlightNamesUtil.highlightMethodName(methodOrClass, referenceNameElement, range, colorsScheme, false));
}
}
}
catch (IndexNotReadyException ignored) { }
}
else if (resolved instanceof PsiPackage) {
// highlight package (and following dot) as a class
myHolder.add(HighlightNamesUtil.highlightPackage(resolved, element, colorsScheme));
}
else if (resolved instanceof PsiClass) {
myHolder.add(HighlightNamesUtil.highlightClassName((PsiClass)resolved, element, colorsScheme));
}
}
@Override
public void visitMethodCallExpression(PsiMethodCallExpression expression) {
if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkEnumSuperConstructorCall(expression));
if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkSuperQualifierType(myFile.getProject(), expression));
if (!myHolder.hasErrorResults()) {
try {
myHolder.add(HighlightMethodUtil.checkMethodCall(expression, getResolveHelper(myHolder.getProject()), myLanguageLevel, myJavaSdkVersion, myFile));
}
catch (IndexNotReadyException ignored) { }
}
if (!myHolder.hasErrorResults()) myHolder.add(HighlightMethodUtil.checkConstructorCallMustBeFirstStatement(expression));
if (!myHolder.hasErrorResults()) myHolder.add(HighlightMethodUtil.checkSuperAbstractMethodDirectCall(expression));
if (!myHolder.hasErrorResults()) visitExpression(expression);
}
@Override
public void visitModifierList(PsiModifierList list) {
super.visitModifierList(list);
PsiElement parent = list.getParent();
if (parent instanceof PsiMethod) {
PsiMethod method = (PsiMethod)parent;
if (!myHolder.hasErrorResults()) myHolder.add(HighlightMethodUtil.checkMethodCanHaveBody(method, myLanguageLevel));
MethodSignatureBackedByPsiMethod methodSignature = MethodSignatureBackedByPsiMethod.create(method, PsiSubstitutor.EMPTY);
PsiClass aClass = method.getContainingClass();
if (!method.isConstructor()) {
try {
List<HierarchicalMethodSignature> superMethodSignatures = method.getHierarchicalMethodSignature().getSuperSignatures();
if (!superMethodSignatures.isEmpty()) {
if (!method.hasModifierProperty(PsiModifier.STATIC)) {
if (!myHolder.hasErrorResults()) myHolder.add(HighlightMethodUtil.checkMethodWeakerPrivileges(methodSignature, superMethodSignatures, true, myFile));
if (!myHolder.hasErrorResults()) myHolder.add(HighlightMethodUtil.checkMethodOverridesFinal(methodSignature, superMethodSignatures));
}
if (!myHolder.hasErrorResults()) myHolder.add(HighlightMethodUtil.checkMethodIncompatibleReturnType(methodSignature, superMethodSignatures, true));
if (aClass != null && !myHolder.hasErrorResults()) myHolder.add(HighlightMethodUtil.checkMethodIncompatibleThrows(methodSignature, superMethodSignatures, true, aClass));
}
}
catch (IndexNotReadyException ignored) { }
}
if (!myHolder.hasErrorResults()) myHolder.add(HighlightMethodUtil.checkMethodMustHaveBody(method, aClass));
if (!myHolder.hasErrorResults()) myHolder.add(HighlightMethodUtil.checkConstructorCallsBaseClassConstructor(method, myRefCountHolder, getResolveHelper(myHolder.getProject())));
if (!myHolder.hasErrorResults()) myHolder.add(HighlightMethodUtil.checkStaticMethodOverride(method,myFile));
if (!myHolder.hasErrorResults() && aClass != null && myOverrideEquivalentMethodsVisitedClasses.add(aClass)) {
myHolder.addAll(GenericsHighlightUtil.checkOverrideEquivalentMethods(aClass));
}
}
else if (parent instanceof PsiClass) {
PsiClass aClass = (PsiClass)parent;
try {
if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkDuplicateNestedClass(aClass));
if (!myHolder.hasErrorResults()) {
TextRange textRange = HighlightNamesUtil.getClassDeclarationTextRange(aClass);
myHolder.add(HighlightClassUtil.checkClassMustBeAbstract(aClass, textRange));
}
if (!myHolder.hasErrorResults()) {
myHolder.add(HighlightClassUtil.checkClassDoesNotCallSuperConstructorOrHandleExceptions(aClass, myRefCountHolder, getResolveHelper(myHolder.getProject())));
}
if (!myHolder.hasErrorResults()) myHolder.add(HighlightMethodUtil.checkOverrideEquivalentInheritedMethods(aClass, myFile, myLanguageLevel));
if (!myHolder.hasErrorResults() && myOverrideEquivalentMethodsVisitedClasses.add(aClass)) {
myHolder.addAll(GenericsHighlightUtil.checkOverrideEquivalentMethods(aClass));
}
if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkCyclicInheritance(aClass));
}
catch (IndexNotReadyException ignored) {
}
}
else if (parent instanceof PsiEnumConstant) {
if (!myHolder.hasErrorResults()) myHolder.addAll(GenericsHighlightUtil.checkEnumConstantModifierList(list));
}
}
@Override
public void visitNameValuePair(PsiNameValuePair pair) {
myHolder.add(AnnotationsHighlightUtil.checkNameValuePair(pair, myRefCountHolder));
if (!myHolder.hasErrorResults()) {
PsiIdentifier nameId = pair.getNameIdentifier();
if (nameId != null) {
HighlightInfo result = HighlightInfo.newHighlightInfo(JavaHighlightInfoTypes.ANNOTATION_ATTRIBUTE_NAME).range(nameId).create();
myHolder.add(result);
}
}
}
@Override
public void visitNewExpression(PsiNewExpression expression) {
final PsiType type = expression.getType();
final PsiClass aClass = PsiUtil.resolveClassInType(type);
PsiJavaCodeReferenceElement classReference = expression.getClassReference();
myHolder.add(HighlightUtil.checkUnhandledExceptions(expression, classReference != null ? classReference.getTextRange() : null));
if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkAnonymousInheritFinal(expression));
if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkQualifiedNew(expression, type, aClass));
if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkCreateInnerClassFromStaticContext(expression, type, aClass));
if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkTypeParameterInstantiation(expression));
if (aClass != null && !myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkInstantiationOfAbstractClass(aClass, expression));
try {
if (!myHolder.hasErrorResults()) HighlightMethodUtil.checkNewExpression(expression, type, myHolder, myJavaSdkVersion);
}
catch (IndexNotReadyException ignored) { }
if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkEnumInstantiation(expression, aClass));