use of org.eclipse.jdt.core.dom.IBinding in project che by eclipse.
the class ModifierCorrectionSubProcessor method addNeedToEmulateProposal.
public static void addNeedToEmulateProposal(IInvocationContext context, IProblemLocation problem, Collection<ModifierChangeCorrectionProposal> proposals) {
ICompilationUnit cu = context.getCompilationUnit();
ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
if (!(selectedNode instanceof SimpleName)) {
return;
}
IBinding binding = ((SimpleName) selectedNode).resolveBinding();
if (binding instanceof IVariableBinding) {
binding = ((IVariableBinding) binding).getVariableDeclaration();
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
String label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertofinal_description, BasicElementLabels.getJavaElementName(binding.getName()));
proposals.add(new ModifierChangeCorrectionProposal(label, cu, binding, selectedNode, Modifier.FINAL, 0, IProposalRelevance.CHANGE_MODIFIER_OF_VARIABLE_TO_FINAL, image));
}
}
use of org.eclipse.jdt.core.dom.IBinding in project che by eclipse.
the class CastCorrectionProposal method getNewCastTypeNode.
private Type getNewCastTypeNode(ASTRewrite rewrite, ImportRewrite importRewrite) {
AST ast = rewrite.getAST();
ImportRewriteContext context = new ContextSensitiveImportRewriteContext((CompilationUnit) fNodeToCast.getRoot(), fNodeToCast.getStartPosition(), importRewrite);
if (fCastType != null) {
return importRewrite.addImport(fCastType, ast, context);
}
ASTNode node = fNodeToCast;
ASTNode parent = node.getParent();
if (parent instanceof CastExpression) {
node = parent;
parent = parent.getParent();
}
while (parent instanceof ParenthesizedExpression) {
node = parent;
parent = parent.getParent();
}
if (parent instanceof MethodInvocation) {
MethodInvocation invocation = (MethodInvocation) node.getParent();
if (invocation.getExpression() == node) {
IBinding targetContext = ASTResolving.getParentMethodOrTypeBinding(node);
ITypeBinding[] bindings = ASTResolving.getQualifierGuess(node.getRoot(), invocation.getName().getIdentifier(), invocation.arguments(), targetContext);
if (bindings.length > 0) {
ITypeBinding first = getCastFavorite(bindings, fNodeToCast.resolveTypeBinding());
Type newTypeNode = importRewrite.addImport(first, ast, context);
//$NON-NLS-1$
addLinkedPosition(rewrite.track(newTypeNode), true, "casttype");
for (int i = 0; i < bindings.length; i++) {
//$NON-NLS-1$
addLinkedPositionProposal("casttype", bindings[i]);
}
return newTypeNode;
}
}
}
//$NON-NLS-1$
Type newCastType = ast.newSimpleType(ast.newSimpleName("Object"));
//$NON-NLS-1$
addLinkedPosition(rewrite.track(newCastType), true, "casttype");
return newCastType;
}
use of org.eclipse.jdt.core.dom.IBinding in project che by eclipse.
the class ChangeMethodSignatureProposal method modifyParameters.
private void modifyParameters(ASTRewrite rewrite, MethodDeclaration methodDecl) {
AST ast = methodDecl.getAST();
ArrayList<String> usedNames = new ArrayList<String>();
boolean hasCreatedVariables = false;
IVariableBinding[] declaredFields = fSenderBinding.getDeclaringClass().getDeclaredFields();
for (int i = 0; i < declaredFields.length; i++) {
// avoid to take parameter names that are equal to field names
usedNames.add(declaredFields[i].getName());
}
ImportRewrite imports = getImportRewrite();
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(methodDecl, imports);
ListRewrite listRewrite = rewrite.getListRewrite(methodDecl, MethodDeclaration.PARAMETERS_PROPERTY);
// old parameters
List<SingleVariableDeclaration> parameters = methodDecl.parameters();
// index over the oldParameters
int k = 0;
for (int i = 0; i < fParameterChanges.length; i++) {
ChangeDescription curr = fParameterChanges[i];
if (curr == null) {
SingleVariableDeclaration oldParam = parameters.get(k);
usedNames.add(oldParam.getName().getIdentifier());
k++;
} else if (curr instanceof InsertDescription) {
InsertDescription desc = (InsertDescription) curr;
SingleVariableDeclaration newNode = ast.newSingleVariableDeclaration();
newNode.setType(imports.addImport(desc.type, ast, context));
//$NON-NLS-1$
newNode.setName(ast.newSimpleName("x"));
// remember to set name later
desc.resultingParamName = new SimpleName[] { newNode.getName() };
desc.resultingParamType = newNode.getType();
hasCreatedVariables = true;
listRewrite.insertAt(newNode, i, null);
Javadoc javadoc = methodDecl.getJavadoc();
if (javadoc != null) {
TagElement newTagElement = ast.newTagElement();
newTagElement.setTagName(TagElement.TAG_PARAM);
//$NON-NLS-1$
SimpleName arg = ast.newSimpleName("x");
newTagElement.fragments().add(arg);
//$NON-NLS-1$
insertTabStop(rewrite, newTagElement.fragments(), "param_tagcomment" + i);
insertParamTag(rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY), parameters, k, newTagElement);
// set the name later
desc.resultingTagArg = arg;
} else {
desc.resultingTagArg = null;
}
} else if (curr instanceof RemoveDescription) {
SingleVariableDeclaration decl = parameters.get(k);
listRewrite.remove(decl, null);
k++;
TagElement tagNode = findParamTag(methodDecl, decl);
if (tagNode != null) {
rewrite.remove(tagNode, null);
}
} else if (curr instanceof EditDescription) {
EditDescription desc = (EditDescription) curr;
ITypeBinding newTypeBinding = desc.type;
SingleVariableDeclaration decl = parameters.get(k);
if (k == parameters.size() - 1 && i == fParameterChanges.length - 1 && decl.isVarargs() && newTypeBinding.isArray()) {
// stick with varargs if it was before
newTypeBinding = newTypeBinding.getElementType();
} else {
rewrite.set(decl, SingleVariableDeclaration.VARARGS_PROPERTY, Boolean.FALSE, null);
}
Type newType = imports.addImport(newTypeBinding, ast, context);
rewrite.replace(decl.getType(), newType, null);
DimensionRewrite.removeAllChildren(decl, SingleVariableDeclaration.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
IBinding binding = decl.getName().resolveBinding();
if (binding != null) {
SimpleName[] names = LinkedNodeFinder.findByBinding(decl.getRoot(), binding);
SimpleName[] newNames = new SimpleName[names.length];
for (int j = 0; j < names.length; j++) {
//$NON-NLS-1$ // name will be set later
SimpleName newName = ast.newSimpleName("x");
newNames[j] = newName;
rewrite.replace(names[j], newName, null);
}
desc.resultingParamName = newNames;
} else {
//$NON-NLS-1$ // name will be set later
SimpleName newName = ast.newSimpleName("x");
rewrite.replace(decl.getName(), newName, null);
// remember to set name later
desc.resultingParamName = new SimpleName[] { newName };
}
desc.resultingParamType = newType;
desc.orginalName = decl.getName().getIdentifier();
hasCreatedVariables = true;
k++;
TagElement tagNode = findParamTag(methodDecl, decl);
if (tagNode != null) {
List<? extends ASTNode> fragments = tagNode.fragments();
if (!fragments.isEmpty()) {
//$NON-NLS-1$
SimpleName arg = ast.newSimpleName("x");
rewrite.replace(fragments.get(0), arg, null);
desc.resultingTagArg = arg;
}
}
} else if (curr instanceof SwapDescription) {
SingleVariableDeclaration decl1 = parameters.get(k);
SingleVariableDeclaration decl2 = parameters.get(((SwapDescription) curr).index);
rewrite.replace(decl1, rewrite.createCopyTarget(decl2), null);
rewrite.replace(decl2, rewrite.createCopyTarget(decl1), null);
usedNames.add(decl1.getName().getIdentifier());
k++;
TagElement tagNode1 = findParamTag(methodDecl, decl1);
TagElement tagNode2 = findParamTag(methodDecl, decl2);
if (tagNode1 != null && tagNode2 != null) {
rewrite.replace(tagNode1, rewrite.createCopyTarget(tagNode2), null);
rewrite.replace(tagNode2, rewrite.createCopyTarget(tagNode1), null);
}
}
}
if (!hasCreatedVariables) {
return;
}
if (methodDecl.getBody() != null) {
// avoid take a name of a local variable inside
CompilationUnit root = (CompilationUnit) methodDecl.getRoot();
IBinding[] bindings = (new ScopeAnalyzer(root)).getDeclarationsAfter(methodDecl.getBody().getStartPosition(), ScopeAnalyzer.VARIABLES);
for (int i = 0; i < bindings.length; i++) {
usedNames.add(bindings[i].getName());
}
}
fixupNames(rewrite, usedNames);
}
use of org.eclipse.jdt.core.dom.IBinding in project che by eclipse.
the class AddArgumentCorrectionProposal method evaluateArgumentExpressions.
private Expression evaluateArgumentExpressions(AST ast, ITypeBinding requiredType, String key) {
CompilationUnit root = (CompilationUnit) fCallerNode.getRoot();
int offset = fCallerNode.getStartPosition();
Expression best = null;
ITypeBinding bestType = null;
ScopeAnalyzer analyzer = new ScopeAnalyzer(root);
IBinding[] bindings = analyzer.getDeclarationsInScope(offset, ScopeAnalyzer.VARIABLES);
for (int i = 0; i < bindings.length; i++) {
IVariableBinding curr = (IVariableBinding) bindings[i];
ITypeBinding type = curr.getType();
if (type != null && canAssign(type, requiredType) && testModifier(curr)) {
if (best == null || isMoreSpecific(bestType, type)) {
best = ast.newSimpleName(curr.getName());
bestType = type;
}
addLinkedPositionProposal(key, curr.getName(), null);
}
}
Expression defaultExpression = ASTNodeFactory.newDefaultExpression(ast, requiredType);
if (best == null) {
best = defaultExpression;
}
addLinkedPositionProposal(key, ASTNodes.asString(defaultExpression), null);
return best;
}
use of org.eclipse.jdt.core.dom.IBinding in project che by eclipse.
the class ModifierCorrectionSubProcessor method addAddMethodModifierProposal.
private static void addAddMethodModifierProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals, int modifier, String label) {
ICompilationUnit cu = context.getCompilationUnit();
ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
if (!(selectedNode instanceof MethodDeclaration)) {
return;
}
IBinding binding = ((MethodDeclaration) selectedNode).resolveBinding();
if (binding instanceof IMethodBinding) {
binding = ((IMethodBinding) binding).getMethodDeclaration();
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
proposals.add(new ModifierChangeCorrectionProposal(label, cu, binding, selectedNode, modifier, 0, IProposalRelevance.ADD_METHOD_MODIFIER, image));
}
}
Aggregations