KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > util > JdtFlags


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.util;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.jdt.core.Flags;
16 import org.eclipse.jdt.core.IJavaElement;
17 import org.eclipse.jdt.core.IMember;
18 import org.eclipse.jdt.core.IMethod;
19 import org.eclipse.jdt.core.IType;
20 import org.eclipse.jdt.core.JavaModelException;
21
22 import org.eclipse.jdt.core.dom.AnnotationTypeDeclaration;
23 import org.eclipse.jdt.core.dom.BodyDeclaration;
24 import org.eclipse.jdt.core.dom.IBinding;
25 import org.eclipse.jdt.core.dom.IMethodBinding;
26 import org.eclipse.jdt.core.dom.ITypeBinding;
27 import org.eclipse.jdt.core.dom.IVariableBinding;
28 import org.eclipse.jdt.core.dom.Modifier;
29 import org.eclipse.jdt.core.dom.TypeDeclaration;
30
31
32 public class JdtFlags {
33     private JdtFlags(){
34     }
35     
36     public static final String JavaDoc VISIBILITY_STRING_PRIVATE= "private"; //$NON-NLS-1$
37
public static final String JavaDoc VISIBILITY_STRING_PACKAGE= ""; //$NON-NLS-1$
38
public static final String JavaDoc VISIBILITY_STRING_PROTECTED= "protected"; //$NON-NLS-1$
39
public static final String JavaDoc VISIBILITY_STRING_PUBLIC= "public"; //$NON-NLS-1$
40

41
42     public static final int VISIBILITY_CODE_INVALID= -1;
43
44     public static boolean isAbstract(IMember member) throws JavaModelException{
45         if (isInterfaceOrAnnotationMethod(member))
46             return true;
47         return Flags.isAbstract(member.getFlags());
48     }
49     
50     public static boolean isAbstract(IMethodBinding member) {
51         if (isInterfaceOrAnnotationMember(member))
52             return true;
53         return Modifier.isAbstract(member.getModifiers());
54     }
55
56     public static boolean isDeprecated(IMember member) throws JavaModelException{
57         return Flags.isDeprecated(member.getFlags());
58     }
59
60     public static boolean isFinal(IMember member) throws JavaModelException{
61         if (isInterfaceOrAnnotationField(member))
62             return true;
63         if (isAnonymousType(member))
64             return true;
65         if (isEnumConstant(member))
66             return true;
67         return Flags.isFinal(member.getFlags());
68     }
69
70     public static boolean isNative(IMember member) throws JavaModelException{
71         return Flags.isNative(member.getFlags());
72     }
73
74     public static boolean isPackageVisible(IMember member) throws JavaModelException{
75         return (! isPrivate(member) && ! isProtected(member) && ! isPublic(member));
76     }
77
78     public static boolean isPackageVisible(BodyDeclaration bodyDeclaration) {
79         return (! isPrivate(bodyDeclaration) && ! isProtected(bodyDeclaration) && ! isPublic(bodyDeclaration));
80     }
81     
82     public static boolean isPackageVisible(IBinding binding) {
83         return (! isPrivate(binding) && ! isProtected(binding) && ! isPublic(binding));
84     }
85     
86     public static boolean isPrivate(IMember member) throws JavaModelException{
87         return Flags.isPrivate(member.getFlags());
88     }
89
90     public static boolean isPrivate(BodyDeclaration bodyDeclaration) {
91         return Modifier.isPrivate(bodyDeclaration.getModifiers());
92     }
93     
94     public static boolean isPrivate(IBinding binding) {
95         return Modifier.isPrivate(binding.getModifiers());
96     }
97
98     public static boolean isProtected(IMember member) throws JavaModelException{
99         return Flags.isProtected(member.getFlags());
100     }
101
102     public static boolean isProtected(BodyDeclaration bodyDeclaration) {
103         return Modifier.isProtected(bodyDeclaration.getModifiers());
104     }
105     
106     public static boolean isProtected(IBinding binding) {
107         return Modifier.isProtected(binding.getModifiers());
108     }
109
110     public static boolean isPublic(IMember member) throws JavaModelException{
111         if (isInterfaceOrAnnotationMember(member))
112             return true;
113         if (isEnumConstant(member))
114             return true;
115         return Flags.isPublic(member.getFlags());
116     }
117     
118     public static boolean isPublic(IBinding binding) {
119         if (isInterfaceOrAnnotationMember(binding))
120             return true;
121         return Modifier.isPublic(binding.getModifiers());
122     }
123     
124
125     public static boolean isPublic(BodyDeclaration bodyDeclaration) {
126         if (isInterfaceOrAnnotationMember(bodyDeclaration))
127             return true;
128         return Modifier.isPublic(bodyDeclaration.getModifiers());
129     }
130
131     public static boolean isStatic(IMember member) throws JavaModelException{
132         if (isNestedInterfaceOrAnnotation(member))
133             return true;
134         if (member.getElementType() != IJavaElement.METHOD && isInterfaceOrAnnotationMember(member))
135             return true;
136         if (isEnumConstant(member))
137             return true;
138         return Flags.isStatic(member.getFlags());
139     }
140
141     public static boolean isStatic(IMethodBinding methodBinding){
142         return Modifier.isStatic(methodBinding.getModifiers());
143     }
144
145     public static boolean isStatic(IVariableBinding variableBinding){
146         if (isInterfaceOrAnnotationMember(variableBinding))
147             return true;
148         return Modifier.isStatic(variableBinding.getModifiers());
149     }
150
151     public static boolean isStrictfp(IMember member) throws JavaModelException{
152         return Flags.isStrictfp(member.getFlags());
153     }
154
155     public static boolean isSynchronized(IMember member) throws JavaModelException{
156         return Flags.isSynchronized(member.getFlags());
157     }
158
159     public static boolean isSynthetic(IMember member) throws JavaModelException{
160         return Flags.isSynthetic(member.getFlags());
161     }
162
163     public static boolean isAnnotation(IMember member) throws JavaModelException{
164         return Flags.isAnnotation(member.getFlags());
165     }
166
167     public static boolean isEnum(IMember member) throws JavaModelException{
168         return Flags.isEnum(member.getFlags());
169     }
170
171     public static boolean isVarargs(IMethod method) throws JavaModelException{
172         return Flags.isVarargs(method.getFlags());
173     }
174
175     public static boolean isTransient(IMember member) throws JavaModelException{
176         return Flags.isTransient(member.getFlags());
177     }
178
179     public static boolean isVolatile(IMember member) throws JavaModelException{
180         return Flags.isVolatile(member.getFlags());
181     }
182     
183     private static boolean isInterfaceOrAnnotationMethod(IMember member) throws JavaModelException {
184         return member.getElementType() == IJavaElement.METHOD && isInterfaceOrAnnotationMember(member);
185     }
186
187     private static boolean isInterfaceOrAnnotationField(IMember member) throws JavaModelException {
188         return member.getElementType() == IJavaElement.FIELD && isInterfaceOrAnnotationMember(member);
189     }
190
191     private static boolean isInterfaceOrAnnotationMember(IMember member) throws JavaModelException {
192         return member.getDeclaringType() != null && JavaModelUtil.isInterfaceOrAnnotation(member.getDeclaringType());
193     }
194     
195     private static boolean isInterfaceOrAnnotationMember(IBinding binding) {
196         ITypeBinding declaringType= null;
197         if (binding instanceof IVariableBinding) {
198             declaringType= ((IVariableBinding) binding).getDeclaringClass();
199         } else if (binding instanceof IMethodBinding) {
200             declaringType= ((IMethodBinding) binding).getDeclaringClass();
201         } else if (binding instanceof ITypeBinding) {
202             declaringType= ((ITypeBinding) binding).getDeclaringClass();
203         }
204         return declaringType != null && (declaringType.isInterface() || declaringType.isAnnotation());
205     }
206     
207     private static boolean isInterfaceOrAnnotationMember(BodyDeclaration bodyDeclaration) {
208         boolean isInterface= (bodyDeclaration.getParent() instanceof TypeDeclaration) &&
209                 ((TypeDeclaration)bodyDeclaration.getParent()).isInterface();
210         boolean isAnnotation= bodyDeclaration.getParent() instanceof AnnotationTypeDeclaration;
211         return isInterface || isAnnotation;
212     }
213
214     private static boolean isNestedInterfaceOrAnnotation(IMember member) throws JavaModelException{
215         return member.getElementType() == IJavaElement.TYPE &&
216                 member.getDeclaringType() != null &&
217                 JavaModelUtil.isInterfaceOrAnnotation((IType)member);
218     }
219     
220     private static boolean isEnumConstant(IMember member) throws JavaModelException {
221         return member.getElementType() == IJavaElement.FIELD && isEnum(member);
222     }
223
224     private static boolean isAnonymousType(IMember member) throws JavaModelException {
225         return member.getElementType() == IJavaElement.TYPE &&
226                 ((IType)member).isAnonymous();
227     }
228
229     public static int getVisibilityCode(IMember member) throws JavaModelException {
230         if (isPublic(member))
231             return Modifier.PUBLIC;
232         else if (isProtected(member))
233             return Modifier.PROTECTED;
234         else if (isPackageVisible(member))
235             return Modifier.NONE;
236         else if (isPrivate(member))
237             return Modifier.PRIVATE;
238         Assert.isTrue(false);
239         return VISIBILITY_CODE_INVALID;
240     }
241     
242     public static int getVisibilityCode(BodyDeclaration bodyDeclaration) {
243         if (isPublic(bodyDeclaration))
244             return Modifier.PUBLIC;
245         else if (isProtected(bodyDeclaration))
246             return Modifier.PROTECTED;
247         else if (isPackageVisible(bodyDeclaration))
248             return Modifier.NONE;
249         else if (isPrivate(bodyDeclaration))
250             return Modifier.PRIVATE;
251         Assert.isTrue(false);
252         return VISIBILITY_CODE_INVALID;
253     }
254     
255     public static int getVisibilityCode(IBinding binding) {
256         if (isPublic(binding))
257             return Modifier.PUBLIC;
258         else if (isProtected(binding))
259             return Modifier.PROTECTED;
260         else if (isPackageVisible(binding))
261             return Modifier.NONE;
262         else if (isPrivate(binding))
263             return Modifier.PRIVATE;
264         Assert.isTrue(false);
265         return VISIBILITY_CODE_INVALID;
266     }
267     
268     
269     public static String JavaDoc getVisibilityString(int visibilityCode){
270         if (Modifier.isPublic(visibilityCode))
271             return VISIBILITY_STRING_PUBLIC;
272         if (Modifier.isProtected(visibilityCode))
273             return VISIBILITY_STRING_PROTECTED;
274         if (Modifier.isPrivate(visibilityCode))
275             return VISIBILITY_STRING_PRIVATE;
276         return VISIBILITY_STRING_PACKAGE;
277     }
278
279     public static int getVisibilityCode(String JavaDoc visibilityString) {
280         Assert.isNotNull(visibilityString);
281         if (VISIBILITY_STRING_PACKAGE.equals(visibilityString))
282             return 0;
283         else if (VISIBILITY_STRING_PRIVATE.equals(visibilityString))
284             return Modifier.PRIVATE;
285         else if (VISIBILITY_STRING_PROTECTED.equals(visibilityString))
286             return Modifier.PROTECTED;
287         else if (VISIBILITY_STRING_PUBLIC.equals(visibilityString))
288             return Modifier.PUBLIC;
289         return VISIBILITY_CODE_INVALID;
290     }
291
292     public static void assertVisibility(int visibility){
293         Assert.isTrue( visibility == Modifier.PUBLIC ||
294                         visibility == Modifier.PROTECTED ||
295                         visibility == Modifier.NONE ||
296                         visibility == Modifier.PRIVATE);
297     }
298     
299     public static boolean isHigherVisibility(int newVisibility, int oldVisibility){
300         assertVisibility(oldVisibility);
301         assertVisibility(newVisibility);
302         switch (oldVisibility) {
303             case Modifier.PRIVATE :
304                 return newVisibility == Modifier.NONE
305                         || newVisibility == Modifier.PUBLIC
306                         || newVisibility == Modifier.PROTECTED;
307             case Modifier.NONE :
308                 return newVisibility == Modifier.PUBLIC
309                         || newVisibility == Modifier.PROTECTED;
310
311             case Modifier.PROTECTED :
312                 return newVisibility == Modifier.PUBLIC;
313
314             case Modifier.PUBLIC :
315                 return false;
316             default:
317                 Assert.isTrue(false);
318                 return false;
319         }
320     }
321     
322     public static int getLowerVisibility(int visibility1, int visibility2) {
323         if (isHigherVisibility(visibility1, visibility2))
324             return visibility2;
325         else
326             return visibility1;
327     }
328     
329     public static int clearAccessModifiers(int flags) {
330         return clearFlag(Modifier.PROTECTED | Modifier.PUBLIC | Modifier.PRIVATE, flags);
331     }
332
333     public static int clearFlag(int flag, int flags){
334         return flags & ~ flag;
335     }
336 }
337
Popular Tags