cdb35a0c9106caf20da9276fee525651a25bd657
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / corext / util / JdtFlags.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.corext.util;
12
13 import java.lang.reflect.Modifier;
14
15 import net.sourceforge.phpdt.core.Flags;
16 import net.sourceforge.phpdt.core.IJavaElement;
17 import net.sourceforge.phpdt.core.IMember;
18 import net.sourceforge.phpdt.core.IType;
19 import net.sourceforge.phpdt.core.JavaModelException;
20 import net.sourceforge.phpdt.internal.corext.Assert;
21
22 public class JdtFlags {
23         private JdtFlags() {
24         }
25
26         public static final String VISIBILITY_STRING_PRIVATE = "private"; //$NON-NLS-1$
27
28         public static final String VISIBILITY_STRING_PACKAGE = ""; //$NON-NLS-1$
29
30         public static final String VISIBILITY_STRING_PROTECTED = "protected"; //$NON-NLS-1$
31
32         public static final String VISIBILITY_STRING_PUBLIC = "public"; //$NON-NLS-1$
33
34         public static final int VISIBILITY_CODE_INVALID = -1;
35
36         public static boolean isAbstract(IMember member) throws JavaModelException {
37                 if (isInterfaceMethod(member))
38                         return true;
39                 return Flags.isAbstract(member.getFlags());
40         }
41
42         // public static boolean isAbstract(IMethodBinding member) {
43         // if (isInterfaceMember(member))
44         // return true;
45         // return Modifier.isAbstract(member.getModifiers());
46         // }
47
48         public static boolean isDeprecated(IMember member)
49                         throws JavaModelException {
50                 return Flags.isDeprecated(member.getFlags());
51         }
52
53         public static boolean isFinal(IMember member) throws JavaModelException {
54                 if (isInterfaceField(member))
55                         return true;
56                 if (isAnonymousType(member))
57                         return true;
58                 return Flags.isFinal(member.getFlags());
59         }
60
61         // public static boolean isNative(IMember member) throws JavaModelException{
62         // return Flags.isNative(member.getFlags());
63         // }
64
65         public static boolean isPackageVisible(IMember member)
66                         throws JavaModelException {
67                 return (!isPrivate(member) && !isProtected(member) && !isPublic(member));
68         }
69
70         // public static boolean isPackageVisible(BodyDeclaration bodyDeclaration) {
71         // return (! isPrivate(bodyDeclaration) && ! isProtected(bodyDeclaration) &&
72         // ! isPublic(bodyDeclaration));
73         // }
74         //      
75         // public static boolean isPackageVisible(IBinding binding) {
76         // return (! isPrivate(binding) && ! isProtected(binding) && !
77         // isPublic(binding));
78         // }
79
80         public static boolean isPrivate(IMember member) throws JavaModelException {
81                 return Flags.isPrivate(member.getFlags());
82         }
83
84         // public static boolean isPrivate(BodyDeclaration bodyDeclaration) {
85         // return Modifier.isPrivate(bodyDeclaration.getModifiers());
86         // }
87         //      
88         // public static boolean isPrivate(IBinding binding) {
89         // return Modifier.isPrivate(binding.getModifiers());
90         // }
91
92         public static boolean isProtected(IMember member) throws JavaModelException {
93                 return Flags.isProtected(member.getFlags());
94         }
95
96         // public static boolean isProtected(BodyDeclaration bodyDeclaration) {
97         // return Modifier.isProtected(bodyDeclaration.getModifiers());
98         // }
99         //      
100         // public static boolean isProtected(IBinding binding) {
101         // return Modifier.isProtected(binding.getModifiers());
102         // }
103
104         public static boolean isPublic(IMember member) throws JavaModelException {
105                 if (isInterfaceMember(member))
106                         return true;
107                 return Flags.isPublic(member.getFlags());
108         }
109
110         // public static boolean isPublic(IBinding binding) {
111         // if (isInterfaceMember(binding))
112         // return true;
113         // return Modifier.isPublic(binding.getModifiers());
114         // }
115         //      
116         //
117         // public static boolean isPublic(BodyDeclaration bodyDeclaration) {
118         // if (isInterfaceMember(bodyDeclaration))
119         // return true;
120         // return Modifier.isPublic(bodyDeclaration.getModifiers());
121         // }
122
123         public static boolean isStatic(IMember member) throws JavaModelException {
124                 if (isNestedInterface(member))
125                         return true;
126                 if (member.getElementType() != IJavaElement.METHOD
127                                 && isInterfaceMember(member))
128                         return true;
129                 return Flags.isStatic(member.getFlags());
130         }
131
132         // public static boolean isStatic(IMethodBinding methodBinding){
133         // return Modifier.isStatic(methodBinding.getModifiers());
134         // }
135         //
136         // public static boolean isStatic(IVariableBinding variableBinding){
137         // if (isInterfaceMember(variableBinding))
138         // return true;
139         // return Modifier.isStatic(variableBinding.getModifiers());
140         // }
141
142         // public static boolean isStrictfp(IMember member) throws
143         // JavaModelException{
144         // return Flags.isStrictfp(member.getFlags());
145         // }
146         //
147         // public static boolean isSynchronized(IMember member) throws
148         // JavaModelException{
149         // return Flags.isSynchronized(member.getFlags());
150         // }
151         //
152         // public static boolean isSynthetic(IMember member) throws
153         // JavaModelException{
154         // return Flags.isSynthetic(member.getFlags());
155         // }
156         //
157         // public static boolean isTransient(IMember member) throws
158         // JavaModelException{
159         // return Flags.isTransient(member.getFlags());
160         // }
161
162         // public static boolean isVolatile(IMember member) throws
163         // JavaModelException{
164         // return Flags.isVolatile(member.getFlags());
165         // }
166
167         private static boolean isInterfaceMethod(IMember member)
168                         throws JavaModelException {
169                 return member.getElementType() == IJavaElement.METHOD
170                                 && isInterfaceMember(member);
171         }
172
173         private static boolean isInterfaceField(IMember member)
174                         throws JavaModelException {
175                 return member.getElementType() == IJavaElement.FIELD
176                                 && isInterfaceMember(member);
177         }
178
179         private static boolean isInterfaceMember(IMember member)
180                         throws JavaModelException {
181                 return member.getDeclaringType() != null
182                                 && member.getDeclaringType().isInterface();
183         }
184
185         // private static boolean isInterfaceMember(IBinding binding) {
186         // ITypeBinding declaringType= null;
187         // if (binding instanceof IVariableBinding) {
188         // declaringType= ((IVariableBinding) binding).getDeclaringClass();
189         // } else if (binding instanceof IMethodBinding) {
190         // declaringType= ((IMethodBinding) binding).getDeclaringClass();
191         // } else if (binding instanceof ITypeBinding) {
192         // declaringType= ((ITypeBinding) binding).getDeclaringClass();
193         // }
194         // return declaringType != null && declaringType.isInterface();
195         // }
196
197         // private static boolean isInterfaceMember(BodyDeclaration bodyDeclaration)
198         // {
199         // return (bodyDeclaration.getParent() instanceof TypeDeclaration) &&
200         // ((TypeDeclaration)bodyDeclaration.getParent()).isInterface();
201         // }
202
203         private static boolean isNestedInterface(IMember member)
204                         throws JavaModelException {
205                 return member.getElementType() == IJavaElement.TYPE
206                                 && member.getDeclaringType() != null
207                                 && ((IType) member).isInterface();
208         }
209
210         private static boolean isAnonymousType(IMember member)
211                         throws JavaModelException {
212                 return member.getElementType() == IJavaElement.TYPE
213                                 && ((IType) member).isAnonymous();
214         }
215
216         public static int getVisibilityCode(IMember member)
217                         throws JavaModelException {
218                 if (isPublic(member))
219                         return Modifier.PUBLIC;
220                 else if (isProtected(member))
221                         return Modifier.PROTECTED;
222                 // else if (isPackageVisible(member))
223                 // return Modifier.NONE;
224                 else if (isPrivate(member))
225                         return Modifier.PRIVATE;
226                 // Assert.isTrue(false);
227                 // return VISIBILITY_CODE_INVALID;
228                 return Modifier.PUBLIC;
229         }
230
231         // public static int getVisibilityCode(BodyDeclaration bodyDeclaration) {
232         // if (isPublic(bodyDeclaration))
233         // return Modifier.PUBLIC;
234         // else if (isProtected(bodyDeclaration))
235         // return Modifier.PROTECTED;
236         // else if (isPackageVisible(bodyDeclaration))
237         // return Modifier.NONE;
238         // else if (isPrivate(bodyDeclaration))
239         // return Modifier.PRIVATE;
240         // Assert.isTrue(false);
241         // return VISIBILITY_CODE_INVALID;
242         // }
243
244         // public static int getVisibilityCode(IBinding binding) {
245         // if (isPublic(binding))
246         // return Modifier.PUBLIC;
247         // else if (isProtected(binding))
248         // return Modifier.PROTECTED;
249         // else if (isPackageVisible(binding))
250         // return Modifier.NONE;
251         // else if (isPrivate(binding))
252         // return Modifier.PRIVATE;
253         // Assert.isTrue(false);
254         // return VISIBILITY_CODE_INVALID;
255         // }
256
257         public static String getVisibilityString(int visibilityCode) {
258                 if (Modifier.isPublic(visibilityCode))
259                         return VISIBILITY_STRING_PUBLIC;
260                 if (Modifier.isProtected(visibilityCode))
261                         return VISIBILITY_STRING_PROTECTED;
262                 if (Modifier.isPrivate(visibilityCode))
263                         return VISIBILITY_STRING_PRIVATE;
264                 return VISIBILITY_STRING_PACKAGE;
265         }
266
267         public static void assertVisibility(int visibility) {
268                 Assert.isTrue(visibility == Modifier.PUBLIC
269                                 || visibility == Modifier.PROTECTED ||
270                                 // visibility == Modifier.NONE ||
271                                 visibility == Modifier.PRIVATE);
272         }
273
274         public static boolean isHigherVisibility(int newVisibility,
275                         int oldVisibility) {
276                 assertVisibility(oldVisibility);
277                 assertVisibility(newVisibility);
278                 switch (oldVisibility) {
279                 case Modifier.PRIVATE:
280                         return // newVisibility == Modifier.NONE ||
281                         newVisibility == Modifier.PUBLIC
282                                         || newVisibility == Modifier.PROTECTED;
283                         // case Modifier.NONE :
284                         // return newVisibility == Modifier.PUBLIC
285                         // || newVisibility == Modifier.PROTECTED;
286
287                 case Modifier.PROTECTED:
288                         return newVisibility == Modifier.PUBLIC;
289
290                 case Modifier.PUBLIC:
291                         return false;
292                 default:
293                         // Assert.isTrue(false);
294                         return false;
295                 }
296         }
297
298         public static int getLowerVisibility(int visibility1, int visibility2) {
299                 if (isHigherVisibility(visibility1, visibility2))
300                         return visibility2;
301                 else
302                         return visibility1;
303         }
304
305         public static int clearAccessModifiers(int flags) {
306                 return clearFlag(Modifier.PROTECTED | Modifier.PUBLIC
307                                 | Modifier.PRIVATE, flags);
308         }
309
310         public static int clearFlag(int flag, int flags) {
311                 return flags & ~flag;
312         }
313 }