use of org.eclipse.jdt.core.dom.ThisExpression in project flux by eclipse.
the class QuickAssistProcessor method getInvertEqualsProposal.
private static boolean getInvertEqualsProposal(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
if (!(node instanceof MethodInvocation)) {
node = node.getParent();
if (!(node instanceof MethodInvocation)) {
return false;
}
}
MethodInvocation method = (MethodInvocation) node;
String identifier = method.getName().getIdentifier();
if (!"equals".equals(identifier) && !"equalsIgnoreCase".equals(identifier)) {
// $NON-NLS-1$ //$NON-NLS-2$
return false;
}
List<Expression> arguments = method.arguments();
if (arguments.size() != 1) {
// overloaded equals w/ more than 1 argument
return false;
}
Expression right = arguments.get(0);
ITypeBinding binding = right.resolveTypeBinding();
if (binding != null && !(binding.isClass() || binding.isInterface() || binding.isEnum())) {
// overloaded equals w/ non-class/interface argument or null
return false;
}
if (resultingCollections == null) {
return true;
}
Expression left = method.getExpression();
AST ast = method.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
if (left == null) {
// equals(x) -> x.equals(this)
MethodInvocation replacement = ast.newMethodInvocation();
replacement.setName((SimpleName) rewrite.createCopyTarget(method.getName()));
replacement.arguments().add(ast.newThisExpression());
replacement.setExpression((Expression) rewrite.createCopyTarget(right));
rewrite.replace(method, replacement, null);
} else if (right instanceof ThisExpression) {
// x.equals(this) -> equals(x)
MethodInvocation replacement = ast.newMethodInvocation();
replacement.setName((SimpleName) rewrite.createCopyTarget(method.getName()));
replacement.arguments().add(rewrite.createCopyTarget(left));
rewrite.replace(method, replacement, null);
} else {
ASTNode leftExpression = left;
while (leftExpression instanceof ParenthesizedExpression) {
leftExpression = ((ParenthesizedExpression) left).getExpression();
}
rewrite.replace(right, rewrite.createCopyTarget(leftExpression), null);
if (right instanceof CastExpression || right instanceof Assignment || right instanceof ConditionalExpression || right instanceof InfixExpression) {
ParenthesizedExpression paren = ast.newParenthesizedExpression();
paren.setExpression((Expression) rewrite.createCopyTarget(right));
rewrite.replace(left, paren, null);
} else {
rewrite.replace(left, rewrite.createCopyTarget(right), null);
}
}
String label = CorrectionMessages.QuickAssistProcessor_invertequals_description;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INVERT_EQUALS);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.core.dom.ThisExpression in project AutoRefactor by JnRouvignac.
the class CFGBuilder method addVariableAccess.
/**
* @return whether the current variable access can throw an exception.
*/
private boolean addVariableAccess(final CFGBasicBlock basicBlock, final Expression node, final int flags, final ThrowerBlocks throwers) {
if (node == null) {
return false;
}
switch(node.getNodeType()) {
case ASTNode.ARRAY_ACCESS:
ArrayAccess aa = (ArrayAccess) node;
addVariableAccess(basicBlock, aa.getArray(), flags, throwers);
addVariableAccess(basicBlock, aa.getIndex(), flags, throwers);
throwers.addThrow(aa, newException(node, ArrayIndexOutOfBoundsException.class.getCanonicalName()));
return true;
case ASTNode.ARRAY_CREATION:
ArrayCreation ac = (ArrayCreation) node;
boolean acMightThrow1 = addVariableAccess(basicBlock, ac.getInitializer(), flags, throwers);
boolean acMightThrow2 = addVariableAccesses(basicBlock, ac.dimensions(), flags, throwers);
return acMightThrow1 || acMightThrow2;
case ASTNode.ARRAY_INITIALIZER:
ArrayInitializer ai = (ArrayInitializer) node;
return addVariableAccesses(basicBlock, ai.expressions(), flags, throwers);
case ASTNode.ASSIGNMENT:
Assignment a = (Assignment) node;
boolean aMightThrow1 = addVariableAccess(basicBlock, a.getLeftHandSide(), VariableAccess.WRITE, throwers);
boolean aMightThrow2 = addVariableAccess(basicBlock, a.getRightHandSide(), VariableAccess.READ, throwers);
return aMightThrow1 || aMightThrow2;
case ASTNode.BOOLEAN_LITERAL:
case ASTNode.CHARACTER_LITERAL:
case ASTNode.NULL_LITERAL:
case ASTNode.NUMBER_LITERAL:
case ASTNode.STRING_LITERAL:
case ASTNode.TYPE_LITERAL:
// Nothing to do
return false;
case ASTNode.CAST_EXPRESSION:
CastExpression cae = (CastExpression) node;
return addVariableAccess(basicBlock, cae.getExpression(), flags, throwers);
case ASTNode.CLASS_INSTANCE_CREATION:
ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) node;
addVariableAccess(basicBlock, classInstanceCreation.getExpression(), flags, throwers);
addVariableAccesses(basicBlock, classInstanceCreation.arguments(), flags, throwers);
IMethodBinding cicBinding = classInstanceCreation.resolveConstructorBinding();
if (cicBinding != null) {
ITypeBinding[] declaredThrows = cicBinding.getExceptionTypes();
throwers.addThrow(classInstanceCreation, declaredThrows);
return declaredThrows.length > 0;
}
return false;
case ASTNode.CONDITIONAL_EXPRESSION:
ConditionalExpression coe = (ConditionalExpression) node;
boolean mightThrow1 = addVariableAccess(basicBlock, coe.getExpression(), flags, throwers);
boolean mightThrow2 = addVariableAccess(basicBlock, coe.getThenExpression(), flags, throwers);
boolean mightThrow3 = addVariableAccess(basicBlock, coe.getElseExpression(), flags, throwers);
return mightThrow1 || mightThrow2 || mightThrow3;
case ASTNode.FIELD_ACCESS:
FieldAccess fa = (FieldAccess) node;
boolean mightThrow = addVariableAccess(basicBlock, fa.getExpression(), flags, throwers);
basicBlock.addVariableAccess(new VariableAccess(fa, flags));
if (is(flags, VariableAccess.READ)) {
throwers.addThrow(fa, newException(node, NullPointerException.class.getCanonicalName()));
mightThrow = true;
}
return mightThrow;
case ASTNode.INFIX_EXPRESSION:
InfixExpression infixExpression = (InfixExpression) node;
boolean ieMightThrow1 = addVariableAccess(basicBlock, infixExpression.getLeftOperand(), flags, throwers);
boolean ieMightThrow2 = addVariableAccess(basicBlock, infixExpression.getRightOperand(), flags, throwers);
return ieMightThrow1 || ieMightThrow2;
case ASTNode.INSTANCEOF_EXPRESSION:
InstanceofExpression ioe = (InstanceofExpression) node;
return addVariableAccess(basicBlock, ioe.getLeftOperand(), flags, throwers);
case ASTNode.METHOD_INVOCATION:
MethodInvocation methodInvocation = (MethodInvocation) node;
addVariableAccess(basicBlock, methodInvocation.getExpression(), flags, throwers);
addVariableAccesses(basicBlock, methodInvocation.arguments(), flags, throwers);
IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
if (methodBinding != null) {
ITypeBinding[] declaredThrows = methodBinding.getExceptionTypes();
throwers.addThrow(methodInvocation, declaredThrows);
return declaredThrows.length > 0;
}
return false;
case ASTNode.SIMPLE_NAME:
SimpleName sn = (SimpleName) node;
basicBlock.addVariableAccess(new VariableAccess(sn, flags));
if (is(flags, VariableAccess.READ)) {
throwers.addThrow(sn, newException(node, NullPointerException.class.getCanonicalName()));
return true;
}
return false;
case ASTNode.QUALIFIED_NAME:
QualifiedName qn = (QualifiedName) node;
basicBlock.addVariableAccess(new VariableAccess(qn, flags));
throwers.addThrow(qn, newException(node, NullPointerException.class.getCanonicalName()));
return true;
case ASTNode.PARENTHESIZED_EXPRESSION:
ParenthesizedExpression pe = (ParenthesizedExpression) node;
return addVariableAccess(basicBlock, pe.getExpression(), flags, throwers);
case ASTNode.POSTFIX_EXPRESSION:
PostfixExpression poe = (PostfixExpression) node;
return addVariableAccess(basicBlock, poe.getOperand(), flags, throwers);
case ASTNode.PREFIX_EXPRESSION:
PrefixExpression pre = (PrefixExpression) node;
return addVariableAccess(basicBlock, pre.getOperand(), flags, throwers);
case ASTNode.SUPER_FIELD_ACCESS:
SuperFieldAccess sfa = (SuperFieldAccess) node;
boolean sfaMightThrow1 = addVariableAccess(basicBlock, sfa.getQualifier(), flags, throwers);
boolean sfaMightThrow2 = addVariableAccess(basicBlock, sfa.getName(), flags, throwers);
return sfaMightThrow1 || sfaMightThrow2;
case ASTNode.SUPER_METHOD_INVOCATION:
SuperMethodInvocation smi = (SuperMethodInvocation) node;
addVariableAccess(basicBlock, smi.getQualifier(), flags, throwers);
addVariableAccess(basicBlock, smi.getName(), flags, throwers);
IMethodBinding sMethodBinding = smi.resolveMethodBinding();
if (sMethodBinding != null) {
ITypeBinding[] declaredThrows = sMethodBinding.getExceptionTypes();
throwers.addThrow(smi, declaredThrows);
return declaredThrows.length > 0;
}
return false;
case ASTNode.THIS_EXPRESSION:
ThisExpression te = (ThisExpression) node;
// TODO JNR remember use of "this" here
return addVariableAccess(basicBlock, te.getQualifier(), flags, throwers);
case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
return addDeclarations(basicBlock, (VariableDeclarationExpression) node, throwers);
default:
throw new NotImplementedException(node);
}
}
use of org.eclipse.jdt.core.dom.ThisExpression in project AutoRefactor by JnRouvignac.
the class ObsoleteJava7HashRatherThanEclipseJava6HashCleanUp method getField.
private SimpleName getField(final Expression expression) {
SimpleName simpleName = ASTNodes.as(expression, SimpleName.class);
if (simpleName != null) {
return simpleName;
}
FieldAccess fieldName = ASTNodes.as(expression, FieldAccess.class);
if (fieldName != null) {
ThisExpression te = ASTNodes.as(fieldName.getExpression(), ThisExpression.class);
if (te != null) {
if (te.getQualifier() == null) {
return fieldName.getName();
}
if (te.getQualifier().isSimpleName()) {
SimpleName qualifier = (SimpleName) te.getQualifier();
TypeDeclaration visitedClass = ASTNodes.getTypedAncestor(expression, TypeDeclaration.class);
if (visitedClass != null && ASTNodes.isSameVariable(visitedClass.getName(), qualifier)) {
return fieldName.getName();
}
}
}
}
SuperFieldAccess superFieldAccess = ASTNodes.as(expression, SuperFieldAccess.class);
if (superFieldAccess != null) {
if (superFieldAccess.getQualifier() == null) {
return superFieldAccess.getName();
}
if (superFieldAccess.getQualifier().isSimpleName()) {
SimpleName qualifier = (SimpleName) superFieldAccess.getQualifier();
TypeDeclaration visitedClass = ASTNodes.getTypedAncestor(expression, TypeDeclaration.class);
if (visitedClass != null && ASTNodes.isSameVariable(visitedClass.getName(), qualifier)) {
return superFieldAccess.getName();
}
}
}
return null;
}
use of org.eclipse.jdt.core.dom.ThisExpression in project che by eclipse.
the class ExtractMethodAnalyzer method endVisit.
@Override
public void endVisit(CompilationUnit node) {
RefactoringStatus status = getStatus();
superCall: {
if (status.hasFatalError())
break superCall;
if (!hasSelectedNodes()) {
ASTNode coveringNode = getLastCoveringNode();
if (coveringNode instanceof Block && coveringNode.getParent() instanceof MethodDeclaration) {
MethodDeclaration methodDecl = (MethodDeclaration) coveringNode.getParent();
Message[] messages = ASTNodes.getMessages(methodDecl, ASTNodes.NODE_ONLY);
if (messages.length > 0) {
status.addFatalError(Messages.format(RefactoringCoreMessages.ExtractMethodAnalyzer_compile_errors, BasicElementLabels.getJavaElementName(methodDecl.getName().getIdentifier())), JavaStatusContext.create(fCUnit, methodDecl));
break superCall;
}
}
status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_invalid_selection);
break superCall;
}
fEnclosingBodyDeclaration = (BodyDeclaration) ASTNodes.getParent(getFirstSelectedNode(), BodyDeclaration.class);
if (fEnclosingBodyDeclaration == null || (fEnclosingBodyDeclaration.getNodeType() != ASTNode.METHOD_DECLARATION && fEnclosingBodyDeclaration.getNodeType() != ASTNode.FIELD_DECLARATION && fEnclosingBodyDeclaration.getNodeType() != ASTNode.INITIALIZER)) {
status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_invalid_selection);
break superCall;
} else if (ASTNodes.getEnclosingType(fEnclosingBodyDeclaration) == null) {
status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_compile_errors_no_parent_binding);
break superCall;
} else if (fEnclosingBodyDeclaration.getNodeType() == ASTNode.METHOD_DECLARATION) {
fEnclosingMethodBinding = ((MethodDeclaration) fEnclosingBodyDeclaration).resolveBinding();
}
if (!isSingleExpressionOrStatementSet()) {
status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_single_expression_or_set);
break superCall;
}
if (isExpressionSelected()) {
ASTNode expression = getFirstSelectedNode();
if (expression instanceof Name) {
Name name = (Name) expression;
if (name.resolveBinding() instanceof ITypeBinding) {
status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_type_reference);
break superCall;
}
if (name.resolveBinding() instanceof IMethodBinding) {
status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_method_name_reference);
break superCall;
}
if (name.resolveBinding() instanceof IVariableBinding) {
StructuralPropertyDescriptor locationInParent = name.getLocationInParent();
if (locationInParent == QualifiedName.NAME_PROPERTY || (locationInParent == FieldAccess.NAME_PROPERTY && !(((FieldAccess) name.getParent()).getExpression() instanceof ThisExpression))) {
status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_part_of_qualified_name);
break superCall;
}
}
if (name.isSimpleName() && ((SimpleName) name).isDeclaration()) {
status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_name_in_declaration);
break superCall;
}
}
fForceStatic = ASTNodes.getParent(expression, ASTNode.SUPER_CONSTRUCTOR_INVOCATION) != null || ASTNodes.getParent(expression, ASTNode.CONSTRUCTOR_INVOCATION) != null;
}
status.merge(LocalTypeAnalyzer.perform(fEnclosingBodyDeclaration, getSelection()));
computeLastStatementSelected();
}
super.endVisit(node);
}
use of org.eclipse.jdt.core.dom.ThisExpression in project che by eclipse.
the class UnresolvedElementsSubProcessor method addQualifierToOuterProposal.
private static void addQualifierToOuterProposal(IInvocationContext context, MethodInvocation invocationNode, IMethodBinding binding, Collection<ICommandAccess> proposals) {
ITypeBinding declaringType = binding.getDeclaringClass();
ITypeBinding parentType = Bindings.getBindingOfParentType(invocationNode);
ITypeBinding currType = parentType;
boolean isInstanceMethod = !Modifier.isStatic(binding.getModifiers());
while (currType != null && !Bindings.isSuperType(declaringType, currType)) {
if (isInstanceMethod && Modifier.isStatic(currType.getModifiers())) {
return;
}
currType = currType.getDeclaringClass();
}
if (currType == null || currType == parentType) {
return;
}
ASTRewrite rewrite = ASTRewrite.create(invocationNode.getAST());
String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changetoouter_description, ASTResolving.getTypeSignature(currType));
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.QUALIFY_WITH_ENCLOSING_TYPE, image);
ImportRewrite imports = proposal.createImportRewrite(context.getASTRoot());
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(invocationNode, imports);
AST ast = invocationNode.getAST();
String qualifier = imports.addImport(currType, importRewriteContext);
Name name = ASTNodeFactory.newName(ast, qualifier);
Expression newExpression;
if (isInstanceMethod) {
ThisExpression expr = ast.newThisExpression();
expr.setQualifier(name);
newExpression = expr;
} else {
newExpression = name;
}
rewrite.set(invocationNode, MethodInvocation.EXPRESSION_PROPERTY, newExpression, null);
proposals.add(proposal);
}
Aggregations