Search in sources :

Example 6 with ASTParser

use of org.eclipse.jdt.core.dom.ASTParser in project che by eclipse.

the class TypeEnvironment method createStandardType.

private StandardType createStandardType(String fullyQualifiedName, IJavaProject focus) {
    try {
        IType javaElementType = focus.findType(fullyQualifiedName);
        StandardType result = fStandardTypes.get(javaElementType);
        if (result != null)
            return result;
        ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
        parser.setProject(focus);
        IBinding[] bindings = parser.createBindings(new IJavaElement[] { javaElementType }, null);
        return createStandardType((ITypeBinding) bindings[0]);
    } catch (JavaModelException e) {
    // fall through
    }
    return null;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IBinding(org.eclipse.jdt.core.dom.IBinding) ASTParser(org.eclipse.jdt.core.dom.ASTParser) IType(org.eclipse.jdt.core.IType)

Example 7 with ASTParser

use of org.eclipse.jdt.core.dom.ASTParser in project flux by eclipse.

the class ASTNodeFactory method newTypeParameter.

public static TypeParameter newTypeParameter(AST ast, String content) {
    StringBuffer buffer = new StringBuffer(TYPEPARAM_HEADER);
    buffer.append(content);
    buffer.append(TYPEPARAM_FOOTER);
    ASTParser p = ASTParser.newParser(ast.apiLevel());
    p.setSource(buffer.toString().toCharArray());
    CompilationUnit root = (CompilationUnit) p.createAST(null);
    List<AbstractTypeDeclaration> list = root.types();
    TypeDeclaration typeDecl = (TypeDeclaration) list.get(0);
    MethodDeclaration methodDecl = typeDecl.getMethods()[0];
    TypeParameter tp = (TypeParameter) methodDecl.typeParameters().get(0);
    ASTNode result = ASTNode.copySubtree(ast, tp);
    result.accept(new PositionClearer());
    return (TypeParameter) result;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) TypeParameter(org.eclipse.jdt.core.dom.TypeParameter) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTParser(org.eclipse.jdt.core.dom.ASTParser) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 8 with ASTParser

use of org.eclipse.jdt.core.dom.ASTParser in project flux by eclipse.

the class ASTNodeFactory method newStatement.

public static ASTNode newStatement(AST ast, String content) {
    StringBuffer buffer = new StringBuffer(STATEMENT_HEADER);
    buffer.append(content);
    buffer.append(STATEMENT_FOOTER);
    ASTParser p = ASTParser.newParser(ast.apiLevel());
    p.setSource(buffer.toString().toCharArray());
    CompilationUnit root = (CompilationUnit) p.createAST(null);
    ASTNode result = ASTNode.copySubtree(ast, NodeFinder.perform(root, STATEMENT_HEADER.length(), content.length()));
    result.accept(new PositionClearer());
    return result;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTParser(org.eclipse.jdt.core.dom.ASTParser)

Example 9 with ASTParser

use of org.eclipse.jdt.core.dom.ASTParser in project flux by eclipse.

the class ASTProvider method createAST.

private static CompilationUnit createAST(final ITypeRoot input, final IProgressMonitor progressMonitor) {
    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;
                root[0] = (CompilationUnit) parser.createAST(progressMonitor);
                //mark as unmodifiable
                ASTNodes.setFlagsToAST(root[0], ASTNode.PROTECT);
            } catch (OperationCanceledException ex) {
                return;
            }
        }

        public void handleException(Throwable ex) {
            //$NON-NLS-1$
            IStatus status = new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK, "Error in JDT Core during AST creation", ex);
            JavaPlugin.getDefault().getLog().log(status);
        }
    });
    return root[0];
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) IStatus(org.eclipse.core.runtime.IStatus) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ISafeRunnable(org.eclipse.core.runtime.ISafeRunnable) ASTParser(org.eclipse.jdt.core.dom.ASTParser)

Example 10 with ASTParser

use of org.eclipse.jdt.core.dom.ASTParser in project flux by eclipse.

the class QuickAssistService method getProblemLocations.

public IProblemLocation getProblemLocations(ICompilationUnit liveEditUnit, int problemID, int offset, int length) {
    final ASTParser parser = ASTParser.newParser(AST.JLS4);
    // Parse the class as a compilation unit.
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    parser.setSource(liveEditUnit);
    parser.setResolveBindings(true);
    // Return the compiled class as a compilation unit
    final CompilationUnit unit = (CompilationUnit) parser.createAST(null);
    IProblem[] problems = unit.getProblems();
    if (problemID > 0) {
        return filterOutProblems(problems, problemID);
    } else {
        IProblemLocation[] locations = convertProblems(unit.getProblems());
        for (IProblemLocation iProblemLocation : locations) {
            if (offset >= iProblemLocation.getOffset() && offset <= (iProblemLocation.getOffset() + iProblemLocation.getLength())) {
                return iProblemLocation;
            }
        }
    }
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation) ASTParser(org.eclipse.jdt.core.dom.ASTParser) IProblem(org.eclipse.jdt.core.compiler.IProblem)

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