use of org.eclipse.jdt.core.dom.MethodDeclaration in project che by eclipse.
the class IntroduceFactoryRefactoring method checkSelection.
/**
* Determines what kind of AST node was selected, and returns an error status
* if the kind of node is inappropriate for this refactoring.
* @param pm
* @return a RefactoringStatus indicating whether the selection is valid
* @throws JavaModelException
*/
private RefactoringStatus checkSelection(IProgressMonitor pm) throws JavaModelException {
try {
pm.beginTask(RefactoringCoreMessages.IntroduceFactory_examiningSelection, 2);
fSelectedNode = getTargetNode(fCUHandle, fSelectionStart, fSelectionLength);
if (fSelectedNode == null)
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceFactory_notAConstructorInvocation);
// constructor MethodDeclaration; nothing else.
if (fSelectedNode instanceof ClassInstanceCreation) {
ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) fSelectedNode;
fCtorBinding = classInstanceCreation.resolveConstructorBinding();
} else if (fSelectedNode instanceof MethodDeclaration) {
MethodDeclaration methodDeclaration = (MethodDeclaration) fSelectedNode;
fCtorBinding = methodDeclaration.resolveBinding();
}
if (fCtorBinding == null)
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceFactory_unableToResolveConstructorBinding);
// If this constructor is of a generic type, get the generic version,
// not some instantiation thereof.
fCtorBinding = fCtorBinding.getMethodDeclaration();
pm.worked(1);
// We don't handle constructors of nested types at the moment
if (fCtorBinding.getDeclaringClass().isNested())
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceFactory_unsupportedNestedTypes);
ITypeBinding ctorType = fCtorBinding.getDeclaringClass();
IType ctorOwningType = (IType) ctorType.getJavaElement();
if (ctorOwningType.isBinary())
// Can't modify binary CU; don't know what CU to put factory method
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceFactory_constructorInBinaryClass);
if (ctorOwningType.isEnum())
// Doesn't make sense to encapsulate enum constructors
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceFactory_constructorInEnum);
// Put the generated factory method inside the type that owns the constructor
fFactoryUnitHandle = ctorOwningType.getCompilationUnit();
fFactoryCU = getASTFor(fFactoryUnitHandle);
Name ctorOwnerName = (Name) NodeFinder.perform(fFactoryCU, ctorOwningType.getNameRange());
fCtorOwningClass = (AbstractTypeDeclaration) ASTNodes.getParent(ctorOwnerName, AbstractTypeDeclaration.class);
fFactoryOwningClass = fCtorOwningClass;
pm.worked(1);
if (fNewMethodName == null)
//$NON-NLS-1$
return setNewMethodName("create" + fCtorBinding.getName());
else
return new RefactoringStatus();
} finally {
pm.done();
}
}
use of org.eclipse.jdt.core.dom.MethodDeclaration in project che by eclipse.
the class ExtractMethodRefactoring method getSignature.
/**
* Returns the signature of the new method.
*
* @param methodName the method name used for the new method
* @return the signature of the extracted method
*/
public String getSignature(String methodName) {
MethodDeclaration methodDecl = createNewMethodDeclaration();
methodDecl.setBody(null);
String str = ASTNodes.asString(methodDecl);
return str.substring(0, str.indexOf(';'));
}
use of org.eclipse.jdt.core.dom.MethodDeclaration in project che by eclipse.
the class ExtractMethodAnalyzer method computeLastStatementSelected.
private void computeLastStatementSelected() {
ASTNode[] nodes = getSelectedNodes();
if (nodes.length == 0) {
fIsLastStatementSelected = false;
} else {
Block body = null;
LambdaExpression enclosingLambdaExpr = ASTResolving.findEnclosingLambdaExpression(getFirstSelectedNode());
if (enclosingLambdaExpr != null) {
ASTNode lambdaBody = enclosingLambdaExpr.getBody();
if (lambdaBody instanceof Block) {
body = (Block) lambdaBody;
} else {
fIsLastStatementSelected = true;
return;
}
} else {
if (fEnclosingBodyDeclaration instanceof MethodDeclaration) {
body = ((MethodDeclaration) fEnclosingBodyDeclaration).getBody();
} else if (fEnclosingBodyDeclaration instanceof Initializer) {
body = ((Initializer) fEnclosingBodyDeclaration).getBody();
}
}
if (body != null) {
List<Statement> statements = body.statements();
if (statements.size() > 0) {
fIsLastStatementSelected = nodes[nodes.length - 1] == statements.get(statements.size() - 1);
} else {
fIsLastStatementSelected = true;
}
}
}
}
use of org.eclipse.jdt.core.dom.MethodDeclaration in project che by eclipse.
the class ExtractMethodAnalyzer method initReturnType.
private void initReturnType(ImportRewrite rewriter) {
AST ast = fEnclosingBodyDeclaration.getAST();
fReturnType = null;
fReturnTypeBinding = null;
switch(fReturnKind) {
case ACCESS_TO_LOCAL:
VariableDeclaration declaration = ASTNodes.findVariableDeclaration(fReturnValue, fEnclosingBodyDeclaration);
fReturnType = ASTNodeFactory.newType(ast, declaration, rewriter, new ContextSensitiveImportRewriteContext(declaration, rewriter));
if (declaration.resolveBinding() != null) {
fReturnTypeBinding = declaration.resolveBinding().getType();
}
break;
case EXPRESSION:
Expression expression = (Expression) getFirstSelectedNode();
if (expression.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) {
fExpressionBinding = ((ClassInstanceCreation) expression).getType().resolveBinding();
} else {
fExpressionBinding = expression.resolveTypeBinding();
}
if (fExpressionBinding != null) {
if (fExpressionBinding.isNullType()) {
getStatus().addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_null_type, JavaStatusContext.create(fCUnit, expression));
} else {
ITypeBinding normalizedBinding = Bindings.normalizeForDeclarationUse(fExpressionBinding, ast);
if (normalizedBinding != null) {
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(fEnclosingBodyDeclaration, rewriter);
fReturnType = rewriter.addImport(normalizedBinding, ast, context);
fReturnTypeBinding = normalizedBinding;
}
}
} else {
fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
//$NON-NLS-1$
fReturnTypeBinding = ast.resolveWellKnownType("void");
getStatus().addError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_determine_return_type, JavaStatusContext.create(fCUnit, expression));
}
break;
case RETURN_STATEMENT_VALUE:
LambdaExpression enclosingLambdaExpr = ASTResolving.findEnclosingLambdaExpression(getFirstSelectedNode());
if (enclosingLambdaExpr != null) {
fReturnType = ASTNodeFactory.newReturnType(enclosingLambdaExpr, ast, rewriter, null);
IMethodBinding methodBinding = enclosingLambdaExpr.resolveMethodBinding();
fReturnTypeBinding = methodBinding != null ? methodBinding.getReturnType() : null;
} else if (fEnclosingBodyDeclaration.getNodeType() == ASTNode.METHOD_DECLARATION) {
fReturnType = ((MethodDeclaration) fEnclosingBodyDeclaration).getReturnType2();
fReturnTypeBinding = fReturnType != null ? fReturnType.resolveBinding() : null;
}
break;
default:
fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
//$NON-NLS-1$
fReturnTypeBinding = ast.resolveWellKnownType("void");
}
if (fReturnType == null) {
fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
//$NON-NLS-1$
fReturnTypeBinding = ast.resolveWellKnownType("void");
}
}
use of org.eclipse.jdt.core.dom.MethodDeclaration 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("");
}
Aggregations