use of org.eclipse.jdt.core.dom.MethodInvocation in project che by eclipse.
the class CodeStyleFix method addToStaticAccessOperations.
private static void addToStaticAccessOperations(CompilationUnit compilationUnit, IProblemLocation[] problems, boolean changeNonStaticAccessToStatic, boolean changeIndirectStaticAccessToDirect, List<CompilationUnitRewriteOperation> result) {
if (!changeNonStaticAccessToStatic && !changeIndirectStaticAccessToDirect)
return;
HashMap<ASTNode, Block> createdBlocks = new HashMap<ASTNode, Block>();
for (int i = 0; i < problems.length; i++) {
IProblemLocation problem = problems[i];
boolean isNonStaticAccess = changeNonStaticAccessToStatic && isNonStaticAccess(problem);
boolean isIndirectStaticAccess = changeIndirectStaticAccessToDirect && isIndirectStaticAccess(problem);
if (isNonStaticAccess || isIndirectStaticAccess) {
ToStaticAccessOperation[] nonStaticAccessInformation = createToStaticAccessOperations(compilationUnit, createdBlocks, problem, true);
if (nonStaticAccessInformation != null) {
ToStaticAccessOperation op = nonStaticAccessInformation[0];
Expression qualifier = op.fQualifier;
if (!(qualifier instanceof MethodInvocation) || !isMethodArgument(qualifier)) {
for (Iterator<CompilationUnitRewriteOperation> it = result.iterator(); it.hasNext(); ) {
// see bug 346230
CompilationUnitRewriteOperation oper = it.next();
if (oper instanceof CodeStyleFix.AddThisQualifierOperation && ((CodeStyleFix.AddThisQualifierOperation) oper).fName.equals(qualifier)) {
result.remove(oper);
break;
}
}
result.add(op);
}
}
}
}
}
use of org.eclipse.jdt.core.dom.MethodInvocation in project che by eclipse.
the class Java50Fix method getRawReference.
private static SimpleType getRawReference(MethodInvocation invocation, CompilationUnit compilationUnit) {
Name name1 = (Name) invocation.getStructuralProperty(MethodInvocation.NAME_PROPERTY);
if (name1 instanceof SimpleName) {
SimpleType rawReference = getRawReference((SimpleName) name1, compilationUnit);
if (rawReference != null) {
return rawReference;
}
}
Expression expr = (Expression) invocation.getStructuralProperty(MethodInvocation.EXPRESSION_PROPERTY);
if (expr instanceof SimpleName) {
SimpleType rawReference = getRawReference((SimpleName) expr, compilationUnit);
if (rawReference != null) {
return rawReference;
}
} else if (expr instanceof QualifiedName) {
Name name = (Name) expr;
while (name instanceof QualifiedName) {
SimpleName simpleName = (SimpleName) name.getStructuralProperty(QualifiedName.NAME_PROPERTY);
SimpleType rawReference = getRawReference(simpleName, compilationUnit);
if (rawReference != null) {
return rawReference;
}
name = (Name) name.getStructuralProperty(QualifiedName.QUALIFIER_PROPERTY);
}
if (name instanceof SimpleName) {
SimpleType rawReference = getRawReference((SimpleName) name, compilationUnit);
if (rawReference != null) {
return rawReference;
}
}
} else if (expr instanceof MethodInvocation) {
SimpleType rawReference = getRawReference((MethodInvocation) expr, compilationUnit);
if (rawReference != null) {
return rawReference;
}
}
return null;
}
use of org.eclipse.jdt.core.dom.MethodInvocation in project che by eclipse.
the class Java50Fix method createRawTypeReferenceOperations.
private static SimpleType createRawTypeReferenceOperations(CompilationUnit compilationUnit, IProblemLocation[] locations, List<CompilationUnitRewriteOperation> operations) {
if (hasFatalError(compilationUnit))
return null;
List<SimpleType> result = new ArrayList<SimpleType>();
for (int i = 0; i < locations.length; i++) {
IProblemLocation problem = locations[i];
if (isRawTypeReferenceProblem(problem.getProblemId())) {
ASTNode node = problem.getCoveredNode(compilationUnit);
if (node instanceof ClassInstanceCreation) {
Type rawReference = (Type) node.getStructuralProperty(ClassInstanceCreation.TYPE_PROPERTY);
if (isRawTypeReference(rawReference)) {
result.add((SimpleType) rawReference);
}
} else if (node instanceof SimpleName) {
ASTNode rawReference = node.getParent();
if (isRawTypeReference(rawReference)) {
ASTNode parent = rawReference.getParent();
if (!(parent instanceof ArrayType || parent instanceof ParameterizedType))
result.add((SimpleType) rawReference);
}
} else if (node instanceof MethodInvocation) {
MethodInvocation invocation = (MethodInvocation) node;
SimpleType rawReference = getRawReference(invocation, compilationUnit);
if (rawReference != null) {
result.add(rawReference);
}
}
}
}
if (result.size() == 0)
return null;
SimpleType[] types = result.toArray(new SimpleType[result.size()]);
operations.add(new AddTypeParametersOperation(types));
return types[0];
}
use of org.eclipse.jdt.core.dom.MethodInvocation in project che by eclipse.
the class NullAnnotationsRewriteOperations method createAddAnnotationOperation.
private static SignatureAnnotationRewriteOperation createAddAnnotationOperation(CompilationUnit compilationUnit, IProblemLocation problem, String annotationToAdd, String annotationToRemove, boolean changeTargetMethod, boolean thisUnitOnly, boolean allowRemove, boolean isArgumentProblem) {
ICompilationUnit cu = (ICompilationUnit) compilationUnit.getJavaElement();
if (!JavaModelUtil.is50OrHigher(cu.getJavaProject()))
return null;
ASTNode selectedNode = problem.getCoveringNode(compilationUnit);
if (selectedNode == null)
return null;
ASTNode declaringNode = getDeclaringNode(selectedNode);
switch(problem.getProblemId()) {
case IProblem.IllegalDefinitionToNonNullParameter:
// these affect another method
break;
case IProblem.IllegalReturnNullityRedefinition:
if (declaringNode == null)
declaringNode = selectedNode;
// do propose changes even if we already have an annotation
break;
default:
// if this method has annotations, don't change'em
if (!allowRemove && NullAnnotationsFix.hasExplicitNullAnnotation(cu, problem.getOffset()))
return null;
}
String annotationNameLabel = annotationToAdd;
int lastDot = annotationToAdd.lastIndexOf('.');
if (lastDot != -1)
annotationNameLabel = annotationToAdd.substring(lastDot + 1);
annotationNameLabel = BasicElementLabels.getJavaElementName(annotationNameLabel);
if (changeTargetMethod) {
MethodInvocation methodInvocation = null;
if (isArgumentProblem) {
if (selectedNode.getParent() instanceof MethodInvocation)
methodInvocation = (MethodInvocation) selectedNode.getParent();
} else {
if (selectedNode instanceof MethodInvocation)
methodInvocation = (MethodInvocation) selectedNode;
}
if (methodInvocation != null) {
// DefiniteNullToNonNullParameter || PotentialNullToNonNullParameter
int paramIdx = methodInvocation.arguments().indexOf(selectedNode);
IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
compilationUnit = findCUForMethod(compilationUnit, cu, methodBinding);
if (compilationUnit == null)
return null;
if (thisUnitOnly && !compilationUnit.getJavaElement().equals(cu))
return null;
ASTNode methodDecl = compilationUnit.findDeclaringNode(methodBinding.getKey());
if (methodDecl == null)
return null;
if (isArgumentProblem) {
String message = Messages.format(FixMessages.NullAnnotationsRewriteOperations_change_target_method_parameter_nullness, new Object[] { methodInvocation.getName(), annotationNameLabel });
return new ParameterAnnotationRewriteOperation(compilationUnit, (MethodDeclaration) methodDecl, annotationToAdd, annotationToRemove, paramIdx, allowRemove, message);
} else {
MethodDeclaration declaration = (MethodDeclaration) methodDecl;
String message = Messages.format(FixMessages.NullAnnotationsRewriteOperations_change_method_return_nullness, new String[] { declaration.getName().getIdentifier(), annotationNameLabel });
return new ReturnAnnotationRewriteOperation(compilationUnit, declaration, annotationToAdd, annotationToRemove, allowRemove, message);
}
}
} else if (declaringNode instanceof MethodDeclaration) {
// complaint is in signature of this method
MethodDeclaration declaration = (MethodDeclaration) declaringNode;
switch(problem.getProblemId()) {
case IProblem.ParameterLackingNonNullAnnotation:
case IProblem.ParameterLackingNullableAnnotation:
case IProblem.IllegalDefinitionToNonNullParameter:
case IProblem.IllegalRedefinitionToNonNullParameter:
case IProblem.SpecdNonNullLocalVariableComparisonYieldsFalse:
case IProblem.RedundantNullCheckOnSpecdNonNullLocalVariable:
// problems regarding the argument declaration:
if (declaration.getNodeType() == ASTNode.METHOD_DECLARATION) {
String paramName = findAffectedParameterName(selectedNode);
if (paramName != null) {
String message = Messages.format(FixMessages.NullAnnotationsRewriteOperations_change_method_parameter_nullness, new Object[] { paramName, annotationNameLabel });
return new ParameterAnnotationRewriteOperation(compilationUnit, declaration, annotationToAdd, annotationToRemove, paramName, allowRemove, message);
}
}
break;
case IProblem.RequiredNonNullButProvidedNull:
case IProblem.RequiredNonNullButProvidedPotentialNull:
case IProblem.RequiredNonNullButProvidedSpecdNullable:
case IProblem.RequiredNonNullButProvidedUnknown:
case IProblem.ConflictingNullAnnotations:
case IProblem.ConflictingInheritedNullAnnotations:
if (isArgumentProblem) {
// statement suggests changing parameters:
if (declaration.getNodeType() == ASTNode.METHOD_DECLARATION && selectedNode instanceof SimpleName) {
// don't call findAffectedParameterName(), in this branch we're not interested in any target method
String paramName = ((SimpleName) selectedNode).getIdentifier();
if (paramName != null) {
String message = Messages.format(FixMessages.NullAnnotationsRewriteOperations_change_method_parameter_nullness, new Object[] { paramName, annotationNameLabel });
return new ParameterAnnotationRewriteOperation(compilationUnit, declaration, annotationToAdd, annotationToRemove, paramName, allowRemove, message);
}
}
break;
}
//$FALL-THROUGH$
case IProblem.IllegalReturnNullityRedefinition:
String message = Messages.format(FixMessages.NullAnnotationsRewriteOperations_change_method_return_nullness, new String[] { declaration.getName().getIdentifier(), annotationNameLabel });
return new ReturnAnnotationRewriteOperation(compilationUnit, declaration, annotationToAdd, annotationToRemove, allowRemove, message);
}
}
return null;
}
use of org.eclipse.jdt.core.dom.MethodInvocation in project che by eclipse.
the class AccessAnalyzer method visit.
@Override
public boolean visit(Assignment node) {
Expression leftHandSide = node.getLeftHandSide();
if (!considerBinding(resolveBinding(leftHandSide), leftHandSide))
return true;
checkParent(node);
Expression rightHandSide = node.getRightHandSide();
if (!fIsFieldFinal) {
// Write access.
AST ast = node.getAST();
MethodInvocation invocation = ast.newMethodInvocation();
invocation.setName(ast.newSimpleName(fSetter));
fReferencingSetter = true;
Expression receiver = getReceiver(leftHandSide);
if (receiver != null)
invocation.setExpression((Expression) fRewriter.createCopyTarget(receiver));
List<Expression> arguments = invocation.arguments();
if (node.getOperator() == Assignment.Operator.ASSIGN) {
arguments.add((Expression) fRewriter.createCopyTarget(rightHandSide));
} else {
// This is the compound assignment case: field+= 10;
InfixExpression exp = ast.newInfixExpression();
exp.setOperator(ASTNodes.convertToInfixOperator(node.getOperator()));
MethodInvocation getter = ast.newMethodInvocation();
getter.setName(ast.newSimpleName(fGetter));
fReferencingGetter = true;
if (receiver != null)
getter.setExpression((Expression) fRewriter.createCopyTarget(receiver));
exp.setLeftOperand(getter);
Expression rhs = (Expression) fRewriter.createCopyTarget(rightHandSide);
if (NecessaryParenthesesChecker.needsParenthesesForRightOperand(rightHandSide, exp, leftHandSide.resolveTypeBinding())) {
ParenthesizedExpression p = ast.newParenthesizedExpression();
p.setExpression(rhs);
rhs = p;
}
exp.setRightOperand(rhs);
arguments.add(exp);
}
fRewriter.replace(node, invocation, createGroupDescription(WRITE_ACCESS));
}
rightHandSide.accept(this);
return false;
}
Aggregations