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;
}
Aggregations