Search in sources :

Example 16 with ASTParser

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);
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ASTParser(org.eclipse.jdt.core.dom.ASTParser)

Example 17 with ASTParser

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);
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) ASTParser(org.eclipse.jdt.core.dom.ASTParser) IProblem(org.eclipse.jdt.core.compiler.IProblem)

Example 18 with ASTParser

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) {
    }
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ASTParser(org.eclipse.jdt.core.dom.ASTParser) Map(java.util.Map) HashMap(java.util.HashMap)

Example 19 with ASTParser

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];
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ISafeRunnable(org.eclipse.core.runtime.ISafeRunnable) ASTParser(org.eclipse.jdt.core.dom.ASTParser)

Example 20 with ASTParser

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;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IBinding(org.eclipse.jdt.core.dom.IBinding) ArrayList(java.util.ArrayList) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTRequestor(org.eclipse.jdt.core.dom.ASTRequestor) ASTParser(org.eclipse.jdt.core.dom.ASTParser)

Aggregations

ASTParser (org.eclipse.jdt.core.dom.ASTParser)42 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)34 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)24 ASTNode (org.eclipse.jdt.core.dom.ASTNode)11 RefactoringASTParser (org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser)9 IBinding (org.eclipse.jdt.core.dom.IBinding)7 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 IType (org.eclipse.jdt.core.IType)5 IProblem (org.eclipse.jdt.core.compiler.IProblem)5 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)5 ASTRequestor (org.eclipse.jdt.core.dom.ASTRequestor)4 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)4 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)3 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)3 ISourceRange (org.eclipse.jdt.core.ISourceRange)3 ArrayType (org.eclipse.jdt.core.dom.ArrayType)3 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)3 Type (org.eclipse.jdt.core.dom.Type)3 Map (java.util.Map)2