use of org.eclipse.jdt.core.dom.IMethodBinding in project che by eclipse.
the class IntroduceIndirectionRefactoring method findMethodBindingInHierarchy.
// ********* SMALL HELPERS ********************
/*
* Helper method for finding an IMethod inside a binding hierarchy
*/
private IMethodBinding findMethodBindingInHierarchy(ITypeBinding currentTypeBinding, IMethod methodDeclaration) {
IMethodBinding[] bindings = currentTypeBinding.getDeclaredMethods();
for (int i = 0; i < bindings.length; i++) if (methodDeclaration.equals(bindings[i].getJavaElement()))
return bindings[i];
ITypeBinding superClass = currentTypeBinding.getSuperclass();
if (superClass != null) {
IMethodBinding b = findMethodBindingInHierarchy(superClass, methodDeclaration);
if (b != null)
return b;
}
ITypeBinding[] interfaces = currentTypeBinding.getInterfaces();
for (int i = 0; i < interfaces.length; i++) {
IMethodBinding b = findMethodBindingInHierarchy(interfaces[i], methodDeclaration);
if (b != null)
return b;
}
return null;
}
use of org.eclipse.jdt.core.dom.IMethodBinding in project che by eclipse.
the class IntroduceIndirectionRefactoring method qualifyThisExpression.
/**
* Attempts to qualify a "this" expression for a method invocation with an appropriate qualifier.
* The invoked method is analyzed according to the following specs:
*
* 'this' must be qualified iff method is declared in an enclosing type or a supertype of an enclosing type
*
* 1) The method is declared somewhere outside of the cu of the invocation
* 1a) inside a supertype of the current type
* 1b) inside a supertype of an enclosing type
* 2) The method is declared inside of the cu of the invocation
* 2a) inside the type of the invocation
* 2b) outside the type of the invocation
*
* In case of 1a) and 2b), qualify with the enclosing type.
* @param expr a {@link ThisExpression}
* @param originalInvocation the original method invocation
* @param enclosing the enclosing member of the original method invocation
* @param unitRewriter the rewrite
* @return resulting status
*
*/
private RefactoringStatus qualifyThisExpression(ThisExpression expr, MethodInvocation originalInvocation, IMember enclosing, CompilationUnitRewrite unitRewriter) {
RefactoringStatus status = new RefactoringStatus();
IMethodBinding methodBinding = originalInvocation.resolveMethodBinding();
MethodDeclaration methodDeclaration = (MethodDeclaration) ASTNodes.findDeclaration(methodBinding, originalInvocation.getRoot());
ITypeBinding currentTypeBinding = null;
if (methodDeclaration != null) {
// Case 1) : Declaring type is inside this cu => use its name if it's declared in an enclosing type
if (ASTNodes.isParent(originalInvocation, methodDeclaration.getParent()))
currentTypeBinding = methodBinding.getDeclaringClass();
else
currentTypeBinding = ASTNodes.getEnclosingType(originalInvocation);
} else {
// Case 2) : Declaring type is outside of this cu => find subclass in this cu
ASTNode currentTypeDeclaration = getEnclosingTypeDeclaration(originalInvocation);
currentTypeBinding = ASTNodes.getEnclosingType(currentTypeDeclaration);
while (currentTypeDeclaration != null && (Bindings.findMethodInHierarchy(currentTypeBinding, methodBinding.getName(), methodBinding.getParameterTypes()) == null)) {
currentTypeDeclaration = getEnclosingTypeDeclaration(currentTypeDeclaration.getParent());
currentTypeBinding = ASTNodes.getEnclosingType(currentTypeDeclaration);
}
}
if (currentTypeBinding == null) {
status.merge(createWarningAboutCall(enclosing, originalInvocation, RefactoringCoreMessages.IntroduceIndirectionRefactoring_call_warning_declaring_type_not_found));
return status;
}
currentTypeBinding = currentTypeBinding.getTypeDeclaration();
ITypeBinding typeOfCall = ASTNodes.getEnclosingType(originalInvocation);
if (!typeOfCall.equals(currentTypeBinding)) {
if (currentTypeBinding.isAnonymous()) {
// Cannot qualify, see bug 115277
status.merge(createWarningAboutCall(enclosing, originalInvocation, RefactoringCoreMessages.IntroduceIndirectionRefactoring_call_warning_anonymous_cannot_qualify));
} else {
expr.setQualifier(unitRewriter.getAST().newSimpleName(currentTypeBinding.getName()));
}
} else {
// do not qualify, only use "this.".
}
return status;
}
use of org.eclipse.jdt.core.dom.IMethodBinding in project che by eclipse.
the class ExtractMethodAnalyzer method initReturnType.
private void initReturnType(ImportRewrite rewriter) {
AST ast = fEnclosingBodyDeclaration.getAST();
fReturnType = null;
fReturnTypeBinding = null;
switch(fReturnKind) {
case ACCESS_TO_LOCAL:
VariableDeclaration declaration = ASTNodes.findVariableDeclaration(fReturnValue, fEnclosingBodyDeclaration);
fReturnType = ASTNodeFactory.newType(ast, declaration, rewriter, new ContextSensitiveImportRewriteContext(declaration, rewriter));
if (declaration.resolveBinding() != null) {
fReturnTypeBinding = declaration.resolveBinding().getType();
}
break;
case EXPRESSION:
Expression expression = (Expression) getFirstSelectedNode();
if (expression.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) {
fExpressionBinding = ((ClassInstanceCreation) expression).getType().resolveBinding();
} else {
fExpressionBinding = expression.resolveTypeBinding();
}
if (fExpressionBinding != null) {
if (fExpressionBinding.isNullType()) {
getStatus().addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_null_type, JavaStatusContext.create(fCUnit, expression));
} else {
ITypeBinding normalizedBinding = Bindings.normalizeForDeclarationUse(fExpressionBinding, ast);
if (normalizedBinding != null) {
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(fEnclosingBodyDeclaration, rewriter);
fReturnType = rewriter.addImport(normalizedBinding, ast, context);
fReturnTypeBinding = normalizedBinding;
}
}
} else {
fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
//$NON-NLS-1$
fReturnTypeBinding = ast.resolveWellKnownType("void");
getStatus().addError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_determine_return_type, JavaStatusContext.create(fCUnit, expression));
}
break;
case RETURN_STATEMENT_VALUE:
LambdaExpression enclosingLambdaExpr = ASTResolving.findEnclosingLambdaExpression(getFirstSelectedNode());
if (enclosingLambdaExpr != null) {
fReturnType = ASTNodeFactory.newReturnType(enclosingLambdaExpr, ast, rewriter, null);
IMethodBinding methodBinding = enclosingLambdaExpr.resolveMethodBinding();
fReturnTypeBinding = methodBinding != null ? methodBinding.getReturnType() : null;
} else if (fEnclosingBodyDeclaration.getNodeType() == ASTNode.METHOD_DECLARATION) {
fReturnType = ((MethodDeclaration) fEnclosingBodyDeclaration).getReturnType2();
fReturnTypeBinding = fReturnType != null ? fReturnType.resolveBinding() : null;
}
break;
default:
fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
//$NON-NLS-1$
fReturnTypeBinding = ast.resolveWellKnownType("void");
}
if (fReturnType == null) {
fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
//$NON-NLS-1$
fReturnTypeBinding = ast.resolveWellKnownType("void");
}
}
use of org.eclipse.jdt.core.dom.IMethodBinding in project che by eclipse.
the class InlineMethodRefactoring method resolveSourceProvider.
private static SourceProvider resolveSourceProvider(RefactoringStatus status, ITypeRoot typeRoot, ASTNode invocation) {
CompilationUnit root = (CompilationUnit) invocation.getRoot();
IMethodBinding methodBinding = Invocations.resolveBinding(invocation);
if (methodBinding == null) {
status.addFatalError(RefactoringCoreMessages.InlineMethodRefactoring_error_noMethodDeclaration);
return null;
}
MethodDeclaration declaration = (MethodDeclaration) root.findDeclaringNode(methodBinding);
if (declaration != null) {
return new SourceProvider(typeRoot, declaration);
}
IMethod method = (IMethod) methodBinding.getJavaElement();
if (method != null) {
CompilationUnit methodDeclarationAstRoot;
ICompilationUnit methodCu = method.getCompilationUnit();
if (methodCu != null) {
methodDeclarationAstRoot = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(methodCu, true);
} else {
IClassFile classFile = method.getClassFile();
if (!JavaElementUtil.isSourceAvailable(classFile)) {
String methodLabel = JavaElementLabels.getTextLabel(method, JavaElementLabels.M_FULLY_QUALIFIED | JavaElementLabels.M_PARAMETER_TYPES);
status.addFatalError(Messages.format(RefactoringCoreMessages.InlineMethodRefactoring_error_classFile, methodLabel));
return null;
}
methodDeclarationAstRoot = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(classFile, true);
}
ASTNode node = methodDeclarationAstRoot.findDeclaringNode(methodBinding.getMethodDeclaration().getKey());
if (node instanceof MethodDeclaration) {
return new SourceProvider(methodDeclarationAstRoot.getTypeRoot(), (MethodDeclaration) node);
}
}
status.addFatalError(RefactoringCoreMessages.InlineMethodRefactoring_error_noMethodDeclaration);
return null;
}
use of org.eclipse.jdt.core.dom.IMethodBinding in project che by eclipse.
the class Invocations method getInferredTypeArguments.
public static ITypeBinding[] getInferredTypeArguments(Expression invocation) {
IMethodBinding methodBinding;
switch(invocation.getNodeType()) {
case ASTNode.METHOD_INVOCATION:
methodBinding = ((MethodInvocation) invocation).resolveMethodBinding();
return methodBinding == null ? null : methodBinding.getTypeArguments();
case ASTNode.SUPER_METHOD_INVOCATION:
methodBinding = ((SuperMethodInvocation) invocation).resolveMethodBinding();
return methodBinding == null ? null : methodBinding.getTypeArguments();
case ASTNode.CLASS_INSTANCE_CREATION:
Type type = ((ClassInstanceCreation) invocation).getType();
ITypeBinding typeBinding = type.resolveBinding();
return typeBinding == null ? null : typeBinding.getTypeArguments();
default:
throw new IllegalArgumentException(invocation.toString());
}
}
Aggregations