'Is there a way to recognise class associations of java code using ANTLR grammar?
I would like to use ANTLR for static code analysis and with that I'm looking for a way to recognise references within a class to another class. Possibly something like this:
class A {
private B myReferenceToB;
A(){
this.myReferenceToB = new B();
}
public method(){
B refrenceFromMethod = new B();
}
private method2(){
B refrenceFromPrivateMethod = new B();
}
}
class B {}
Any of these cases would mean, that class A and B are in some sort related to each other and I'm trying to figure out how to recognise this in ANTLR.
I did some search and went through some tutorials, but I didn't find anything that would move me in the right direction.
Using this grammar: https://github.com/antlr/grammars-v4/tree/master/java/java
Many thanks for any help.
Solution 1:[1]
I'm trying to figure out how to recognise this in ANTLR
That is not what ANTLR will recognise. ANTLR does not tell you if input is semantically correct (the Java grammar will parse input like boolean q = 42; just fine).
ANTLR will create a lexer and parser for you, and the means to traverse the parse tree of your input (a .java source file, in your case) using a listener or visitor. What happens then is up to you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
