use of org.eclipse.jdt.internal.corext.dom.TypeBindingVisitor in project che by eclipse.
the class ASTResolving method getQualifierGuess.
/**
* Finds all type bindings that contain a method of a given signature
* @param searchRoot the ast node to start the search from
* @param selector the method name
* @param arguments the method arguments
* @param context the context in which the method would be called
* @return returns all types known in the AST that have a method with a given name
*/
public static ITypeBinding[] getQualifierGuess(ASTNode searchRoot, final String selector, List<Expression> arguments, final IBinding context) {
final int nArgs = arguments.size();
final ArrayList<ITypeBinding> result = new ArrayList<ITypeBinding>();
// test if selector is a object method
//$NON-NLS-1$
ITypeBinding binding = searchRoot.getAST().resolveWellKnownType("java.lang.Object");
IMethodBinding[] objectMethods = binding.getDeclaredMethods();
for (int i = 0; i < objectMethods.length; i++) {
IMethodBinding meth = objectMethods[i];
if (meth.getName().equals(selector) && meth.getParameterTypes().length == nArgs) {
return new ITypeBinding[] { binding };
}
}
visitAllBindings(searchRoot, new TypeBindingVisitor() {
private HashSet<String> fVisitedBindings = new HashSet<String>(100);
public boolean visit(ITypeBinding node) {
node = Bindings.normalizeTypeBinding(node);
if (node == null) {
return true;
}
if (!fVisitedBindings.add(node.getKey())) {
return true;
}
if (node.isGenericType()) {
// only look at parameterized types
return true;
}
if (context != null && !isUseableTypeInContext(node, context, false)) {
return true;
}
IMethodBinding[] methods = node.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
IMethodBinding meth = methods[i];
if (meth.getName().equals(selector) && meth.getParameterTypes().length == nArgs) {
result.add(node);
}
}
return true;
}
});
return result.toArray(new ITypeBinding[result.size()]);
}
use of org.eclipse.jdt.internal.corext.dom.TypeBindingVisitor 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.internal.corext.dom.TypeBindingVisitor in project flux by eclipse.
the class ASTResolving method getQualifierGuess.
/**
* Finds all type bindings that contain a method of a given signature
* @param searchRoot the ast node to start the search from
* @param selector the method name
* @param arguments the method arguments
* @param context the context in which the method would be called
* @return returns all types known in the AST that have a method with a given name
*/
public static ITypeBinding[] getQualifierGuess(ASTNode searchRoot, final String selector, List<Expression> arguments, final IBinding context) {
final int nArgs = arguments.size();
final ArrayList<ITypeBinding> result = new ArrayList<ITypeBinding>();
// test if selector is a object method
//$NON-NLS-1$
ITypeBinding binding = searchRoot.getAST().resolveWellKnownType("java.lang.Object");
IMethodBinding[] objectMethods = binding.getDeclaredMethods();
for (int i = 0; i < objectMethods.length; i++) {
IMethodBinding meth = objectMethods[i];
if (meth.getName().equals(selector) && meth.getParameterTypes().length == nArgs) {
return new ITypeBinding[] { binding };
}
}
visitAllBindings(searchRoot, new TypeBindingVisitor() {
private HashSet<String> fVisitedBindings = new HashSet<String>(100);
public boolean visit(ITypeBinding node) {
node = Bindings.normalizeTypeBinding(node);
if (node == null) {
return true;
}
if (!fVisitedBindings.add(node.getKey())) {
return true;
}
if (node.isGenericType()) {
// only look at parameterized types
return true;
}
if (context != null && !isUseableTypeInContext(node, context, false)) {
return true;
}
IMethodBinding[] methods = node.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
IMethodBinding meth = methods[i];
if (meth.getName().equals(selector) && meth.getParameterTypes().length == nArgs) {
result.add(node);
}
}
return true;
}
});
return result.toArray(new ITypeBinding[result.size()]);
}
Aggregations