Help detecting interface declarations using Sun Compiler Tree API

Hi,

I have an instance of com.sun.source.tree.ModifiersTree when parsing a Java source-code file. Invoking toString() on the ModifiersTree clearly shows the "interface" modifier yet when I try using the public getModifiers() method it does not return this modifier. The actual Modifier.java source code contains the following lines:

// See JLS2 sections 8.1.1, 8.3.1, 8.4.3, 8.8.3, and 9.1.1.

// java.lang.reflect.Modifier includes INTERFACE, but that's a VMism.

As far as I can tell, there is no way to detect whether a class declaration is an interface using the public API methods, and parsing toString() isn't really an option. Any ideas?

Thank you,

Gili

[703 byte] By [cowwoca] at [2007-11-27 7:37:51]
# 1

> Hi,

>

> I have an instance of

> com.sun.source.tree.ModifiersTree when parsing a Java

> source-code file. Invoking toString() on the

> ModifiersTree clearly shows the "interface" modifier

> yet when I try using the public getModifiers() method

> it does not return this modifier. The actual

> Modifier.java source code contains the following

> lines:

You can't have an instance of an interface.

The toString() works because you are using the Class (java.lang.Class) not an instance.

>

> As far as I can tell, there is no way to detect

> whether a class declaration is an interface using the

> public API methods, and parsing toString() isn't

> really an option. Any ideas?

The following works exactly as I would expect it to and seems to demonstrate that the interface attribute is in use.

int m = Appendable.class.getModifiers();

System.out.println("getModifiers()=" + m);

System.out.println("interface=" + (m & java.lang.reflect.Modifier.INTERFACE));

jschella at 2007-7-12 19:18:29 > top of Java-index,Developer Tools,Java Compiler...
# 2

You seem to misunderstand. When you use the Tree API you get the following method to implement:

public Void visitModifiers(ModifiersTree node, Void passback);

there is no way for me to use Class.getModifiers() in this context. I tried invoking node.getClass().getModifiers() and it didn't work. Aside from that I don't know what else you could have meant.

I peeked into the source-code of /j2se/src/share/classes/com/sun/tools/javac/tree/Pretty.java in the jdk7 repository and it looks like they cheat by invoking:

public void visitClassDef(JCClassDecl tree)

{

[...]

if ((tree.mods.flags & INTERFACE) != 0) {

print("interface " + tree.name);

}

When you could against the public API you don't have an instance of JCClassDecl (at least not portably) and you definitely can't access tree.mods.flags directly. I wish I could ask the authors of the Tree API somehow but I'm not sure how to reach them.

Gili

cowwoca at 2007-7-12 19:18:29 > top of Java-index,Developer Tools,Java Compiler...
# 3

The problem seems to be that class Flags.asModifierSet() as seen at https://jdk-jrl-sources.dev.java.net/source/browse/jdk-jrl-sources/jdk7/trunk/j2se/src/share/classes/com/sun/tools/javac/code/Flags.java?rev=85&view=markup never returns INTERFACE as a possible flag. Could this be a bug in their code or am I missing something?

Thank you,

Gili

cowwoca at 2007-7-12 19:18:29 > top of Java-index,Developer Tools,Java Compiler...
# 4

> You seem to misunderstand.

Yes.

> The problem seems to be that class

> Flags.asModifierSet() as seen at

> https://jdk-jrl-sources.dev.java.net/source/browse/jdk

> -jrl-sources/jdk7/trunk/j2se/src/share/classes/com/sun

> /tools/javac/code/Flags.java?rev=85&view=markup never

> returns INTERFACE as a possible flag. Could this be a

> bug in their code or am I missing something?

You can create a bug for it.

In terms of what you are doing you can create your own modification of the code that correctly implements what you need.

jschella at 2007-7-12 19:18:29 > top of Java-index,Developer Tools,Java Compiler...