1 | /******************************************************************************* |
2 | * Copyright (c) 2008, 2009 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.pde.api.tools.internal.builder; |
12 | |
13 | import java.util.ArrayList; |
14 | import java.util.HashSet; |
15 | import java.util.List; |
16 | import java.util.Set; |
17 | |
18 | import org.eclipse.core.resources.IProject; |
19 | import org.eclipse.pde.api.tools.internal.model.PluginProjectApiComponent; |
20 | import org.eclipse.pde.api.tools.internal.provisional.ApiDescriptionVisitor; |
21 | import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin; |
22 | import org.eclipse.pde.api.tools.internal.provisional.IApiAnnotations; |
23 | import org.eclipse.pde.api.tools.internal.provisional.RestrictionModifiers; |
24 | import org.eclipse.pde.api.tools.internal.provisional.VisibilityModifiers; |
25 | import org.eclipse.pde.api.tools.internal.provisional.descriptors.IElementDescriptor; |
26 | import org.eclipse.pde.api.tools.internal.provisional.descriptors.IFieldDescriptor; |
27 | import org.eclipse.pde.api.tools.internal.provisional.descriptors.IMethodDescriptor; |
28 | import org.eclipse.pde.api.tools.internal.provisional.descriptors.IPackageDescriptor; |
29 | import org.eclipse.pde.api.tools.internal.provisional.descriptors.IReferenceTypeDescriptor; |
30 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiComponent; |
31 | import org.eclipse.pde.api.tools.internal.provisional.problems.IApiProblemTypes; |
32 | |
33 | /** |
34 | * Builds problem detectors for reference analysis. |
35 | * |
36 | * @since 1.1 |
37 | */ |
38 | public class ProblemDetectorBuilder extends ApiDescriptionVisitor { |
39 | |
40 | /** |
41 | * Problem detectors |
42 | */ |
43 | private IllegalExtendsProblemDetector fIllegalExtends = null; |
44 | private IllegalImplementsProblemDetector fIllegalImplements = null; |
45 | private IllegalInstantiateProblemDetector fIllegalInstantiate = null; |
46 | private IllegalOverrideProblemDetector fIllegalOverride = null; |
47 | private IllegalMethodReferenceDetector fIllegalMethodRef = null; |
48 | private IllegalFieldReferenceDetector fIllegalFieldRef = null; |
49 | private SystemApiDetector fSystemApiDetector = null; |
50 | /** |
51 | * Cache of non-API package names visited |
52 | */ |
53 | private Set fNonApiPackageNames = new HashSet(); |
54 | |
55 | /** |
56 | * The owning {@link IApiComponent} of this builder |
57 | */ |
58 | private IApiComponent fComponent = null; |
59 | |
60 | /** |
61 | * Problem detectors used for a component's analysis |
62 | */ |
63 | private List fDetectors; |
64 | |
65 | /** |
66 | * Build problem detectors for a component. |
67 | * |
68 | * @param component |
69 | */ |
70 | public ProblemDetectorBuilder(IApiComponent component) { |
71 | initializeDetectors(component); |
72 | } |
73 | |
74 | /* (non-Javadoc) |
75 | * @see org.eclipse.pde.api.tools.ApiDescriptionVisitor#visitElement(org.eclipse.pde.api.tools.descriptors.IElementDescriptor, java.lang.String, org.eclipse.pde.api.tools.IApiAnnotations) |
76 | */ |
77 | public boolean visitElement(IElementDescriptor element, IApiAnnotations description) { |
78 | int mask = description.getRestrictions(); |
79 | switch (element.getElementType()) { |
80 | case IElementDescriptor.PACKAGE: |
81 | if (VisibilityModifiers.isPrivate(description.getVisibility())) { |
82 | fNonApiPackageNames.add(((IPackageDescriptor)element).getName()); |
83 | return false; // no need to visit types in non-API package |
84 | } |
85 | break; |
86 | default: |
87 | if (!RestrictionModifiers.isUnrestricted(mask)) { |
88 | if(RestrictionModifiers.isOverrideRestriction(mask) && fIllegalOverride != null) { |
89 | fIllegalOverride.addIllegalMethod((IMethodDescriptor) element, fComponent.getId()); |
90 | } |
91 | if (RestrictionModifiers.isExtendRestriction(mask) && fIllegalExtends != null) { |
92 | fIllegalExtends.addIllegalType((IReferenceTypeDescriptor) element, fComponent.getId()); |
93 | } |
94 | if (RestrictionModifiers.isImplementRestriction(mask) && fIllegalImplements != null) { |
95 | fIllegalImplements.addIllegalType((IReferenceTypeDescriptor) element, fComponent.getId()); |
96 | } |
97 | if (RestrictionModifiers.isInstantiateRestriction(mask) && fIllegalInstantiate != null) { |
98 | fIllegalInstantiate.addIllegalType((IReferenceTypeDescriptor) element, fComponent.getId()); |
99 | } |
100 | if (RestrictionModifiers.isReferenceRestriction(mask)) { |
101 | if (element.getElementType() == IElementDescriptor.METHOD && fIllegalMethodRef != null) { |
102 | fIllegalMethodRef.addIllegalMethod((IMethodDescriptor) element, fComponent.getId()); |
103 | } else if (element.getElementType() == IElementDescriptor.FIELD && fIllegalFieldRef != null) { |
104 | fIllegalFieldRef.addIllegalField((IFieldDescriptor) element, fComponent.getId()); |
105 | } |
106 | } |
107 | } |
108 | } |
109 | return true; |
110 | } |
111 | |
112 | /** |
113 | * Sets the owning component of this builder |
114 | * @param component |
115 | */ |
116 | public void setOwningComponent(IApiComponent component) { |
117 | fComponent = component; |
118 | } |
119 | |
120 | /** |
121 | * @return the {@link IProject} associated with the set {@link IApiComponent} or <code>null</code> |
122 | * if the component is not a {@link PluginProjectApiComponent} |
123 | */ |
124 | private IProject getProject(IApiComponent component) { |
125 | if(component instanceof PluginProjectApiComponent) { |
126 | PluginProjectApiComponent comp = (PluginProjectApiComponent) component; |
127 | return comp.getJavaProject().getProject(); |
128 | } |
129 | return null; |
130 | } |
131 | |
132 | /** |
133 | * Initializes the detectors for this builder. This method is only |
134 | * called when an owning component is set |
135 | */ |
136 | private void initializeDetectors(IApiComponent component) { |
137 | fDetectors = new ArrayList(); |
138 | IProject project = getProject(component); |
139 | if(project != null) { |
140 | if(!isIgnore(IApiProblemTypes.ILLEGAL_EXTEND, project) && fIllegalExtends == null) { |
141 | fIllegalExtends = new IllegalExtendsProblemDetector(); |
142 | } |
143 | if(!isIgnore(IApiProblemTypes.ILLEGAL_IMPLEMENT, project) && fIllegalImplements == null) { |
144 | fIllegalImplements = new IllegalImplementsProblemDetector(); |
145 | } |
146 | if(!isIgnore(IApiProblemTypes.ILLEGAL_INSTANTIATE, project) && fIllegalInstantiate == null) { |
147 | fIllegalInstantiate = new IllegalInstantiateProblemDetector(); |
148 | } |
149 | if(!isIgnore(IApiProblemTypes.ILLEGAL_OVERRIDE, project) && fIllegalOverride == null) { |
150 | fIllegalOverride = new IllegalOverrideProblemDetector(); |
151 | } |
152 | if(!isIgnore(IApiProblemTypes.ILLEGAL_REFERENCE, project) && fIllegalMethodRef == null) { |
153 | fIllegalMethodRef = new IllegalMethodReferenceDetector(); |
154 | fIllegalFieldRef = new IllegalFieldReferenceDetector(); |
155 | } |
156 | } |
157 | else { |
158 | //add all detectors by default if we have no preference context |
159 | fIllegalExtends = new IllegalExtendsProblemDetector(); |
160 | fIllegalImplements = new IllegalImplementsProblemDetector(); |
161 | fIllegalInstantiate = new IllegalInstantiateProblemDetector(); |
162 | fIllegalOverride = new IllegalOverrideProblemDetector(); |
163 | fIllegalMethodRef = new IllegalMethodReferenceDetector(); |
164 | fIllegalFieldRef = new IllegalFieldReferenceDetector(); |
165 | } |
166 | if (project != null) { |
167 | if (!isIgnore(IApiProblemTypes.INVALID_REFERENCE_IN_SYSTEM_LIBRARIES, project) |
168 | && fSystemApiDetector == null) { |
169 | fSystemApiDetector = new SystemApiDetector(); |
170 | } |
171 | } else { |
172 | //add detector by default only outside of the ide if we have no preference context |
173 | fSystemApiDetector = new SystemApiDetector(); |
174 | } |
175 | if (fIllegalExtends != null) { |
176 | fDetectors.add(fIllegalExtends); |
177 | } |
178 | if (fIllegalImplements != null) { |
179 | fDetectors.add(fIllegalImplements); |
180 | } |
181 | if (fIllegalInstantiate != null) { |
182 | fDetectors.add(fIllegalInstantiate); |
183 | } |
184 | if (fIllegalOverride != null) { |
185 | fDetectors.add(fIllegalOverride); |
186 | } |
187 | if (fIllegalMethodRef != null) { |
188 | fDetectors.add(fIllegalMethodRef); |
189 | } |
190 | if (fIllegalFieldRef != null) { |
191 | fDetectors.add(fIllegalFieldRef); |
192 | } |
193 | if (fSystemApiDetector != null) { |
194 | fDetectors.add(fSystemApiDetector); |
195 | } |
196 | addLeakDetectors(fDetectors, component); |
197 | } |
198 | |
199 | /** |
200 | * Returns whether the given problem kind should be ignored. |
201 | * |
202 | * @param problemKey |
203 | * @return whether the given problem kind should be ignored |
204 | */ |
205 | private boolean isIgnore(String problemKey, IProject project) { |
206 | int severity = ApiPlugin.getDefault().getSeverityLevel(problemKey, project); |
207 | return severity == ApiPlugin.SEVERITY_IGNORE; |
208 | } |
209 | |
210 | /** |
211 | * Returns a set of all non-API package names that are in prerequisite components. |
212 | * |
213 | * @return |
214 | */ |
215 | Set getNonApiPackageNames() { |
216 | return fNonApiPackageNames; |
217 | } |
218 | |
219 | /** |
220 | * Adds additional non-API package descriptors to the detector builder. |
221 | * @param packagee |
222 | * @return true if the descriptor did not exist in the current collection and was added, false otherwise |
223 | */ |
224 | public boolean addNonApiPackageName(String packagee) { |
225 | if(packagee != null) { |
226 | return fNonApiPackageNames.add(packagee); |
227 | } |
228 | return false; |
229 | } |
230 | |
231 | /** |
232 | * Returns a list of problem detectors to be used. |
233 | * |
234 | * @return problem detectors |
235 | */ |
236 | List getProblemDetectors() { |
237 | return fDetectors; |
238 | } |
239 | |
240 | /** |
241 | * Adds any leak detectors to the listing. If a project context is available we |
242 | * filter out disabled detectors based on project / workspace preference settings |
243 | * @param detectors |
244 | */ |
245 | private void addLeakDetectors(List detectors, IApiComponent component) { |
246 | IProject project = getProject(component); |
247 | if(project != null) { |
248 | if(!isIgnore(IApiProblemTypes.LEAK_EXTEND, project)) { |
249 | detectors.add(new LeakExtendsProblemDetector(fNonApiPackageNames)); |
250 | } |
251 | if(!isIgnore(IApiProblemTypes.LEAK_IMPLEMENT, project)) { |
252 | detectors.add(new LeakImplementsProblemDetector(fNonApiPackageNames)); |
253 | } |
254 | if(!isIgnore(IApiProblemTypes.LEAK_FIELD_DECL, project)) { |
255 | detectors.add(new LeakFieldProblemDetector(fNonApiPackageNames)); |
256 | } |
257 | if(!isIgnore(IApiProblemTypes.LEAK_METHOD_PARAM, project)) { |
258 | detectors.add(new LeakParameterTypeDetector(fNonApiPackageNames)); |
259 | } |
260 | if(!isIgnore(IApiProblemTypes.LEAK_METHOD_RETURN_TYPE, project)) { |
261 | detectors.add(new LeakReturnTypeDetector(fNonApiPackageNames)); |
262 | } |
263 | } |
264 | else { |
265 | // add all leak detectors by default if we have no preference context |
266 | detectors.add(new LeakExtendsProblemDetector(fNonApiPackageNames)); |
267 | detectors.add(new LeakImplementsProblemDetector(fNonApiPackageNames)); |
268 | detectors.add(new LeakFieldProblemDetector(fNonApiPackageNames)); |
269 | detectors.add(new LeakReturnTypeDetector(fNonApiPackageNames)); |
270 | detectors.add(new LeakParameterTypeDetector(fNonApiPackageNames)); |
271 | } |
272 | } |
273 | } |