use of org.eclipse.jdt.core.dom.BodyDeclaration in project che by eclipse.
the class AbstractMethodCorrectionProposal method findConstructorInsertIndex.
private int findConstructorInsertIndex(List<BodyDeclaration> decls) {
int nDecls = decls.size();
int lastMethod = 0;
for (int i = nDecls - 1; i >= 0; i--) {
BodyDeclaration curr = decls.get(i);
if (curr instanceof MethodDeclaration) {
if (((MethodDeclaration) curr).isConstructor()) {
return i + 1;
}
lastMethod = i;
}
}
return lastMethod;
}
use of org.eclipse.jdt.core.dom.BodyDeclaration in project che by eclipse.
the class ReplaceInvocationsRefactoring method checkFinalConditions.
@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException {
//$NON-NLS-1$
pm.beginTask("", 20);
fChangeManager = new TextChangeManager();
RefactoringStatus result = new RefactoringStatus();
fSourceProvider = resolveSourceProvider(fMethodBinding, result);
if (result.hasFatalError())
return result;
result.merge(fSourceProvider.checkActivation());
if (result.hasFatalError())
return result;
fSourceProvider.initialize();
fTargetProvider.initialize();
pm.setTaskName(RefactoringCoreMessages.InlineMethodRefactoring_searching);
RefactoringStatus searchStatus = new RefactoringStatus();
String binaryRefsDescription = Messages.format(RefactoringCoreMessages.ReferencesInBinaryContext_ref_in_binaries_description, BasicElementLabels.getJavaElementName(fSourceProvider.getMethodName()));
ReferencesInBinaryContext binaryRefs = new ReferencesInBinaryContext(binaryRefsDescription);
ICompilationUnit[] units = fTargetProvider.getAffectedCompilationUnits(searchStatus, binaryRefs, new SubProgressMonitor(pm, 1));
binaryRefs.addErrorIfNecessary(searchStatus);
if (searchStatus.hasFatalError()) {
result.merge(searchStatus);
return result;
}
IFile[] filesToBeModified = getFilesToBeModified(units);
result.merge(Checks.validateModifiesFiles(filesToBeModified, getValidationContext()));
if (result.hasFatalError())
return result;
result.merge(ResourceChangeChecker.checkFilesToBeChanged(filesToBeModified, new SubProgressMonitor(pm, 1)));
checkOverridden(result, new SubProgressMonitor(pm, 4));
IProgressMonitor sub = new SubProgressMonitor(pm, 15);
//$NON-NLS-1$
sub.beginTask("", units.length * 3);
for (int c = 0; c < units.length; c++) {
ICompilationUnit unit = units[c];
sub.subTask(Messages.format(RefactoringCoreMessages.InlineMethodRefactoring_processing, BasicElementLabels.getFileName(unit)));
CallInliner inliner = null;
try {
boolean added = false;
MultiTextEdit root = new MultiTextEdit();
CompilationUnitChange change = (CompilationUnitChange) fChangeManager.get(unit);
change.setEdit(root);
BodyDeclaration[] bodies = fTargetProvider.getAffectedBodyDeclarations(unit, new SubProgressMonitor(pm, 1));
if (bodies.length == 0)
continue;
inliner = new CallInliner(unit, (CompilationUnit) bodies[0].getRoot(), fSourceProvider);
for (int b = 0; b < bodies.length; b++) {
BodyDeclaration body = bodies[b];
inliner.initialize(body);
RefactoringStatus nestedInvocations = new RefactoringStatus();
ASTNode[] invocations = removeNestedCalls(nestedInvocations, unit, fTargetProvider.getInvocations(body, new SubProgressMonitor(sub, 2)));
for (int i = 0; i < invocations.length; i++) {
ASTNode invocation = invocations[i];
result.merge(inliner.initialize(invocation, fTargetProvider.getStatusSeverity()));
if (result.hasFatalError())
break;
if (result.getSeverity() < fTargetProvider.getStatusSeverity()) {
added = true;
TextEditGroup group = new TextEditGroup(RefactoringCoreMessages.InlineMethodRefactoring_edit_inline);
change.addTextEditGroup(group);
result.merge(inliner.perform(group));
}
}
// do this after we have inlined the method calls. We still want
// to generate the modifications.
result.merge(nestedInvocations);
}
if (!added) {
fChangeManager.remove(unit);
} else {
root.addChild(inliner.getModifications());
ImportRewrite rewrite = inliner.getImportEdit();
if (rewrite.hasRecordedChanges()) {
TextEdit edit = rewrite.rewriteImports(null);
if (edit instanceof MultiTextEdit ? ((MultiTextEdit) edit).getChildrenSize() > 0 : true) {
root.addChild(edit);
change.addTextEditGroup(new TextEditGroup(RefactoringCoreMessages.InlineMethodRefactoring_edit_import, new TextEdit[] { edit }));
}
}
}
} finally {
if (inliner != null)
inliner.dispose();
}
sub.worked(1);
if (sub.isCanceled())
throw new OperationCanceledException();
}
result.merge(searchStatus);
sub.done();
pm.done();
return result;
}
use of org.eclipse.jdt.core.dom.BodyDeclaration in project che by eclipse.
the class Java50Fix method createAddDeprecatedAnnotationOperations.
private static void createAddDeprecatedAnnotationOperations(CompilationUnit compilationUnit, IProblemLocation[] locations, List<CompilationUnitRewriteOperation> result) {
for (int i = 0; i < locations.length; i++) {
IProblemLocation problem = locations[i];
if (isMissingDeprecationProblem(problem.getProblemId())) {
ASTNode selectedNode = problem.getCoveringNode(compilationUnit);
if (selectedNode != null) {
ASTNode declaringNode = getDeclaringNode(selectedNode);
if (declaringNode instanceof BodyDeclaration) {
BodyDeclaration declaration = (BodyDeclaration) declaringNode;
AnnotationRewriteOperation operation = new AnnotationRewriteOperation(declaration, DEPRECATED);
result.add(operation);
}
}
}
}
}
use of org.eclipse.jdt.core.dom.BodyDeclaration in project che by eclipse.
the class LambdaExpressionsFix method isFunctionalAnonymous.
static boolean isFunctionalAnonymous(ClassInstanceCreation node) {
ITypeBinding typeBinding = node.resolveTypeBinding();
if (typeBinding == null)
return false;
ITypeBinding[] interfaces = typeBinding.getInterfaces();
if (interfaces.length != 1)
return false;
if (interfaces[0].getFunctionalInterfaceMethod() == null)
return false;
AnonymousClassDeclaration anonymTypeDecl = node.getAnonymousClassDeclaration();
if (anonymTypeDecl == null || anonymTypeDecl.resolveBinding() == null)
return false;
List<BodyDeclaration> bodyDeclarations = anonymTypeDecl.bodyDeclarations();
// cannot convert if there are fields or additional methods
if (bodyDeclarations.size() != 1)
return false;
BodyDeclaration bodyDeclaration = bodyDeclarations.get(0);
if (!(bodyDeclaration instanceof MethodDeclaration))
return false;
MethodDeclaration methodDecl = (MethodDeclaration) bodyDeclaration;
IMethodBinding methodBinding = methodDecl.resolveBinding();
if (methodBinding == null)
return false;
// generic lambda expressions are not allowed
if (methodBinding.isGenericMethod())
return false;
// lambda cannot refer to 'this'/'super' literals
if (SuperThisReferenceFinder.hasReference(methodDecl))
return false;
if (!isInTargetTypeContext(node))
return false;
return true;
}
use of org.eclipse.jdt.core.dom.BodyDeclaration in project che by eclipse.
the class SelfEncapsulateFieldRefactoring method addGetterSetterChanges.
private List<TextEditGroup> addGetterSetterChanges(CompilationUnit root, ASTRewrite rewriter, String lineDelimiter, boolean usingLocalSetter, boolean usingLocalGetter) throws CoreException {
List<TextEditGroup> result = new ArrayList<TextEditGroup>(2);
AST ast = root.getAST();
FieldDeclaration decl = (FieldDeclaration) ASTNodes.getParent(fFieldDeclaration, ASTNode.FIELD_DECLARATION);
int position = 0;
int numberOfMethods = 0;
List<BodyDeclaration> members = ASTNodes.getBodyDeclarations(decl.getParent());
for (Iterator<BodyDeclaration> iter = members.iterator(); iter.hasNext(); ) {
BodyDeclaration element = iter.next();
if (element.getNodeType() == ASTNode.METHOD_DECLARATION) {
if (fInsertionIndex == -1) {
break;
} else if (fInsertionIndex == numberOfMethods) {
position++;
break;
}
numberOfMethods++;
}
position++;
}
TextEditGroup description;
ListRewrite rewrite = fRewriter.getListRewrite(decl.getParent(), getBodyDeclarationsProperty(decl.getParent()));
if (!usingLocalGetter) {
description = new TextEditGroup(RefactoringCoreMessages.SelfEncapsulateField_add_getter);
result.add(description);
rewrite.insertAt(createGetterMethod(ast, rewriter, lineDelimiter), position++, description);
}
if (!JdtFlags.isFinal(fField) && !usingLocalSetter) {
description = new TextEditGroup(RefactoringCoreMessages.SelfEncapsulateField_add_setter);
result.add(description);
rewrite.insertAt(createSetterMethod(ast, rewriter, lineDelimiter), position, description);
}
if (!JdtFlags.isPrivate(fField))
result.add(makeDeclarationPrivate(rewriter, decl));
return result;
}
Aggregations