Package org.springframework.ide.eclipse.quickfix.jdt.proposals

Source Code of org.springframework.ide.eclipse.quickfix.jdt.proposals.AddResponseBodyCompletionProposal

/*******************************************************************************
*  Copyright (c) 2012 VMware, Inc.
*  All rights reserved. This program and the accompanying materials
*  are made available under the terms of the Eclipse Public License v1.0
*  which accompanies this distribution, and is available at
*  http://www.eclipse.org/legal/epl-v10.html
*
*  Contributors:
*      VMware, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.eclipse.quickfix.jdt.proposals;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.MarkerAnnotation;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
import org.eclipse.jdt.internal.ui.text.correction.ASTResolving;
import org.springframework.ide.eclipse.quickfix.QuickfixImages;
import org.springframework.ide.eclipse.quickfix.jdt.util.ProposalCalculatorUtil;
import org.springframework.web.bind.annotation.ResponseBody;


/**
* @author Terry Denney
* @since 2.6
*/
public class AddResponseBodyCompletionProposal extends AnnotationCompletionProposal {

  private final MethodDeclaration methodDecl;

  public AddResponseBodyCompletionProposal(MethodDeclaration methodDecl, ICompilationUnit cu) {
    super("Add @ResponseBody", cu, QuickfixImages.getImage(QuickfixImages.ANNOTATION));
    this.methodDecl = methodDecl;
  }

  @Override
  protected ASTRewrite getRewrite() throws CoreException {
    CompilationUnit astRoot = ASTResolving.findParentCompilationUnit(methodDecl);
    ASTRewrite astRewrite = ASTRewrite.create(astRoot.getAST());

    AST ast = astRewrite.getAST();

    String importName = ResponseBody.class.getCanonicalName();
    if (!ProposalCalculatorUtil.containsImport(getCompilationUnit(), importName)) {
      ImportRewrite importRewrite = createImportRewrite(astRoot);
      importRewrite.addImport(importName);
    }

    MarkerAnnotation annotation = ast.newMarkerAnnotation();
    annotation.setTypeName(ast.newSimpleName("ResponseBody"));

    astRewrite.getListRewrite(methodDecl, MethodDeclaration.MODIFIERS2_PROPERTY).insertLast(annotation, null);

    return astRewrite;
  }
}
TOP

Related Classes of org.springframework.ide.eclipse.quickfix.jdt.proposals.AddResponseBodyCompletionProposal

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.