1 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.IJavaElement; 25 import org.eclipse.jdt.core.IMethod; 26 import org.eclipse.jdt.core.IType; 27 import org.eclipse.jdt.core.ITypeHierarchy; 28 import org.eclipse.jdt.core.JavaModelException; 29 import org.eclipse.jdt.core.dom.ASTNode; 30 import org.eclipse.jdt.core.dom.CompilationUnit; 31 import org.eclipse.jdt.core.dom.IMethodBinding; 32 import org.eclipse.jdt.core.dom.MethodDeclaration; 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.dom.NodeFinder; 37 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 38 import org.eclipse.jdt.internal.corext.util.JdtFlags; 39 import org.eclipse.jdt.internal.corext.util.MethodOverrideTester; 40 import org.eclipse.jdt.internal.corext.util.SuperTypeHierarchyCache; 41 42 import org.eclipse.jdt.internal.ui.JavaPlugin; 43 import org.eclipse.jdt.internal.ui.JavaPluginImages; 44 import org.eclipse.jdt.internal.ui.javaeditor.ASTProvider; 45 import org.eclipse.jdt.internal.ui.viewsupport.ImageDescriptorRegistry; 46 import org.eclipse.jdt.internal.ui.viewsupport.ImageImageDescriptor; 47 48 58 public class OverrideIndicatorLabelDecorator implements ILabelDecorator, ILightweightLabelDecorator { 59 60 private ImageDescriptorRegistry fRegistry; 61 private boolean fUseNewRegistry= false; 62 63 67 public OverrideIndicatorLabelDecorator() { 68 this(null); 69 fUseNewRegistry= true; 70 } 71 72 78 82 public OverrideIndicatorLabelDecorator(ImageDescriptorRegistry registry) { 83 fRegistry= registry; 84 } 85 86 private ImageDescriptorRegistry getRegistry() { 87 if (fRegistry == null) { 88 fRegistry= fUseNewRegistry ? new ImageDescriptorRegistry() : JavaPlugin.getImageDescriptorRegistry(); 89 } 90 return fRegistry; 91 } 92 93 94 97 public String decorateText(String text, Object element) { 98 return text; 99 } 100 101 104 public Image decorateImage(Image image, Object element) { 105 int adornmentFlags= computeAdornmentFlags(element); 106 if (adornmentFlags != 0) { 107 ImageDescriptor baseImage= new ImageImageDescriptor(image); 108 Rectangle bounds= image.getBounds(); 109 return getRegistry().get(new JavaElementImageDescriptor(baseImage, adornmentFlags, new Point(bounds.width, bounds.height))); 110 } 111 return image; 112 } 113 114 120 public int computeAdornmentFlags(Object element) { 121 if (element instanceof IMethod) { 122 try { 123 IMethod method= (IMethod) element; 124 if (!method.getJavaProject().isOnClasspath(method)) { 125 return 0; 126 } 127 int flags= method.getFlags(); 128 if (!method.isConstructor() && !Flags.isPrivate(flags) && !Flags.isStatic(flags)) { 129 int res= getOverrideIndicators(method); 130 if (res != 0 && Flags.isSynchronized(flags)) { 131 return res | JavaElementImageDescriptor.SYNCHRONIZED; 132 } 133 return res; 134 } 135 } catch (JavaModelException e) { 136 if (!e.isDoesNotExist()) { 137 JavaPlugin.log(e); 138 } 139 } 140 } 141 return 0; 142 } 143 144 151 protected int getOverrideIndicators(IMethod method) throws JavaModelException { 152 CompilationUnit astRoot= JavaPlugin.getDefault().getASTProvider().getAST((IJavaElement) method.getOpenable(), ASTProvider.WAIT_ACTIVE_ONLY, null); 153 if (astRoot != null) { 154 int res= findInHierarchyWithAST(astRoot, method); 155 if (res != -1) { 156 return res; 157 } 158 } 159 160 IType type= method.getDeclaringType(); 161 162 MethodOverrideTester methodOverrideTester= SuperTypeHierarchyCache.getMethodOverrideTester(type); 163 IMethod defining= methodOverrideTester.findOverriddenMethod(method, true); 164 if (defining != null) { 165 if (JdtFlags.isAbstract(defining)) { 166 return JavaElementImageDescriptor.IMPLEMENTS; 167 } else { 168 return JavaElementImageDescriptor.OVERRIDES; 169 } 170 } 171 return 0; 172 } 173 174 private int findInHierarchyWithAST(CompilationUnit astRoot, IMethod method) throws JavaModelException { 175 ASTNode node= NodeFinder.perform(astRoot, method.getNameRange()); 176 if (node instanceof SimpleName && node.getParent() instanceof MethodDeclaration) { 177 IMethodBinding binding= ((MethodDeclaration) node.getParent()).resolveBinding(); 178 if (binding != null) { 179 IMethodBinding defining= Bindings.findOverriddenMethod(binding, true); 180 if (defining != null) { 181 if (JdtFlags.isAbstract(defining)) { 182 return JavaElementImageDescriptor.IMPLEMENTS; 183 } else { 184 return JavaElementImageDescriptor.OVERRIDES; 185 } 186 } 187 return 0; 188 } 189 } 190 return -1; 191 } 192 193 203 protected int findInHierarchy(IType type, ITypeHierarchy hierarchy, String name, String [] paramTypes) throws JavaModelException { 204 IType superClass= hierarchy.getSuperclass(type); 205 if (superClass != null) { 206 IMethod res= JavaModelUtil.findMethodInHierarchy(hierarchy, superClass, name, paramTypes, false); 207 if (res != null && !Flags.isPrivate(res.getFlags()) && JavaModelUtil.isVisibleInHierarchy(res, type.getPackageFragment())) { 208 if (JdtFlags.isAbstract(res)) { 209 return JavaElementImageDescriptor.IMPLEMENTS; 210 } else { 211 return JavaElementImageDescriptor.OVERRIDES; 212 } 213 } 214 } 215 IType[] interfaces= hierarchy.getSuperInterfaces(type); 216 for (int i= 0; i < interfaces.length; i++) { 217 IMethod res= JavaModelUtil.findMethodInHierarchy(hierarchy, interfaces[i], name, paramTypes, false); 218 if (res != null) { 219 if (JdtFlags.isAbstract(res)) { 220 return JavaElementImageDescriptor.IMPLEMENTS; 221 } else { 222 return JavaElementImageDescriptor.OVERRIDES; 223 } 224 } 225 } 226 return 0; 227 } 228 229 232 public void addListener(ILabelProviderListener listener) { 233 } 234 235 238 public void dispose() { 239 if (fRegistry != null && fUseNewRegistry) { 240 fRegistry.dispose(); 241 } 242 } 243 244 247 public boolean isLabelProperty(Object element, String property) { 248 return true; 249 } 250 251 254 public void removeListener(ILabelProviderListener listener) { 255 } 256 257 260 public void decorate(Object element, IDecoration decoration) { 261 int adornmentFlags= computeAdornmentFlags(element); 262 if ((adornmentFlags & JavaElementImageDescriptor.IMPLEMENTS) != 0) { 263 if ((adornmentFlags & JavaElementImageDescriptor.SYNCHRONIZED) != 0) { 264 decoration.addOverlay(JavaPluginImages.DESC_OVR_SYNCH_AND_IMPLEMENTS); 265 } else { 266 decoration.addOverlay(JavaPluginImages.DESC_OVR_IMPLEMENTS); 267 } 268 } else if ((adornmentFlags & JavaElementImageDescriptor.OVERRIDES) != 0) { 269 if ((adornmentFlags & JavaElementImageDescriptor.SYNCHRONIZED) != 0) { 270 decoration.addOverlay(JavaPluginImages.DESC_OVR_SYNCH_AND_OVERRIDES); 271 } else { 272 decoration.addOverlay(JavaPluginImages.DESC_OVR_OVERRIDES); 273 } 274 } 275 } 276 277 } 278 | Popular Tags |