use of org.eclipse.jdt.core.dom.ASTParser in project che by eclipse.
the class ASTBatchParser method createParser.
/**
* Creates a new parser which can be used to create ASTs
* for compilation units in <code>project</code>
* <p>
* Subclasses may override
* </p>
*
* @param project the project for which ASTs are been generated
* @return an AST parser capable of creating ASTs of compilation units in project
*/
protected ASTParser createParser(IJavaProject project) {
ASTParser result = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
result.setResolveBindings(true);
result.setProject(project);
return result;
}
use of org.eclipse.jdt.core.dom.ASTParser in project che by eclipse.
the class ASTProvider method createAST.
/**
* Creates a new compilation unit AST.
*
* @param input the Java element for which to create the AST
* @param progressMonitor the progress monitor
* @return AST
*/
public static CompilationUnit createAST(final ITypeRoot input, final IProgressMonitor progressMonitor) {
if (!hasSource(input))
return null;
if (progressMonitor != null && progressMonitor.isCanceled())
return null;
final ASTParser parser = ASTParser.newParser(SHARED_AST_LEVEL);
parser.setResolveBindings(true);
parser.setStatementsRecovery(SHARED_AST_STATEMENT_RECOVERY);
parser.setBindingsRecovery(SHARED_BINDING_RECOVERY);
parser.setSource(input);
if (progressMonitor != null && progressMonitor.isCanceled())
return null;
final CompilationUnit[] root = new CompilationUnit[1];
SafeRunner.run(new ISafeRunnable() {
public void run() {
try {
if (progressMonitor != null && progressMonitor.isCanceled())
return;
if (DEBUG)
System.err.println(getThreadName() + " - " + DEBUG_PREFIX + "creating AST for: " + //$NON-NLS-1$ //$NON-NLS-2$
input.getElementName());
root[0] = (CompilationUnit) parser.createAST(progressMonitor);
//mark as unmodifiable
ASTNodes.setFlagsToAST(root[0], ASTNode.PROTECT);
} catch (OperationCanceledException ex) {
return;
}
}
public void handleException(Throwable ex) {
LOG.error(ex.getMessage(), ex);
}
});
return root[0];
}
use of org.eclipse.jdt.core.dom.ASTParser in project che by eclipse.
the class TypeEnvironment method createTypeBindings.
public static ITypeBinding[] createTypeBindings(TType[] types, IJavaProject project) {
final Map<String, Object> mapping = new HashMap<String, Object>();
List<String> keys = new ArrayList<String>();
for (int i = 0; i < types.length; i++) {
TType type = types[i];
String bindingKey = type.getBindingKey();
mapping.put(bindingKey, type);
keys.add(bindingKey);
}
ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
parser.setProject(project);
parser.setResolveBindings(true);
parser.createASTs(new ICompilationUnit[0], keys.toArray(new String[keys.size()]), new ASTRequestor() {
@Override
public void acceptBinding(String bindingKey, IBinding binding) {
mapping.put(bindingKey, binding);
}
}, null);
ITypeBinding[] result = new ITypeBinding[types.length];
for (int i = 0; i < types.length; i++) {
TType type = types[i];
String bindingKey = type.getBindingKey();
Object value = mapping.get(bindingKey);
if (value instanceof ITypeBinding) {
result[i] = (ITypeBinding) value;
}
}
return result;
}
use of org.eclipse.jdt.core.dom.ASTParser in project XobotOS by xamarin.
the class XobotBuilder method stage1_parse.
private List<CompilationUnitPair> stage1_parse(final List<ICompilationUnit> sources, IProgressMonitor monitor) {
final int totalWork = PARSING_PRICE * sources.size();
final IProgressMonitor subMonitor = new SubProgressMonitor(monitor, totalWork);
final ArrayList<CompilationUnitPair> pairs = new ArrayList<CompilationUnitPair>(sources.size());
ASTRequestor requestor = new ASTRequestor() {
@Override
public void acceptAST(ICompilationUnit source, CompilationUnit ast) {
pairs.add(new CompilationUnitPair(source, ast));
subMonitor.subTask(String.format("Parsing (%d/%d): %s", pairs.size(), sources.size(), getUnitName(source)));
subMonitor.worked(PARSING_PRICE);
}
};
final ASTParser _parser = ASTParser.newParser(AST.JLS3);
_parser.setKind(ASTParser.K_COMPILATION_UNIT);
_parser.setProject(sources.get(0).getJavaProject());
_parser.setResolveBindings(true);
final ICompilationUnit[] sourceArray = sources.toArray(new ICompilationUnit[0]);
Sharpen.Log(Level.INFO, "Parsing %d compilation units.", sources.size());
try {
subMonitor.beginTask("parsing compile units", totalWork);
_parser.createASTs(sourceArray, new String[0], requestor, null);
} finally {
subMonitor.done();
}
return pairs;
}
use of org.eclipse.jdt.core.dom.ASTParser in project AutoRefactor by JnRouvignac.
the class ApplyRefactoringsJob method applyRefactoring.
/**
* Applies the refactorings provided inside the {@link AggregateASTVisitor} to the provided
* {@link ICompilationUnit}.
*
* @param document the document where the compilation unit comes from
* @param compilationUnit the compilation unit to refactor
* @param refactoring the {@link AggregateASTVisitor} to apply to the compilation unit
* @param options the Java project options used to compile the project
* @param monitor the progress monitor of the current job
* @throws Exception if any problem occurs
*
* @see <a
* href="http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.jdt.doc.isv%2Fguide%2Fjdt_api_manip.htm"
* >Eclipse JDT core - Manipulating Java code</a>
* @see <a href="
* http://help.eclipse.org/indigo/index.jsp?topic=/org.eclipse.platform.doc.isv/guide/workbench_cmd_menus.htm"
* > Eclipse Platform Plug-in Developer Guide > Plugging into the workbench
* > Basic workbench extension points using commands > org.eclipse.ui.menus</a>
* @see <a
* href="http://www.eclipse.org/articles/article.php?file=Article-JavaCodeManipulation_AST/index.html"
* >Abstract Syntax Tree > Write it down</a>
*/
public void applyRefactoring(IDocument document, ICompilationUnit compilationUnit, AggregateASTVisitor refactoring, JavaProjectOptions options, IProgressMonitor monitor) throws Exception {
// creation of DOM/AST from a ICompilationUnit
final ASTParser parser = ASTParser.newParser(AST.JLS4);
resetParser(compilationUnit, parser, options);
CompilationUnit astRoot = (CompilationUnit) parser.createAST(null);
int totalNbLoops = 0;
Set<ASTVisitor> lastLoopVisitors = Collections.emptySet();
int nbLoopsWithSameVisitors = 0;
while (true) {
if (totalNbLoops > 100) {
// Oops! Something went wrong.
final String errorMsg = "An infinite loop has been detected for file " + getFileName(astRoot) + "." + " A possible cause is that code is being incorrectly" + " refactored one way then refactored back to what it was." + " Fix the code before pursuing." + getPossibleCulprits(nbLoopsWithSameVisitors, lastLoopVisitors);
environment.getLogger().error(errorMsg, new IllegalStateException(astRoot, errorMsg));
break;
}
final RefactoringContext ctx = new RefactoringContext(compilationUnit, astRoot, options, monitor, environment);
refactoring.setRefactoringContext(ctx);
final Refactorings refactorings = refactoring.getRefactorings(astRoot);
if (!refactorings.hasRefactorings()) {
// we are done with applying the refactorings.
return;
}
// apply the refactorings and save the compilation unit
refactorings.applyTo(document);
final boolean hadUnsavedChanges = compilationUnit.hasUnsavedChanges();
compilationUnit.getBuffer().setContents(document.get());
// , null, null);
if (!hadUnsavedChanges) {
compilationUnit.save(null, true);
}
// I did not find any other way to directly modify the AST
// while still keeping the resolved type bindings working.
// Using astRoot.recordModifications() did not work:
// type bindings were lost. Is there a way to recover them?
// FIXME we should find a way to apply all the changes at
// the AST level and refresh the bindings
resetParser(compilationUnit, parser, options);
astRoot = (CompilationUnit) parser.createAST(null);
++totalNbLoops;
final Set<ASTVisitor> thisLoopVisitors = refactoring.getVisitorsContributingRefactoring();
if (!thisLoopVisitors.equals(lastLoopVisitors)) {
lastLoopVisitors = new HashSet<ASTVisitor>(thisLoopVisitors);
nbLoopsWithSameVisitors = 0;
} else {
++nbLoopsWithSameVisitors;
}
}
}
Aggregations