1 | /******************************************************************************* |
---|---|
2 | * Copyright (c) 2000, 2015 IBM Corporation and others. |
3 | * |
4 | * This program and the accompanying materials |
5 | * are made available under the terms of the Eclipse Public License 2.0 |
6 | * which accompanies this distribution, and is available at |
7 | * https://www.eclipse.org/legal/epl-2.0/ |
8 | * |
9 | * SPDX-License-Identifier: EPL-2.0 |
10 | * |
11 | * Contributors: |
12 | * IBM Corporation - initial API and implementation |
13 | *******************************************************************************/ |
14 | package org.eclipse.jdt.astview.views; |
15 | |
16 | import java.lang.reflect.Field; |
17 | import java.util.ArrayList; |
18 | import java.util.Objects; |
19 | |
20 | import org.eclipse.swt.graphics.Image; |
21 | |
22 | import org.eclipse.jdt.core.JavaCore; |
23 | import org.eclipse.jdt.core.compiler.CategorizedProblem; |
24 | import org.eclipse.jdt.core.compiler.IProblem; |
25 | |
26 | /** |
27 | * |
28 | */ |
29 | public class ProblemNode extends ASTAttribute { |
30 | |
31 | private final IProblem fProblem; |
32 | private final Object fParent; |
33 | |
34 | public ProblemNode(Object parent, IProblem problem) { |
35 | fParent= parent; |
36 | fProblem= problem; |
37 | } |
38 | |
39 | @Override |
40 | public Object getParent() { |
41 | return fParent; |
42 | } |
43 | |
44 | @Override |
45 | public Object[] getChildren() { |
46 | String[] arguments= fProblem.getArguments(); |
47 | ArrayList<GeneralAttribute> children= new ArrayList<>(); |
48 | |
49 | children.add(new GeneralAttribute(this, "CONSTANT NAME", getConstantName())); |
50 | children.add(new GeneralAttribute(this, "ID", getErrorLabel())); |
51 | children.add(new GeneralAttribute(this, "OPTION FOR CONFIGURABLE SEVERITY", JavaCore.getOptionForConfigurableSeverity(fProblem.getID()))); |
52 | if (fProblem instanceof CategorizedProblem) { |
53 | children.add(new GeneralAttribute(this, "CATEGORY ID", getCategoryCode())); |
54 | children.add(new GeneralAttribute(this, "MARKER TYPE", ((CategorizedProblem) fProblem).getMarkerType())); |
55 | } |
56 | for (int i= 0; i < arguments.length; i++) { |
57 | children.add(new GeneralAttribute(this, "ARGUMENT " + i, arguments[i])); |
58 | } |
59 | return children.toArray(); |
60 | } |
61 | |
62 | @Override |
63 | public String getLabel() { |
64 | StringBuilder buf= new StringBuilder(); |
65 | int offset= fProblem.getSourceStart(); |
66 | int length= fProblem.getSourceEnd() + 1 - offset; |
67 | |
68 | if (fProblem.isError()) |
69 | buf.append("E"); |
70 | if (fProblem.isWarning()) |
71 | buf.append("W"); |
72 | if (fProblem.isInfo()) |
73 | buf.append("I"); |
74 | buf.append('[').append(offset).append(", ").append(length).append(']').append(' '); |
75 | buf.append(fProblem.getMessage()); |
76 | |
77 | return buf.toString(); |
78 | } |
79 | |
80 | private String getErrorLabel() { |
81 | int id= fProblem.getID(); |
82 | StringBuilder buf= new StringBuilder(); |
83 | |
84 | if ((id & IProblem.TypeRelated) != 0) { |
85 | buf.append("TypeRelated + "); //$NON-NLS-1$ |
86 | } |
87 | if ((id & IProblem.FieldRelated) != 0) { |
88 | buf.append("FieldRelated + "); //$NON-NLS-1$ |
89 | } |
90 | if ((id & IProblem.ConstructorRelated) != 0) { |
91 | buf.append("ConstructorRelated + "); //$NON-NLS-1$ |
92 | } |
93 | if ((id & IProblem.MethodRelated) != 0) { |
94 | buf.append("MethodRelated + "); //$NON-NLS-1$ |
95 | } |
96 | if ((id & IProblem.ImportRelated) != 0) { |
97 | buf.append("ImportRelated + "); //$NON-NLS-1$ |
98 | } |
99 | if ((id & IProblem.Internal) != 0) { |
100 | buf.append("Internal + "); //$NON-NLS-1$ |
101 | } |
102 | if ((id & IProblem.Syntax) != 0) { |
103 | buf.append("Syntax + "); //$NON-NLS-1$ |
104 | } |
105 | if ((id & IProblem.Javadoc) != 0) { |
106 | buf.append("Javadoc + "); //$NON-NLS-1$ |
107 | } |
108 | buf.append(id & IProblem.IgnoreCategoriesMask); |
109 | |
110 | buf.append(" = 0x").append(Integer.toHexString(id)).append(" = ").append(id); |
111 | |
112 | return buf.toString(); |
113 | } |
114 | |
115 | private String getConstantName() { |
116 | int id= fProblem.getID(); |
117 | for (Field f : IProblem.class.getFields()) { |
118 | try { |
119 | if (f.getType() == int.class && f.getInt(f) == id) { |
120 | return "IProblem." + f.getName(); |
121 | } |
122 | } catch (IllegalArgumentException | IllegalAccessException e) { |
123 | } |
124 | } |
125 | return "<UNKNOWN CONSTANT>"; |
126 | } |
127 | |
128 | private String getCategoryCode() { |
129 | CategorizedProblem categorized= (CategorizedProblem) fProblem; |
130 | int categoryID= categorized.getCategoryID(); |
131 | StringBuilder buf= new StringBuilder(); |
132 | |
133 | switch (categoryID) { |
134 | case CategorizedProblem.CAT_UNSPECIFIED: |
135 | buf.append("Unspecified"); |
136 | break; |
137 | |
138 | case CategorizedProblem.CAT_BUILDPATH: |
139 | buf.append("Buildpath"); |
140 | break; |
141 | case CategorizedProblem.CAT_SYNTAX: |
142 | buf.append("Syntax"); |
143 | break; |
144 | case CategorizedProblem.CAT_IMPORT: |
145 | buf.append("Import"); |
146 | break; |
147 | case CategorizedProblem.CAT_TYPE: |
148 | buf.append("Type"); |
149 | break; |
150 | case CategorizedProblem.CAT_MEMBER: |
151 | buf.append("Member"); |
152 | break; |
153 | case CategorizedProblem.CAT_INTERNAL: |
154 | buf.append("Internal"); |
155 | break; |
156 | case CategorizedProblem.CAT_JAVADOC: |
157 | buf.append("Javadoc"); |
158 | break; |
159 | case CategorizedProblem.CAT_CODE_STYLE: |
160 | buf.append("Code Style"); |
161 | break; |
162 | case CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM: |
163 | buf.append("Potential Programming Problem"); |
164 | break; |
165 | case CategorizedProblem.CAT_NAME_SHADOWING_CONFLICT: |
166 | buf.append("Name Shadowing Conflict"); |
167 | break; |
168 | case CategorizedProblem.CAT_DEPRECATION: |
169 | buf.append("Deprecation"); |
170 | break; |
171 | case CategorizedProblem.CAT_UNNECESSARY_CODE: |
172 | buf.append("Unnecessary Code"); |
173 | break; |
174 | case CategorizedProblem.CAT_UNCHECKED_RAW: |
175 | buf.append("Unchecked Raw"); |
176 | break; |
177 | case CategorizedProblem.CAT_NLS: |
178 | buf.append("NLS"); |
179 | break; |
180 | case CategorizedProblem.CAT_RESTRICTION: |
181 | buf.append("Restriction"); |
182 | break; |
183 | case CategorizedProblem.CAT_MODULE: |
184 | buf.append("Module"); |
185 | break; |
186 | default: |
187 | buf.append("<UNKNOWN CATEGORY>"); |
188 | break; |
189 | } |
190 | |
191 | buf.append(" = ").append(categoryID); |
192 | |
193 | return buf.toString(); |
194 | } |
195 | |
196 | @Override |
197 | public Image getImage() { |
198 | return null; |
199 | } |
200 | |
201 | /** |
202 | * @return Returns the offset of the problem |
203 | */ |
204 | public int getOffset() { |
205 | return fProblem.getSourceStart(); |
206 | } |
207 | |
208 | /** |
209 | * @return Returns the length of the problem |
210 | */ |
211 | public int getLength() { |
212 | return fProblem.getSourceEnd() + 1 - fProblem.getSourceStart(); |
213 | } |
214 | |
215 | /* |
216 | * @see java.lang.Object#equals(java.lang.Object) |
217 | */ |
218 | @Override |
219 | public boolean equals(Object obj) { |
220 | if (this == obj) |
221 | return true; |
222 | if (obj == null || !obj.getClass().equals(getClass())) { |
223 | return false; |
224 | } |
225 | |
226 | ProblemNode other= (ProblemNode) obj; |
227 | if (!Objects.equals(fParent, other.fParent)) { |
228 | return false; |
229 | } |
230 | |
231 | if (!Objects.equals(fProblem, other.fProblem)) { |
232 | return false; |
233 | } |
234 | |
235 | return true; |
236 | } |
237 | |
238 | /* |
239 | * @see java.lang.Object#hashCode() |
240 | */ |
241 | @Override |
242 | public int hashCode() { |
243 | return (fParent != null ? fParent.hashCode() : 0) + (fProblem != null ? fProblem.hashCode() : 0); |
244 | } |
245 | } |
246 |
Members