Search in sources :

Example 1 with CheASTParser

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

the class ASTNodeFactory method newType.

public static Type newType(AST ast, String content) {
    StringBuffer buffer = new StringBuffer(TYPE_HEADER);
    buffer.append(content);
    buffer.append(TYPE_FOOTER);
    CheASTParser p = CheASTParser.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];
    ASTNode type = methodDecl.getReturnType2();
    ASTNode result = ASTNode.copySubtree(ast, type);
    result.accept(new PositionClearer());
    return (Type) result;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) Type(org.eclipse.jdt.core.dom.Type) UnionType(org.eclipse.jdt.core.dom.UnionType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) CheASTParser(org.eclipse.jdt.core.dom.CheASTParser) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 2 with CheASTParser

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

the class StubUtility method getParameterTypeNamesForSeeTag.

/*
	 * Returns the parameters type names used in see tags. Currently, these are always fully qualified.
	 */
private static String[] getParameterTypeNamesForSeeTag(IMethod overridden) {
    try {
        CheASTParser parser = CheASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
        parser.setProject(overridden.getJavaProject());
        IBinding[] bindings = parser.createBindings(new IJavaElement[] { overridden }, null);
        if (bindings.length == 1 && bindings[0] instanceof IMethodBinding) {
            return getParameterTypeNamesForSeeTag((IMethodBinding) bindings[0]);
        }
    } catch (IllegalStateException e) {
    // method does not exist
    }
    // fall back code. Not good for generic methods!
    String[] paramTypes = overridden.getParameterTypes();
    String[] paramTypeNames = new String[paramTypes.length];
    for (int i = 0; i < paramTypes.length; i++) {
        paramTypeNames[i] = Signature.toString(Signature.getTypeErasure(paramTypes[i]));
    }
    return paramTypeNames;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) IBinding(org.eclipse.jdt.core.dom.IBinding) CheASTParser(org.eclipse.jdt.core.dom.CheASTParser)

Example 3 with CheASTParser

use of org.eclipse.jdt.core.dom.CheASTParser 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 CheASTParser parser = CheASTParser.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) CheASTParser(org.eclipse.jdt.core.dom.CheASTParser)

Example 4 with CheASTParser

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

the class JavadocContentAccess2 method createAST.

private static CompilationUnit createAST(IJavaElement element, String cuSource) {
    Assert.isNotNull(element);
    CheASTParser parser = CheASTParser.newParser(AST.JLS8);
    IJavaProject javaProject = element.getJavaProject();
    parser.setProject(javaProject);
    Map<String, String> options = javaProject.getOptions(true);
    options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=212207
    JavaCore.ENABLED);
    parser.setCompilerOptions(options);
    parser.setSource(cuSource.toCharArray());
    return (CompilationUnit) parser.createAST(null);
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IJavaProject(org.eclipse.jdt.core.IJavaProject) CheASTParser(org.eclipse.jdt.core.dom.CheASTParser)

Example 5 with CheASTParser

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

the class OverrideCompletionProposal method getRecoveredAST.

private CompilationUnit getRecoveredAST(IDocument document, int offset, Document recoveredDocument) {
    CompilationUnit ast = SharedASTProvider.getAST(fCompilationUnit, SharedASTProvider.WAIT_ACTIVE_ONLY, null);
    if (ast != null) {
        recoveredDocument.set(document.get());
        return ast;
    }
    char[] content = document.get().toCharArray();
    // clear prefix to avoid compile errors
    int index = offset - 1;
    while (index >= 0 && Character.isJavaIdentifierPart(content[index])) {
        content[index] = ' ';
        index--;
    }
    recoveredDocument.set(new String(content));
    final CheASTParser parser = CheASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
    parser.setResolveBindings(true);
    parser.setStatementsRecovery(true);
    parser.setSource(content);
    parser.setUnitName(fCompilationUnit.getElementName());
    parser.setProject(fCompilationUnit.getJavaProject());
    return (CompilationUnit) parser.createAST(new NullProgressMonitor());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) StyledString(org.eclipse.jface.viewers.StyledString) CheASTParser(org.eclipse.jdt.core.dom.CheASTParser)

Aggregations

CheASTParser (org.eclipse.jdt.core.dom.CheASTParser)10 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)8 ASTNode (org.eclipse.jdt.core.dom.ASTNode)5 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)3 ArrayList (java.util.ArrayList)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 IJavaProject (org.eclipse.jdt.core.IJavaProject)2 IBinding (org.eclipse.jdt.core.dom.IBinding)2 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)2 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)2 StyledString (org.eclipse.jface.viewers.StyledString)2 HashMap (java.util.HashMap)1 ISafeRunnable (org.eclipse.core.runtime.ISafeRunnable)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 IMethod (org.eclipse.jdt.core.IMethod)1 ISourceRange (org.eclipse.jdt.core.ISourceRange)1