-
-
Notifications
You must be signed in to change notification settings - Fork 357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to find CtCompilationUnit with spoon? #4103
Comments
A compilation unit is not quite part of the model in the same sense that other elements are. It's a compiler concept, while Spoon models the language syntax and semantics. It That being said, you can reach the original compilation unit of any element with a position. CtElement element = ...; // some element
if (element.getPosition().isValidPosition()) {
CtCompilationUnit cu = element.getPosition().getCompilationUnit();
} You can also get all compilation units from the Launcher launcher = new Launcher();
// do Spoon stuff
Map<String, CompilationUnit> compilationUnits = launcher.getFactory().CompilationUnit().getMap(); Note that the factory uses the older and deprecated |
Thanks for your reply. I want to know the exact AST info if I got the offset and length token info, So that's why I search CompilationUnit in spoon. I do it with Eclipse-JDT before and I am sure I can find the target node with offset and length in CompilationUnit with JDT. I check Navigation and Query and can't find the API I need. |
I don't know what you mean by this. Could you give a concrete example?
Tokens, in the lexer sense of the word, are not carried over one-to-one to Spoon. For example, If you by token you mean something else, then do clarify and I'll try to answer again. |
There is a helper class in eclipse-jdt called NodeFinder /**
* Maps a selection to an ASTNode, where the selection is defined using a start and a length.
* The result node is determined as follows:
* <ul>
* <li>First, tries to find a node whose range is the exactly the given selection.
* If multiple matching nodes are found, the innermost is returned.</li>
* <li>If no such node exists, then the last node in a preorder traversal of the AST is returned, where
* the node range fully contains the selection.
* If the length is zero, then ties between adjacent nodes are broken by choosing the right side.</li>
* </ul>
*
* @param root the root node from which the search starts
* @param start the start of the selection
* @param length the length of the selection
*
* @return the innermost node that exactly matches the selection, or the first node that contains the selection
*/
public static ASTNode perform(ASTNode root, int start, int length) {
NodeFinder finder = new NodeFinder(root, start, length);
ASTNode result= finder.getCoveredNode();
if (result == null || result.getStartPosition() != start || result.getLength() != length) {
return finder.getCoveringNode();
}
return result;
} I can use this API to find a target ASTNode and then I can use this information do something like navigation or go to declaration with Binding if it is possible. |
I just want to know if there are some similar methods like this. I hope the needed AST can be located with offset,and length . |
A class SpoonNodeFinder similar to JDT's NodeFinder would not be hard to implement. |
So, yes, matching a Spoon AST element to a source position is possible, but not currently provided by Spoon. We do something very similar to this in Sorald, in our BestFitScanner. It would probably be feasible to generalize said scanner and put it into Spoon, while providing the API needed for Sorald to do it's more specialized matchings. |
As I known, each java file can be parsed as a single CompilationUnit in eclipse jdt. I find that spoon has CtCompilationUnit
but It doesn't work. I don't know where is the problem.Here is my test code.
I can use spoon to get each CtMethod info like this.
However, If i use filter to collect CtCompilationUnit, I get nothing. Anyone can tell me if I miss something?
The text was updated successfully, but these errors were encountered: