]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 IBM Corporation and others. | |
3 | * All rights reserved. This program and the accompanying materials | |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | * | |
8 | * Contributors: | |
9 | * IBM Corporation - initial API and implementation | |
10 | *******************************************************************************/ | |
11 | package org.eclipse.jdt.internal.ui.text.correction; | |
12 | ||
13 | import org.eclipse.jdt.core.IJavaModelMarker; | |
14 | import org.eclipse.jdt.core.compiler.CategorizedProblem; | |
15 | import org.eclipse.jdt.core.compiler.IProblem; | |
16 | import org.eclipse.jdt.core.dom.ASTNode; | |
17 | import org.eclipse.jdt.core.dom.CompilationUnit; | |
18 | import org.eclipse.jdt.core.dom.NodeFinder; | |
19 | ||
20 | import org.eclipse.jdt.ui.text.java.IProblemLocation; | |
21 | ||
22 | import org.eclipse.jdt.internal.ui.javaeditor.IJavaAnnotation; | |
23 | import org.eclipse.jdt.internal.ui.javaeditor.JavaMarkerAnnotation; | |
24 | ||
25 | /** | |
26 | * | |
27 | */ | |
28 | public class ProblemLocation implements IProblemLocation { | |
29 | ||
30 | private final int fId; | |
31 | private final String[] fArguments; | |
32 | private final int fOffset; | |
33 | private final int fLength; | |
34 | private final boolean fIsError; | |
35 | private final String fMarkerType; | |
36 | ||
37 | public ProblemLocation(int offset, int length, IJavaAnnotation annotation) { | |
38 | fId= annotation.getId(); | |
39 | String[] arguments= annotation.getArguments(); | |
40 | fArguments= arguments != null ? arguments : new String[0]; | |
41 | fOffset= offset; | |
42 | fLength= length; | |
43 | fIsError= JavaMarkerAnnotation.ERROR_ANNOTATION_TYPE.equals(annotation.getType()); | |
44 | ||
45 | String markerType= annotation.getMarkerType(); | |
46 | fMarkerType= markerType != null ? markerType : IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER; | |
47 | } | |
48 | ||
49 | public ProblemLocation(int offset, int length, int id, String[] arguments, boolean isError, String markerType) { | |
50 | fId= id; | |
51 | fArguments= arguments; | |
52 | fOffset= offset; | |
53 | fLength= length; | |
54 | fIsError= isError; | |
55 | fMarkerType= markerType; | |
56 | } | |
57 | ||
58 | public ProblemLocation(IProblem problem) { | |
59 | fId= problem.getID(); | |
60 | fArguments= problem.getArguments(); | |
61 | fOffset= problem.getSourceStart(); | |
62 | fLength= problem.getSourceEnd() - fOffset + 1; | |
63 | fIsError= problem.isError(); | |
64 | fMarkerType= problem instanceof CategorizedProblem ? ((CategorizedProblem) problem).getMarkerType() : IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER; | |
65 | } | |
66 | ||
67 | ||
68 | /* (non-Javadoc) | |
69 | * @see org.eclipse.jdt.internal.ui.text.correction.IProblemLocation#getProblemId() | |
70 | */ | |
71 | public int getProblemId() { | |
72 | return fId; | |
73 | } | |
74 | ||
75 | /* (non-Javadoc) | |
76 | * @see org.eclipse.jdt.internal.ui.text.correction.IProblemLocation#getProblemArguments() | |
77 | */ | |
78 | public String[] getProblemArguments() { | |
79 | return fArguments; | |
80 | } | |
81 | ||
82 | /* (non-Javadoc) | |
83 | * @see org.eclipse.jdt.internal.ui.text.correction.IProblemLocation#getLength() | |
84 | */ | |
85 | public int getLength() { | |
86 | return fLength; | |
87 | } | |
88 | ||
89 | /* (non-Javadoc) | |
90 | * @see org.eclipse.jdt.internal.ui.text.correction.IProblemLocation#getOffset() | |
91 | */ | |
92 | public int getOffset() { | |
93 | return fOffset; | |
94 | } | |
95 | ||
96 | /* (non-Javadoc) | |
97 | * @see org.eclipse.jdt.ui.text.java.IProblemLocation#isError() | |
98 | */ | |
99 | public boolean isError() { | |
100 | return fIsError; | |
101 | } | |
102 | ||
103 | /* (non-Javadoc) | |
104 | * @see org.eclipse.jdt.ui.text.java.IProblemLocation#getMarkerType() | |
105 | */ | |
106 | public String getMarkerType() { | |
107 | return fMarkerType; | |
108 | } | |
109 | ||
110 | /* | |
111 | * (non-Javadoc) | |
112 | * @see org.eclipse.jdt.internal.ui.text.correction.IProblemLocation#getCoveringNode(org.eclipse.jdt.core.dom.CompilationUnit) | |
113 | */ | |
114 | public ASTNode getCoveringNode(CompilationUnit astRoot) { | |
115 | NodeFinder finder= new NodeFinder(astRoot, fOffset, fLength); | |
116 | return finder.getCoveringNode(); | |
117 | } | |
118 | ||
119 | /* | |
120 | * (non-Javadoc) | |
121 | * @see org.eclipse.jdt.internal.ui.text.correction.IProblemLocation#getCoveredNode(org.eclipse.jdt.core.dom.CompilationUnit) | |
122 | */ | |
123 | public ASTNode getCoveredNode(CompilationUnit astRoot) { | |
124 | NodeFinder finder= new NodeFinder(astRoot, fOffset, fLength); | |
125 | return finder.getCoveredNode(); | |
126 | } | |
127 | ||
128 | @Override | |
129 | public String toString() { | |
130 | StringBuffer buf= new StringBuffer(); | |
131 | buf.append("Id: ").append(getErrorCode(fId)).append('\n'); //$NON-NLS-1$ | |
132 | buf.append('[').append(fOffset).append(", ").append(fLength).append(']').append('\n'); //$NON-NLS-1$ | |
133 | String[] arg= fArguments; | |
134 | for (int i= 0; i < arg.length; i++) { | |
135 | buf.append(arg[i]); | |
136 | buf.append('\n'); | |
137 | } | |
138 | return buf.toString(); | |
139 | } | |
140 | ||
141 | private String getErrorCode(int code) { | |
142 | StringBuffer buf= new StringBuffer(); | |
143 | ||
144 | if ((code & IProblem.TypeRelated) != 0) { | |
145 | buf.append("TypeRelated + "); //$NON-NLS-1$ | |
146 | } | |
147 | if ((code & IProblem.FieldRelated) != 0) { | |
148 | buf.append("FieldRelated + "); //$NON-NLS-1$ | |
149 | } | |
150 | if ((code & IProblem.ConstructorRelated) != 0) { | |
151 | buf.append("ConstructorRelated + "); //$NON-NLS-1$ | |
152 | } | |
153 | if ((code & IProblem.MethodRelated) != 0) { | |
154 | buf.append("MethodRelated + "); //$NON-NLS-1$ | |
155 | } | |
156 | if ((code & IProblem.ImportRelated) != 0) { | |
157 | buf.append("ImportRelated + "); //$NON-NLS-1$ | |
158 | } | |
159 | if ((code & IProblem.Internal) != 0) { | |
160 | buf.append("Internal + "); //$NON-NLS-1$ | |
161 | } | |
162 | if ((code & IProblem.Syntax) != 0) { | |
163 | buf.append("Syntax + "); //$NON-NLS-1$ | |
164 | } | |
165 | if ((code & IProblem.Javadoc) != 0) { | |
166 | buf.append("Javadoc + "); //$NON-NLS-1$ | |
167 | } | |
168 | buf.append(code & IProblem.IgnoreCategoriesMask); | |
169 | ||
170 | return buf.toString(); | |
171 | } | |
172 | ||
173 | ||
174 | } |