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

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

/*******************************************************************************
*  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.SimpleName;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
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.InitBinder;


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

  private final MethodDeclaration methodDecl;

  public AddInitBinderCompletionProposal(MethodDeclaration methodDecl, ICompilationUnit cu) {
    super("Add @InitBinder", 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());

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

    AST ast = astRewrite.getAST();

    MarkerAnnotation annotation = ast.newMarkerAnnotation();
    SimpleName name = ast.newSimpleName("InitBinder");
    annotation.setTypeName(name);

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

    return astRewrite;
  }

}
TOP

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

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.