use of org.eclipse.jdt.core.dom.rewrite.ImportRewrite in project che by eclipse.
the class UnresolvedElementsSubProcessor method addQualifierToOuterProposal.
private static void addQualifierToOuterProposal(IInvocationContext context, MethodInvocation invocationNode, IMethodBinding binding, Collection<ICommandAccess> proposals) {
ITypeBinding declaringType = binding.getDeclaringClass();
ITypeBinding parentType = Bindings.getBindingOfParentType(invocationNode);
ITypeBinding currType = parentType;
boolean isInstanceMethod = !Modifier.isStatic(binding.getModifiers());
while (currType != null && !Bindings.isSuperType(declaringType, currType)) {
if (isInstanceMethod && Modifier.isStatic(currType.getModifiers())) {
return;
}
currType = currType.getDeclaringClass();
}
if (currType == null || currType == parentType) {
return;
}
ASTRewrite rewrite = ASTRewrite.create(invocationNode.getAST());
String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changetoouter_description, ASTResolving.getTypeSignature(currType));
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.QUALIFY_WITH_ENCLOSING_TYPE, image);
ImportRewrite imports = proposal.createImportRewrite(context.getASTRoot());
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(invocationNode, imports);
AST ast = invocationNode.getAST();
String qualifier = imports.addImport(currType, importRewriteContext);
Name name = ASTNodeFactory.newName(ast, qualifier);
Expression newExpression;
if (isInstanceMethod) {
ThisExpression expr = ast.newThisExpression();
expr.setQualifier(name);
newExpression = expr;
} else {
newExpression = name;
}
rewrite.set(invocationNode, MethodInvocation.EXPRESSION_PROPERTY, newExpression, null);
proposals.add(proposal);
}
use of org.eclipse.jdt.core.dom.rewrite.ImportRewrite in project che by eclipse.
the class UnresolvedElementsSubProcessor method createTypeRefChangeProposal.
private static CUCorrectionProposal createTypeRefChangeProposal(ICompilationUnit cu, String fullName, Name node, int relevance, int maxProposals) {
ImportRewrite importRewrite = null;
String simpleName = fullName;
String packName = Signature.getQualifier(fullName);
if (packName.length() > 0) {
// no imports for primitive types, type variables
importRewrite = StubUtility.createImportRewrite((CompilationUnit) node.getRoot(), true);
// can be null in package-info.java
BodyDeclaration scope = ASTResolving.findParentBodyDeclaration(node);
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(scope != null ? scope : node, importRewrite);
simpleName = importRewrite.addImport(fullName, context);
}
if (!isLikelyTypeName(simpleName)) {
relevance -= 2;
}
ASTRewriteCorrectionProposal proposal;
if (importRewrite != null && node.isSimpleName() && simpleName.equals(((SimpleName) node).getIdentifier())) {
// import only
// import only
String[] arg = { BasicElementLabels.getJavaElementName(simpleName), BasicElementLabels.getJavaElementName(packName) };
String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_importtype_description, arg);
Image image = JavaPluginImages.get(JavaPluginImages.IMG_OBJS_IMPDECL);
int boost = QualifiedTypeNameHistory.getBoost(fullName, 0, maxProposals);
proposal = new AddImportCorrectionProposal(label, cu, relevance + 100 + boost, image, packName, simpleName, (SimpleName) node);
proposal.setCommandId(ADD_IMPORT_ID);
} else {
String label;
if (packName.length() == 0) {
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changetype_nopack_description, BasicElementLabels.getJavaElementName(simpleName));
} else {
String[] arg = { BasicElementLabels.getJavaElementName(simpleName), BasicElementLabels.getJavaElementName(packName) };
label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changetype_description, arg);
}
ASTRewrite rewrite = ASTRewrite.create(node.getAST());
rewrite.replace(node, rewrite.createStringPlaceholder(simpleName, ASTNode.SIMPLE_TYPE), null);
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, relevance, image);
}
if (importRewrite != null) {
proposal.setImportRewrite(importRewrite);
}
return proposal;
}
use of org.eclipse.jdt.core.dom.rewrite.ImportRewrite in project che by eclipse.
the class UnresolvedElementsSubProcessor method getAmbiguosTypeReferenceProposals.
public static void getAmbiguosTypeReferenceProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException {
final ICompilationUnit cu = context.getCompilationUnit();
int offset = problem.getOffset();
int len = problem.getLength();
IJavaElement[] elements = cu.codeSelect(offset, len);
for (int i = 0; i < elements.length; i++) {
IJavaElement curr = elements[i];
if (curr instanceof IType && !TypeFilter.isFiltered((IType) curr)) {
String qualifiedTypeName = ((IType) curr).getFullyQualifiedName('.');
CompilationUnit root = context.getASTRoot();
String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_importexplicit_description, BasicElementLabels.getJavaElementName(qualifiedTypeName));
Image image = JavaPluginImages.get(JavaPluginImages.IMG_OBJS_IMPDECL);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, ASTRewrite.create(root.getAST()), IProposalRelevance.IMPORT_EXPLICIT, image);
ImportRewrite imports = proposal.createImportRewrite(root);
imports.addImport(qualifiedTypeName);
proposals.add(proposal);
}
}
}
use of org.eclipse.jdt.core.dom.rewrite.ImportRewrite in project che by eclipse.
the class QuickAssistProcessor method getArrayInitializerToArrayCreation.
private static boolean getArrayInitializerToArrayCreation(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
if (!(node instanceof ArrayInitializer)) {
return false;
}
ArrayInitializer initializer = (ArrayInitializer) node;
ASTNode parent = initializer.getParent();
while (parent instanceof ArrayInitializer) {
initializer = (ArrayInitializer) parent;
parent = parent.getParent();
}
ITypeBinding typeBinding = initializer.resolveTypeBinding();
if (!(parent instanceof VariableDeclaration) || typeBinding == null || !typeBinding.isArray()) {
return false;
}
if (resultingCollections == null) {
return true;
}
AST ast = node.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
String label = CorrectionMessages.QuickAssistProcessor_typetoarrayInitializer_description;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.ADD_TYPE_TO_ARRAY_INITIALIZER, image);
ImportRewrite imports = proposal.createImportRewrite(context.getASTRoot());
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(node, imports);
String typeName = imports.addImport(typeBinding, importRewriteContext);
ArrayCreation creation = ast.newArrayCreation();
creation.setInitializer((ArrayInitializer) rewrite.createMoveTarget(initializer));
creation.setType((ArrayType) ASTNodeFactory.newType(ast, typeName));
rewrite.replace(initializer, creation, null);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.core.dom.rewrite.ImportRewrite in project che by eclipse.
the class CastCorrectionProposal method getRewrite.
@Override
protected ASTRewrite getRewrite() throws CoreException {
AST ast = fNodeToCast.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
ImportRewrite importRewrite = createImportRewrite((CompilationUnit) fNodeToCast.getRoot());
Type newTypeNode = getNewCastTypeNode(rewrite, importRewrite);
if (fNodeToCast.getNodeType() == ASTNode.CAST_EXPRESSION) {
CastExpression expression = (CastExpression) fNodeToCast;
rewrite.replace(expression.getType(), newTypeNode, null);
} else {
Expression expressionCopy = (Expression) rewrite.createCopyTarget(fNodeToCast);
if (needsInnerParantheses(fNodeToCast)) {
ParenthesizedExpression parenthesizedExpression = ast.newParenthesizedExpression();
parenthesizedExpression.setExpression(expressionCopy);
expressionCopy = parenthesizedExpression;
}
CastExpression castExpression = ast.newCastExpression();
castExpression.setExpression(expressionCopy);
castExpression.setType(newTypeNode);
ASTNode replacingNode = castExpression;
if (needsOuterParantheses(fNodeToCast)) {
ParenthesizedExpression parenthesizedExpression = ast.newParenthesizedExpression();
parenthesizedExpression.setExpression(castExpression);
replacingNode = parenthesizedExpression;
}
rewrite.replace(fNodeToCast, replacingNode, null);
}
return rewrite;
}
Aggregations