use of org.eclipse.jdt.core.dom.Expression in project che by eclipse.
the class CallContext method getReceiverType.
public ITypeBinding getReceiverType() {
Expression expression = Invocations.getExpression(invocation);
if (expression != null) {
return expression.resolveTypeBinding();
}
IMethodBinding method = Invocations.resolveBinding(invocation);
if (method != null) {
return method.getDeclaringClass();
}
return null;
}
use of org.eclipse.jdt.core.dom.Expression in project che by eclipse.
the class CallInliner method needsExplicitCast.
/**
* @param status the status
* @return <code>true</code> if explicit cast is needed otherwise <code>false</code>
*/
private boolean needsExplicitCast(RefactoringStatus status) {
// returned expression then we don't need an explicit cast.
if (fSourceProvider.returnTypeMatchesReturnExpressions())
return false;
List<Expression> returnExprs = fSourceProvider.getReturnExpressions();
// method invocations
if (returnExprs.size() != 1)
return false;
if (fTargetNode.getLocationInParent() == MethodInvocation.ARGUMENTS_PROPERTY) {
MethodInvocation methodInvocation = (MethodInvocation) fTargetNode.getParent();
if (methodInvocation.getExpression() == fTargetNode)
return false;
IMethodBinding method = methodInvocation.resolveMethodBinding();
if (method == null) {
status.addError(RefactoringCoreMessages.CallInliner_cast_analysis_error, JavaStatusContext.create(fCUnit, methodInvocation));
return false;
}
ITypeBinding[] parameters = method.getParameterTypes();
int argumentIndex = methodInvocation.arguments().indexOf(fInvocation);
ITypeBinding parameterType = returnExprs.get(0).resolveTypeBinding();
if (method.isVarargs() && argumentIndex >= parameters.length - 1) {
argumentIndex = parameters.length - 1;
parameterType = parameterType.createArrayType(1);
}
parameters[argumentIndex] = parameterType;
ITypeBinding type = ASTNodes.getReceiverTypeBinding(methodInvocation);
TypeBindingVisitor visitor = new AmbiguousMethodAnalyzer(fTypeEnvironment, method, fTypeEnvironment.create(parameters));
if (!visitor.visit(type)) {
return true;
} else if (type.isInterface()) {
return !Bindings.visitInterfaces(type, visitor);
} else if (Modifier.isAbstract(type.getModifiers())) {
return !Bindings.visitHierarchy(type, visitor);
} else {
// it is not needed to visit interfaces if receiver is a concrete class
return !Bindings.visitSuperclasses(type, visitor);
}
} else {
ITypeBinding explicitCast = ASTNodes.getExplicitCast(returnExprs.get(0), (Expression) fTargetNode);
return explicitCast != null;
}
}
use of org.eclipse.jdt.core.dom.Expression in project che by eclipse.
the class CallInliner method replaceCall.
private void replaceCall(RefactoringStatus status, String[] blocks, TextEditGroup textEditGroup) {
// Inline empty body
if (blocks.length == 0 && fTargetNode != null) {
if (fNeedsStatement) {
fRewrite.replace(fTargetNode, fTargetNode.getAST().newEmptyStatement(), textEditGroup);
} else {
fRewrite.remove(fTargetNode, textEditGroup);
}
} else {
ASTNode node = null;
for (int i = 0; i < blocks.length - 1; i++) {
node = fRewrite.createStringPlaceholder(blocks[i], ASTNode.RETURN_STATEMENT);
fListRewrite.insertAt(node, fInsertionIndex++, textEditGroup);
}
String block = blocks[blocks.length - 1];
// returned expression must be evaluated.
if (fContext.callMode == ASTNode.EXPRESSION_STATEMENT && fSourceProvider.hasReturnValue()) {
if (fSourceProvider.mustEvaluateReturnedExpression()) {
if (fSourceProvider.returnValueNeedsLocalVariable()) {
IMethodBinding invocation = Invocations.resolveBinding(fInvocation);
node = createLocalDeclaration(invocation.getReturnType(), fInvocationScope.createName(fSourceProvider.getMethodName(), true), (Expression) fRewrite.createStringPlaceholder(block, ASTNode.METHOD_INVOCATION));
} else {
node = fRewrite.getAST().newExpressionStatement((Expression) fRewrite.createStringPlaceholder(block, ASTNode.METHOD_INVOCATION));
}
} else {
node = null;
}
} else if (fTargetNode instanceof Expression) {
node = fRewrite.createStringPlaceholder(block, ASTNode.METHOD_INVOCATION);
// fixes bug #24941
if (needsExplicitCast(status)) {
AST ast = node.getAST();
CastExpression castExpression = ast.newCastExpression();
Type returnType = fImportRewrite.addImport(fSourceProvider.getReturnType(), ast);
castExpression.setType(returnType);
if (NecessaryParenthesesChecker.needsParentheses(fSourceProvider.getReturnExpressions().get(0), castExpression, CastExpression.EXPRESSION_PROPERTY)) {
ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
parenthesized.setExpression((Expression) node);
node = parenthesized;
}
castExpression.setExpression((Expression) node);
node = castExpression;
if (NecessaryParenthesesChecker.needsParentheses(castExpression, fTargetNode.getParent(), fTargetNode.getLocationInParent())) {
ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
parenthesized.setExpression((Expression) node);
node = parenthesized;
}
} else if (fSourceProvider.needsReturnedExpressionParenthesis(fTargetNode.getParent(), fTargetNode.getLocationInParent())) {
ParenthesizedExpression pExp = fTargetNode.getAST().newParenthesizedExpression();
pExp.setExpression((Expression) node);
node = pExp;
}
} else {
node = fRewrite.createStringPlaceholder(block, ASTNode.RETURN_STATEMENT);
}
// Now replace the target node with the source node
if (node != null) {
if (fTargetNode == null) {
fListRewrite.insertAt(node, fInsertionIndex++, textEditGroup);
} else {
fRewrite.replace(fTargetNode, node, textEditGroup);
}
} else {
if (fTargetNode != null) {
fRewrite.remove(fTargetNode, textEditGroup);
}
}
}
}
use of org.eclipse.jdt.core.dom.Expression in project che by eclipse.
the class ConvertAnonymousToNestedRefactoring method copyArguments.
private void copyArguments(CompilationUnitRewrite rewrite, ClassInstanceCreation newClassCreation) {
Iterator<Expression> iter = ((ClassInstanceCreation) fAnonymousInnerClassNode.getParent()).arguments().iterator();
if (!iter.hasNext())
return;
IMethodBinding superConstructorBinding = getSuperConstructorBinding();
ITypeBinding[] parameterTypes = superConstructorBinding.getParameterTypes();
List<Expression> arguments = newClassCreation.arguments();
ASTRewrite astRewrite = rewrite.getASTRewrite();
int last = parameterTypes.length - 1;
for (int i = 0; i < last; i++) {
arguments.add((Expression) astRewrite.createCopyTarget(iter.next()));
}
if (superConstructorBinding.isVarargs()) {
AST ast = astRewrite.getAST();
ArrayCreation arrayCreation = ast.newArrayCreation();
arrayCreation.setType((ArrayType) rewrite.getImportRewrite().addImport(parameterTypes[last], ast));
ArrayInitializer initializer = ast.newArrayInitializer();
arrayCreation.setInitializer(initializer);
arguments.add(arrayCreation);
arguments = initializer.expressions();
}
while (iter.hasNext()) {
arguments.add((Expression) astRewrite.createCopyTarget(iter.next()));
}
}
use of org.eclipse.jdt.core.dom.Expression in project che by eclipse.
the class ConvertAnonymousToNestedRefactoring method updateAndMoveBodyDeclarations.
private void updateAndMoveBodyDeclarations(CompilationUnitRewrite rewriter, IVariableBinding[] bindings, String[] fieldNames, List<BodyDeclaration> newBodyDeclarations, MethodDeclaration newConstructorDecl) {
final ASTRewrite astRewrite = rewriter.getASTRewrite();
final AST ast = astRewrite.getAST();
final boolean useThisAccess = useThisForFieldAccess();
int fieldInsertIndex = newConstructorDecl != null ? newBodyDeclarations.lastIndexOf(newConstructorDecl) : newBodyDeclarations.size();
for (Iterator<BodyDeclaration> iterator = fAnonymousInnerClassNode.bodyDeclarations().iterator(); iterator.hasNext(); ) {
BodyDeclaration body = iterator.next();
for (int i = 0; i < bindings.length; i++) {
SimpleName[] names = LinkedNodeFinder.findByBinding(body, bindings[i]);
String fieldName = fieldNames[i];
for (int k = 0; k < names.length; k++) {
SimpleName newNode = ast.newSimpleName(fieldName);
if (useThisAccess) {
FieldAccess access = ast.newFieldAccess();
access.setExpression(ast.newThisExpression());
access.setName(newNode);
astRewrite.replace(names[k], access, null);
} else {
astRewrite.replace(names[k], newNode, null);
}
addLinkedPosition(KEY_FIELD_NAME_EXT + i, newNode, astRewrite, false);
}
}
if (body instanceof Initializer || body instanceof FieldDeclaration) {
newBodyDeclarations.add(fieldInsertIndex++, (BodyDeclaration) astRewrite.createMoveTarget(body));
} else {
newBodyDeclarations.add((BodyDeclaration) astRewrite.createMoveTarget(body));
}
}
if (newConstructorDecl != null) {
// move initialization of existing fields to constructor if an outer is referenced
List<Statement> bodyStatements = newConstructorDecl.getBody().statements();
List<VariableDeclarationFragment> fieldsToInitializeInConstructor = getFieldsToInitializeInConstructor();
for (Iterator<VariableDeclarationFragment> iter = fieldsToInitializeInConstructor.iterator(); iter.hasNext(); ) {
VariableDeclarationFragment fragment = iter.next();
Expression initializer = fragment.getInitializer();
Expression replacement = (Expression) astRewrite.get(fragment, VariableDeclarationFragment.INITIALIZER_PROPERTY);
if (replacement == initializer) {
replacement = (Expression) astRewrite.createMoveTarget(initializer);
}
astRewrite.remove(initializer, null);
SimpleName fieldNameNode = ast.newSimpleName(fragment.getName().getIdentifier());
bodyStatements.add(newFieldAssignment(ast, fieldNameNode, replacement, useThisAccess));
}
}
}
Aggregations