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$
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$
32 public static final int VISIBILITY_CODE_INVALID= -1;
34 public static boolean isAbstract(IMember member) throws JavaModelException{
35 if (isInterfaceMethod(member))
37 return Flags.isAbstract(member.getFlags());
40 // public static boolean isAbstract(IMethodBinding member) {
41 // if (isInterfaceMember(member))
43 // return Modifier.isAbstract(member.getModifiers());
46 public static boolean isDeprecated(IMember member) throws JavaModelException{
47 return Flags.isDeprecated(member.getFlags());
50 public static boolean isFinal(IMember member) throws JavaModelException{
51 if (isInterfaceField(member))
53 if (isAnonymousType(member))
55 return Flags.isFinal(member.getFlags());
58 // public static boolean isNative(IMember member) throws JavaModelException{
59 // return Flags.isNative(member.getFlags());
62 public static boolean isPackageVisible(IMember member) throws JavaModelException{
63 return (! isPrivate(member) && ! isProtected(member) && ! isPublic(member));
66 // public static boolean isPackageVisible(BodyDeclaration bodyDeclaration) {
67 // return (! isPrivate(bodyDeclaration) && ! isProtected(bodyDeclaration) && ! isPublic(bodyDeclaration));
70 // public static boolean isPackageVisible(IBinding binding) {
71 // return (! isPrivate(binding) && ! isProtected(binding) && ! isPublic(binding));
74 public static boolean isPrivate(IMember member) throws JavaModelException{
75 return Flags.isPrivate(member.getFlags());
78 // public static boolean isPrivate(BodyDeclaration bodyDeclaration) {
79 // return Modifier.isPrivate(bodyDeclaration.getModifiers());
82 // public static boolean isPrivate(IBinding binding) {
83 // return Modifier.isPrivate(binding.getModifiers());
86 public static boolean isProtected(IMember member) throws JavaModelException{
87 return Flags.isProtected(member.getFlags());
90 // public static boolean isProtected(BodyDeclaration bodyDeclaration) {
91 // return Modifier.isProtected(bodyDeclaration.getModifiers());
94 // public static boolean isProtected(IBinding binding) {
95 // return Modifier.isProtected(binding.getModifiers());
98 public static boolean isPublic(IMember member) throws JavaModelException{
99 if (isInterfaceMember(member))
101 return Flags.isPublic(member.getFlags());
104 // public static boolean isPublic(IBinding binding) {
105 // if (isInterfaceMember(binding))
107 // return Modifier.isPublic(binding.getModifiers());
111 // public static boolean isPublic(BodyDeclaration bodyDeclaration) {
112 // if (isInterfaceMember(bodyDeclaration))
114 // return Modifier.isPublic(bodyDeclaration.getModifiers());
117 public static boolean isStatic(IMember member) throws JavaModelException{
118 if (isNestedInterface(member))
120 if (member.getElementType() != IJavaElement.METHOD && isInterfaceMember(member))
122 return Flags.isStatic(member.getFlags());
125 // public static boolean isStatic(IMethodBinding methodBinding){
126 // return Modifier.isStatic(methodBinding.getModifiers());
129 // public static boolean isStatic(IVariableBinding variableBinding){
130 // if (isInterfaceMember(variableBinding))
132 // return Modifier.isStatic(variableBinding.getModifiers());
135 // public static boolean isStrictfp(IMember member) throws JavaModelException{
136 // return Flags.isStrictfp(member.getFlags());
139 // public static boolean isSynchronized(IMember member) throws JavaModelException{
140 // return Flags.isSynchronized(member.getFlags());
143 // public static boolean isSynthetic(IMember member) throws JavaModelException{
144 // return Flags.isSynthetic(member.getFlags());
147 // public static boolean isTransient(IMember member) throws JavaModelException{
148 // return Flags.isTransient(member.getFlags());
151 // public static boolean isVolatile(IMember member) throws JavaModelException{
152 // return Flags.isVolatile(member.getFlags());
155 private static boolean isInterfaceMethod(IMember member) throws JavaModelException {
156 return member.getElementType() == IJavaElement.METHOD && isInterfaceMember(member);
159 private static boolean isInterfaceField(IMember member) throws JavaModelException {
160 return member.getElementType() == IJavaElement.FIELD && isInterfaceMember(member);
163 private static boolean isInterfaceMember(IMember member) throws JavaModelException {
164 return member.getDeclaringType() != null && member.getDeclaringType().isInterface();
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();
176 // return declaringType != null && declaringType.isInterface();
179 // private static boolean isInterfaceMember(BodyDeclaration bodyDeclaration) {
180 // return (bodyDeclaration.getParent() instanceof TypeDeclaration) &&
181 // ((TypeDeclaration)bodyDeclaration.getParent()).isInterface();
184 private static boolean isNestedInterface(IMember member) throws JavaModelException{
185 return member.getElementType() == IJavaElement.TYPE &&
186 member.getDeclaringType() != null &&
187 ((IType)member).isInterface();
190 private static boolean isAnonymousType(IMember member) throws JavaModelException {
191 return member.getElementType() == IJavaElement.TYPE &&
192 ((IType)member).isAnonymous();
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;
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;
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;
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;
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);
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;
265 case Modifier.PROTECTED :
266 return newVisibility == Modifier.PUBLIC;
268 case Modifier.PUBLIC :
271 // Assert.isTrue(false);
276 public static int getLowerVisibility(int visibility1, int visibility2) {
277 if (isHigherVisibility(visibility1, visibility2))
283 public static int clearAccessModifiers(int flags) {
284 return clearFlag(Modifier.PROTECTED | Modifier.PUBLIC | Modifier.PRIVATE, flags);
287 public static int clearFlag(int flag, int flags){
288 return flags & ~ flag;