Search in sources :

Example 1 with MethodDeclaration

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

the class LocalVariableIndex method internalPerform.

private static int internalPerform(BodyDeclaration methodOrInitializer) {
    // we have to find the outermost method/initializer/field declaration since a local or anonymous
    // type can reference final variables from the outer scope.
    BodyDeclaration target = methodOrInitializer;
    ASTNode parent = target.getParent();
    while (parent != null) {
        if (parent instanceof MethodDeclaration || parent instanceof Initializer || parent instanceof FieldDeclaration) {
            target = (BodyDeclaration) parent;
        }
        parent = parent.getParent();
    }
    return doPerform(target);
}
Also used : Initializer(org.eclipse.jdt.core.dom.Initializer) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 2 with MethodDeclaration

use of org.eclipse.jdt.core.dom.MethodDeclaration 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 3 with MethodDeclaration

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

the class StubUtility2 method createConstructorStub.

public static MethodDeclaration createConstructorStub(ICompilationUnit unit, ASTRewrite rewrite, ImportRewrite imports, ImportRewriteContext context, ITypeBinding typeBinding, IMethodBinding superConstructor, IVariableBinding[] variableBindings, int modifiers, CodeGenerationSettings settings) throws CoreException {
    AST ast = rewrite.getAST();
    MethodDeclaration decl = ast.newMethodDeclaration();
    decl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, modifiers & ~Modifier.ABSTRACT & ~Modifier.NATIVE));
    decl.setName(ast.newSimpleName(typeBinding.getName()));
    decl.setConstructor(true);
    List<SingleVariableDeclaration> parameters = decl.parameters();
    if (superConstructor != null) {
        createTypeParameters(imports, context, ast, superConstructor, decl);
        createParameters(unit.getJavaProject(), imports, context, ast, superConstructor, null, decl);
        createThrownExceptions(decl, superConstructor, imports, context, ast);
    }
    Block body = ast.newBlock();
    decl.setBody(body);
    String delimiter = StubUtility.getLineDelimiterUsed(unit);
    if (superConstructor != null) {
        SuperConstructorInvocation invocation = ast.newSuperConstructorInvocation();
        SingleVariableDeclaration varDecl = null;
        for (Iterator<SingleVariableDeclaration> iterator = parameters.iterator(); iterator.hasNext(); ) {
            varDecl = iterator.next();
            invocation.arguments().add(ast.newSimpleName(varDecl.getName().getIdentifier()));
        }
        body.statements().add(invocation);
    }
    List<String> prohibited = new ArrayList<String>();
    for (final Iterator<SingleVariableDeclaration> iterator = parameters.iterator(); iterator.hasNext(); ) prohibited.add(iterator.next().getName().getIdentifier());
    String param = null;
    List<String> list = new ArrayList<String>(prohibited);
    String[] excluded = null;
    for (int i = 0; i < variableBindings.length; i++) {
        SingleVariableDeclaration var = ast.newSingleVariableDeclaration();
        var.setType(imports.addImport(variableBindings[i].getType(), ast, context));
        excluded = new String[list.size()];
        list.toArray(excluded);
        param = suggestParameterName(unit, variableBindings[i], excluded);
        list.add(param);
        var.setName(ast.newSimpleName(param));
        parameters.add(var);
    }
    list = new ArrayList<String>(prohibited);
    for (int i = 0; i < variableBindings.length; i++) {
        excluded = new String[list.size()];
        list.toArray(excluded);
        final String paramName = suggestParameterName(unit, variableBindings[i], excluded);
        list.add(paramName);
        final String fieldName = variableBindings[i].getName();
        Expression expression = null;
        if (paramName.equals(fieldName) || settings.useKeywordThis) {
            FieldAccess access = ast.newFieldAccess();
            access.setExpression(ast.newThisExpression());
            access.setName(ast.newSimpleName(fieldName));
            expression = access;
        } else
            expression = ast.newSimpleName(fieldName);
        Assignment assignment = ast.newAssignment();
        assignment.setLeftHandSide(expression);
        assignment.setRightHandSide(ast.newSimpleName(paramName));
        assignment.setOperator(Assignment.Operator.ASSIGN);
        body.statements().add(ast.newExpressionStatement(assignment));
    }
    if (settings != null && settings.createComments) {
        String string = CodeGeneration.getMethodComment(unit, typeBinding.getName(), decl, superConstructor, delimiter);
        if (string != null) {
            Javadoc javadoc = (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
            decl.setJavadoc(javadoc);
        }
    }
    return decl;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ArrayList(java.util.ArrayList) Javadoc(org.eclipse.jdt.core.dom.Javadoc) Assignment(org.eclipse.jdt.core.dom.Assignment) Expression(org.eclipse.jdt.core.dom.Expression) Block(org.eclipse.jdt.core.dom.Block) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess)

Example 4 with MethodDeclaration

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

the class RenameMethodProcessor method addOccurrences.

/**
	 * Add occurrences
	 *
	 * @param manager the text change manager
	 * @param pm the progress monitor
	 * @param status the status
	 * @throws CoreException if change creation failed
	 */
protected void addOccurrences(TextChangeManager manager, IProgressMonitor pm, RefactoringStatus status) throws CoreException /*thrown in subtype*/
{
    //$NON-NLS-1$
    pm.beginTask("", fOccurrences.length);
    for (int i = 0; i < fOccurrences.length; i++) {
        ICompilationUnit cu = fOccurrences[i].getCompilationUnit();
        if (cu == null)
            continue;
        SearchMatch[] results = fOccurrences[i].getSearchResults();
        // Split matches into declaration and non-declaration matches
        List<SearchMatch> declarationsInThisCu = new ArrayList<SearchMatch>();
        List<SearchMatch> referencesInThisCu = new ArrayList<SearchMatch>();
        for (int j = 0; j < results.length; j++) {
            if (results[j] instanceof MethodDeclarationMatch)
                declarationsInThisCu.add(results[j]);
            else
                referencesInThisCu.add(results[j]);
        }
        // First, handle the declarations
        if (declarationsInThisCu.size() > 0) {
            if (fDelegateUpdating) {
                // Update with delegates
                CompilationUnitRewrite rewrite = new CompilationUnitRewrite(cu);
                rewrite.setResolveBindings(true);
                for (Iterator<SearchMatch> iter = declarationsInThisCu.iterator(); iter.hasNext(); ) {
                    SearchMatch element = iter.next();
                    MethodDeclaration method = ASTNodeSearchUtil.getMethodDeclarationNode((IMethod) element.getElement(), rewrite.getRoot());
                    DelegateCreator creator = new DelegateMethodCreator();
                    creator.setDeclareDeprecated(fDelegateDeprecation);
                    creator.setDeclaration(method);
                    creator.setSourceRewrite(rewrite);
                    creator.setNewElementName(getNewElementName());
                    creator.prepareDelegate();
                    creator.createEdit();
                }
                // Need to handle all delegates first as this
                // creates a completely new change object.
                TextChange changeForThisCu = rewrite.createChange(true);
                changeForThisCu.setKeepPreviewEdits(true);
                manager.manage(cu, changeForThisCu);
            }
            // Update the normal methods
            for (Iterator<SearchMatch> iter = declarationsInThisCu.iterator(); iter.hasNext(); ) {
                SearchMatch element = iter.next();
                simpleUpdate(element, cu, manager.get(cu));
            }
        }
        // Second, handle references
        if (fUpdateReferences) {
            for (Iterator<SearchMatch> iter = referencesInThisCu.iterator(); iter.hasNext(); ) {
                SearchMatch element = iter.next();
                simpleUpdate(element, cu, manager.get(cu));
            }
        }
        pm.worked(1);
        if (pm.isCanceled())
            throw new OperationCanceledException();
    }
    pm.done();
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ArrayList(java.util.ArrayList) TextChange(org.eclipse.ltk.core.refactoring.TextChange) MethodDeclarationMatch(org.eclipse.jdt.core.search.MethodDeclarationMatch) DelegateMethodCreator(org.eclipse.jdt.internal.corext.refactoring.delegates.DelegateMethodCreator) DelegateCreator(org.eclipse.jdt.internal.corext.refactoring.delegates.DelegateCreator)

Example 5 with MethodDeclaration

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

the class RenameFieldProcessor method addMethodDelegate.

private void addMethodDelegate(IMethod getter, String newName, CompilationUnitRewrite rewrite) throws JavaModelException {
    MethodDeclaration declaration = ASTNodeSearchUtil.getMethodDeclarationNode(getter, rewrite.getRoot());
    DelegateCreator creator = new DelegateMethodCreator();
    creator.setDeclareDeprecated(fDelegateDeprecation);
    creator.setDeclaration(declaration);
    creator.setNewElementName(newName);
    creator.setSourceRewrite(rewrite);
    creator.prepareDelegate();
    creator.createEdit();
}
Also used : DelegateMethodCreator(org.eclipse.jdt.internal.corext.refactoring.delegates.DelegateMethodCreator) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) DelegateCreator(org.eclipse.jdt.internal.corext.refactoring.delegates.DelegateCreator)

Aggregations

MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)131 ASTNode (org.eclipse.jdt.core.dom.ASTNode)80 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)48 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)40 AST (org.eclipse.jdt.core.dom.AST)37 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)35 Type (org.eclipse.jdt.core.dom.Type)35 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)35 Block (org.eclipse.jdt.core.dom.Block)33 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)33 Expression (org.eclipse.jdt.core.dom.Expression)30 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)29 SimpleName (org.eclipse.jdt.core.dom.SimpleName)28 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)26 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)23 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)23 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)20 Javadoc (org.eclipse.jdt.core.dom.Javadoc)19 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)18 ArrayList (java.util.ArrayList)17