1

I want to get typed AST from JavaParser or another parser of Java code. It means I would be able to get the type for a specific variable or parameters+returning type of a method. I googled a lot about this feature of JavaParser, but didn't find anything, I assume this is because JavaParser makes untyped AST. So, advise me how I can get this. But please don't say to parse all the code and make my own set of types, I tried and this is very hard, I think this is harder than making my own AST parser.

leppie
  • 115,091
  • 17
  • 196
  • 297
ivan
  • 287
  • 3
  • 14
  • I have no idea what you are trying to achieve (and this might be interesting to precise that in your question) but you may want to have a look at sonar-java-plugin of sonarqube : https://github.com/SonarSource/sonar-java as we parse and construct a Syntax Tree. – benzonico May 05 '15 at 06:39
  • OP was very clear that a "pure syntax tree" was not what he wanted. – Ira Baxter May 05 '15 at 06:59
  • "Making your own AST parser" and adding what it necessary to compute types for language like Java is much bigger exercise than you might guess; my company has done this for our own tools. Building the AST is by far the easier part. I suggest you find something that already does this and use it. The Java compiler has some kind of internal API for accessing parsed programs; I would assume it has type information somewhere, too. – Ira Baxter May 21 '15 at 10:31

1 Answers1

2

I am a JavaParser contributor and I just did that in Clojure, on top of JavaParser. I explain how implement that in one post How to build a symbol solver for Java, in Clojure

JavaParser, or any other parser just build an Abstract Syntax Tree (AST) of the code, then you have to resolve symbols to understand which references are associated to which declarations.

Suppose you have in your code something like:

a = 1;

Now, to understand the type of a you should find where it is declared. It could be a reference to a parameter, to a local variable, to field declared in a current class or to a field inherited. If it is a field inherited you should find the code (or the bytecode) of the parent class and look there for the declaration of a. A parser does not do that, a parse just take a string (or a file) and build an AST.

Build a symbol resolver is not rocket science but it requires a bit of work. The solution I described in the post linked above is available on GitHub and I would be glad to help you use it if you want (even if it is written in Clojure you can call it from Java quite easily)

Federico Tomassetti
  • 2,100
  • 1
  • 19
  • 26
  • Ira, I am not talking about realizing a fully feature-complete solution nearly as powerful as yours. I am talking about solving the 95% simple cases in which you basically need to consider local variables, parameters, fields and inheritance. – Federico Tomassetti May 21 '15 at 09:11
  • For software, most people don't want an answer that is 95% correct, especially if that last 5% is a vast amount of work (import lookup, generics, type erasure, anonymous classes, class files, ...) That was not clear from your answer. Yes, one needs something like what you have done as a starting place. – Ira Baxter May 21 '15 at 10:33
  • Yes, I want looking each inner block to find variable declaration, but it's a public static variable of another class, it doesn't work. So, I find the solution with JDT, it resolves variable types by itself – ivan May 26 '15 at 05:41