use of org.eclipse.jdt.core.dom.SuperMethodInvocation in project che by eclipse.
the class NewMethodCorrectionProposal method getNewName.
/* (non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.proposals.AbstractMethodCorrectionProposal#getNewName(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)
*/
@Override
protected SimpleName getNewName(ASTRewrite rewrite) {
ASTNode invocationNode = getInvocationNode();
String name;
if (invocationNode instanceof MethodInvocation) {
name = ((MethodInvocation) invocationNode).getName().getIdentifier();
} else if (invocationNode instanceof SuperMethodInvocation) {
name = ((SuperMethodInvocation) invocationNode).getName().getIdentifier();
} else {
// name of the class
name = getSenderBinding().getName();
}
AST ast = rewrite.getAST();
SimpleName newNameNode = ast.newSimpleName(name);
addLinkedPosition(rewrite.track(newNameNode), false, KEY_NAME);
ASTNode invocationName = getInvocationNameNode();
if (invocationName != null && invocationName.getAST() == ast) {
// in the same CU
addLinkedPosition(rewrite.track(invocationName), true, KEY_NAME);
}
return newNameNode;
}
use of org.eclipse.jdt.core.dom.SuperMethodInvocation 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.SuperMethodInvocation 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;
}
}
use of org.eclipse.jdt.core.dom.SuperMethodInvocation in project che by eclipse.
the class InlineMethodRefactoring method setCurrentMode.
public RefactoringStatus setCurrentMode(Mode mode) throws JavaModelException {
if (fCurrentMode == mode)
return new RefactoringStatus();
Assert.isTrue(getInitialMode() == Mode.INLINE_SINGLE);
fCurrentMode = mode;
if (mode == Mode.INLINE_SINGLE) {
if (fInitialNode instanceof MethodInvocation)
fTargetProvider = TargetProvider.create((ICompilationUnit) fInitialTypeRoot, (MethodInvocation) fInitialNode);
else if (fInitialNode instanceof SuperMethodInvocation)
fTargetProvider = TargetProvider.create((ICompilationUnit) fInitialTypeRoot, (SuperMethodInvocation) fInitialNode);
else if (fInitialNode instanceof ConstructorInvocation)
fTargetProvider = TargetProvider.create((ICompilationUnit) fInitialTypeRoot, (ConstructorInvocation) fInitialNode);
else
throw new IllegalStateException(String.valueOf(fInitialNode));
} else {
fTargetProvider = TargetProvider.create(fSourceProvider.getDeclaration());
}
return fTargetProvider.checkActivation();
}
use of org.eclipse.jdt.core.dom.SuperMethodInvocation in project che by eclipse.
the class IntroduceIndirectionRefactoring method updateReferences.
private RefactoringStatus updateReferences(IProgressMonitor monitor) throws CoreException {
RefactoringStatus result = new RefactoringStatus();
//$NON-NLS-1$
monitor.beginTask("", 90);
if (monitor.isCanceled())
throw new OperationCanceledException();
IMethod[] ripple = RippleMethodFinder2.getRelatedMethods(fTargetMethod, false, new NoOverrideProgressMonitor(monitor, 10), null);
if (monitor.isCanceled())
throw new OperationCanceledException();
SearchResultGroup[] references = Checks.excludeCompilationUnits(getReferences(ripple, new NoOverrideProgressMonitor(monitor, 10), result), result);
if (result.hasFatalError())
return result;
result.merge(Checks.checkCompileErrorsInAffectedFiles(references));
if (monitor.isCanceled())
throw new OperationCanceledException();
int ticksPerCU = references.length == 0 ? 0 : 70 / references.length;
for (int i = 0; i < references.length; i++) {
SearchResultGroup group = references[i];
SearchMatch[] searchResults = group.getSearchResults();
CompilationUnitRewrite currentCURewrite = getCachedCURewrite(group.getCompilationUnit());
for (int j = 0; j < searchResults.length; j++) {
SearchMatch match = searchResults[j];
if (match.isInsideDocComment())
continue;
IMember enclosingMember = (IMember) match.getElement();
ASTNode target = getSelectedNode(group.getCompilationUnit(), currentCURewrite.getRoot(), match.getOffset(), match.getLength());
if (target instanceof SuperMethodInvocation) {
// Cannot retarget calls to super - add a warning
result.merge(createWarningAboutCall(enclosingMember, target, RefactoringCoreMessages.IntroduceIndirectionRefactoring_call_warning_super_keyword));
continue;
}
//$NON-NLS-1$
Assert.isTrue(target instanceof MethodInvocation, "Element of call should be a MethodInvocation.");
MethodInvocation invocation = (MethodInvocation) target;
ITypeBinding typeBinding = getExpressionType(invocation);
if (fIntermediaryFirstParameterType == null) {
// no highest type yet
fIntermediaryFirstParameterType = typeBinding.getTypeDeclaration();
} else {
// check if current type is higher
result.merge(findCommonParent(typeBinding.getTypeDeclaration()));
}
if (result.hasFatalError())
return result;
// create an edit for this particular call
result.merge(updateMethodInvocation(invocation, enclosingMember, currentCURewrite));
// does call see the intermediary method?
// => increase visibility of the type of the intermediary method.
result.merge(adjustVisibility(fIntermediaryType, enclosingMember.getDeclaringType(), new NoOverrideProgressMonitor(monitor, 0)));
if (monitor.isCanceled())
throw new OperationCanceledException();
}
if (!isRewriteKept(group.getCompilationUnit()))
createChangeAndDiscardRewrite(group.getCompilationUnit());
monitor.worked(ticksPerCU);
}
monitor.done();
return result;
}
Aggregations