1 11 12 package org.eclipse.jdt.apt.core.internal.util; 13 14 import org.eclipse.jdt.apt.core.internal.NonEclipseImplementationException; 15 import org.eclipse.jdt.apt.core.internal.declaration.DeclarationImpl; 16 import org.eclipse.jdt.apt.core.internal.declaration.EclipseDeclarationImpl; 17 import org.eclipse.jdt.apt.core.internal.declaration.EclipseMirrorObject; 18 import org.eclipse.jdt.apt.core.internal.declaration.MemberDeclarationImpl; 19 import org.eclipse.jdt.core.dom.IBinding; 20 import org.eclipse.jdt.core.dom.IMethodBinding; 21 import org.eclipse.jdt.core.dom.IPackageBinding; 22 import org.eclipse.jdt.core.dom.ITypeBinding; 23 import org.eclipse.jdt.core.dom.IVariableBinding; 24 import org.eclipse.jdt.core.dom.Modifier; 25 26 import com.sun.mirror.declaration.MemberDeclaration; 27 import com.sun.mirror.declaration.MethodDeclaration; 28 import com.sun.mirror.util.Declarations; 29 30 public class DeclarationsUtil implements Declarations 31 { 32 public boolean hides(MemberDeclaration sub, MemberDeclaration sup) { 33 34 if (sub == sup || sub.equals(sup)) 36 return false; 37 38 if( ! ((EclipseDeclarationImpl)sub).isBindingBased() || 39 ! ((EclipseDeclarationImpl)sup).isBindingBased() ) 40 return false; 41 42 MemberDeclarationImpl subImpl = (MemberDeclarationImpl)sub; 43 MemberDeclarationImpl supImpl = (MemberDeclarationImpl)sup; 44 45 IBinding subBinding = subImpl.getDeclarationBinding(); 46 IBinding supBinding = supImpl.getDeclarationBinding(); 47 48 49 int subKind = subBinding.getKind(); 52 int supKind = supBinding.getKind(); 53 if (subKind != supKind || subKind == IBinding.PACKAGE) 54 return false; 55 if (!subBinding.getName().equals(supBinding.getName())) 56 return false; 57 58 if (subKind == IBinding.METHOD) { 60 boolean allowed = false; 61 int modifiers = subBinding.getModifiers(); 62 if ((modifiers & Modifier.STATIC) == Modifier.STATIC) { 63 IMethodBinding methodBinding = (IMethodBinding)subBinding; 64 if (methodBinding.isSubsignature((IMethodBinding)supBinding)) { 65 allowed = true; 66 } 67 } 68 if (!allowed) 69 return false; 70 } 71 72 ITypeBinding subClass = getDeclaringClass(subBinding); 74 ITypeBinding supClass = getDeclaringClass(supBinding); 75 if (subClass == null || supClass == null) 76 return false; 77 if (!subClass.isSubTypeCompatible(supClass)) 78 return false; 79 80 if (!isVisibleForHiding(supClass, supClass, supBinding.getModifiers())) 82 return false; 83 84 return true; 85 } 86 87 95 private static boolean isVisibleForHiding( 96 final ITypeBinding declaringTarget, 97 final ITypeBinding declaringViewer, 98 final int modifiers) { 99 100 if ((modifiers & Modifier.PUBLIC) == Modifier.PUBLIC) 102 return true; 103 if ((modifiers & Modifier.PRIVATE) == Modifier.PRIVATE) { 104 if (declaringTarget.equals(declaringViewer)) 106 return true; 107 else 108 return false; 109 } 110 if ((modifiers & Modifier.PROTECTED) == Modifier.PROTECTED) { 111 return true; 113 } 114 IPackageBinding targetPackage = declaringTarget.getPackage(); 117 IPackageBinding viewerPackage = declaringViewer.getPackage(); 118 return targetPackage.equals(viewerPackage); 119 } 120 121 public boolean overrides(MethodDeclaration sub, MethodDeclaration sup) { 122 final IMethodBinding subBinding = (IMethodBinding)getBinding(sub); 123 final IMethodBinding supBinding = (IMethodBinding)getBinding(sup); 124 if(subBinding == null || supBinding == null) return false; 125 return subBinding.overrides(supBinding); 126 } 127 128 private static IBinding getBinding(MemberDeclaration memberDecl) 129 throws NonEclipseImplementationException 130 { 131 if( memberDecl == null ) return null; 132 if( memberDecl instanceof EclipseMirrorObject ){ 133 if( memberDecl instanceof DeclarationImpl ) 134 return ((DeclarationImpl)memberDecl).getDeclarationBinding(); 135 else 136 return null; 137 } 138 throw new NonEclipseImplementationException("only applicable to eclipse type system objects." + " Found " + memberDecl.getClass().getName()); } 141 142 private static ITypeBinding getDeclaringClass(IBinding binding) { 143 int kind = binding.getKind(); 144 if (kind == IBinding.TYPE) 145 return ((ITypeBinding)binding).getDeclaringClass(); 146 if (kind == IBinding.METHOD) 147 return ((IMethodBinding)binding).getDeclaringClass(); 148 if (kind == IBinding.VARIABLE) 149 return ((IVariableBinding)binding).getDeclaringClass(); 150 151 return null; 153 } 154 } 155 | Popular Tags |