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