KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > apt > core > internal > util > DeclarationsUtil


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 BEA Systems, Inc.
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  * tyeung@bea.com - initial API and implementation
10  *******************************************************************************/

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         // A declaration cannot hide itself
35
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         // Hiding can only take place between declarations of the same kind and name,
50
// and types, variables and methods
51
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         // Methods must be both static and the sub a subsignature of the sup
59
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         // sub's enclosing class must be a subclass of sup's
73
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         // sup must be visible from sub
81
if (!isVisibleForHiding(supClass, supClass, supBinding.getModifiers()))
82             return false;
83         
84         return true;
85     }
86     
87     /**
88      * Is a method, field, type visible from the viewer?
89      * That is, do accessibility rules allow it? (public, protected, etc.)<P>
90      *
91      * Note that we make an assumption about protected here since
92      * its use in hides() already determines that the declaringTarget
93      * must be a subclass of the declaringViewer.
94      */

95     private static boolean isVisibleForHiding(
96             final ITypeBinding declaringTarget,
97             final ITypeBinding declaringViewer,
98             final int modifiers) {
99         
100         // Public is always visible
101
if ((modifiers & Modifier.PUBLIC) == Modifier.PUBLIC)
102             return true;
103         if ((modifiers & Modifier.PRIVATE) == Modifier.PRIVATE) {
104             // Must be the same class
105
if (declaringTarget.equals(declaringViewer))
106                 return true;
107             else
108                 return false;
109         }
110         if ((modifiers & Modifier.PROTECTED) == Modifier.PROTECTED) {
111             // We've already checked for subclassing
112
return true;
113         }
114         // Package-friendly (no accessibility modifier)
115
// Classes must be in the same package
116
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." + //$NON-NLS-1$
139
" Found " + memberDecl.getClass().getName()); //$NON-NLS-1$
140
}
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         // Package binding -- no declaring class
152
return null;
153     }
154 }
155
Popular Tags