use of org.eclipse.jdt.core.IMethod in project che by eclipse.
the class Util method findMethod.
/**
* Finds the IMethod element corresponding to the given selector,
* without creating a new dummy instance of a binary method.
* @param type the type in which the method is declared
* @param selector the method name
* @param paramTypeSignatures the type signatures of the method arguments
* @param isConstructor whether we're looking for a constructor
* @return an IMethod if found, otherwise null
* @throws JavaModelException
*/
public static IMethod findMethod(IType type, char[] selector, String[] paramTypeSignatures, boolean isConstructor) throws JavaModelException {
IMethod method = null;
int startingIndex = 0;
String[] args;
IType enclosingType = type.getDeclaringType();
// additional parameter to the constructor
if (enclosingType != null && isConstructor && !Flags.isStatic(type.getFlags())) {
args = new String[paramTypeSignatures.length + 1];
startingIndex = 1;
args[0] = Signature.createTypeSignature(enclosingType.getFullyQualifiedName(), true);
} else {
args = new String[paramTypeSignatures.length];
}
int length = args.length;
for (int i = startingIndex; i < length; i++) {
args[i] = new String(paramTypeSignatures[i - startingIndex]);
}
method = type.getMethod(new String(selector), args);
IMethod[] methods = type.findMethods(method);
if (methods != null && methods.length > 0) {
method = methods[0];
}
return method;
}
use of org.eclipse.jdt.core.IMethod in project che by eclipse.
the class Util method getUnresolvedJavaElement.
/**
* Return the java element corresponding to the given compiler binding.
*/
public static JavaElement getUnresolvedJavaElement(MethodBinding methodBinding, WorkingCopyOwner workingCopyOwner, BindingsToNodesMap bindingsToNodes) {
JavaElement unresolvedJavaElement = getUnresolvedJavaElement(methodBinding.declaringClass, workingCopyOwner, bindingsToNodes);
if (unresolvedJavaElement == null || unresolvedJavaElement.getElementType() != IJavaElement.TYPE) {
return null;
}
IType declaringType = (IType) unresolvedJavaElement;
org.eclipse.jdt.internal.compiler.ast.ASTNode node = bindingsToNodes == null ? null : bindingsToNodes.get(methodBinding);
if (node != null && !declaringType.isBinary()) {
if (node instanceof AnnotationMethodDeclaration) {
// node is an AnnotationMethodDeclaration
AnnotationMethodDeclaration typeMemberDeclaration = (AnnotationMethodDeclaration) node;
// annotation type members don't have parameters
return (JavaElement) declaringType.getMethod(String.valueOf(typeMemberDeclaration.selector), CharOperation.NO_STRINGS);
} else {
// node is an MethodDeclaration
MethodDeclaration methodDeclaration = (MethodDeclaration) node;
Argument[] arguments = methodDeclaration.arguments;
String[] parameterSignatures;
if (arguments != null) {
parameterSignatures = new String[arguments.length];
for (int i = 0; i < arguments.length; i++) {
Argument argument = arguments[i];
TypeReference typeReference = argument.type;
int arrayDim = typeReference.dimensions();
String typeSig = Signature.createTypeSignature(CharOperation.concatWith(typeReference.getTypeName(), '.'), false);
if (arrayDim > 0) {
typeSig = Signature.createArraySignature(typeSig, arrayDim);
}
parameterSignatures[i] = typeSig;
}
} else {
parameterSignatures = CharOperation.NO_STRINGS;
}
return (JavaElement) declaringType.getMethod(String.valueOf(methodDeclaration.selector), parameterSignatures);
}
} else {
// case of method not in the created AST, or a binary method
org.eclipse.jdt.internal.compiler.lookup.MethodBinding original = methodBinding.original();
String selector = original.isConstructor() ? declaringType.getElementName() : new String(original.selector);
boolean isBinary = declaringType.isBinary();
ReferenceBinding enclosingType = original.declaringClass.enclosingType();
// Static inner types' constructors don't get receivers (https://bugs.eclipse.org/bugs/show_bug.cgi?id=388137)
boolean isInnerBinaryTypeConstructor = isBinary && original.isConstructor() && !original.declaringClass.isStatic() && enclosingType != null;
TypeBinding[] parameters = original.parameters;
int length = parameters == null ? 0 : parameters.length;
int declaringIndex = isInnerBinaryTypeConstructor ? 1 : 0;
String[] parameterSignatures = new String[declaringIndex + length];
if (isInnerBinaryTypeConstructor)
parameterSignatures[0] = new String(enclosingType.genericTypeSignature()).replace('/', '.');
for (int i = 0; i < length; i++) {
char[] signature = parameters[i].genericTypeSignature();
if (isBinary) {
signature = CharOperation.replaceOnCopy(signature, '/', '.');
} else {
signature = toUnresolvedTypeSignature(signature);
}
parameterSignatures[declaringIndex + i] = new String(signature);
}
IMethod result = declaringType.getMethod(selector, parameterSignatures);
if (isBinary)
return (JavaElement) result;
if (// if perfect match (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=249567 )
result.exists())
return (JavaElement) result;
IMethod[] methods = null;
try {
methods = declaringType.getMethods();
} catch (JavaModelException e) {
// declaring type doesn't exist
return null;
}
IMethod[] candidates = Member.findMethods(result, methods);
if (candidates == null || candidates.length == 0)
return null;
return (JavaElement) candidates[0];
}
}
use of org.eclipse.jdt.core.IMethod in project che by eclipse.
the class StubUtility method getBaseNameFromLocationInParent.
private static String getBaseNameFromLocationInParent(Expression assignedExpression, List<Expression> arguments, IMethodBinding binding) {
if (binding == null)
return null;
ITypeBinding[] parameterTypes = binding.getParameterTypes();
if (// beware of guessed method bindings
parameterTypes.length != arguments.size())
return null;
int index = arguments.indexOf(assignedExpression);
if (index == -1)
return null;
ITypeBinding expressionBinding = assignedExpression.resolveTypeBinding();
if (expressionBinding != null && !expressionBinding.isAssignmentCompatible(parameterTypes[index]))
return null;
try {
IJavaElement javaElement = binding.getJavaElement();
if (javaElement instanceof IMethod) {
IMethod method = (IMethod) javaElement;
if (method.getOpenable().getBuffer() != null) {
// avoid dummy names and lookup from Javadoc
String[] parameterNames = method.getParameterNames();
if (index < parameterNames.length) {
return NamingConventions.getBaseName(NamingConventions.VK_PARAMETER, parameterNames[index], method.getJavaProject());
}
}
}
} catch (JavaModelException e) {
// ignore
}
return null;
}
use of org.eclipse.jdt.core.IMethod in project che by eclipse.
the class StubUtility method suggestArgumentNamesWithProposals.
public static String[][] suggestArgumentNamesWithProposals(IJavaProject project, IMethodBinding binding) {
int nParams = binding.getParameterTypes().length;
if (nParams > 0) {
try {
IMethod method = (IMethod) binding.getMethodDeclaration().getJavaElement();
if (method != null) {
String[] parameterNames = method.getParameterNames();
if (parameterNames.length == nParams) {
return suggestArgumentNamesWithProposals(project, parameterNames);
}
}
} catch (JavaModelException e) {
// ignore
e.printStackTrace();
}
}
String[][] names = new String[nParams][];
for (int i = 0; i < names.length; i++) {
//$NON-NLS-1$
names[i] = new String[] { "arg" + i };
}
return names;
}
use of org.eclipse.jdt.core.IMethod in project che by eclipse.
the class RenameMethodProcessor method doCheckFinalConditions.
@Override
protected RefactoringStatus doCheckFinalConditions(IProgressMonitor pm, CheckConditionsContext context) throws CoreException {
try {
RefactoringStatus result = new RefactoringStatus();
//$NON-NLS-1$
pm.beginTask("", 9);
// TODO workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=40367
if (!Checks.isAvailable(fMethod)) {
result.addFatalError(RefactoringCoreMessages.RenameMethodProcessor_is_binary, JavaStatusContext.create(fMethod));
return result;
}
result.merge(Checks.checkIfCuBroken(fMethod));
if (result.hasFatalError())
return result;
pm.setTaskName(RefactoringCoreMessages.RenameMethodRefactoring_taskName_checkingPreconditions);
result.merge(checkNewElementName(getNewElementName()));
if (result.hasFatalError())
return result;
boolean mustAnalyzeShadowing;
IMethod[] newNameMethods = searchForDeclarationsOfClashingMethods(new SubProgressMonitor(pm, 1));
if (newNameMethods.length == 0) {
mustAnalyzeShadowing = false;
pm.worked(1);
} else {
IType[] outerTypes = searchForOuterTypesOfReferences(newNameMethods, new SubProgressMonitor(pm, 1));
if (outerTypes.length > 0) {
//There exists a reference to a clashing method, where the reference is in a nested type.
//That nested type could be a type in a ripple method's hierarchy, which could
//cause the reference to bind to the new ripple method instead of to
//its old binding (a method of an enclosing scope).
//-> Getting *more* references than before -> Semantics not preserved.
//Examples: RenameVirtualMethodInClassTests#testFail39() and #testFail41()
//TODO: could pass declaringTypes to the RippleMethodFinder and check whether
//a hierarchy contains one of outerTypes (or an outer type of an outerType, recursively).
mustAnalyzeShadowing = true;
} else {
boolean hasOldRefsInInnerTypes = true;
//(recursively), whether they declare a rippleMethod
if (hasOldRefsInInnerTypes) {
//There exists a reference to a ripple method in a nested type
//of a type in the hierarchy of any ripple method.
//When that reference is renamed, and one of the supertypes of the
//nested type declared a method matching the new name, then
//the renamed reference will bind to the method in its supertype,
//since inherited methods bind stronger than methods from enclosing scopes.
//Getting *less* references than before -> Semantics not preserved.
//Examples: RenamePrivateMethodTests#testFail2(), RenamePrivateMethodTests#testFail5()
mustAnalyzeShadowing = true;
} else {
mustAnalyzeShadowing = false;
}
}
}
String binaryRefsDescription = Messages.format(RefactoringCoreMessages.ReferencesInBinaryContext_ref_in_binaries_description, BasicElementLabels.getJavaElementName(getCurrentElementName()));
ReferencesInBinaryContext binaryRefs = new ReferencesInBinaryContext(binaryRefsDescription);
initializeMethodsToRename(new SubProgressMonitor(pm, 1), binaryRefs);
pm.setTaskName(RefactoringCoreMessages.RenameMethodRefactoring_taskName_searchingForReferences);
fOccurrences = getOccurrences(new SubProgressMonitor(pm, 3), result, binaryRefs);
binaryRefs.addErrorIfNecessary(result);
pm.setTaskName(RefactoringCoreMessages.RenameMethodRefactoring_taskName_checkingPreconditions);
if (fUpdateReferences)
result.merge(checkRelatedMethods());
//removes CUs with syntax errors
result.merge(analyzeCompilationUnits());
pm.worked(1);
if (result.hasFatalError())
return result;
createChanges(new SubProgressMonitor(pm, 1), result);
if (fUpdateReferences & mustAnalyzeShadowing)
result.merge(analyzeRenameChanges(new SubProgressMonitor(pm, 1)));
else
pm.worked(1);
return result;
} finally {
pm.done();
}
}
Aggregations