use of org.eclipse.jdt.core.dom.IMethodBinding in project che by eclipse.
the class SelfEncapsulateFieldRefactoring method checkName.
private static void checkName(RefactoringStatus status, String name, List<IMethodBinding> usedNames, IType type, boolean reUseExistingField, IField field) {
if ("".equals(name)) {
//$NON-NLS-1$
status.addFatalError(RefactoringCoreMessages.Checks_Choose_name);
return;
}
boolean isStatic = false;
try {
isStatic = Flags.isStatic(field.getFlags());
} catch (JavaModelException e) {
JavaPlugin.log(e);
}
status.merge(Checks.checkMethodName(name, field));
for (Iterator<IMethodBinding> iter = usedNames.iterator(); iter.hasNext(); ) {
IMethodBinding method = iter.next();
String selector = method.getName();
if (selector.equals(name)) {
if (!reUseExistingField) {
status.addFatalError(Messages.format(RefactoringCoreMessages.SelfEncapsulateField_method_exists, new String[] { BindingLabelProvider.getBindingLabel(method, JavaElementLabels.ALL_FULLY_QUALIFIED), BasicElementLabels.getJavaElementName(type.getElementName()) }));
} else {
boolean methodIsStatic = Modifier.isStatic(method.getModifiers());
if (methodIsStatic && !isStatic)
status.addWarning(Messages.format(RefactoringCoreMessages.SelfEncapsulateFieldRefactoring_static_method_but_nonstatic_field, new String[] { BasicElementLabels.getJavaElementName(method.getName()), BasicElementLabels.getJavaElementName(field.getElementName()) }));
if (!methodIsStatic && isStatic)
status.addFatalError(Messages.format(RefactoringCoreMessages.SelfEncapsulateFieldRefactoring_nonstatic_method_but_static_field, new String[] { BasicElementLabels.getJavaElementName(method.getName()), BasicElementLabels.getJavaElementName(field.getElementName()) }));
return;
}
}
}
if (reUseExistingField)
status.addFatalError(Messages.format(RefactoringCoreMessages.SelfEncapsulateFieldRefactoring_methoddoesnotexist_status_fatalError, new String[] { BasicElementLabels.getJavaElementName(name), BasicElementLabels.getJavaElementName(type.getElementName()) }));
}
use of org.eclipse.jdt.core.dom.IMethodBinding in project che by eclipse.
the class JavadocFinder method resolveBinding.
private static IBinding resolveBinding(ASTNode node) {
if (node instanceof SimpleName) {
SimpleName simpleName = (SimpleName) node;
// workaround for https://bugs.eclipse.org/62605 (constructor name resolves to type, not method)
ASTNode normalized = ASTNodes.getNormalizedNode(simpleName);
if (normalized.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) {
ClassInstanceCreation cic = (ClassInstanceCreation) normalized.getParent();
IMethodBinding constructorBinding = cic.resolveConstructorBinding();
if (constructorBinding == null)
return null;
ITypeBinding declaringClass = constructorBinding.getDeclaringClass();
if (!declaringClass.isAnonymous())
return constructorBinding;
ITypeBinding superTypeDeclaration = declaringClass.getSuperclass().getTypeDeclaration();
return resolveSuperclassConstructor(superTypeDeclaration, constructorBinding);
}
return simpleName.resolveBinding();
} else if (node instanceof SuperConstructorInvocation) {
return ((SuperConstructorInvocation) node).resolveConstructorBinding();
} else if (node instanceof ConstructorInvocation) {
return ((ConstructorInvocation) node).resolveConstructorBinding();
} else {
return null;
}
}
use of org.eclipse.jdt.core.dom.IMethodBinding 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.IMethodBinding 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.IMethodBinding 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);
}
}
}
}
Aggregations