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;
20 import net.sourceforge.phpdt.internal.corext.Assert;
22 public class JdtFlags {
26 public static final String VISIBILITY_STRING_PRIVATE = "private"; //$NON-NLS-1$
28 public static final String VISIBILITY_STRING_PACKAGE = ""; //$NON-NLS-1$
30 public static final String VISIBILITY_STRING_PROTECTED = "protected"; //$NON-NLS-1$
32 public static final String VISIBILITY_STRING_PUBLIC = "public"; //$NON-NLS-1$
34 public static final int VISIBILITY_CODE_INVALID = -1;
36 public static boolean isAbstract(IMember member) throws JavaModelException {
37 if (isInterfaceMethod(member))
39 return Flags.isAbstract(member.getFlags());
42 // public static boolean isAbstract(IMethodBinding member) {
43 // if (isInterfaceMember(member))
45 // return Modifier.isAbstract(member.getModifiers());
48 public static boolean isDeprecated(IMember member)
49 throws JavaModelException {
50 return Flags.isDeprecated(member.getFlags());
53 public static boolean isFinal(IMember member) throws JavaModelException {
54 if (isInterfaceField(member))
56 if (isAnonymousType(member))
58 return Flags.isFinal(member.getFlags());
61 // public static boolean isNative(IMember member) throws JavaModelException{
62 // return Flags.isNative(member.getFlags());
65 public static boolean isPackageVisible(IMember member)
66 throws JavaModelException {
67 return (!isPrivate(member) && !isProtected(member) && !isPublic(member));
70 // public static boolean isPackageVisible(BodyDeclaration bodyDeclaration) {
71 // return (! isPrivate(bodyDeclaration) && ! isProtected(bodyDeclaration) &&
72 // ! isPublic(bodyDeclaration));
75 // public static boolean isPackageVisible(IBinding binding) {
76 // return (! isPrivate(binding) && ! isProtected(binding) && !
77 // isPublic(binding));
80 public static boolean isPrivate(IMember member) throws JavaModelException {
81 return Flags.isPrivate(member.getFlags());
84 // public static boolean isPrivate(BodyDeclaration bodyDeclaration) {
85 // return Modifier.isPrivate(bodyDeclaration.getModifiers());
88 // public static boolean isPrivate(IBinding binding) {
89 // return Modifier.isPrivate(binding.getModifiers());
92 public static boolean isProtected(IMember member) throws JavaModelException {
93 return Flags.isProtected(member.getFlags());
96 // public static boolean isProtected(BodyDeclaration bodyDeclaration) {
97 // return Modifier.isProtected(bodyDeclaration.getModifiers());
100 // public static boolean isProtected(IBinding binding) {
101 // return Modifier.isProtected(binding.getModifiers());
104 public static boolean isPublic(IMember member) throws JavaModelException {
105 if (isInterfaceMember(member))
107 return Flags.isPublic(member.getFlags());
110 // public static boolean isPublic(IBinding binding) {
111 // if (isInterfaceMember(binding))
113 // return Modifier.isPublic(binding.getModifiers());
117 // public static boolean isPublic(BodyDeclaration bodyDeclaration) {
118 // if (isInterfaceMember(bodyDeclaration))
120 // return Modifier.isPublic(bodyDeclaration.getModifiers());
123 public static boolean isStatic(IMember member) throws JavaModelException {
124 if (isNestedInterface(member))
126 if (member.getElementType() != IJavaElement.METHOD
127 && isInterfaceMember(member))
129 return Flags.isStatic(member.getFlags());
132 // public static boolean isStatic(IMethodBinding methodBinding){
133 // return Modifier.isStatic(methodBinding.getModifiers());
136 // public static boolean isStatic(IVariableBinding variableBinding){
137 // if (isInterfaceMember(variableBinding))
139 // return Modifier.isStatic(variableBinding.getModifiers());
142 // public static boolean isStrictfp(IMember member) throws
143 // JavaModelException{
144 // return Flags.isStrictfp(member.getFlags());
147 // public static boolean isSynchronized(IMember member) throws
148 // JavaModelException{
149 // return Flags.isSynchronized(member.getFlags());
152 // public static boolean isSynthetic(IMember member) throws
153 // JavaModelException{
154 // return Flags.isSynthetic(member.getFlags());
157 // public static boolean isTransient(IMember member) throws
158 // JavaModelException{
159 // return Flags.isTransient(member.getFlags());
162 // public static boolean isVolatile(IMember member) throws
163 // JavaModelException{
164 // return Flags.isVolatile(member.getFlags());
167 private static boolean isInterfaceMethod(IMember member)
168 throws JavaModelException {
169 return member.getElementType() == IJavaElement.METHOD
170 && isInterfaceMember(member);
173 private static boolean isInterfaceField(IMember member)
174 throws JavaModelException {
175 return member.getElementType() == IJavaElement.FIELD
176 && isInterfaceMember(member);
179 private static boolean isInterfaceMember(IMember member)
180 throws JavaModelException {
181 return member.getDeclaringType() != null
182 && member.getDeclaringType().isInterface();
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();
194 // return declaringType != null && declaringType.isInterface();
197 // private static boolean isInterfaceMember(BodyDeclaration bodyDeclaration)
199 // return (bodyDeclaration.getParent() instanceof TypeDeclaration) &&
200 // ((TypeDeclaration)bodyDeclaration.getParent()).isInterface();
203 private static boolean isNestedInterface(IMember member)
204 throws JavaModelException {
205 return member.getElementType() == IJavaElement.TYPE
206 && member.getDeclaringType() != null
207 && ((IType) member).isInterface();
210 private static boolean isAnonymousType(IMember member)
211 throws JavaModelException {
212 return member.getElementType() == IJavaElement.TYPE
213 && ((IType) member).isAnonymous();
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;
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;
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;
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;
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);
274 public static boolean isHigherVisibility(int newVisibility,
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;
287 case Modifier.PROTECTED:
288 return newVisibility == Modifier.PUBLIC;
290 case Modifier.PUBLIC:
293 // Assert.isTrue(false);
298 public static int getLowerVisibility(int visibility1, int visibility2) {
299 if (isHigherVisibility(visibility1, visibility2))
305 public static int clearAccessModifiers(int flags) {
306 return clearFlag(Modifier.PROTECTED | Modifier.PUBLIC
307 | Modifier.PRIVATE, flags);
310 public static int clearFlag(int flag, int flags) {
311 return flags & ~flag;