Example usage for org.eclipse.jdt.core.dom MethodDeclaration setReturnType

List of usage examples for org.eclipse.jdt.core.dom MethodDeclaration setReturnType

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom MethodDeclaration setReturnType.

Prototype

public void setReturnType(Type type) 

Source Link

Document

Sets the return type of the method declared in this method declaration to the given type, exclusive of any extra array dimensions (JLS2 API only).

Usage

From source file:java5totext.input.JDTVisitor.java

License:Open Source License

@Override
public void endVisit(org.eclipse.jdt.core.dom.MethodDeclaration node) {
    MethodDeclaration element = (MethodDeclaration) this.binding.get(node);
    this.initializeNode(element, node);
    element.setExtraArrayDimensions(node.getExtraDimensions());
    element.setConstructor(node.isConstructor());
    element.setVarargs(false);/*from w  ww. j  a v  a2s. co m*/

    for (Iterator<?> i = node.parameters().iterator(); i.hasNext();) {
        SingleVariableDeclaration itElement = (SingleVariableDeclaration) this.binding.get(i.next());
        if (itElement != null)
            element.getParameters().add(itElement);
    }
    if (this.binding.get(node.getBody()) != null)
        element.setBody((Block) this.binding.get(node.getBody()));
    for (Iterator<?> i = node.thrownExceptions().iterator(); i.hasNext();) {
        NamedElementRef itElement = (NamedElementRef) this.binding.get(i.next());
        if (itElement != null)
            element.getThrownExceptions().add(itElement);
    }
    element.setName(node.getName().getIdentifier());
    if (this.binding.get(node.getReturnType2()) != null)
        element.setReturnType((NamedElementRef) this.binding.get(node.getReturnType2()));
    for (Iterator<?> i = node.typeParameters().iterator(); i.hasNext();) {
        TypeParameter itElement = (TypeParameter) this.binding.get(i.next());
        if (itElement != null)
            element.getTypeParameters().add(itElement);
    }
    endVisitBD(node, element);

    this.localBindings.resolveBindings();
    // localBindings should be kept if method is declared in anonymous class declared in a method body
    if (!(node.getParent() instanceof org.eclipse.jdt.core.dom.AnonymousClassDeclaration
            || node.getParent().getParent() instanceof org.eclipse.jdt.core.dom.TypeDeclarationStatement))
        this.localBindings = null;
    JDTVisitorUtils.manageBindingDeclaration(element, node.getName(), this);
}

From source file:org.eclipse.modisco.java.discoverer.internal.io.java.JDTVisitor.java

License:Open Source License

@Override
public void endVisit(final org.eclipse.jdt.core.dom.MethodDeclaration node) {
    AbstractMethodDeclaration element = (AbstractMethodDeclaration) this.binding.get(node);
    initializeNode(element, node);/*from w w w .j  a v a  2  s .  co m*/

    if (!node.isConstructor()) {
        ((MethodDeclaration) element).setExtraArrayDimensions(node.getExtraDimensions());
    }

    for (Iterator<?> i = node.parameters().iterator(); i.hasNext();) {
        SingleVariableDeclaration itElement = (SingleVariableDeclaration) this.binding.get(i.next());
        if (itElement != null) {
            itElement.setProxy(false);
            itElement.setMethodDeclaration(element);
            element.getParameters().add(itElement);
        }
    }

    if (this.binding.get(node.getBody()) != null) {
        element.setBody((Block) this.binding.get(node.getBody()));
    }
    if (this.isINCREMENTALDISCOVERING) {
        // To avoid to re-create an existing access type
        List<TypeAccess> thrownExceptionsList = new ArrayList<TypeAccess>(element.getThrownExceptions());
        Iterator<TypeAccess> thrownExceptions = thrownExceptionsList.iterator();
        while (thrownExceptions.hasNext()) {
            TypeAccess typeAccess = thrownExceptions.next();
            element.getThrownExceptions().remove(typeAccess);
            deepRemove(typeAccess);
        }
    }
    for (Iterator<?> i = node.thrownExceptions().iterator(); i.hasNext();) {
        ASTNode itElement = this.binding.get(i.next());
        if (itElement != null) {
            element.getThrownExceptions().add(JDTVisitorUtils.completeTypeAccess(itElement, this));
        }
    }

    element.setName(node.getName().getIdentifier());

    if (this.binding.get(node.getReturnType2()) != null) {
        MethodDeclaration methodDeclaration = (MethodDeclaration) element;
        if (this.isINCREMENTALDISCOVERING && methodDeclaration.getReturnType() != null) {
            // To avoid to re-create an existing access type
            deepRemove(methodDeclaration.getReturnType());
            methodDeclaration.setReturnType(null);
        }
        methodDeclaration.setReturnType(
                JDTVisitorUtils.completeTypeAccess(this.binding.get(node.getReturnType2()), this));
    }

    for (Iterator<?> i = node.typeParameters().iterator(); i.hasNext();) {
        TypeParameter itElement = (TypeParameter) this.binding.get(i.next());
        if (itElement != null) {
            element.getTypeParameters().add(itElement);
        }
    }
    endVisitBD(node, element);

    // long debut = System.currentTimeMillis();

    getLocalBindings().resolveBindings();

    // long fin = System.currentTimeMillis();
    // TimeResults.resolveBindings += fin - debut;

    // localBindings should be kept if method is declared in anonymous class
    // declared in a method body
    if (!(node.getParent() instanceof org.eclipse.jdt.core.dom.AnonymousClassDeclaration
            || node.getParent().getParent() instanceof org.eclipse.jdt.core.dom.TypeDeclarationStatement)) {
        setLocalBindings(null);
    }

    JDTVisitorUtils.manageBindingDeclaration(element, node.getName(), this);
}

From source file:org.smartfrog.tools.eclipse.ui.project.document.BaseComponentCreationWizardPage.java

License:Open Source License

/**
 * add smartFrog methods to template (memory only)
 * @param cu//w  ww .  j av  a2s  . c  om
 * @param unit
 * @param typeDeclaration
 * @param methodName
 * @param needToThrow
 * @param needParam
 * @throws CoreException
 * @throws InterruptedException
 */
private void addSfDeployMethod(ICompilationUnit cu, CompilationUnit unit, TypeDeclaration typeDeclaration,
        String methodName, boolean needToThrow, boolean needParam) throws CoreException, InterruptedException {
    AST localAst = typeDeclaration.getAST();
    MethodDeclaration methodDeclaration = localAst.newMethodDeclaration();
    methodDeclaration.setConstructor(false);
    methodDeclaration.setModifiers(Modifier.PUBLIC | Modifier.SYNCHRONIZED);
    methodDeclaration.setName(localAst.newSimpleName(methodName));
    methodDeclaration.setReturnType(localAst.newPrimitiveType(PrimitiveType.VOID));

    if (needParam) {
        SingleVariableDeclaration variableDeclaration = localAst.newSingleVariableDeclaration();
        variableDeclaration.setModifiers(Modifier.NONE);
        variableDeclaration.setType(localAst.newSimpleType(localAst.newSimpleName(TERMINATIONRECORD_VAR)));
        variableDeclaration.setName(localAst.newSimpleName(SFTERMINATE_STATUS_ARGUMENT_NAME));
        methodDeclaration.parameters().add(variableDeclaration);
    }

    Block block = localAst.newBlock();
    methodDeclaration.setBody(block);

    SuperMethodInvocation superMethodInvocation = localAst.newSuperMethodInvocation();
    superMethodInvocation.setName(localAst.newSimpleName(methodName));

    if (needParam) {
        List list = superMethodInvocation.arguments();
        list.add(localAst.newSimpleName(SFTERMINATE_STATUS_ARGUMENT_NAME));
    }

    ExpressionStatement expressionStatement = localAst.newExpressionStatement(superMethodInvocation);
    block.statements().add(expressionStatement);

    if (needToThrow) {
        List throwsExceptions = methodDeclaration.thrownExceptions();
        throwsExceptions.add(localAst.newSimpleName(SMARTFROGEXCEPTION_NAME));
        throwsExceptions.add(localAst.newSimpleName(REMOTEEXCEPTION_NAME));
    }

    typeDeclaration.bodyDeclarations().add(methodDeclaration);
}

From source file:org.whole.lang.java.util.JDTTransformerVisitor.java

License:Open Source License

public boolean visit(MethodDeclaration node) {
    if (node.isConstructor()) {
        ConstructorDeclaration constructorDecl;
        appendBodyDeclaration(constructorDecl = lf.create(JavaEntityDescriptorEnum.ConstructorDeclaration));

        if (acceptChild(node.getJavadoc()))
            constructorDecl.setJavadoc(this.javadoc);

        List<?> modifiers = node.modifiers();
        if (!modifiers.isEmpty()) {
            constructorDecl.setModifiers(lf.create(JavaEntityDescriptorEnum.ExtendedModifiers));
            setExtendedModifiers(constructorDecl.getModifiers(), modifiers);
        }//  w w  w. j av  a  2  s . c  o m

        Iterator<?> j = node.typeParameters().iterator();
        while (j.hasNext()) {
            ((ASTNode) j.next()).accept(this);
            constructorDecl.getTypeParameters().wAdd(typeParameter);
        }

        if (acceptChild(node.getName()))
            constructorDecl.setName((org.whole.lang.java.model.SimpleName) name);

        constructorDecl.setParameters(params = lf.create(JavaEntityDescriptorEnum.Parameters));
        acceptChildren(node.parameters());

        if (node.thrownExceptionTypes().isEmpty())
            thrownExceptions = createResolver(JavaEntityDescriptorEnum.Types);
        else
            thrownExceptions = lf.create(JavaEntityDescriptorEnum.Types);
        constructorDecl.setThrownExceptions(thrownExceptions);
        for (Object child : node.thrownExceptionTypes()) {
            ((ASTNode) child).accept(this);
            thrownExceptions.wAdd(type);
        }

        acceptChild(node.getBody());
        constructorDecl.setBody((org.whole.lang.java.model.Block) stm);

        constructorDecl.getExtraDimensions().wSetValue(node.getExtraDimensions());
    } else {
        org.whole.lang.java.model.MethodDeclaration methodDecl;
        appendBodyDeclaration(methodDecl = lf.create(JavaEntityDescriptorEnum.MethodDeclaration));

        if (acceptChild(node.getJavadoc()))
            methodDecl.setJavadoc(this.javadoc);

        List<?> modifiers = node.modifiers();
        if (!modifiers.isEmpty()) {
            methodDecl.setModifiers(lf.create(JavaEntityDescriptorEnum.ExtendedModifiers));
            setExtendedModifiers(methodDecl.getModifiers(), modifiers);
        }

        Iterator<?> j = node.typeParameters().iterator();
        while (j.hasNext()) {
            ((ASTNode) j.next()).accept(this);
            methodDecl.getTypeParameters().wAdd(typeParameter);
        }

        if (acceptChild(node.getReturnType2()))
            methodDecl.setReturnType(type);

        if (acceptChild(node.getName()))
            methodDecl.setName((org.whole.lang.java.model.SimpleName) name);

        methodDecl.setParameters(params = lf.create(JavaEntityDescriptorEnum.Parameters));
        acceptChildren(node.parameters());

        if (node.thrownExceptionTypes().isEmpty())
            thrownExceptions = createResolver(JavaEntityDescriptorEnum.Types);
        else
            thrownExceptions = lf.create(JavaEntityDescriptorEnum.Types);
        methodDecl.setThrownExceptions(thrownExceptions);
        for (Object child : node.thrownExceptionTypes()) {
            ((ASTNode) child).accept(this);
            thrownExceptions.wAdd(type);
        }

        if (acceptChild(node.getBody()))
            methodDecl.setBody((org.whole.lang.java.model.Block) stm);

        methodDecl.getExtraDimensions().wSetValue(node.getExtraDimensions());
    }
    return false;
}