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