]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/ui/OverrideIndicatorLabelDecorator.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / ui / OverrideIndicatorLabelDecorator.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2010 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.ui;
12
13 import org.eclipse.swt.graphics.Image;
14 import org.eclipse.swt.graphics.Point;
15 import org.eclipse.swt.graphics.Rectangle;
16
17 import org.eclipse.jface.resource.ImageDescriptor;
18 import org.eclipse.jface.viewers.IDecoration;
19 import org.eclipse.jface.viewers.ILabelDecorator;
20 import org.eclipse.jface.viewers.ILabelProviderListener;
21 import org.eclipse.jface.viewers.ILightweightLabelDecorator;
22
23 import org.eclipse.jdt.core.Flags;
24 import org.eclipse.jdt.core.IMethod;
25 import org.eclipse.jdt.core.IType;
26 import org.eclipse.jdt.core.ITypeHierarchy;
27 import org.eclipse.jdt.core.JavaModelException;
28 import org.eclipse.jdt.core.dom.ASTNode;
29 import org.eclipse.jdt.core.dom.CompilationUnit;
30 import org.eclipse.jdt.core.dom.IMethodBinding;
31 import org.eclipse.jdt.core.dom.MethodDeclaration;
32 import org.eclipse.jdt.core.dom.NodeFinder;
33 import org.eclipse.jdt.core.dom.SimpleName;
34
35 import org.eclipse.jdt.internal.corext.dom.Bindings;
36 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
37 import org.eclipse.jdt.internal.corext.util.JdtFlags;
38 import org.eclipse.jdt.internal.corext.util.MethodOverrideTester;
39 import org.eclipse.jdt.internal.corext.util.SuperTypeHierarchyCache;
40
41 import org.eclipse.jdt.internal.ui.JavaPlugin;
42 import org.eclipse.jdt.internal.ui.JavaPluginImages;
43 import org.eclipse.jdt.internal.ui.viewsupport.ImageDescriptorRegistry;
44 import org.eclipse.jdt.internal.ui.viewsupport.ImageImageDescriptor;
45
46 /**
47  * LabelDecorator that decorates an method's image with override or implements overlays.
48  * The viewer using this decorator is responsible for updating the images on element changes.
49  *
50  * <p>
51  * This class may be instantiated; it is not intended to be subclassed.
52  * </p>
53  *
54  * @since 2.0
55  *
56  * @noextend This class is not intended to be subclassed by clients.
57  */
58 public class OverrideIndicatorLabelDecorator implements ILabelDecorator, ILightweightLabelDecorator {
59
60         private ImageDescriptorRegistry fRegistry;
61         private boolean fUseNewRegistry= false;
62
63         /**
64          * Creates a decorator. The decorator creates an own image registry to cache
65          * images.
66          */
67         public OverrideIndicatorLabelDecorator() {
68                 this(null);
69                 fUseNewRegistry= true;
70         }
71
72         /*
73          * Creates decorator with a shared image registry.
74          *
75          * @param registry The registry to use or <code>null</code> to use the Java plugin's
76          * image registry.
77          */
78         /**
79          * Note: This constructor is for internal use only. Clients should not call this constructor.
80          *
81          * @param registry The registry to use.
82          * @noreference This method is not intended to be referenced by clients.
83          */
84         public OverrideIndicatorLabelDecorator(ImageDescriptorRegistry registry) {
85                 fRegistry= registry;
86         }
87
88         private ImageDescriptorRegistry getRegistry() {
89                 if (fRegistry == null) {
90                         fRegistry= fUseNewRegistry ? new ImageDescriptorRegistry() : JavaPlugin.getImageDescriptorRegistry();
91                 }
92                 return fRegistry;
93         }
94
95
96         /* (non-Javadoc)
97          * @see ILabelDecorator#decorateText(String, Object)
98          */
99         public String decorateText(String text, Object element) {
100                 return text;
101         }
102
103         /* (non-Javadoc)
104          * @see ILabelDecorator#decorateImage(Image, Object)
105          */
106         public Image decorateImage(Image image, Object element) {
107                 if (image == null)
108                         return null;
109
110                 int adornmentFlags= computeAdornmentFlags(element);
111                 if (adornmentFlags != 0) {
112                         ImageDescriptor baseImage= new ImageImageDescriptor(image);
113                         Rectangle bounds= image.getBounds();
114                         return getRegistry().get(new JavaElementImageDescriptor(baseImage, adornmentFlags, new Point(bounds.width, bounds.height)));
115                 }
116                 return image;
117         }
118
119         /**
120          * Note: This method is for internal use only. Clients should not call this method.
121          * @param element The element to decorate
122          * @return Resulting decorations (combination of JavaElementImageDescriptor.IMPLEMENTS
123          * and JavaElementImageDescriptor.OVERRIDES)
124          *
125          * @noreference This method is not intended to be referenced by clients.
126          */
127         public int computeAdornmentFlags(Object element) {
128                 if (element instanceof IMethod) {
129                         try {
130                                 IMethod method= (IMethod) element;
131                                 if (!method.getJavaProject().isOnClasspath(method)) {
132                                         return 0;
133                                 }
134                                 int flags= method.getFlags();
135                                 if (!method.isConstructor() && !Flags.isPrivate(flags) && !Flags.isStatic(flags)) {
136                                         int res= getOverrideIndicators(method);
137                                         if (res != 0 && Flags.isSynchronized(flags)) {
138                                                 return res | JavaElementImageDescriptor.SYNCHRONIZED;
139                                         }
140                                         return res;
141                                 }
142                         } catch (JavaModelException e) {
143                                 if (!e.isDoesNotExist()) {
144                                         JavaPlugin.log(e);
145                                 }
146                         }
147                 }
148                 return 0;
149         }
150
151         /**
152          * Note: This method is for internal use only. Clients should not call this method.
153          * 
154          * @param method The element to decorate
155          * @return Resulting decorations (combination of JavaElementImageDescriptor.IMPLEMENTS and
156          *         JavaElementImageDescriptor.OVERRIDES)
157          * @throws JavaModelException if accessing a Java Model element fails
158          * @noreference This method is not intended to be referenced by clients.
159          */
160         protected int getOverrideIndicators(IMethod method) throws JavaModelException {
161                 CompilationUnit astRoot= SharedASTProvider.getAST(method.getTypeRoot(), SharedASTProvider.WAIT_ACTIVE_ONLY, null);
162                 if (astRoot != null) {
163                         int res= findInHierarchyWithAST(astRoot, method);
164                         if (res != -1) {
165                                 return res;
166                         }
167                 }
168
169                 IType type= method.getDeclaringType();
170
171                 MethodOverrideTester methodOverrideTester= SuperTypeHierarchyCache.getMethodOverrideTester(type);
172                 IMethod defining= methodOverrideTester.findOverriddenMethod(method, true);
173                 if (defining != null) {
174                         if (JdtFlags.isAbstract(defining)) {
175                                 return JavaElementImageDescriptor.IMPLEMENTS;
176                         } else {
177                                 return JavaElementImageDescriptor.OVERRIDES;
178                         }
179                 }
180                 return 0;
181         }
182
183         private int findInHierarchyWithAST(CompilationUnit astRoot, IMethod method) throws JavaModelException {
184                 ASTNode node= NodeFinder.perform(astRoot, method.getNameRange());
185                 if (node instanceof SimpleName && node.getParent() instanceof MethodDeclaration) {
186                         IMethodBinding binding= ((MethodDeclaration) node.getParent()).resolveBinding();
187                         if (binding != null) {
188                                 IMethodBinding defining= Bindings.findOverriddenMethod(binding, true);
189                                 if (defining != null) {
190                                         if (JdtFlags.isAbstract(defining)) {
191                                                 return JavaElementImageDescriptor.IMPLEMENTS;
192                                         } else {
193                                                 return JavaElementImageDescriptor.OVERRIDES;
194                                         }
195                                 }
196                                 return 0;
197                         }
198                 }
199                 return -1;
200         }
201
202         /**
203          * Note: This method is for internal use only. Clients should not call this method.
204          * 
205          * @param type The declaring type of the method to decorate.
206          * @param hierarchy The type hierarchy of the declaring type.
207          * @param name The name of the method to find.
208          * @param paramTypes The parameter types of the method to find.
209          * @return The resulting decoration.
210          * @throws JavaModelException if accessing a Java Model element fails
211          * @deprecated Not used anymore. This method is not accurate for methods in generic types.
212          * 
213          * @noreference This method is not intended to be referenced by clients.
214          */
215         protected int findInHierarchy(IType type, ITypeHierarchy hierarchy, String name, String[] paramTypes) throws JavaModelException {
216                 IType superClass= hierarchy.getSuperclass(type);
217                 if (superClass != null) {
218                         IMethod res= JavaModelUtil.findMethodInHierarchy(hierarchy, superClass, name, paramTypes, false);
219                         if (res != null && !Flags.isPrivate(res.getFlags()) && JavaModelUtil.isVisibleInHierarchy(res, type.getPackageFragment())) {
220                                 if (JdtFlags.isAbstract(res)) {
221                                         return JavaElementImageDescriptor.IMPLEMENTS;
222                                 } else {
223                                         return JavaElementImageDescriptor.OVERRIDES;
224                                 }
225                         }
226                 }
227                 IType[] interfaces= hierarchy.getSuperInterfaces(type);
228                 for (int i= 0; i < interfaces.length; i++) {
229                         IMethod res= JavaModelUtil.findMethodInHierarchy(hierarchy, interfaces[i], name, paramTypes, false);
230                         if (res != null) {
231                                 if (JdtFlags.isAbstract(res)) {
232                                         return JavaElementImageDescriptor.IMPLEMENTS;
233                                 } else {
234                                         return JavaElementImageDescriptor.OVERRIDES;
235                                 }
236                         }
237                 }
238                 return 0;
239         }
240
241         /* (non-Javadoc)
242          * @see IBaseLabelProvider#addListener(ILabelProviderListener)
243          */
244         public void addListener(ILabelProviderListener listener) {
245         }
246
247         /* (non-Javadoc)
248          * @see IBaseLabelProvider#dispose()
249          */
250         public void dispose() {
251                 if (fRegistry != null && fUseNewRegistry) {
252                         fRegistry.dispose();
253                 }
254         }
255
256         /* (non-Javadoc)
257          * @see IBaseLabelProvider#isLabelProperty(Object, String)
258          */
259         public boolean isLabelProperty(Object element, String property) {
260                 return true;
261         }
262
263         /* (non-Javadoc)
264          * @see IBaseLabelProvider#removeListener(ILabelProviderListener)
265          */
266         public void removeListener(ILabelProviderListener listener) {
267         }
268
269         /* (non-Javadoc)
270          * @see org.eclipse.jface.viewers.ILightweightLabelDecorator#decorate(java.lang.Object, org.eclipse.jface.viewers.IDecoration)
271          */
272         public void decorate(Object element, IDecoration decoration) {
273                 int adornmentFlags= computeAdornmentFlags(element);
274                 if ((adornmentFlags & JavaElementImageDescriptor.IMPLEMENTS) != 0) {
275                         if ((adornmentFlags & JavaElementImageDescriptor.SYNCHRONIZED) != 0) {
276                                 decoration.addOverlay(JavaPluginImages.DESC_OVR_SYNCH_AND_IMPLEMENTS);
277                         } else {
278                                 decoration.addOverlay(JavaPluginImages.DESC_OVR_IMPLEMENTS);
279                         }
280                 } else if ((adornmentFlags & JavaElementImageDescriptor.OVERRIDES) != 0) {
281                         if ((adornmentFlags & JavaElementImageDescriptor.SYNCHRONIZED) != 0) {
282                                 decoration.addOverlay(JavaPluginImages.DESC_OVR_SYNCH_AND_OVERRIDES);
283                         } else {
284                                 decoration.addOverlay(JavaPluginImages.DESC_OVR_OVERRIDES);
285                         }
286                 }
287         }
288
289 }