Search in sources :

Example 1 with ConvertAnonymousToNestedRefactoring

use of org.eclipse.jdt.internal.corext.refactoring.code.ConvertAnonymousToNestedRefactoring in project che by eclipse.

the class QuickAssistProcessor method getConvertAnonymousToNestedProposal.

private static boolean getConvertAnonymousToNestedProposal(IInvocationContext context, final ASTNode node, Collection<ICommandAccess> proposals) throws CoreException {
    if (!(node instanceof Name))
        return false;
    ASTNode normalized = ASTNodes.getNormalizedNode(node);
    if (normalized.getLocationInParent() != ClassInstanceCreation.TYPE_PROPERTY)
        return false;
    final AnonymousClassDeclaration anonymTypeDecl = ((ClassInstanceCreation) normalized.getParent()).getAnonymousClassDeclaration();
    if (anonymTypeDecl == null || anonymTypeDecl.resolveBinding() == null) {
        return false;
    }
    if (proposals == null) {
        return true;
    }
    final ICompilationUnit cu = context.getCompilationUnit();
    final ConvertAnonymousToNestedRefactoring refactoring = new ConvertAnonymousToNestedRefactoring(anonymTypeDecl);
    String extTypeName = ASTNodes.getSimpleNameIdentifier((Name) node);
    ITypeBinding anonymTypeBinding = anonymTypeDecl.resolveBinding();
    String className;
    if (anonymTypeBinding.getInterfaces().length == 0) {
        className = Messages.format(CorrectionMessages.QuickAssistProcessor_name_extension_from_interface, extTypeName);
    } else {
        className = Messages.format(CorrectionMessages.QuickAssistProcessor_name_extension_from_class, extTypeName);
    }
    String[][] existingTypes = ((IType) anonymTypeBinding.getJavaElement()).resolveType(className);
    int i = 1;
    while (existingTypes != null) {
        i++;
        existingTypes = ((IType) anonymTypeBinding.getJavaElement()).resolveType(className + i);
    }
    refactoring.setClassName(i == 1 ? className : className + i);
    if (refactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
        LinkedProposalModel linkedProposalModel = new LinkedProposalModel();
        refactoring.setLinkedProposalModel(linkedProposalModel);
        String label = CorrectionMessages.QuickAssistProcessor_convert_anonym_to_nested;
        Image image = JavaPlugin.getImageDescriptorRegistry().get(JavaElementImageProvider.getTypeImageDescriptor(true, false, Flags.AccPrivate, false));
        RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, cu, refactoring, IProposalRelevance.CONVERT_ANONYMOUS_TO_NESTED, image);
        proposal.setLinkedProposalModel(linkedProposalModel);
        proposal.setCommandId(CONVERT_ANONYMOUS_TO_LOCAL_ID);
        proposals.add(proposal);
    }
    return false;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ConvertAnonymousToNestedRefactoring(org.eclipse.jdt.internal.corext.refactoring.code.ConvertAnonymousToNestedRefactoring) LinkedProposalModel(org.eclipse.jdt.internal.corext.fix.LinkedProposalModel) Image(org.eclipse.swt.graphics.Image) RefactoringCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.RefactoringCorrectionProposal) IType(org.eclipse.jdt.core.IType)

Aggregations

NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IType (org.eclipse.jdt.core.IType)1 LinkedProposalModel (org.eclipse.jdt.internal.corext.fix.LinkedProposalModel)1 ConvertAnonymousToNestedRefactoring (org.eclipse.jdt.internal.corext.refactoring.code.ConvertAnonymousToNestedRefactoring)1 RefactoringCorrectionProposal (org.eclipse.jdt.internal.ui.text.correction.proposals.RefactoringCorrectionProposal)1 Image (org.eclipse.swt.graphics.Image)1