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.Set; |
14 | |
15 | import org.eclipse.core.runtime.CoreException; |
16 | import org.eclipse.core.runtime.IStatus; |
17 | import org.eclipse.core.runtime.Status; |
18 | import org.eclipse.jdt.core.Flags; |
19 | import org.eclipse.jdt.core.ISourceRange; |
20 | import org.eclipse.jdt.core.IType; |
21 | import org.eclipse.jface.text.BadLocationException; |
22 | import org.eclipse.jface.text.IDocument; |
23 | import org.eclipse.jface.text.Position; |
24 | import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin; |
25 | import org.eclipse.pde.api.tools.internal.provisional.IApiAnnotations; |
26 | import org.eclipse.pde.api.tools.internal.provisional.VisibilityModifiers; |
27 | import org.eclipse.pde.api.tools.internal.provisional.builder.IReference; |
28 | import org.eclipse.pde.api.tools.internal.provisional.descriptors.IElementDescriptor; |
29 | import org.eclipse.pde.api.tools.internal.provisional.model.IApiMember; |
30 | import org.eclipse.pde.api.tools.internal.provisional.problems.IApiProblem; |
31 | |
32 | import com.ibm.icu.text.MessageFormat; |
33 | |
34 | /** |
35 | * Detects leaked types. |
36 | * |
37 | * @since 1.1 |
38 | */ |
39 | public abstract class AbstractTypeLeakDetector extends AbstractLeakProblemDetector { |
40 | |
41 | /** |
42 | * @param nonApiPackageNames |
43 | */ |
44 | public AbstractTypeLeakDetector(Set nonApiPackageNames) { |
45 | super(nonApiPackageNames); |
46 | } |
47 | |
48 | /* (non-Javadoc) |
49 | * @see org.eclipse.pde.api.tools.internal.provisional.search.IApiProblemDetector#considerReference(org.eclipse.pde.api.tools.internal.provisional.model.IReference) |
50 | */ |
51 | public boolean considerReference(IReference reference) { |
52 | // consider the reference if the location the reference is made from is visible: |
53 | // i.e. a public or protected class in an API package |
54 | if (isNonAPIReference(reference)) { |
55 | IApiMember member = reference.getMember(); |
56 | int modifiers = member.getModifiers(); |
57 | if (((Flags.AccPublic | Flags.AccProtected) & modifiers) > 0) { |
58 | try { |
59 | IApiAnnotations annotations = member.getApiComponent().getApiDescription().resolveAnnotations(member.getHandle()); |
60 | // annotations can be null for members in top level non public types, but they are not visible/API |
61 | if (annotations != null) { |
62 | if (isApplicable(annotations) && isEnclosingTypeVisible(member)) { |
63 | retainReference(reference); |
64 | return true; |
65 | } |
66 | } |
67 | } catch (CoreException e) { |
68 | ApiPlugin.log(e.getStatus()); |
69 | } |
70 | } |
71 | } |
72 | return false; |
73 | } |
74 | |
75 | /* (non-Javadoc) |
76 | * @see org.eclipse.pde.api.tools.internal.search.AbstractProblemDetector#isProblem(org.eclipse.pde.api.tools.internal.provisional.model.IReference) |
77 | */ |
78 | protected boolean isProblem(IReference reference) { |
79 | IApiMember member = reference.getResolvedReference(); |
80 | try { |
81 | IApiAnnotations annotations = member.getApiComponent().getApiDescription().resolveAnnotations(member.getHandle()); |
82 | if (annotations != null) { |
83 | return VisibilityModifiers.isPrivate(annotations.getVisibility()); |
84 | } else { |
85 | // could be a reference to a top level secondary/non-public type |
86 | if (isEnclosingTypeVisible(member)) { |
87 | // this is an unexpected condition - the enclosing type is visible, but it has no annotations - log an error |
88 | ApiPlugin.log( |
89 | new Status( |
90 | IStatus.INFO, ApiPlugin.PLUGIN_ID, |
91 | MessageFormat.format(BuilderMessages.AbstractTypeLeakDetector_vis_type_has_no_api_description, new String[]{member.getName()}))); |
92 | } else { |
93 | // enclosing type is not visible - this is a problem |
94 | return true; |
95 | } |
96 | } |
97 | } catch (CoreException e) { |
98 | ApiPlugin.log(e); |
99 | } |
100 | return false; |
101 | } |
102 | |
103 | /** |
104 | * Returns whether the given annotations should be considered. |
105 | */ |
106 | protected boolean isApplicable(IApiAnnotations annotations) { |
107 | return VisibilityModifiers.isAPI(annotations.getVisibility()); |
108 | } |
109 | |
110 | /* (non-Javadoc) |
111 | * @see org.eclipse.pde.api.tools.internal.search.AbstractProblemDetector#getQualifiedMessageArgs(org.eclipse.pde.api.tools.internal.provisional.model.IReference) |
112 | */ |
113 | protected String[] getQualifiedMessageArgs(IReference reference) throws CoreException { |
114 | return new String[] {getQualifiedTypeName(reference.getResolvedReference()), getQualifiedTypeName(reference.getMember())}; |
115 | } |
116 | |
117 | /* (non-Javadoc) |
118 | * @see org.eclipse.pde.api.tools.internal.search.AbstractProblemDetector#getMessageArgs(org.eclipse.pde.api.tools.internal.provisional.model.IReference) |
119 | */ |
120 | protected String[] getMessageArgs(IReference reference) throws CoreException { |
121 | return new String[] {getSimpleTypeName(reference.getResolvedReference()), getSimpleTypeName(reference.getMember())}; |
122 | } |
123 | |
124 | /* (non-Javadoc) |
125 | * @see org.eclipse.pde.api.tools.internal.search.AbstractProblemDetector#getSourceRange(org.eclipse.jdt.core.IType, org.eclipse.jface.text.IDocument, org.eclipse.pde.api.tools.internal.provisional.model.IReference) |
126 | */ |
127 | protected Position getSourceRange(IType type, IDocument doc, IReference reference) throws CoreException, BadLocationException { |
128 | ISourceRange range = type.getNameRange(); |
129 | Position pos = null; |
130 | if(range != null) { |
131 | pos = new Position(range.getOffset(), range.getLength()); |
132 | } |
133 | if(pos == null) { |
134 | return defaultSourcePosition(type, reference); |
135 | } |
136 | return pos; |
137 | } |
138 | |
139 | /* (non-Javadoc) |
140 | * @see org.eclipse.pde.api.tools.internal.search.AbstractProblemDetector#getElementType(org.eclipse.pde.api.tools.internal.provisional.model.IReference) |
141 | */ |
142 | protected int getElementType(IReference reference) { |
143 | return IElementDescriptor.TYPE; |
144 | } |
145 | |
146 | /* (non-Javadoc) |
147 | * @see org.eclipse.pde.api.tools.internal.search.AbstractProblemDetector#getProblemKind() |
148 | */ |
149 | protected int getProblemKind() { |
150 | return IApiProblem.API_LEAK; |
151 | } |
152 | |
153 | } |