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