Add import statements

Hey I'm trying to write my first plugin. Basically it is meant to highlight deprecated method and then replace it with the suggested function(if user decides to apply a fix).
Right now I've managed my plugin to highlight the deprecated function and then replace it with the supported one. Here is my applyFix method.

public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    try {
        PsiMethodCallExpression methodCall = (PsiMethodCallExpression) descriptor.getPsiElement();
        // System.out.println(methodCall.getText());
        if
(methodCall.getText().equals("class.deprecatedMethod()")) {
            PsiExpressionList arg = methodCall.getArgumentList();

            PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
            PsiMethodCallExpression getInstance = (PsiMethodCallExpression) factory.createExpressionFromText("class.SupportedMethod" + arg.getText() + "", null);

            PsiExpression result = (PsiExpression) methodCall.replace(getInstance);
            //doesn't work
            //PsiManager mgr = PsiManager.getInstance(project);
            //PsiExpression importStatement = (PsiExpression) factory.createExpressionFromText("import com.NumberFormat.NumberFormatFactory;", null);
            //JavaCodeStyleManager.getInstance(project).shortenClassReferences(getInstance);

        
}

    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}

After that I need to import the new class which contains the supported method. I've tried one thing(see comments) which didn't really work. I couldn't find any existing plugin which adds an import statement.

Any idea what I could do?

Thanks in advance

3 comments
Comment actions Permalink

JavaCodeStyleManager.shortenClassReferences will do the work if you
produce valid code with fully qualified references.

On 9/16/2015 4:31 PM, Kamil Lewiński wrote:

Hey I'm trying to write my first plugin. Basically it is meant to highlight deprecated method and then replace it with the suggested function(if user decides to apply a fix).
Right now I've managed my plugin to highlight the deprecated function and then replace it with the supported one. Here is my applyFix method.

      try {
>          PsiMethodCallExpression methodCall = (PsiMethodCallExpression) descriptor.getPsiElement();
>          // System.out.println(methodCall.getText());
>          if (methodCall.getText().equals("class.deprecatedMethod()")) {
>              PsiExpressionList arg = methodCall.getArgumentList();
>
>              PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
>              PsiMethodCallExpression getInstance = (PsiMethodCallExpression) factory.createExpressionFromText("class.SupportedMethod" + arg.getText() + "", null);
>
>              PsiExpression result = (PsiExpression) methodCall.replace(getInstance);
>              //doesn't work
>              //PsiManager mgr = PsiManager.getInstance(project);
>              //PsiExpression importStatement = (PsiExpression) factory.createExpressionFromText("import com.NumberFormat.NumberFormatFactory;", null);
>              //JavaCodeStyleManager.getInstance(project).shortenClassReferences(getInstance);
>
>          }
>
>      } catch (IncorrectOperationException e) {
>          LOG.error(e);
>      }
> }
>
> ]]>

After that I need to import the new class which contains the supported method. I've tried one thing(see comments) which didn't really work. I couldn't find any existing plugin which adds an import statement.

>

Any idea what I could do?

>

Thanks in advance

>

---
Original message URL: https://devnet.jetbrains.com/message/5558488#5558488

>

0
Comment actions Permalink

What exactly do you mean by fully qualified references
I've tried something like that now

allScope(project);
PsiClass[] psiClass = PsiShortNamesCache.getInstance(project).getClassesByName("className",searchScope);
PsiImportStatement importStatement = factory.createImportStatement(psiClass[0]);
JavaCodeStyleManager.getInstance(project).shortenClassReferences(importStatement.getQualifiedName());


And it didn't work. I know I can call getQualifiedName from my importStatement but where should I pass it? Could you please be more descriptive? This is the first plugin I'm writing.

Thanks for reply

0
Comment actions Permalink

The method call you are inserting should contain fully qualified class name e.g.
java.util.Collections.emptyList(myList); then shortenClassReferences call with insert imports for the class automatically.
Check for com.intellij.codeInsight.intention.impl.ConcatenationToMessageFormatAction for code example of task, similar to yours
.

0

Please sign in to leave a comment.