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

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

Introduction

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

Prototype

public void setModifiers(int modifiers) 

Source Link

Document

Sets the modifiers explicitly specified on this declaration (JLS2 API only).

Usage

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//from   www  . j  a  v a 2  s  . co m
 * @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 a2 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;
}