use of org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IExpressionFragment in project eclipse.jdt.ls by eclipse.
the class ExtractTempRefactoring method shouldReplaceSelectedExpressionWithTempDeclaration.
private boolean shouldReplaceSelectedExpressionWithTempDeclaration() throws JavaModelException {
IExpressionFragment selectedFragment = getSelectedExpression();
IExpressionFragment firstExpression = getFirstReplacedExpression();
if (firstExpression.getStartPosition() < selectedFragment.getStartPosition()) {
return false;
}
ASTNode associatedNode = selectedFragment.getAssociatedNode();
return (associatedNode.getParent() instanceof ExpressionStatement || associatedNode.getParent() instanceof LambdaExpression) && selectedFragment.matches(ASTFragmentFactory.createFragmentForFullSubtree(associatedNode));
}
use of org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IExpressionFragment in project eclipse.jdt.ls by eclipse.
the class ExtractConstantRefactoring method getConstantType.
private Type getConstantType() throws JavaModelException {
if (fConstantTypeCache == null) {
IExpressionFragment fragment = getSelectedExpression();
ITypeBinding typeBinding = guessBindingForReference(fragment.getAssociatedExpression());
AST ast = fCuRewrite.getAST();
typeBinding = Bindings.normalizeForDeclarationUse(typeBinding, ast);
ImportRewrite importRewrite = fCuRewrite.getImportRewrite();
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(fCuRewrite.getRoot(), fSelectionStart, importRewrite);
fConstantTypeCache = importRewrite.addImport(typeBinding, ast, context, TypeLocation.FIELD);
}
return fConstantTypeCache;
}
use of org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IExpressionFragment in project eclipse.jdt.ls by eclipse.
the class ExtractTempRefactoring method checkSelection.
private RefactoringStatus checkSelection(IProgressMonitor pm) throws JavaModelException {
try {
// $NON-NLS-1$
pm.beginTask("", 8);
IExpressionFragment selectedExpression = getSelectedExpression();
if (selectedExpression == null) {
String message = RefactoringCoreMessages.ExtractTempRefactoring_select_expression;
return CodeRefactoringUtil.checkMethodSyntaxErrors(fSelectionStart, fSelectionLength, fCompilationUnitNode, message);
}
pm.worked(1);
if (isUsedInExplicitConstructorCall()) {
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_explicit_constructor);
}
pm.worked(1);
ASTNode associatedNode = selectedExpression.getAssociatedNode();
if (getEnclosingBodyNode() == null || ASTNodes.getParent(associatedNode, Annotation.class) != null) {
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_expr_in_method_or_initializer);
}
pm.worked(1);
if (associatedNode instanceof Name && associatedNode.getParent() instanceof ClassInstanceCreation && associatedNode.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) {
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_name_in_new);
}
pm.worked(1);
RefactoringStatus result = new RefactoringStatus();
result.merge(checkExpression());
if (result.hasFatalError()) {
return result;
}
pm.worked(1);
result.merge(checkExpressionFragmentIsRValue());
if (result.hasFatalError()) {
return result;
}
pm.worked(1);
if (isUsedInForInitializerOrUpdater(getSelectedExpression().getAssociatedExpression())) {
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_for_initializer_updater);
}
pm.worked(1);
if (isReferringToLocalVariableFromFor(getSelectedExpression().getAssociatedExpression())) {
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_refers_to_for_variable);
}
pm.worked(1);
return result;
} finally {
pm.done();
}
}
use of org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IExpressionFragment in project eclipse.jdt.ls by eclipse.
the class ExtractTempRefactoring method getSelectedExpression.
private IExpressionFragment getSelectedExpression() throws JavaModelException {
if (fSelectedExpression != null) {
return fSelectedExpression;
}
IASTFragment selectedFragment = ASTFragmentFactory.createFragmentForSourceRange(new SourceRange(fSelectionStart, fSelectionLength), fCompilationUnitNode, fCu);
if (selectedFragment instanceof IExpressionFragment && !Checks.isInsideJavadoc(selectedFragment.getAssociatedNode())) {
fSelectedExpression = (IExpressionFragment) selectedFragment;
} else if (selectedFragment != null) {
if (selectedFragment.getAssociatedNode() instanceof ExpressionStatement) {
ExpressionStatement exprStatement = (ExpressionStatement) selectedFragment.getAssociatedNode();
Expression expression = exprStatement.getExpression();
fSelectedExpression = (IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(expression);
} else if (selectedFragment.getAssociatedNode() instanceof Assignment) {
Assignment assignment = (Assignment) selectedFragment.getAssociatedNode();
fSelectedExpression = (IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(assignment);
}
}
if (fSelectedExpression != null && Checks.isEnumCase(fSelectedExpression.getAssociatedExpression().getParent())) {
fSelectedExpression = null;
}
return fSelectedExpression;
}
use of org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IExpressionFragment in project eclipse.jdt.ls by eclipse.
the class ExtractTempRefactoring method isLiteralNodeSelected.
private boolean isLiteralNodeSelected() throws JavaModelException {
IExpressionFragment fragment = getSelectedExpression();
if (fragment == null) {
return false;
}
Expression expression = fragment.getAssociatedExpression();
if (expression == null) {
return false;
}
switch(expression.getNodeType()) {
case ASTNode.BOOLEAN_LITERAL:
case ASTNode.CHARACTER_LITERAL:
case ASTNode.NULL_LITERAL:
case ASTNode.NUMBER_LITERAL:
return true;
default:
return false;
}
}
Aggregations