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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.corext.util;
13 import java.lang.reflect.Modifier;
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;
21 //import net.sourceforge.phpdt.internal.corext.Assert;
22 import org.eclipse.core.runtime.Assert;
24 public class JdtFlags {
28 public static final String VISIBILITY_STRING_PRIVATE = "private"; //$NON-NLS-1$
30 public static final String VISIBILITY_STRING_PACKAGE = ""; //$NON-NLS-1$
32 public static final String VISIBILITY_STRING_PROTECTED = "protected"; //$NON-NLS-1$
34 public static final String VISIBILITY_STRING_PUBLIC = "public"; //$NON-NLS-1$
36 public static final int VISIBILITY_CODE_INVALID = -1;
38 public static boolean isAbstract(IMember member) throws JavaModelException {
39 if (isInterfaceMethod(member))
41 return Flags.isAbstract(member.getFlags());
44 // public static boolean isAbstract(IMethodBinding member) {
45 // if (isInterfaceMember(member))
47 // return Modifier.isAbstract(member.getModifiers());
50 public static boolean isDeprecated(IMember member)
51 throws JavaModelException {
52 return Flags.isDeprecated(member.getFlags());
55 public static boolean isFinal(IMember member) throws JavaModelException {
56 if (isInterfaceField(member))
58 if (isAnonymousType(member))
60 return Flags.isFinal(member.getFlags());
63 // public static boolean isNative(IMember member) throws JavaModelException{
64 // return Flags.isNative(member.getFlags());
67 public static boolean isPackageVisible(IMember member)
68 throws JavaModelException {
69 return (!isPrivate(member) && !isProtected(member) && !isPublic(member));
72 // public static boolean isPackageVisible(BodyDeclaration bodyDeclaration) {
73 // return (! isPrivate(bodyDeclaration) && ! isProtected(bodyDeclaration) &&
74 // ! isPublic(bodyDeclaration));
77 // public static boolean isPackageVisible(IBinding binding) {
78 // return (! isPrivate(binding) && ! isProtected(binding) && !
79 // isPublic(binding));
82 public static boolean isPrivate(IMember member) throws JavaModelException {
83 return Flags.isPrivate(member.getFlags());
86 // public static boolean isPrivate(BodyDeclaration bodyDeclaration) {
87 // return Modifier.isPrivate(bodyDeclaration.getModifiers());
90 // public static boolean isPrivate(IBinding binding) {
91 // return Modifier.isPrivate(binding.getModifiers());
94 public static boolean isProtected(IMember member) throws JavaModelException {
95 return Flags.isProtected(member.getFlags());
98 // public static boolean isProtected(BodyDeclaration bodyDeclaration) {
99 // return Modifier.isProtected(bodyDeclaration.getModifiers());
102 // public static boolean isProtected(IBinding binding) {
103 // return Modifier.isProtected(binding.getModifiers());
106 public static boolean isPublic(IMember member) throws JavaModelException {
107 if (isInterfaceMember(member))
109 return Flags.isPublic(member.getFlags());
112 // public static boolean isPublic(IBinding binding) {
113 // if (isInterfaceMember(binding))
115 // return Modifier.isPublic(binding.getModifiers());
119 // public static boolean isPublic(BodyDeclaration bodyDeclaration) {
120 // if (isInterfaceMember(bodyDeclaration))
122 // return Modifier.isPublic(bodyDeclaration.getModifiers());
125 public static boolean isStatic(IMember member) throws JavaModelException {
126 if (isNestedInterface(member))
128 if (member.getElementType() != IJavaElement.METHOD
129 && isInterfaceMember(member))
131 return Flags.isStatic(member.getFlags());
134 // public static boolean isStatic(IMethodBinding methodBinding){
135 // return Modifier.isStatic(methodBinding.getModifiers());
138 // public static boolean isStatic(IVariableBinding variableBinding){
139 // if (isInterfaceMember(variableBinding))
141 // return Modifier.isStatic(variableBinding.getModifiers());
144 // public static boolean isStrictfp(IMember member) throws
145 // JavaModelException{
146 // return Flags.isStrictfp(member.getFlags());
149 // public static boolean isSynchronized(IMember member) throws
150 // JavaModelException{
151 // return Flags.isSynchronized(member.getFlags());
154 // public static boolean isSynthetic(IMember member) throws
155 // JavaModelException{
156 // return Flags.isSynthetic(member.getFlags());
159 // public static boolean isTransient(IMember member) throws
160 // JavaModelException{
161 // return Flags.isTransient(member.getFlags());
164 // public static boolean isVolatile(IMember member) throws
165 // JavaModelException{
166 // return Flags.isVolatile(member.getFlags());
169 private static boolean isInterfaceMethod(IMember member)
170 throws JavaModelException {
171 return member.getElementType() == IJavaElement.METHOD
172 && isInterfaceMember(member);
175 private static boolean isInterfaceField(IMember member)
176 throws JavaModelException {
177 return member.getElementType() == IJavaElement.FIELD
178 && isInterfaceMember(member);
181 private static boolean isInterfaceMember(IMember member)
182 throws JavaModelException {
183 return member.getDeclaringType() != null
184 && member.getDeclaringType().isInterface();
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();
196 // return declaringType != null && declaringType.isInterface();
199 // private static boolean isInterfaceMember(BodyDeclaration bodyDeclaration)
201 // return (bodyDeclaration.getParent() instanceof TypeDeclaration) &&
202 // ((TypeDeclaration)bodyDeclaration.getParent()).isInterface();
205 private static boolean isNestedInterface(IMember member)
206 throws JavaModelException {
207 return member.getElementType() == IJavaElement.TYPE
208 && member.getDeclaringType() != null
209 && ((IType) member).isInterface();
212 private static boolean isAnonymousType(IMember member)
213 throws JavaModelException {
214 return member.getElementType() == IJavaElement.TYPE
215 && ((IType) member).isAnonymous();
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;
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;
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;
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;
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);
276 public static boolean isHigherVisibility(int newVisibility,
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;
289 case Modifier.PROTECTED:
290 return newVisibility == Modifier.PUBLIC;
292 case Modifier.PUBLIC:
295 // Assert.isTrue(false);
300 public static int getLowerVisibility(int visibility1, int visibility2) {
301 if (isHigherVisibility(visibility1, visibility2))
307 public static int clearAccessModifiers(int flags) {
308 return clearFlag(Modifier.PROTECTED | Modifier.PUBLIC
309 | Modifier.PRIVATE, flags);
312 public static int clearFlag(int flag, int flags) {
313 return flags & ~flag;