|
35 | 35 | * @author VISTALL |
36 | 36 | * @since 10.06.14 |
37 | 37 | */ |
38 | | -public class CS0106 extends CompilerCheck<DotNetModifierListOwner> |
39 | | -{ |
40 | | - public static enum Owners |
41 | | - { |
42 | | - Constructor(CSharpModifier.PUBLIC, CSharpModifier.PRIVATE, CSharpModifier.PROTECTED, CSharpModifier.INTERNAL), |
43 | | - StaticConstructor(CSharpModifier.STATIC), |
44 | | - Constant(CSharpModifier.PUBLIC, CSharpModifier.PRIVATE, CSharpModifier.PROTECTED, CSharpModifier.INTERNAL), |
45 | | - DeConstructor, |
46 | | - InterfaceMember(CSharpModifier.NEW), |
47 | | - GenericParameter(CSharpModifier.IN, CSharpModifier.OUT), |
48 | | - Parameter(CSharpModifier.REF, CSharpModifier.OUT, CSharpModifier.PARAMS, CSharpModifier.THIS), |
49 | | - NamespaceStruct(CSharpModifier.PUBLIC, CSharpModifier.PROTECTED, CSharpModifier.INTERNAL), |
50 | | - NestedStruct(CSharpModifier.PUBLIC, CSharpModifier.PROTECTED, CSharpModifier.PRIVATE, CSharpModifier.INTERNAL), |
51 | | - NamespaceType(CSharpModifier.STATIC, CSharpModifier.PUBLIC, CSharpModifier.PROTECTED, CSharpModifier.INTERNAL, CSharpModifier.ABSTRACT, CSharpModifier.PARTIAL, CSharpModifier.SEALED, CSharpModifier.UNSAFE), |
52 | | - NestedType(CSharpModifier.PUBLIC, CSharpModifier.PRIVATE, CSharpModifier.PROTECTED, CSharpModifier.INTERNAL, CSharpModifier.ABSTRACT, CSharpModifier.PARTIAL, CSharpModifier.SEALED, |
53 | | - CSharpModifier.STATIC, CSharpModifier.NEW), |
54 | | - Unknown |
55 | | - { |
56 | | - @Override |
57 | | - public boolean isValidModifier(DotNetModifier modifier) |
58 | | - { |
59 | | - return true; |
60 | | - } |
61 | | - }; |
62 | | - |
63 | | - private DotNetModifier[] myValidModifiers; |
64 | | - |
65 | | - Owners(DotNetModifier... validModifiers) |
66 | | - { |
67 | | - myValidModifiers = validModifiers; |
68 | | - } |
69 | | - |
70 | | - public boolean isValidModifier(DotNetModifier modifier) |
71 | | - { |
72 | | - return ArrayUtil.contains(modifier, myValidModifiers); |
73 | | - } |
74 | | - } |
75 | | - |
76 | | - @RequiredReadAction |
77 | | - @Nonnull |
78 | | - @Override |
79 | | - public List<CompilerCheckBuilder> check(@Nonnull CSharpLanguageVersion languageVersion, @Nonnull CSharpHighlightContext highlightContext, @Nonnull DotNetModifierListOwner element) |
80 | | - { |
81 | | - DotNetModifierList modifierList = element.getModifierList(); |
82 | | - if(modifierList == null) |
83 | | - { |
84 | | - return Collections.emptyList(); |
85 | | - } |
86 | | - |
87 | | - List<CompilerCheckBuilder> list = Collections.emptyList(); |
88 | | - Owners owners = toOwners(element); |
89 | | - |
90 | | - DotNetModifier[] modifiers = modifierList.getModifiers(); |
91 | | - if(modifiers.length == 0) |
92 | | - { |
93 | | - return list; |
94 | | - } |
95 | | - |
96 | | - for(DotNetModifier modifier : modifiers) |
97 | | - { |
98 | | - if(!owners.isValidModifier(modifier)) |
99 | | - { |
100 | | - PsiElement modifierElement = modifierList.getModifierElement(modifier); |
101 | | - if(modifierElement == null) |
102 | | - { |
103 | | - continue; |
104 | | - } |
105 | | - |
106 | | - if(list.isEmpty()) |
107 | | - { |
108 | | - list = new ArrayList<>(2); |
109 | | - } |
110 | | - |
111 | | - list.add(newBuilder(modifierElement, modifier.getPresentableText()).withQuickFix(new RemoveModifierFix(modifier, element))); |
112 | | - } |
113 | | - } |
114 | | - return list; |
115 | | - } |
116 | | - |
117 | | - @RequiredReadAction |
118 | | - public static Owners toOwners(DotNetModifierListOwner owner) |
119 | | - { |
120 | | - if(owner instanceof CSharpFieldDeclaration && ((CSharpFieldDeclaration) owner).isConstant()) |
121 | | - { |
122 | | - return Owners.Constant; |
123 | | - } |
124 | | - |
125 | | - if(owner instanceof CSharpConstructorDeclaration) |
126 | | - { |
127 | | - if(((CSharpConstructorDeclaration) owner).isDeConstructor()) |
128 | | - { |
129 | | - return Owners.DeConstructor; |
130 | | - } |
131 | | - |
132 | | - if(owner.hasModifier(DotNetModifier.STATIC)) |
133 | | - { |
134 | | - return Owners.StaticConstructor; |
135 | | - } |
136 | | - return Owners.Constructor; |
137 | | - } |
138 | | - |
139 | | - if(owner instanceof CSharpMethodDeclaration || owner instanceof CSharpPropertyDeclaration || owner instanceof CSharpIndexMethodDeclaration) |
140 | | - { |
141 | | - PsiElement parent = owner.getParent(); |
142 | | - |
143 | | - if(parent instanceof CSharpTypeDeclaration) |
144 | | - { |
145 | | - if(((CSharpTypeDeclaration) parent).isInterface()) |
146 | | - { |
147 | | - return Owners.InterfaceMember; |
148 | | - } |
149 | | - } |
150 | | - } |
151 | | - |
152 | | - if(owner instanceof DotNetGenericParameter) |
153 | | - { |
154 | | - return Owners.GenericParameter; |
155 | | - } |
156 | | - |
157 | | - if(owner instanceof DotNetTypeDeclaration) |
158 | | - { |
159 | | - boolean struct = ((DotNetTypeDeclaration) owner).isEnum() || ((DotNetTypeDeclaration) owner).isStruct(); |
160 | | - |
161 | | - PsiElement parent = owner.getParent(); |
162 | | - if(parent instanceof CSharpNamespaceDeclaration || parent instanceof CSharpFile) |
163 | | - { |
164 | | - return struct ? Owners.NamespaceStruct : Owners.NamespaceType; |
165 | | - } |
166 | | - else if(parent instanceof DotNetTypeDeclaration) |
167 | | - { |
168 | | - return struct ? Owners.NestedStruct : Owners.NestedType; |
169 | | - } |
170 | | - } |
171 | | - |
172 | | - if(owner instanceof DotNetParameter) |
173 | | - { |
174 | | - return Owners.Parameter; |
175 | | - } |
176 | | - return Owners.Unknown; |
177 | | - } |
| 38 | +public class CS0106 extends CompilerCheck<DotNetModifierListOwner> { |
| 39 | + public static enum Owners { |
| 40 | + Constructor(CSharpModifier.PUBLIC, CSharpModifier.PRIVATE, CSharpModifier.PROTECTED, CSharpModifier.INTERNAL), |
| 41 | + StaticConstructor(CSharpModifier.STATIC), |
| 42 | + Constant(CSharpModifier.PUBLIC, CSharpModifier.PRIVATE, CSharpModifier.PROTECTED, CSharpModifier.INTERNAL), |
| 43 | + DeConstructor, |
| 44 | + InterfaceMember(CSharpModifier.NEW), |
| 45 | + GenericParameter(CSharpModifier.IN, CSharpModifier.OUT), |
| 46 | + Parameter(CSharpModifier.REF, CSharpModifier.OUT, CSharpModifier.PARAMS, CSharpModifier.THIS), |
| 47 | + NamespaceStruct(CSharpModifier.PUBLIC, CSharpModifier.PROTECTED, CSharpModifier.INTERNAL, CSharpModifier.UNSAFE), |
| 48 | + NestedStruct(CSharpModifier.PUBLIC, CSharpModifier.PROTECTED, CSharpModifier.PRIVATE, CSharpModifier.INTERNAL, CSharpModifier.UNSAFE), |
| 49 | + NamespaceType(CSharpModifier.STATIC, CSharpModifier.PUBLIC, CSharpModifier.PROTECTED, CSharpModifier.INTERNAL, CSharpModifier.ABSTRACT, CSharpModifier.PARTIAL, CSharpModifier.SEALED), |
| 50 | + NestedType(CSharpModifier.PUBLIC, CSharpModifier.PRIVATE, CSharpModifier.PROTECTED, CSharpModifier.INTERNAL, CSharpModifier.ABSTRACT, CSharpModifier.PARTIAL, CSharpModifier.SEALED, |
| 51 | + CSharpModifier.STATIC, CSharpModifier.NEW), |
| 52 | + Unknown { |
| 53 | + @Override |
| 54 | + public boolean isValidModifier(DotNetModifier modifier) { |
| 55 | + return true; |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + private DotNetModifier[] myValidModifiers; |
| 60 | + |
| 61 | + Owners(DotNetModifier... validModifiers) { |
| 62 | + myValidModifiers = validModifiers; |
| 63 | + } |
| 64 | + |
| 65 | + public boolean isValidModifier(DotNetModifier modifier) { |
| 66 | + return ArrayUtil.contains(modifier, myValidModifiers); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @RequiredReadAction |
| 71 | + @Nonnull |
| 72 | + @Override |
| 73 | + public List<CompilerCheckBuilder> check(@Nonnull CSharpLanguageVersion languageVersion, @Nonnull CSharpHighlightContext highlightContext, @Nonnull DotNetModifierListOwner element) { |
| 74 | + DotNetModifierList modifierList = element.getModifierList(); |
| 75 | + if (modifierList == null) { |
| 76 | + return Collections.emptyList(); |
| 77 | + } |
| 78 | + |
| 79 | + List<CompilerCheckBuilder> list = Collections.emptyList(); |
| 80 | + Owners owners = toOwners(element); |
| 81 | + |
| 82 | + DotNetModifier[] modifiers = modifierList.getModifiers(); |
| 83 | + if (modifiers.length == 0) { |
| 84 | + return list; |
| 85 | + } |
| 86 | + |
| 87 | + for (DotNetModifier modifier : modifiers) { |
| 88 | + if (!owners.isValidModifier(modifier)) { |
| 89 | + PsiElement modifierElement = modifierList.getModifierElement(modifier); |
| 90 | + if (modifierElement == null) { |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + if (list.isEmpty()) { |
| 95 | + list = new ArrayList<>(2); |
| 96 | + } |
| 97 | + |
| 98 | + list.add(newBuilder(modifierElement, modifier.getPresentableText()).withQuickFix(new RemoveModifierFix(modifier, element))); |
| 99 | + } |
| 100 | + } |
| 101 | + return list; |
| 102 | + } |
| 103 | + |
| 104 | + @RequiredReadAction |
| 105 | + public static Owners toOwners(DotNetModifierListOwner owner) { |
| 106 | + if (owner instanceof CSharpFieldDeclaration && ((CSharpFieldDeclaration) owner).isConstant()) { |
| 107 | + return Owners.Constant; |
| 108 | + } |
| 109 | + |
| 110 | + if (owner instanceof CSharpConstructorDeclaration) { |
| 111 | + if (((CSharpConstructorDeclaration) owner).isDeConstructor()) { |
| 112 | + return Owners.DeConstructor; |
| 113 | + } |
| 114 | + |
| 115 | + if (owner.hasModifier(DotNetModifier.STATIC)) { |
| 116 | + return Owners.StaticConstructor; |
| 117 | + } |
| 118 | + return Owners.Constructor; |
| 119 | + } |
| 120 | + |
| 121 | + if (owner instanceof CSharpMethodDeclaration || owner instanceof CSharpPropertyDeclaration || owner instanceof CSharpIndexMethodDeclaration) { |
| 122 | + PsiElement parent = owner.getParent(); |
| 123 | + |
| 124 | + if (parent instanceof CSharpTypeDeclaration) { |
| 125 | + if (((CSharpTypeDeclaration) parent).isInterface()) { |
| 126 | + return Owners.InterfaceMember; |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + if (owner instanceof DotNetGenericParameter) { |
| 132 | + return Owners.GenericParameter; |
| 133 | + } |
| 134 | + |
| 135 | + if (owner instanceof DotNetTypeDeclaration) { |
| 136 | + boolean struct = ((DotNetTypeDeclaration) owner).isEnum() || ((DotNetTypeDeclaration) owner).isStruct(); |
| 137 | + |
| 138 | + PsiElement parent = owner.getParent(); |
| 139 | + if (parent instanceof CSharpNamespaceDeclaration || parent instanceof CSharpFile) { |
| 140 | + return struct ? Owners.NamespaceStruct : Owners.NamespaceType; |
| 141 | + } |
| 142 | + else if (parent instanceof DotNetTypeDeclaration) { |
| 143 | + return struct ? Owners.NestedStruct : Owners.NestedType; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + if (owner instanceof DotNetParameter) { |
| 148 | + return Owners.Parameter; |
| 149 | + } |
| 150 | + return Owners.Unknown; |
| 151 | + } |
178 | 152 | } |
0 commit comments