use of org.eclipse.jdt.core.IMethod in project che by eclipse.
the class IntroduceIndirectionRefactoring method updateTargetVisibility.
private RefactoringStatus updateTargetVisibility(IProgressMonitor monitor) throws JavaModelException, CoreException {
RefactoringStatus result = new RefactoringStatus();
// Adjust the visibility of the method and of the referenced type. Note that
// the target method may not be in the target type; and in this case, the type
// of the target method does not need a visibility adjustment.
// This method is called after all other changes have been
// created. Changes induced by this method will be attached to those changes.
result.merge(adjustVisibility((IType) fIntermediaryFirstParameterType.getJavaElement(), fIntermediaryType, monitor));
if (result.hasError())
// binary
return result;
ModifierKeyword neededVisibility = getNeededVisibility(fTargetMethod, fIntermediaryType);
if (neededVisibility != null) {
result.merge(adjustVisibility(fTargetMethod, neededVisibility, monitor));
if (result.hasError())
// binary
return result;
// Need to adjust the overridden methods of the target method.
ITypeHierarchy hierarchy = fTargetMethod.getDeclaringType().newTypeHierarchy(null);
MethodOverrideTester tester = new MethodOverrideTester(fTargetMethod.getDeclaringType(), hierarchy);
IType[] subtypes = hierarchy.getAllSubtypes(fTargetMethod.getDeclaringType());
for (int i = 0; i < subtypes.length; i++) {
IMethod method = tester.findOverridingMethodInType(subtypes[i], fTargetMethod);
if (method != null && method.exists()) {
result.merge(adjustVisibility(method, neededVisibility, monitor));
if (monitor.isCanceled())
throw new OperationCanceledException();
if (result.hasError())
// binary
return result;
}
}
}
return result;
}
use of org.eclipse.jdt.core.IMethod in project che by eclipse.
the class IntroduceFactoryRefactoring method searchForCallsTo.
/**
* Search for all calls to the given <code>IMethodBinding</code> in the project
* that contains the compilation unit <code>fCUHandle</code>.
* @param methodBinding
* @param pm
* @param status
* @return an array of <code>SearchResultGroup</code>'s that identify the search matches
* @throws JavaModelException
*/
private SearchResultGroup[] searchForCallsTo(IMethodBinding methodBinding, IProgressMonitor pm, RefactoringStatus status) throws JavaModelException {
IMethod method = (IMethod) methodBinding.getJavaElement();
final RefactoringSearchEngine2 engine = new RefactoringSearchEngine2(createSearchPattern(method, methodBinding));
engine.setFiltering(true, true);
engine.setScope(createSearchScope(method, methodBinding));
engine.setStatus(status);
engine.searchPattern(new SubProgressMonitor(pm, 1));
return (SearchResultGroup[]) engine.getResults();
}
use of org.eclipse.jdt.core.IMethod in project che by eclipse.
the class InlineMethodRefactoring method checkOverridden.
private void checkOverridden(RefactoringStatus status, IProgressMonitor pm) throws JavaModelException {
//$NON-NLS-1$
pm.beginTask("", 9);
pm.setTaskName(RefactoringCoreMessages.InlineMethodRefactoring_checking_overridden);
MethodDeclaration decl = fSourceProvider.getDeclaration();
IMethod method = (IMethod) decl.resolveBinding().getJavaElement();
if (method == null || Flags.isPrivate(method.getFlags())) {
pm.worked(8);
return;
}
IType type = method.getDeclaringType();
ITypeHierarchy hierarchy = type.newTypeHierarchy(new SubProgressMonitor(pm, 6));
checkSubTypes(status, method, hierarchy.getAllSubtypes(type), new SubProgressMonitor(pm, 1));
checkSuperClasses(status, method, hierarchy.getAllSuperclasses(type), new SubProgressMonitor(pm, 1));
checkSuperInterfaces(status, method, hierarchy.getAllSuperInterfaces(type), new SubProgressMonitor(pm, 1));
//$NON-NLS-1$
pm.setTaskName("");
}
use of org.eclipse.jdt.core.IMethod 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.IMethod in project che by eclipse.
the class IntroduceIndirectionRefactoring method updateReferences.
private RefactoringStatus updateReferences(IProgressMonitor monitor) throws CoreException {
RefactoringStatus result = new RefactoringStatus();
//$NON-NLS-1$
monitor.beginTask("", 90);
if (monitor.isCanceled())
throw new OperationCanceledException();
IMethod[] ripple = RippleMethodFinder2.getRelatedMethods(fTargetMethod, false, new NoOverrideProgressMonitor(monitor, 10), null);
if (monitor.isCanceled())
throw new OperationCanceledException();
SearchResultGroup[] references = Checks.excludeCompilationUnits(getReferences(ripple, new NoOverrideProgressMonitor(monitor, 10), result), result);
if (result.hasFatalError())
return result;
result.merge(Checks.checkCompileErrorsInAffectedFiles(references));
if (monitor.isCanceled())
throw new OperationCanceledException();
int ticksPerCU = references.length == 0 ? 0 : 70 / references.length;
for (int i = 0; i < references.length; i++) {
SearchResultGroup group = references[i];
SearchMatch[] searchResults = group.getSearchResults();
CompilationUnitRewrite currentCURewrite = getCachedCURewrite(group.getCompilationUnit());
for (int j = 0; j < searchResults.length; j++) {
SearchMatch match = searchResults[j];
if (match.isInsideDocComment())
continue;
IMember enclosingMember = (IMember) match.getElement();
ASTNode target = getSelectedNode(group.getCompilationUnit(), currentCURewrite.getRoot(), match.getOffset(), match.getLength());
if (target instanceof SuperMethodInvocation) {
// Cannot retarget calls to super - add a warning
result.merge(createWarningAboutCall(enclosingMember, target, RefactoringCoreMessages.IntroduceIndirectionRefactoring_call_warning_super_keyword));
continue;
}
//$NON-NLS-1$
Assert.isTrue(target instanceof MethodInvocation, "Element of call should be a MethodInvocation.");
MethodInvocation invocation = (MethodInvocation) target;
ITypeBinding typeBinding = getExpressionType(invocation);
if (fIntermediaryFirstParameterType == null) {
// no highest type yet
fIntermediaryFirstParameterType = typeBinding.getTypeDeclaration();
} else {
// check if current type is higher
result.merge(findCommonParent(typeBinding.getTypeDeclaration()));
}
if (result.hasFatalError())
return result;
// create an edit for this particular call
result.merge(updateMethodInvocation(invocation, enclosingMember, currentCURewrite));
// does call see the intermediary method?
// => increase visibility of the type of the intermediary method.
result.merge(adjustVisibility(fIntermediaryType, enclosingMember.getDeclaringType(), new NoOverrideProgressMonitor(monitor, 0)));
if (monitor.isCanceled())
throw new OperationCanceledException();
}
if (!isRewriteKept(group.getCompilationUnit()))
createChangeAndDiscardRewrite(group.getCompilationUnit());
monitor.worked(ticksPerCU);
}
monitor.done();
return result;
}
Aggregations