use of org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IExpressionFragment in project eclipse.jdt.ls by eclipse.
the class ExtractConstantRefactoring method isLiteralNodeSelected.
// !!! -- same as in ExtractTempRefactoring
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;
}
}
use of org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IExpressionFragment in project eclipse.jdt.ls by eclipse.
the class ExtractConstantRefactoring method checkExpression.
private RefactoringStatus checkExpression() throws JavaModelException {
RefactoringStatus result = new RefactoringStatus();
result.merge(checkExpressionBinding());
if (result.hasFatalError()) {
return result;
}
checkAllStaticFinal();
IExpressionFragment selectedExpression = getSelectedExpression();
Expression associatedExpression = selectedExpression.getAssociatedExpression();
if (associatedExpression instanceof NullLiteral) {
result.merge(RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractConstantRefactoring_null_literals));
} else if (!ConstantChecks.isLoadTimeConstant(selectedExpression)) {
result.merge(RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractConstantRefactoring_not_load_time_constant));
} else if (associatedExpression instanceof SimpleName) {
if (associatedExpression.getParent() instanceof QualifiedName && associatedExpression.getLocationInParent() == QualifiedName.NAME_PROPERTY || associatedExpression.getParent() instanceof FieldAccess && associatedExpression.getLocationInParent() == FieldAccess.NAME_PROPERTY) {
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractConstantRefactoring_select_expression);
}
}
return result;
}
use of org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IExpressionFragment in project eclipse.jdt.ls by eclipse.
the class ExtractConstantRefactoring method createConstantDeclaration.
private void createConstantDeclaration() throws CoreException {
Type type = getConstantType();
IExpressionFragment fragment = getSelectedExpression();
Expression initializer = getSelectedExpression().createCopyTarget(fCuRewrite.getASTRewrite(), true);
AST ast = fCuRewrite.getAST();
VariableDeclarationFragment variableDeclarationFragment = ast.newVariableDeclarationFragment();
variableDeclarationFragment.setName(ast.newSimpleName(fConstantName));
variableDeclarationFragment.setInitializer(initializer);
FieldDeclaration fieldDeclaration = ast.newFieldDeclaration(variableDeclarationFragment);
fieldDeclaration.setType(type);
Modifier.ModifierKeyword accessModifier = Modifier.ModifierKeyword.toKeyword(fVisibility);
if (accessModifier != null) {
fieldDeclaration.modifiers().add(ast.newModifier(accessModifier));
}
fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD));
fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD));
// boolean createComments = JavaPreferencesSettings.getCodeGenerationSettings(fCu.getJavaProject()).createComments;
// if (createComments) {
String comment = CodeGeneration.getFieldComment(fCu, getConstantTypeName(), fConstantName, StubUtility.getLineDelimiterUsed(fCu));
if (comment != null && comment.length() > 0 && !"/**\n *\n */\n".equals(comment)) {
Javadoc doc = (Javadoc) fCuRewrite.getASTRewrite().createStringPlaceholder(comment, ASTNode.JAVADOC);
fieldDeclaration.setJavadoc(doc);
}
// }
AbstractTypeDeclaration parent = getContainingTypeDeclarationNode();
ListRewrite listRewrite = fCuRewrite.getASTRewrite().getListRewrite(parent, parent.getBodyDeclarationsProperty());
TextEditGroup msg = fCuRewrite.createGroupDescription(RefactoringCoreMessages.ExtractConstantRefactoring_declare_constant);
if (insertFirst()) {
listRewrite.insertFirst(fieldDeclaration, msg);
} else {
listRewrite.insertAfter(fieldDeclaration, getNodeToInsertConstantDeclarationAfter(), msg);
}
if (fLinkedProposalModel != null) {
ASTRewrite rewrite = fCuRewrite.getASTRewrite();
LinkedProposalPositionGroup nameGroup = fLinkedProposalModel.getPositionGroup(KEY_NAME, true);
nameGroup.addPosition(rewrite.track(variableDeclarationFragment.getName()), true);
String[] nameSuggestions = guessConstantNames();
if (nameSuggestions.length > 0 && !nameSuggestions[0].equals(fConstantName)) {
nameGroup.addProposal(fConstantName, nameSuggestions.length + 1);
}
for (int i = 0; i < nameSuggestions.length; i++) {
nameGroup.addProposal(nameSuggestions[i], nameSuggestions.length - i);
}
LinkedProposalPositionGroup typeGroup = fLinkedProposalModel.getPositionGroup(KEY_TYPE, true);
typeGroup.addPosition(rewrite.track(type), true);
ITypeBinding typeBinding = guessBindingForReference(fragment.getAssociatedExpression());
if (typeBinding != null) {
ITypeBinding[] relaxingTypes = ASTResolving.getNarrowingTypes(ast, typeBinding);
for (int i = 0; i < relaxingTypes.length; i++) {
typeGroup.addProposal(relaxingTypes[i], fCuRewrite.getCu(), relaxingTypes.length - i);
}
}
boolean isInterface = parent.resolveBinding() != null && parent.resolveBinding().isInterface();
ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(fLinkedProposalModel, rewrite, fieldDeclaration.modifiers(), isInterface);
}
}
use of org.eclipse.jdt.ls.core.internal.corext.dom.fragments.IExpressionFragment in project eclipse.jdt.ls by eclipse.
the class ExtractConstantRefactoring method checkSelection.
private RefactoringStatus checkSelection(IProgressMonitor pm) throws JavaModelException {
try {
// $NON-NLS-1$
pm.beginTask("", 2);
IExpressionFragment selectedExpression = getSelectedExpression();
if (selectedExpression == null) {
String message = RefactoringCoreMessages.ExtractConstantRefactoring_select_expression;
return CodeRefactoringUtil.checkMethodSyntaxErrors(fSelectionStart, fSelectionLength, fCuRewrite.getRoot(), message);
}
pm.worked(1);
RefactoringStatus result = new RefactoringStatus();
result.merge(checkExpression());
if (result.hasFatalError()) {
return result;
}
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 ExtractConstantRefactoring method getExcludedVariableNames.
private String[] getExcludedVariableNames() {
if (fExcludedVariableNames == null) {
try {
IExpressionFragment expr = getSelectedExpression();
Collection<String> takenNames = new ScopeAnalyzer(fCuRewrite.getRoot()).getUsedVariableNames(expr.getStartPosition(), expr.getLength());
fExcludedVariableNames = takenNames.toArray(new String[takenNames.size()]);
} catch (JavaModelException e) {
fExcludedVariableNames = new String[0];
}
}
return fExcludedVariableNames;
}
Aggregations