use of org.eclipse.jdt.core.dom.ASTParser in project bndtools by bndtools.
the class NewTypeWizardPage method createASTForImports.
private CompilationUnit createASTForImports(ICompilationUnit cu) {
ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
parser.setSource(cu);
parser.setResolveBindings(false);
parser.setFocalPosition(0);
return (CompilationUnit) parser.createAST(null);
}
use of org.eclipse.jdt.core.dom.ASTParser in project bndtools by bndtools.
the class NewTypeWizardPage method removeUnusedImports.
private void removeUnusedImports(ICompilationUnit cu, Set<String> existingImports, boolean needsSave) throws CoreException {
ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
parser.setSource(cu);
parser.setResolveBindings(true);
CompilationUnit root = (CompilationUnit) parser.createAST(null);
if (root.getProblems().length == 0) {
return;
}
@SuppressWarnings("unchecked") List<ImportDeclaration> importsDecls = root.imports();
if (importsDecls.isEmpty()) {
return;
}
ImportsManager imports = new ImportsManager(root);
int importsEnd = ASTNodes.getExclusiveEnd(importsDecls.get(importsDecls.size() - 1));
IProblem[] problems = root.getProblems();
for (int i = 0; i < problems.length; i++) {
IProblem curr = problems[i];
if (curr.getSourceEnd() < importsEnd) {
int id = curr.getID();
if (id == IProblem.UnusedImport || id == IProblem.NotVisibleType) {
// not visible problems hide unused -> remove both
int pos = curr.getSourceStart();
for (int k = 0; k < importsDecls.size(); k++) {
ImportDeclaration decl = importsDecls.get(k);
if (decl.getStartPosition() <= pos && pos < decl.getStartPosition() + decl.getLength()) {
if (existingImports.isEmpty() || !existingImports.contains(ASTNodes.asString(decl))) {
String name = decl.getName().getFullyQualifiedName();
if (decl.isOnDemand()) {
//$NON-NLS-1$
name += ".*";
}
if (decl.isStatic()) {
imports.removeStaticImport(name);
} else {
imports.removeImport(name);
}
}
break;
}
}
}
}
}
imports.create(needsSave, null);
}
use of org.eclipse.jdt.core.dom.ASTParser in project compiler by boalang.
the class Java7BaseTest method dumpJava.
protected static void dumpJava(final String content) {
final ASTParser parser = ASTParser.newParser(astLevel);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(content.toCharArray());
final Map options = JavaCore.getOptions();
JavaCore.setComplianceOptions(javaVersion, options);
parser.setCompilerOptions(options);
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
try {
final UglyMathCommentsExtractor cex = new UglyMathCommentsExtractor(cu, content);
final ASTDumper dumper = new ASTDumper(cex);
dumper.dump(cu);
cex.close();
} catch (final Exception e) {
}
}
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;
}
Aggregations