Community
Participate
Working Groups
If I have the following interface definition public interface Test extends javax.ejb.EJBObject { String toUpper(String s) throws RemoteException, EJBException; } then when I ask for the flags for the method and test them it reports that the method is not public int flags = method.getFlags(); Flags.isPublic(flags) <<< returns false. However if I add the optional public keyword public interface Test extends javax.ejb.EJBObject { public String toUpper(String s) throws RemoteException, EJBException; } int flags = method.getFlags(); Flags.isPublic(flags) <<< returns true. In both cases the method is public.
It depends whether you look at the AST or the resolved binding for the method. The AST itself only reflects syntactical information on it (if code did specify 'public', then yes it would answer it is public). The binding is doing all inference, based on the context.
I'm dealing with an IType so I assume it is not easy to obtain a resolved binding for the method?
Ok I see. The JavaModel is only surfacing the information found in the source in a digested way (parsed text). No inference is performed (propagating flags from enclosing contexts). We could add some new API to do so though. How critical is this for you ?
In our situation we need to know if a method is public. We could change our code from int flags = method.getFlags(); return Flags.isPublic(flags) to int flags = method.getFlags(); return (Flags.isPublic(flags) || method.getDeclaringType().isInterface()) However I filed this bug because after reading the API Javadoc I thought the behavior was incorrect. So perhaps the doc should be clarified.
Randy, have a look at our (jdt ui) JdtFlags class - we had the same problem and that class is a solution for it.
Clarified spec as follows: ** * Returns the modifier flags for this member. The flags can be examined using class * <code>Flags</code>. * <p> * Note that only flags as indicated in the source are returned. Thus if an interface * defines a method <code>void myMethod();</code> the flags don't include the * 'public' flag. * * @exception JavaModelException if this element does not exist or if an * exception occurs while accessing its corresponding resource. * @return the modifier flags for this member * @see Flags */ int getFlags() throws JavaModelException;
Verified.