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

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

Introduction

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

Prototype

public final ChildListPropertyDescriptor getModifiersProperty() 

Source Link

Document

Returns structural property descriptor for the "modifiers" property of this node as used in JLS3 (element type: IExtendedModifier ).

Usage

From source file:com.google.gwt.eclipse.core.clientbundle.ClientBundleResource.java

License:Open Source License

public MethodDeclaration createMethodDeclaration(IType clientBundle, ASTRewrite astRewrite,
        ImportRewrite importRewrite, boolean addComments) throws CoreException {
    AST ast = astRewrite.getAST();//  w  ww  . jav a2s. c  o m
    MethodDeclaration methodDecl = ast.newMethodDeclaration();

    // Method is named after the resource it accesses
    methodDecl.setName(ast.newSimpleName(getMethodName()));

    // Method return type is a ResourcePrototype subtype
    ITypeBinding resourceTypeBinding = JavaASTUtils.resolveType(clientBundle.getJavaProject(),
            getReturnTypeName());
    Type resourceType = importRewrite.addImport(resourceTypeBinding, ast);
    methodDecl.setReturnType2(resourceType);

    // Add @Source annotation if necessary
    String sourceAnnotationValue = getSourceAnnotationValue(clientBundle);
    if (sourceAnnotationValue != null) {
        // Build the annotation
        SingleMemberAnnotation sourceAnnotation = ast.newSingleMemberAnnotation();
        sourceAnnotation.setTypeName(ast.newName("Source"));
        StringLiteral annotationValue = ast.newStringLiteral();
        annotationValue.setLiteralValue(sourceAnnotationValue);
        sourceAnnotation.setValue(annotationValue);

        // Add the annotation to the method
        ChildListPropertyDescriptor modifiers = methodDecl.getModifiersProperty();
        ListRewrite modifiersRewriter = astRewrite.getListRewrite(methodDecl, modifiers);
        modifiersRewriter.insertFirst(sourceAnnotation, null);
    }

    return methodDecl;
}

From source file:com.motorola.studio.android.model.java.ActivityClass.java

License:Apache License

@Override
protected void addComments() throws AndroidException {
    ASTNode todoComment;//from  w w  w .j a  va  2  s  .  co  m

    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(document.get().toCharArray());

    compUnit = (CompilationUnit) parser.createAST(null);
    ast = compUnit.getAST();
    rewrite = ASTRewrite.create(ast);

    todoComment = rewrite.createStringPlaceholder(CodeUtilsNLS.MODEL_Common_ToDoPutYourCodeHere,
            ASTNode.EMPTY_STATEMENT);

    TypeDeclaration activityClass = (TypeDeclaration) compUnit.types().get(0);
    MethodDeclaration method;
    Block block;

    // Adds the Override annotation and ToDo comment to all overridden methods
    for (int i = 0; i < activityClass.bodyDeclarations().size(); i++) {
        method = (MethodDeclaration) activityClass.bodyDeclarations().get(i);

        // Adds the Override annotation
        rewrite.getListRewrite(method, method.getModifiersProperty()).insertFirst(OVERRIDE_ANNOTATION, null);

        // Adds the ToDo comment
        block = method.getBody();
        rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY).insertLast(todoComment, null);
    }

    try {
        // Writes the modifications
        TextEdit modifications = rewrite.rewriteAST(document, null);
        modifications.apply(document);
    } catch (IllegalArgumentException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        StudioLogger.error(ActivityClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    } catch (MalformedTreeException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        StudioLogger.error(ActivityClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    } catch (BadLocationException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        StudioLogger.error(ActivityClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    }
}

From source file:com.motorola.studio.android.model.java.BroadcastReceiverClass.java

License:Apache License

@Override
protected void addComments() throws AndroidException {
    ASTNode todoComment;/*from  w  w  w  .  j a va2s  .  c  om*/

    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(document.get().toCharArray());

    compUnit = (CompilationUnit) parser.createAST(null);
    ast = compUnit.getAST();
    rewrite = ASTRewrite.create(ast);

    todoComment = rewrite.createStringPlaceholder(CodeUtilsNLS.MODEL_Common_ToDoPutYourCodeHere,
            ASTNode.EMPTY_STATEMENT);

    TypeDeclaration receiverClass = (TypeDeclaration) compUnit.types().get(0);
    MethodDeclaration method;
    Block block;

    // Adds the Override annotation and ToDo comment to all overridden methods
    for (int i = 0; i < receiverClass.bodyDeclarations().size(); i++) {
        method = (MethodDeclaration) receiverClass.bodyDeclarations().get(i);

        // Adds the Override annotation
        rewrite.getListRewrite(method, method.getModifiersProperty()).insertFirst(OVERRIDE_ANNOTATION, null);

        // Adds the ToDo comment
        block = method.getBody();
        rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY).insertFirst(todoComment, null);
    }

    try {
        // Writes the modifications
        TextEdit modifications = rewrite.rewriteAST(document, null);
        modifications.apply(document);
    } catch (IllegalArgumentException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        StudioLogger.error(BroadcastReceiverClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    } catch (MalformedTreeException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        StudioLogger.error(BroadcastReceiverClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    } catch (BadLocationException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        StudioLogger.error(BroadcastReceiverClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    }
}

From source file:com.motorola.studio.android.model.java.ContentProviderClass.java

License:Apache License

@Override
protected void addComments() throws AndroidException {
    ASTNode todoComment;/*from w w  w.ja v a  2  s  .  c  om*/

    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(document.get().toCharArray());

    compUnit = (CompilationUnit) parser.createAST(null);
    ast = compUnit.getAST();
    rewrite = ASTRewrite.create(ast);

    todoComment = rewrite.createStringPlaceholder(CodeUtilsNLS.MODEL_Common_ToDoPutYourCodeHere,
            ASTNode.EMPTY_STATEMENT);

    TypeDeclaration cpClass = (TypeDeclaration) compUnit.types().get(0);
    MethodDeclaration method;
    Block block;

    // Adds the Override annotation and ToDo comment to all abstract methods
    for (int i = 0; i < cpClass.bodyDeclarations().size(); i++) {
        BodyDeclaration bodyDecl = (BodyDeclaration) cpClass.bodyDeclarations().get(i);

        if (bodyDecl instanceof MethodDeclaration) {
            method = (MethodDeclaration) bodyDecl;

            // Adds the Override annotation
            rewrite.getListRewrite(method, method.getModifiersProperty()).insertFirst(OVERRIDE_ANNOTATION,
                    null);

            // Adds the ToDo comment
            block = method.getBody();
            rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY).insertFirst(todoComment, null);
        }
    }

    try {
        // Writes the modifications
        TextEdit modifications = rewrite.rewriteAST(document, null);
        modifications.apply(document);
    } catch (IllegalArgumentException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        StudioLogger.error(ContentProviderClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    } catch (MalformedTreeException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        StudioLogger.error(ContentProviderClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    } catch (BadLocationException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        StudioLogger.error(ContentProviderClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    }
}

From source file:com.motorola.studio.android.model.java.ServiceClass.java

License:Apache License

@Override
protected void addComments() throws AndroidException {
    ASTNode todoComment;//from   w  w  w  .j a v a2s .c  om

    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(document.get().toCharArray());

    compUnit = (CompilationUnit) parser.createAST(null);
    ast = compUnit.getAST();
    rewrite = ASTRewrite.create(ast);

    todoComment = rewrite.createStringPlaceholder(CodeUtilsNLS.MODEL_Common_ToDoPutYourCodeHere,
            ASTNode.EMPTY_STATEMENT);

    TypeDeclaration serviceClass = (TypeDeclaration) compUnit.types().get(0);
    MethodDeclaration method;
    Block block;

    // Adds the Override annotation and ToDo comment to all abstract methods
    for (int i = 0; i < serviceClass.bodyDeclarations().size(); i++) {
        method = (MethodDeclaration) serviceClass.bodyDeclarations().get(i);

        // Adds the Override annotation
        rewrite.getListRewrite(method, method.getModifiersProperty()).insertFirst(OVERRIDE_ANNOTATION, null);

        // Adds the ToDo comment
        block = method.getBody();
        rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY).insertFirst(todoComment, null);
    }

    try {
        // Writes the modifications
        TextEdit modifications = rewrite.rewriteAST(document, null);
        modifications.apply(document);
    } catch (IllegalArgumentException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        StudioLogger.error(ServiceClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    } catch (MalformedTreeException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        StudioLogger.error(ServiceClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    } catch (BadLocationException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        StudioLogger.error(ServiceClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    }
}

From source file:com.motorola.studio.android.model.java.WidgetProviderClass.java

License:Apache License

@Override
protected void addComments() throws AndroidException {
    ASTNode todoComment;/*from   w  ww . j a va  2  s.c  o m*/

    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(document.get().toCharArray());

    compUnit = (CompilationUnit) parser.createAST(null);
    ast = compUnit.getAST();
    rewrite = ASTRewrite.create(ast);

    todoComment = rewrite.createStringPlaceholder(CodeUtilsNLS.MODEL_Common_ToDoPutYourCodeHere,
            ASTNode.EMPTY_STATEMENT);

    TypeDeclaration widgetProviderClass = (TypeDeclaration) compUnit.types().get(0);
    MethodDeclaration method;
    Block block;

    // Adds the Override annotation and ToDo comment to all overridden methods
    for (int i = 0; i < widgetProviderClass.bodyDeclarations().size(); i++) {
        method = (MethodDeclaration) widgetProviderClass.bodyDeclarations().get(i);

        // Adds the Override annotation
        rewrite.getListRewrite(method, method.getModifiersProperty()).insertFirst(OVERRIDE_ANNOTATION, null);

        // Adds the ToDo comment
        block = method.getBody();
        rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY).insertFirst(todoComment, null);
    }

    try {
        // Writes the modifications
        TextEdit modifications = rewrite.rewriteAST(document, null);
        modifications.apply(document);
    } catch (IllegalArgumentException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        StudioLogger.error(BroadcastReceiverClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    } catch (MalformedTreeException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        StudioLogger.error(BroadcastReceiverClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    } catch (BadLocationException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        StudioLogger.error(BroadcastReceiverClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    }
}

From source file:org.eclipse.andmore.android.model.java.ActivityClass.java

License:Apache License

@Override
protected void addComments() throws AndroidException {
    ASTNode todoComment;/*from   www  . j  a v a 2s .co  m*/

    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(document.get().toCharArray());

    compUnit = (CompilationUnit) parser.createAST(null);
    ast = compUnit.getAST();
    rewrite = ASTRewrite.create(ast);

    todoComment = rewrite.createStringPlaceholder(CodeUtilsNLS.MODEL_Common_ToDoPutYourCodeHere,
            ASTNode.EMPTY_STATEMENT);

    TypeDeclaration activityClass = (TypeDeclaration) compUnit.types().get(0);
    MethodDeclaration method;
    Block block;

    // Adds the Override annotation and ToDo comment to all overridden
    // methods
    for (int i = 0; i < activityClass.bodyDeclarations().size(); i++) {
        method = (MethodDeclaration) activityClass.bodyDeclarations().get(i);

        // Adds the Override annotation
        rewrite.getListRewrite(method, method.getModifiersProperty()).insertFirst(OVERRIDE_ANNOTATION, null);

        // Adds the ToDo comment
        block = method.getBody();
        rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY).insertLast(todoComment, null);
    }

    try {
        // Writes the modifications
        TextEdit modifications = rewrite.rewriteAST(document, null);
        modifications.apply(document);
    } catch (IllegalArgumentException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        AndmoreLogger.error(ActivityClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    } catch (MalformedTreeException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        AndmoreLogger.error(ActivityClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    } catch (BadLocationException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        AndmoreLogger.error(ActivityClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    }
}

From source file:org.eclipse.andmore.android.model.java.BroadcastReceiverClass.java

License:Apache License

@Override
protected void addComments() throws AndroidException {
    ASTNode todoComment;/*www. ja va2 s  .c  o m*/

    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(document.get().toCharArray());

    compUnit = (CompilationUnit) parser.createAST(null);
    ast = compUnit.getAST();
    rewrite = ASTRewrite.create(ast);

    todoComment = rewrite.createStringPlaceholder(CodeUtilsNLS.MODEL_Common_ToDoPutYourCodeHere,
            ASTNode.EMPTY_STATEMENT);

    TypeDeclaration receiverClass = (TypeDeclaration) compUnit.types().get(0);
    MethodDeclaration method;
    Block block;

    // Adds the Override annotation and ToDo comment to all overridden
    // methods
    for (int i = 0; i < receiverClass.bodyDeclarations().size(); i++) {
        method = (MethodDeclaration) receiverClass.bodyDeclarations().get(i);

        // Adds the Override annotation
        rewrite.getListRewrite(method, method.getModifiersProperty()).insertFirst(OVERRIDE_ANNOTATION, null);

        // Adds the ToDo comment
        block = method.getBody();
        rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY).insertFirst(todoComment, null);
    }

    try {
        // Writes the modifications
        TextEdit modifications = rewrite.rewriteAST(document, null);
        modifications.apply(document);
    } catch (IllegalArgumentException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        AndmoreLogger.error(BroadcastReceiverClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    } catch (MalformedTreeException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        AndmoreLogger.error(BroadcastReceiverClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    } catch (BadLocationException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        AndmoreLogger.error(BroadcastReceiverClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    }
}

From source file:org.eclipse.andmore.android.model.java.ContentProviderClass.java

License:Apache License

@Override
protected void addComments() throws AndroidException {
    ASTNode todoComment;//from www.  j av a  2  s . c o m

    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(document.get().toCharArray());

    compUnit = (CompilationUnit) parser.createAST(null);
    ast = compUnit.getAST();
    rewrite = ASTRewrite.create(ast);

    todoComment = rewrite.createStringPlaceholder(CodeUtilsNLS.MODEL_Common_ToDoPutYourCodeHere,
            ASTNode.EMPTY_STATEMENT);

    TypeDeclaration cpClass = (TypeDeclaration) compUnit.types().get(0);
    MethodDeclaration method;
    Block block;

    // Adds the Override annotation and ToDo comment to all abstract methods
    for (int i = 0; i < cpClass.bodyDeclarations().size(); i++) {
        BodyDeclaration bodyDecl = (BodyDeclaration) cpClass.bodyDeclarations().get(i);

        if (bodyDecl instanceof MethodDeclaration) {
            method = (MethodDeclaration) bodyDecl;

            // Adds the Override annotation
            rewrite.getListRewrite(method, method.getModifiersProperty()).insertFirst(OVERRIDE_ANNOTATION,
                    null);

            // Adds the ToDo comment
            block = method.getBody();
            rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY).insertFirst(todoComment, null);
        }
    }

    try {
        // Writes the modifications
        TextEdit modifications = rewrite.rewriteAST(document, null);
        modifications.apply(document);
    } catch (IllegalArgumentException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        AndmoreLogger.error(ContentProviderClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    } catch (MalformedTreeException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        AndmoreLogger.error(ContentProviderClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    } catch (BadLocationException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        AndmoreLogger.error(ContentProviderClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    }
}

From source file:org.eclipse.andmore.android.model.java.ServiceClass.java

License:Apache License

@Override
protected void addComments() throws AndroidException {
    ASTNode todoComment;/*from ww  w .j av  a 2 s .c o  m*/

    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setSource(document.get().toCharArray());

    compUnit = (CompilationUnit) parser.createAST(null);
    ast = compUnit.getAST();
    rewrite = ASTRewrite.create(ast);

    todoComment = rewrite.createStringPlaceholder(CodeUtilsNLS.MODEL_Common_ToDoPutYourCodeHere,
            ASTNode.EMPTY_STATEMENT);

    TypeDeclaration serviceClass = (TypeDeclaration) compUnit.types().get(0);
    MethodDeclaration method;
    Block block;

    // Adds the Override annotation and ToDo comment to all abstract methods
    for (int i = 0; i < serviceClass.bodyDeclarations().size(); i++) {
        method = (MethodDeclaration) serviceClass.bodyDeclarations().get(i);

        // Adds the Override annotation
        rewrite.getListRewrite(method, method.getModifiersProperty()).insertFirst(OVERRIDE_ANNOTATION, null);

        // Adds the ToDo comment
        block = method.getBody();
        rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY).insertFirst(todoComment, null);
    }

    try {
        // Writes the modifications
        TextEdit modifications = rewrite.rewriteAST(document, null);
        modifications.apply(document);
    } catch (IllegalArgumentException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        AndmoreLogger.error(ServiceClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    } catch (MalformedTreeException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        AndmoreLogger.error(ServiceClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    } catch (BadLocationException e) {
        String errMsg = NLS.bind(CodeUtilsNLS.EXC_JavaClass_ErrorApplyingCommentsToCode, className);

        AndmoreLogger.error(ServiceClass.class, errMsg, e);
        throw new AndroidException(errMsg);
    }
}