How do I go to StringLiterals with Eclipse AST?

I need to create an Eclipse plugin that will display a tooltip when I hover over a string literal. But only if this string literal is the first parameter of the special method.

Here is the Test.java file I am using to test my plugin:

package test;

public class Test {
    public static void main(String[] args) {
        String hello = "Hello";
        String world = Translator.get("Test.worldLabel");
        System.out.println(hello + " " + world);
    }
}

      

I have created a class that implements IJavaEditorTextHover and I need to compile the currently edited Java file to calculate if the cursor hovers over a line to be translated or not.

  • Hovering "Hello" will do nothing.
  • Hovering "Test.worldLabel" will display my tooltip because this literal is included inside the call to the Translator.get () method.

I first used this (170 is inside "Test.worldLabel"):

ITypeRoot typeRoot = (ITypeRoot)
    JavaUI.getEditorInputJavaElement(editorPart.getEditorInput());

JavaElement foundElement = (JavaElement) typeRoot.getElementAt(170);

      

But foundElement contains the whole main () method: it's not thin enough.

Then the correct way is, I think:

private static ASTNode parse(ICompilationUnit unit, int position) {
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    parser.setSource(unit);
    parser.setResolveBindings(true);
    parser.setIgnoreMethodBodies(false);
    // TODO Future optimisation: parser.setFocalPosition(position);
    return parser.createAST((IProgressMonitor) null); // parse
}

      

And in my implementation IJavaEditorTextHover.getHoverInfo (...):

ICompilationUnit compilationUnit = (ICompilationUnit)
    JavaUI.getEditorInputJavaElement(editor.getEditorInput())
int position = 170/*hoverRegion.getOffset()*/;
ASTNode ast = parse(compilationUnit, position);

      

And so, here's my question:

How, from this ast node, do I get an ASTNode emitting a StringLiteral at position 170 in the source code (Test.worldLabel string)?

Bonus question: did I pick the right solution? Based on results.


Edit: Well, here is the solution I found:

private StringLiteral findStringLiteralAtPosition(final ASTNode parent, final int position) {

    final List<StringLiteral> stringLiterals = new ArrayList<StringLiteral>();

    parent.accept(new ASTVisitor() {
        @Override
        public boolean visit(StringLiteral stringLiteral) {
            int start = stringLiteral.getStartPosition();
            int end = start + stringLiteral.getLength();
            if (start <= position && position <= end) {
                stringLiterals.add(stringLiteral);
            }
            return super.visit(stringLiteral);
        }
    });

    return (stringLiterals.size() > 0 ? stringLiterals.get(0) : null);
}

      

Is it a good seam? Or is it an easier way or more efficient?

+3


source to share


1 answer


One solution will not use offset logic at all. You can generalize the solution with parent node validation.

Here's some sample code:



public boolean visit(StringLiteral stringLiteral) {

        // Check if parent is a method inovacation.
        if (stringLiteral.getParent().getNodeType() == ASTNode.METHOD_INVOCATION) {

              // get the parent method inovacation.
              MethodInvocation miNode = (MethodInvocation) stringLiteral.getParent();

              //To do: null and empty check on argument list.

              // Check if is the special method and this is the 1st argument
              if (miNode.getName().toString().equals("SpecialMethod")
                        && miNode.arguments().get(0).toString().equals(stringLiteral.toString())) {

                    System.out.println("Found it : " + stringLiteral.toString());
              }
        }

        return true;
    }

      

+1


source







All Articles