use of org.eclipse.jdt.core.dom.MethodInvocation in project che by eclipse.
the class SourceProvider method updateImplicitReceivers.
private void updateImplicitReceivers(ASTRewrite rewriter, CallContext context) {
if (context.receiver == null)
return;
List<Expression> implicitReceivers = fAnalyzer.getImplicitReceivers();
for (Iterator<Expression> iter = implicitReceivers.iterator(); iter.hasNext(); ) {
ASTNode node = iter.next();
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(node, context.importer);
if (node instanceof MethodInvocation) {
final MethodInvocation inv = (MethodInvocation) node;
rewriter.set(inv, MethodInvocation.EXPRESSION_PROPERTY, createReceiver(rewriter, context, (IMethodBinding) inv.getName().resolveBinding(), importRewriteContext), null);
} else if (node instanceof ClassInstanceCreation) {
final ClassInstanceCreation inst = (ClassInstanceCreation) node;
rewriter.set(inst, ClassInstanceCreation.EXPRESSION_PROPERTY, createReceiver(rewriter, context, inst.resolveConstructorBinding(), importRewriteContext), null);
} else if (node instanceof ThisExpression) {
rewriter.replace(node, rewriter.createStringPlaceholder(context.receiver, ASTNode.METHOD_INVOCATION), null);
} else if (node instanceof FieldAccess) {
final FieldAccess access = (FieldAccess) node;
rewriter.set(access, FieldAccess.EXPRESSION_PROPERTY, createReceiver(rewriter, context, access.resolveFieldBinding(), importRewriteContext), null);
} else if (node instanceof SimpleName && ((SimpleName) node).resolveBinding() instanceof IVariableBinding) {
IVariableBinding vb = (IVariableBinding) ((SimpleName) node).resolveBinding();
if (vb.isField()) {
Expression receiver = createReceiver(rewriter, context, vb, importRewriteContext);
if (receiver != null) {
FieldAccess access = node.getAST().newFieldAccess();
ASTNode target = rewriter.createMoveTarget(node);
access.setName((SimpleName) target);
access.setExpression(receiver);
rewriter.replace(node, access, null);
}
}
}
}
}
use of org.eclipse.jdt.core.dom.MethodInvocation in project che by eclipse.
the class ReplaceInvocationsRefactoring method checkInitialConditions.
@Override
public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException {
// TargetProvider must get an untampered AST with original invocation node
// SourceProvider must get a tweaked AST with method body / parameter names replaced
RefactoringStatus result = new RefactoringStatus();
if (fMethod == null) {
if (!(fSelectionTypeRoot instanceof ICompilationUnit))
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReplaceInvocationsRefactoring_cannot_replace_in_binary);
ICompilationUnit cu = (ICompilationUnit) fSelectionTypeRoot;
CompilationUnit root = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(cu, true);
fSelectionNode = getTargetNode(cu, root, fSelectionStart, fSelectionLength);
if (fSelectionNode == null)
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReplaceInvocationsRefactoring_select_method_to_apply);
if (fSelectionNode.getNodeType() == ASTNode.METHOD_DECLARATION) {
MethodDeclaration methodDeclaration = (MethodDeclaration) fSelectionNode;
fTargetProvider = TargetProvider.create(methodDeclaration);
fMethodBinding = methodDeclaration.resolveBinding();
} else {
MethodInvocation methodInvocation = (MethodInvocation) fSelectionNode;
fTargetProvider = TargetProvider.create(cu, methodInvocation);
fMethodBinding = methodInvocation.resolveMethodBinding();
}
if (fMethodBinding == null)
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineMethodRefactoring_error_noMethodDeclaration);
fMethod = (IMethod) fMethodBinding.getJavaElement();
} else {
ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
parser.setProject(fMethod.getJavaProject());
IBinding[] bindings = parser.createBindings(new IJavaElement[] { fMethod }, null);
fMethodBinding = (IMethodBinding) bindings[0];
if (fMethodBinding == null)
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineMethodRefactoring_error_noMethodDeclaration);
fTargetProvider = TargetProvider.create(fMethodBinding);
}
result.merge(fTargetProvider.checkActivation());
return result;
}
use of org.eclipse.jdt.core.dom.MethodInvocation in project che by eclipse.
the class InferTypeArgumentsConstraintCreator method endVisit.
@Override
public void endVisit(CastExpression node) {
// if (! (expressionCv instanceof CollectionElementVariable2))
// return; //TODO: returns too early when dealing with nested collections.
Type type = node.getType();
ITypeBinding typeBinding = type.resolveBinding();
if (typeBinding.isPrimitive()) {
ImmutableTypeVariable2 boxed = fTCModel.makeImmutableTypeVariable(typeBinding, node);
setConstraintVariable(node, boxed);
// avoid removing numeric conversions
return;
}
ConstraintVariable2 typeCv = getConstraintVariable(type);
if (typeCv == null)
return;
//TODO: can this be loosened when we remove casts?
setConstraintVariable(node, typeCv);
Expression expression = node.getExpression();
ConstraintVariable2 expressionCv = getConstraintVariable(expression);
//Avoid removing casts that have not been made obsolete by this refactoring:
if (expressionCv == null)
return;
if (expressionCv instanceof ImmutableTypeVariable2)
return;
if (!(expressionCv instanceof TypeVariable2 || expressionCv instanceof IndependentTypeVariable2 || expressionCv instanceof CollectionElementVariable2) && fTCModel.getElementVariables(expressionCv).size() == 0 && fTCModel.getArrayElementVariable(expressionCv) == null)
return;
fTCModel.createAssignmentElementConstraints(typeCv, expressionCv);
if (expression instanceof MethodInvocation) {
MethodInvocation invoc = (MethodInvocation) expression;
if (!isSpecialCloneInvocation(invoc.resolveMethodBinding(), invoc.getExpression())) {
fTCModel.makeCastVariable(node, expressionCv);
}
} else {
fTCModel.makeCastVariable(node, expressionCv);
}
boolean eitherIsIntf = typeBinding.isInterface() || expression.resolveTypeBinding().isInterface();
if (eitherIsIntf)
return;
//TODO: preserve up- and down-castedness!
}
use of org.eclipse.jdt.core.dom.MethodInvocation in project che by eclipse.
the class ModifierCorrectionSubProcessor method addNonAccessibleReferenceProposal.
public static void addNonAccessibleReferenceProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals, int kind, int relevance) throws CoreException {
ICompilationUnit cu = context.getCompilationUnit();
ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
if (selectedNode == null) {
return;
}
IBinding binding = null;
switch(selectedNode.getNodeType()) {
case ASTNode.SIMPLE_NAME:
binding = ((SimpleName) selectedNode).resolveBinding();
break;
case ASTNode.QUALIFIED_NAME:
binding = ((QualifiedName) selectedNode).resolveBinding();
break;
case ASTNode.SIMPLE_TYPE:
binding = ((SimpleType) selectedNode).resolveBinding();
break;
case ASTNode.NAME_QUALIFIED_TYPE:
binding = ((NameQualifiedType) selectedNode).resolveBinding();
break;
case ASTNode.METHOD_INVOCATION:
binding = ((MethodInvocation) selectedNode).getName().resolveBinding();
break;
case ASTNode.SUPER_METHOD_INVOCATION:
binding = ((SuperMethodInvocation) selectedNode).getName().resolveBinding();
break;
case ASTNode.FIELD_ACCESS:
binding = ((FieldAccess) selectedNode).getName().resolveBinding();
break;
case ASTNode.SUPER_FIELD_ACCESS:
binding = ((SuperFieldAccess) selectedNode).getName().resolveBinding();
break;
case ASTNode.CLASS_INSTANCE_CREATION:
binding = ((ClassInstanceCreation) selectedNode).resolveConstructorBinding();
break;
case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
binding = ((SuperConstructorInvocation) selectedNode).resolveConstructorBinding();
break;
default:
return;
}
ITypeBinding typeBinding = null;
String name;
IBinding bindingDecl;
boolean isLocalVar = false;
if (binding instanceof IVariableBinding && problem.getProblemId() == IProblem.NotVisibleType) {
binding = ((IVariableBinding) binding).getType();
}
if (binding instanceof IMethodBinding && problem.getProblemId() == IProblem.NotVisibleType) {
binding = ((IMethodBinding) binding).getReturnType();
}
if (binding instanceof IMethodBinding) {
IMethodBinding methodDecl = (IMethodBinding) binding;
if (methodDecl.isDefaultConstructor()) {
UnresolvedElementsSubProcessor.getConstructorProposals(context, problem, proposals);
return;
}
bindingDecl = methodDecl.getMethodDeclaration();
typeBinding = methodDecl.getDeclaringClass();
//$NON-NLS-1$
name = BasicElementLabels.getJavaElementName(methodDecl.getName() + "()");
} else if (binding instanceof IVariableBinding) {
IVariableBinding varDecl = (IVariableBinding) binding;
typeBinding = varDecl.getDeclaringClass();
name = BasicElementLabels.getJavaElementName(binding.getName());
isLocalVar = !varDecl.isField();
bindingDecl = varDecl.getVariableDeclaration();
} else if (binding instanceof ITypeBinding) {
typeBinding = (ITypeBinding) binding;
bindingDecl = typeBinding.getTypeDeclaration();
name = BasicElementLabels.getJavaElementName(binding.getName());
} else {
return;
}
if (typeBinding != null && typeBinding.isFromSource() || isLocalVar) {
int includedModifiers = 0;
int excludedModifiers = 0;
String label;
switch(kind) {
case TO_VISIBLE:
excludedModifiers = Modifier.PRIVATE | Modifier.PROTECTED | Modifier.PUBLIC;
includedModifiers = getNeededVisibility(selectedNode, typeBinding, binding);
label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changevisibility_description, new String[] { name, getVisibilityString(includedModifiers) });
break;
case TO_STATIC:
label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertostatic_description, name);
includedModifiers = Modifier.STATIC;
if (bindingDecl.getKind() == IBinding.METHOD) {
excludedModifiers = Modifier.DEFAULT | Modifier.ABSTRACT;
}
break;
case TO_NON_STATIC:
if (typeBinding != null && typeBinding.isInterface())
return;
label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertononstatic_description, name);
excludedModifiers = Modifier.STATIC;
break;
case TO_NON_PRIVATE:
int visibility;
if (cu.getParent().getElementName().equals(typeBinding.getPackage().getName())) {
visibility = Modifier.NONE;
excludedModifiers = Modifier.PRIVATE;
} else {
visibility = Modifier.PUBLIC;
includedModifiers = Modifier.PUBLIC;
excludedModifiers = Modifier.PRIVATE | Modifier.PROTECTED | Modifier.PUBLIC;
}
label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changevisibility_description, new String[] { name, getVisibilityString(visibility) });
break;
case TO_NON_FINAL:
if (typeBinding != null && typeBinding.isInterface())
return;
label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertononfinal_description, name);
excludedModifiers = Modifier.FINAL;
break;
default:
//$NON-NLS-1$
throw new IllegalArgumentException("not supported");
}
ICompilationUnit targetCU = isLocalVar ? cu : ASTResolving.findCompilationUnitForBinding(cu, context.getASTRoot(), typeBinding.getTypeDeclaration());
if (targetCU != null) {
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
proposals.add(new ModifierChangeCorrectionProposal(label, targetCU, bindingDecl, selectedNode, includedModifiers, excludedModifiers, relevance, image));
}
}
if (kind == TO_VISIBLE && bindingDecl.getKind() == IBinding.VARIABLE) {
UnresolvedElementsSubProcessor.getVariableProposals(context, problem, (IVariableBinding) bindingDecl, proposals);
}
}
use of org.eclipse.jdt.core.dom.MethodInvocation in project che by eclipse.
the class GetterSetterCorrectionSubProcessor method createMethodInvocation.
private static Expression createMethodInvocation(ProposalParameter context, IMethodBinding method, Expression argument) {
AST ast = context.astRewrite.getAST();
Expression qualifier = context.qualifier;
if (context.useSuper) {
SuperMethodInvocation invocation = ast.newSuperMethodInvocation();
invocation.setName(ast.newSimpleName(method.getName()));
if (qualifier != null)
invocation.setQualifier((Name) context.astRewrite.createCopyTarget(qualifier));
if (argument != null)
invocation.arguments().add(argument);
return invocation;
} else {
MethodInvocation invocation = ast.newMethodInvocation();
invocation.setName(ast.newSimpleName(method.getName()));
if (qualifier != null)
invocation.setExpression((Expression) context.astRewrite.createCopyTarget(qualifier));
if (argument != null)
invocation.arguments().add(argument);
return invocation;
}
}
Aggregations