Cannot include Eclispe JDT codeAssist objects outside of plugin

Using the Eclipse jdt tools, you can cross AST from snippets of Java code as follows:

ASTParser ASTparser = ASTParser.newParser(AST.JLS3);
ASTparser.setSource("package x;class X{}".toCharArray());
ASTparser.createAST(null).accept(...);

But when I try to execute the full code and the choice of code, it seems that I should do it in a plug-in application, since I need to write codes, for example

IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(somePath));
ICodeAssist i  = JavaCore.createCompilationUnitFrom(f);
i.codeComplete/codeSelect(...)

Is there anyway that I can finally get a standalone java application that includes jdt complete / select functions?

thanks a lot! shi kui


I noticed that using org.eclipse.jdt.internal.codeassist.complete.CompletionParser I can parse a piece of code.

CompletionParser parser =new CompletionParser(new ProblemReporter(
        DefaultErrorHandlingPolicies.proceedWithAllProblems(),
        new CompilerOptions(null),
        new DefaultProblemFactory(Locale.getDefault())),
        false);
org.eclipse.jdt.internal.compiler.batch.CompilationUnit sourceUnit =
new org.eclipse.jdt.internal.compiler.batch.CompilationUnit(
    "class T{f(){new T().=1;}  \nint j;}".toCharArray(), "testName", null);
CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, 0);
CompilationUnitDeclaration unit = parser.dietParse(sourceUnit, compilationResult, 25);

But I have 2 questions: 1. How to get supporting information? 2. How can I specify the path to the key or the source path for the compiler to search for information of type / method / field?

+3
1

, ICodeAssist.

Java-, , , ICodeAssist.

:

  • - Java.
  • - Java, .

Java , : IClassFile ICompilationUnit.
, .

( FAQ), ICodeAssist.

, IFile .

+1

Source: https://habr.com/ru/post/1742755/


All Articles