use of org.eclipse.jdt.core.dom.MethodInvocation in project eclipse-pmd by acanda.
the class SimplifyStartsWithQuickFix method apply.
/**
* Rewrites <code>s.startsWith("a")</code> as <code>s.charAt(0) == 'a'</code>.
*/
@Override
@SuppressWarnings("unchecked")
protected boolean apply(final MethodInvocation node) {
final AST ast = node.getAST();
final MethodInvocation charAt = ast.newMethodInvocation();
charAt.setExpression(copy(node.getExpression()));
charAt.setName(ast.newSimpleName("charAt"));
charAt.arguments().add(ast.newNumberLiteral("0"));
final CharacterLiteral character = ast.newCharacterLiteral();
final StringLiteral s = (StringLiteral) node.arguments().get(0);
character.setEscapedValue(s.getEscapedValue().replace('"', '\''));
final InfixExpression eq = ast.newInfixExpression();
eq.setOperator(Operator.EQUALS);
eq.setLeftOperand(charAt);
eq.setRightOperand(character);
replace(node, eq);
return true;
}
use of org.eclipse.jdt.core.dom.MethodInvocation in project flux by eclipse.
the class ASTResolving method getPossibleReferenceBinding.
private static ITypeBinding getPossibleReferenceBinding(ASTNode node) {
ASTNode parent = node.getParent();
switch(parent.getNodeType()) {
case ASTNode.ASSIGNMENT:
Assignment assignment = (Assignment) parent;
if (node.equals(assignment.getLeftHandSide())) {
// field write access: xx= expression
return assignment.getRightHandSide().resolveTypeBinding();
}
// read access
return assignment.getLeftHandSide().resolveTypeBinding();
case ASTNode.INFIX_EXPRESSION:
InfixExpression infix = (InfixExpression) parent;
InfixExpression.Operator op = infix.getOperator();
if (op == InfixExpression.Operator.CONDITIONAL_AND || op == InfixExpression.Operator.CONDITIONAL_OR) {
//$NON-NLS-1$
return infix.getAST().resolveWellKnownType("boolean");
} else if (op == InfixExpression.Operator.LEFT_SHIFT || op == InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED || op == InfixExpression.Operator.RIGHT_SHIFT_SIGNED) {
//$NON-NLS-1$
return infix.getAST().resolveWellKnownType("int");
}
if (node.equals(infix.getLeftOperand())) {
// xx operation expression
ITypeBinding rigthHandBinding = infix.getRightOperand().resolveTypeBinding();
if (rigthHandBinding != null) {
return rigthHandBinding;
}
} else {
// expression operation xx
ITypeBinding leftHandBinding = infix.getLeftOperand().resolveTypeBinding();
if (leftHandBinding != null) {
return leftHandBinding;
}
}
if (op != InfixExpression.Operator.EQUALS && op != InfixExpression.Operator.NOT_EQUALS) {
//$NON-NLS-1$
return infix.getAST().resolveWellKnownType("int");
}
break;
case ASTNode.INSTANCEOF_EXPRESSION:
InstanceofExpression instanceofExpression = (InstanceofExpression) parent;
return instanceofExpression.getRightOperand().resolveBinding();
case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
VariableDeclarationFragment frag = (VariableDeclarationFragment) parent;
if (frag.getInitializer().equals(node)) {
return frag.getName().resolveTypeBinding();
}
break;
case ASTNode.SUPER_METHOD_INVOCATION:
SuperMethodInvocation superMethodInvocation = (SuperMethodInvocation) parent;
IMethodBinding superMethodBinding = ASTNodes.getMethodBinding(superMethodInvocation.getName());
if (superMethodBinding != null) {
return getParameterTypeBinding(node, superMethodInvocation.arguments(), superMethodBinding);
}
break;
case ASTNode.METHOD_INVOCATION:
MethodInvocation methodInvocation = (MethodInvocation) parent;
IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
if (methodBinding != null) {
return getParameterTypeBinding(node, methodInvocation.arguments(), methodBinding);
}
break;
case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
{
SuperConstructorInvocation superInvocation = (SuperConstructorInvocation) parent;
IMethodBinding superBinding = superInvocation.resolveConstructorBinding();
if (superBinding != null) {
return getParameterTypeBinding(node, superInvocation.arguments(), superBinding);
}
break;
}
case ASTNode.CONSTRUCTOR_INVOCATION:
{
ConstructorInvocation constrInvocation = (ConstructorInvocation) parent;
IMethodBinding constrBinding = constrInvocation.resolveConstructorBinding();
if (constrBinding != null) {
return getParameterTypeBinding(node, constrInvocation.arguments(), constrBinding);
}
break;
}
case ASTNode.CLASS_INSTANCE_CREATION:
{
ClassInstanceCreation creation = (ClassInstanceCreation) parent;
IMethodBinding creationBinding = creation.resolveConstructorBinding();
if (creationBinding != null) {
return getParameterTypeBinding(node, creation.arguments(), creationBinding);
}
break;
}
case ASTNode.PARENTHESIZED_EXPRESSION:
return guessBindingForReference(parent);
case ASTNode.ARRAY_ACCESS:
if (((ArrayAccess) parent).getIndex().equals(node)) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("int");
} else {
ITypeBinding parentBinding = getPossibleReferenceBinding(parent);
if (parentBinding == null) {
//$NON-NLS-1$
parentBinding = parent.getAST().resolveWellKnownType("java.lang.Object");
}
return parentBinding.createArrayType(1);
}
case ASTNode.ARRAY_CREATION:
if (((ArrayCreation) parent).dimensions().contains(node)) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("int");
}
break;
case ASTNode.ARRAY_INITIALIZER:
ASTNode initializerParent = parent.getParent();
int dim = 1;
while (initializerParent instanceof ArrayInitializer) {
initializerParent = initializerParent.getParent();
dim++;
}
Type creationType = null;
if (initializerParent instanceof ArrayCreation) {
creationType = ((ArrayCreation) initializerParent).getType();
} else if (initializerParent instanceof VariableDeclaration) {
VariableDeclaration varDecl = (VariableDeclaration) initializerParent;
creationType = ASTNodes.getType(varDecl);
dim -= varDecl.getExtraDimensions();
} else if (initializerParent instanceof MemberValuePair) {
String name = ((MemberValuePair) initializerParent).getName().getIdentifier();
IMethodBinding annotMember = findAnnotationMember((Annotation) initializerParent.getParent(), name);
if (annotMember != null) {
return getReducedDimensionBinding(annotMember.getReturnType(), dim);
}
}
if (creationType instanceof ArrayType) {
ITypeBinding creationTypeBinding = ((ArrayType) creationType).resolveBinding();
if (creationTypeBinding != null) {
return Bindings.getComponentType(creationTypeBinding, dim);
}
}
break;
case ASTNode.CONDITIONAL_EXPRESSION:
ConditionalExpression expression = (ConditionalExpression) parent;
if (node.equals(expression.getExpression())) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("boolean");
}
if (node.equals(expression.getElseExpression())) {
return expression.getThenExpression().resolveTypeBinding();
}
return expression.getElseExpression().resolveTypeBinding();
case ASTNode.POSTFIX_EXPRESSION:
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("int");
case ASTNode.PREFIX_EXPRESSION:
if (((PrefixExpression) parent).getOperator() == PrefixExpression.Operator.NOT) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("boolean");
}
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("int");
case ASTNode.IF_STATEMENT:
case ASTNode.WHILE_STATEMENT:
case ASTNode.DO_STATEMENT:
if (node instanceof Expression) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("boolean");
}
break;
case ASTNode.SWITCH_STATEMENT:
if (((SwitchStatement) parent).getExpression().equals(node)) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("int");
}
break;
case ASTNode.RETURN_STATEMENT:
MethodDeclaration decl = ASTResolving.findParentMethodDeclaration(parent);
if (decl != null && !decl.isConstructor()) {
return decl.getReturnType2().resolveBinding();
}
LambdaExpression lambdaExpr = ASTResolving.findEnclosingLambdaExpression(parent);
if (lambdaExpr != null) {
IMethodBinding lambdaMethodBinding = lambdaExpr.resolveMethodBinding();
if (lambdaMethodBinding != null && lambdaMethodBinding.getReturnType() != null) {
return lambdaMethodBinding.getReturnType();
}
}
break;
case ASTNode.CAST_EXPRESSION:
return ((CastExpression) parent).getType().resolveBinding();
case ASTNode.THROW_STATEMENT:
case ASTNode.CATCH_CLAUSE:
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("java.lang.Exception");
case ASTNode.FIELD_ACCESS:
if (node.equals(((FieldAccess) parent).getName())) {
return getPossibleReferenceBinding(parent);
}
break;
case ASTNode.SUPER_FIELD_ACCESS:
return getPossibleReferenceBinding(parent);
case ASTNode.QUALIFIED_NAME:
if (node.equals(((QualifiedName) parent).getName())) {
return getPossibleReferenceBinding(parent);
}
break;
case ASTNode.SWITCH_CASE:
if (node.equals(((SwitchCase) parent).getExpression()) && parent.getParent() instanceof SwitchStatement) {
return ((SwitchStatement) parent.getParent()).getExpression().resolveTypeBinding();
}
break;
case ASTNode.ASSERT_STATEMENT:
if (node.getLocationInParent() == AssertStatement.EXPRESSION_PROPERTY) {
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("boolean");
}
//$NON-NLS-1$
return parent.getAST().resolveWellKnownType("java.lang.String");
case ASTNode.SINGLE_MEMBER_ANNOTATION:
{
//$NON-NLS-1$
IMethodBinding annotMember = findAnnotationMember((Annotation) parent, "value");
if (annotMember != null) {
return annotMember.getReturnType();
}
break;
}
case ASTNode.MEMBER_VALUE_PAIR:
{
String name = ((MemberValuePair) parent).getName().getIdentifier();
IMethodBinding annotMember = findAnnotationMember((Annotation) parent.getParent(), name);
if (annotMember != null) {
return annotMember.getReturnType();
}
break;
}
default:
}
return null;
}
use of org.eclipse.jdt.core.dom.MethodInvocation in project flux 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));
}
}
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 flux by eclipse.
the class AdvancedQuickAssistProcessor method createSwitchCaseCondition.
private static Expression createSwitchCaseCondition(AST ast, ASTRewrite rewrite, ImportRewrite importRewrite, ImportRewriteContext importRewriteContext, Name switchExpression, SwitchCase switchCase, boolean isStringsInSwitch, boolean preserveNPE) {
Expression expression = switchCase.getExpression();
if (expression == null)
return null;
if (isStringsInSwitch) {
MethodInvocation methodInvocation = ast.newMethodInvocation();
//$NON-NLS-1$
methodInvocation.setName(ast.newSimpleName("equals"));
if (preserveNPE) {
methodInvocation.setExpression((Expression) rewrite.createStringPlaceholder(switchExpression.getFullyQualifiedName(), ASTNode.QUALIFIED_NAME));
methodInvocation.arguments().add(rewrite.createCopyTarget(expression));
} else {
methodInvocation.setExpression((Expression) rewrite.createCopyTarget(expression));
methodInvocation.arguments().add(rewrite.createStringPlaceholder(switchExpression.getFullyQualifiedName(), ASTNode.QUALIFIED_NAME));
}
return methodInvocation;
} else {
InfixExpression condition = ast.newInfixExpression();
condition.setOperator(InfixExpression.Operator.EQUALS);
condition.setLeftOperand((Expression) rewrite.createStringPlaceholder(switchExpression.getFullyQualifiedName(), ASTNode.QUALIFIED_NAME));
Expression rightExpression = null;
if (expression instanceof SimpleName && ((SimpleName) expression).resolveBinding() instanceof IVariableBinding) {
IVariableBinding binding = (IVariableBinding) ((SimpleName) expression).resolveBinding();
if (binding.isEnumConstant()) {
String qualifiedName = importRewrite.addImport(binding.getDeclaringClass(), importRewriteContext) + '.' + binding.getName();
rightExpression = ast.newName(qualifiedName);
}
}
if (rightExpression == null) {
rightExpression = (Expression) rewrite.createCopyTarget(expression);
}
condition.setRightOperand(rightExpression);
return condition;
}
}
use of org.eclipse.jdt.core.dom.MethodInvocation in project che by eclipse.
the class InlineTempRefactoring method createParameterizedInvocation.
private String createParameterizedInvocation(Expression invocation, ITypeBinding[] typeArguments, CompilationUnitRewrite cuRewrite) throws JavaModelException {
ASTRewrite rewrite = ASTRewrite.create(invocation.getAST());
ListRewrite typeArgsRewrite = Invocations.getInferredTypeArgumentsRewrite(rewrite, invocation);
for (int i = 0; i < typeArguments.length; i++) {
Type typeArgumentNode = cuRewrite.getImportRewrite().addImport(typeArguments[i], cuRewrite.getAST());
typeArgsRewrite.insertLast(typeArgumentNode, null);
}
if (invocation instanceof MethodInvocation) {
MethodInvocation methodInvocation = (MethodInvocation) invocation;
Expression expression = methodInvocation.getExpression();
if (expression == null) {
IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
if (methodBinding != null && Modifier.isStatic(methodBinding.getModifiers())) {
expression = cuRewrite.getAST().newName(cuRewrite.getImportRewrite().addImport(methodBinding.getDeclaringClass().getTypeDeclaration()));
} else {
expression = invocation.getAST().newThisExpression();
}
rewrite.set(invocation, MethodInvocation.EXPRESSION_PROPERTY, expression, null);
}
}
IDocument document = new Document(fCu.getBuffer().getContents());
final RangeMarker marker = new RangeMarker(invocation.getStartPosition(), invocation.getLength());
IJavaProject project = fCu.getJavaProject();
TextEdit[] rewriteEdits = rewrite.rewriteAST(document, project.getOptions(true)).removeChildren();
marker.addChildren(rewriteEdits);
try {
marker.apply(document, TextEdit.UPDATE_REGIONS);
String rewrittenInitializer = document.get(marker.getOffset(), marker.getLength());
IRegion region = document.getLineInformation(document.getLineOfOffset(marker.getOffset()));
int oldIndent = Strings.computeIndentUnits(document.get(region.getOffset(), region.getLength()), project);
//$NON-NLS-1$
return Strings.changeIndent(rewrittenInitializer, oldIndent, project, "", TextUtilities.getDefaultLineDelimiter(document));
} catch (MalformedTreeException e) {
JavaPlugin.log(e);
} catch (BadLocationException e) {
JavaPlugin.log(e);
}
//fallback:
return fCu.getBuffer().getText(invocation.getStartPosition(), invocation.getLength());
}
Aggregations