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;
21 import net.sourceforge.phpeclipse.internal.compiler.ast.TypeDeclaration;
23 public class JdtFlags {
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$
33 public static final int VISIBILITY_CODE_INVALID= -1;
35 public static boolean isAbstract(IMember member) throws JavaModelException{
36 if (isInterfaceMethod(member))
38 return Flags.isAbstract(member.getFlags());
41 // public static boolean isAbstract(IMethodBinding member) {
42 // if (isInterfaceMember(member))
44 // return Modifier.isAbstract(member.getModifiers());
47 public static boolean isDeprecated(IMember member) throws JavaModelException{
48 return Flags.isDeprecated(member.getFlags());
51 public static boolean isFinal(IMember member) throws JavaModelException{
52 if (isInterfaceField(member))
54 if (isAnonymousType(member))
56 return Flags.isFinal(member.getFlags());
59 // public static boolean isNative(IMember member) throws JavaModelException{
60 // return Flags.isNative(member.getFlags());
63 public static boolean isPackageVisible(IMember member) throws JavaModelException{
64 return (! isPrivate(member) && ! isProtected(member) && ! isPublic(member));
67 // public static boolean isPackageVisible(BodyDeclaration bodyDeclaration) {
68 // return (! isPrivate(bodyDeclaration) && ! isProtected(bodyDeclaration) && ! isPublic(bodyDeclaration));
71 // public static boolean isPackageVisible(IBinding binding) {
72 // return (! isPrivate(binding) && ! isProtected(binding) && ! isPublic(binding));
75 public static boolean isPrivate(IMember member) throws JavaModelException{
76 return Flags.isPrivate(member.getFlags());
79 // public static boolean isPrivate(BodyDeclaration bodyDeclaration) {
80 // return Modifier.isPrivate(bodyDeclaration.getModifiers());
83 // public static boolean isPrivate(IBinding binding) {
84 // return Modifier.isPrivate(binding.getModifiers());
87 public static boolean isProtected(IMember member) throws JavaModelException{
88 return Flags.isProtected(member.getFlags());
91 // public static boolean isProtected(BodyDeclaration bodyDeclaration) {
92 // return Modifier.isProtected(bodyDeclaration.getModifiers());
95 // public static boolean isProtected(IBinding binding) {
96 // return Modifier.isProtected(binding.getModifiers());
99 public static boolean isPublic(IMember member) throws JavaModelException{
100 if (isInterfaceMember(member))
102 return Flags.isPublic(member.getFlags());
105 // public static boolean isPublic(IBinding binding) {
106 // if (isInterfaceMember(binding))
108 // return Modifier.isPublic(binding.getModifiers());
112 // public static boolean isPublic(BodyDeclaration bodyDeclaration) {
113 // if (isInterfaceMember(bodyDeclaration))
115 // return Modifier.isPublic(bodyDeclaration.getModifiers());
118 public static boolean isStatic(IMember member) throws JavaModelException{
119 if (isNestedInterface(member))
121 if (member.getElementType() != IJavaElement.METHOD && isInterfaceMember(member))
123 return Flags.isStatic(member.getFlags());
126 // public static boolean isStatic(IMethodBinding methodBinding){
127 // return Modifier.isStatic(methodBinding.getModifiers());
130 // public static boolean isStatic(IVariableBinding variableBinding){
131 // if (isInterfaceMember(variableBinding))
133 // return Modifier.isStatic(variableBinding.getModifiers());
136 // public static boolean isStrictfp(IMember member) throws JavaModelException{
137 // return Flags.isStrictfp(member.getFlags());
140 // public static boolean isSynchronized(IMember member) throws JavaModelException{
141 // return Flags.isSynchronized(member.getFlags());
144 // public static boolean isSynthetic(IMember member) throws JavaModelException{
145 // return Flags.isSynthetic(member.getFlags());
148 // public static boolean isTransient(IMember member) throws JavaModelException{
149 // return Flags.isTransient(member.getFlags());
152 // public static boolean isVolatile(IMember member) throws JavaModelException{
153 // return Flags.isVolatile(member.getFlags());
156 private static boolean isInterfaceMethod(IMember member) throws JavaModelException {
157 return member.getElementType() == IJavaElement.METHOD && isInterfaceMember(member);
160 private static boolean isInterfaceField(IMember member) throws JavaModelException {
161 return member.getElementType() == IJavaElement.FIELD && isInterfaceMember(member);
164 private static boolean isInterfaceMember(IMember member) throws JavaModelException {
165 return member.getDeclaringType() != null && member.getDeclaringType().isInterface();
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();
177 // return declaringType != null && declaringType.isInterface();
180 // private static boolean isInterfaceMember(BodyDeclaration bodyDeclaration) {
181 // return (bodyDeclaration.getParent() instanceof TypeDeclaration) &&
182 // ((TypeDeclaration)bodyDeclaration.getParent()).isInterface();
185 private static boolean isNestedInterface(IMember member) throws JavaModelException{
186 return member.getElementType() == IJavaElement.TYPE &&
187 member.getDeclaringType() != null &&
188 ((IType)member).isInterface();
191 private static boolean isAnonymousType(IMember member) throws JavaModelException {
192 return member.getElementType() == IJavaElement.TYPE &&
193 ((IType)member).isAnonymous();
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;
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;
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;
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;
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);
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;
266 case Modifier.PROTECTED :
267 return newVisibility == Modifier.PUBLIC;
269 case Modifier.PUBLIC :
272 // Assert.isTrue(false);
277 public static int getLowerVisibility(int visibility1, int visibility2) {
278 if (isHigherVisibility(visibility1, visibility2))
284 public static int clearAccessModifiers(int flags) {
285 return clearFlag(Modifier.PROTECTED | Modifier.PUBLIC | Modifier.PRIVATE, flags);
288 public static int clearFlag(int flag, int flags){
289 return flags & ~ flag;