Search in sources :

Example 11 with ASTParser

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

the class ChangeSignatureProcessor method isValidVarargsExpression.

public static boolean isValidVarargsExpression(String string) {
    String trimmed = string.trim();
    if (//speed up for a common case //$NON-NLS-1$
    "".equals(trimmed))
        return true;
    StringBuffer cuBuff = new StringBuffer();
    //$NON-NLS-1$
    cuBuff.append("class A{ {m(");
    int offset = cuBuff.length();
    cuBuff.append(trimmed).append(//$NON-NLS-1$
    ");}}");
    ASTParser p = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
    p.setSource(cuBuff.toString().toCharArray());
    CompilationUnit cu = (CompilationUnit) p.createAST(null);
    Selection selection = Selection.createFromStartLength(offset, trimmed.length());
    SelectionAnalyzer analyzer = new SelectionAnalyzer(selection, false);
    cu.accept(analyzer);
    ASTNode[] selectedNodes = analyzer.getSelectedNodes();
    if (selectedNodes.length == 0)
        return false;
    for (int i = 0; i < selectedNodes.length; i++) {
        if (!(selectedNodes[i] instanceof Expression))
            return false;
    }
    return true;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SelectionAnalyzer(org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer) Expression(org.eclipse.jdt.core.dom.Expression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) Selection(org.eclipse.jdt.internal.corext.dom.Selection) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTParser(org.eclipse.jdt.core.dom.ASTParser) RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser)

Example 12 with ASTParser

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

the class ASTCreator method getCuNode.

private static CompilationUnit getCuNode(WorkingCopyOwner workingCopyOwner, ICompilationUnit cu) {
    ASTParser p = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
    p.setSource(cu);
    p.setResolveBindings(true);
    p.setWorkingCopyOwner(workingCopyOwner);
    p.setCompilerOptions(RefactoringASTParser.getCompilerOptions(cu));
    return (CompilationUnit) p.createAST(null);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ASTParser(org.eclipse.jdt.core.dom.ASTParser) RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser)

Example 13 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 14 with ASTParser

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

the class EclipseDomUtils method getCompilationUnitFromSource.

public static CompilationUnit getCompilationUnitFromSource(String javaSource) {
    ASTParser astParser = ASTParser.newParser(AST.JLS8);
    Map<?, ?> options = JavaCore.getDefaultOptions();
    JavaCore.setComplianceOptions(JavaCore.VERSION_1_8, options);
    astParser.setCompilerOptions(options);
    astParser.setSource(javaSource.toCharArray());
    CompilationUnit cu = (CompilationUnit) astParser.createAST(null);
    return cu;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ASTParser(org.eclipse.jdt.core.dom.ASTParser)

Example 15 with ASTParser

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

the class AbstractBuildErrorDetailsHandler method createAST.

/**
     * Obtain an AST for a source file in a project
     *
     * @param javaProject
     * @param className
     * @return An AST, or null if no source file exists for that class
     * @throws JavaModelException
     */
public static final CompilationUnit createAST(IJavaProject javaProject, String className) throws JavaModelException {
    IType type = javaProject.findType(className);
    if (type == null)
        return null;
    final ICompilationUnit cunit = type.getCompilationUnit();
    if (cunit == null)
        // not a source type
        return null;
    @SuppressWarnings("deprecation") ASTParser parser = ASTParser.newParser(AST.JLS4);
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    parser.setSource(cunit);
    parser.setResolveBindings(true);
    return (CompilationUnit) parser.createAST(null);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ASTParser(org.eclipse.jdt.core.dom.ASTParser) IType(org.eclipse.jdt.core.IType)

Aggregations

ASTParser (org.eclipse.jdt.core.dom.ASTParser)54 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)43 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)27 ASTNode (org.eclipse.jdt.core.dom.ASTNode)12 RefactoringASTParser (org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser)9 IBinding (org.eclipse.jdt.core.dom.IBinding)7 ArrayList (java.util.ArrayList)6 IProblem (org.eclipse.jdt.core.compiler.IProblem)6 HashMap (java.util.HashMap)5 IType (org.eclipse.jdt.core.IType)5 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)5 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)4 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)4 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)4 File (java.io.File)3 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)3 ISourceRange (org.eclipse.jdt.core.ISourceRange)3 ASTRequestor (org.eclipse.jdt.core.dom.ASTRequestor)3 ArrayType (org.eclipse.jdt.core.dom.ArrayType)3 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)3