use of org.eclipse.jdt.ls.core.internal.corrections.proposals.AssignToVariableAssistProposal in project eclipse.jdt.ls by eclipse.
the class RefactorProposalUtility method getAssignFieldProposal.
public static CUCorrectionProposal getAssignFieldProposal(CodeActionParams params, IInvocationContext context, boolean problemsAtLocation, Map formatterOptions, boolean returnAsCommand, IProblemLocationCore[] locations) throws CoreException {
ASTNode node = context.getCoveringNode();
Statement statement = ASTResolving.findParentStatement(node);
if (!(statement instanceof ExpressionStatement)) {
return null;
}
ExpressionStatement expressionStatement = (ExpressionStatement) statement;
Expression expression = expressionStatement.getExpression();
if (expression.getNodeType() == ASTNode.ASSIGNMENT) {
return null;
}
ITypeBinding typeBinding = expression.resolveTypeBinding();
typeBinding = Bindings.normalizeTypeBinding(typeBinding);
if (typeBinding == null) {
return null;
}
if (containsMatchingProblem(locations, IProblem.UnusedObjectAllocation)) {
return null;
}
final ICompilationUnit cu = context.getCompilationUnit();
ASTNode type = ASTResolving.findParentType(expression);
if (type != null) {
int relevance;
if (context.getSelectionLength() == 0) {
relevance = IProposalRelevance.EXTRACT_LOCAL_ZERO_SELECTION;
} else if (problemsAtLocation) {
relevance = IProposalRelevance.EXTRACT_LOCAL_ERROR;
} else {
relevance = IProposalRelevance.EXTRACT_LOCAL;
}
if (returnAsCommand) {
return new AssignToVariableAssistCommandProposal(cu, JavaCodeActionKind.REFACTOR_ASSIGN_FIELD, AssignToVariableAssistProposal.FIELD, expressionStatement, typeBinding, relevance, APPLY_REFACTORING_COMMAND_ID, Arrays.asList(ASSIGN_FIELD_COMMAND, params));
} else {
return new AssignToVariableAssistProposal(cu, JavaCodeActionKind.REFACTOR_ASSIGN_FIELD, AssignToVariableAssistProposal.FIELD, expressionStatement, typeBinding, relevance);
}
}
return null;
}
use of org.eclipse.jdt.ls.core.internal.corrections.proposals.AssignToVariableAssistProposal in project eclipse.jdt.ls by eclipse.
the class QuickAssistProcessor method getAssignAllParamsToFieldsProposals.
private static boolean getAssignAllParamsToFieldsProposals(IInvocationContext context, ASTNode node, Collection<ChangeCorrectionProposal> resultingCollections) {
node = ASTNodes.getNormalizedNode(node);
ASTNode parent = node.getParent();
if (!(parent instanceof SingleVariableDeclaration) || !(parent.getParent() instanceof MethodDeclaration)) {
return false;
}
MethodDeclaration methodDecl = (MethodDeclaration) parent.getParent();
if (methodDecl.getBody() == null) {
return false;
}
List<SingleVariableDeclaration> parameters = methodDecl.parameters();
if (parameters.size() <= 1) {
return false;
}
ITypeBinding parentType = Bindings.getBindingOfParentType(node);
if (parentType == null || parentType.isInterface()) {
return false;
}
for (SingleVariableDeclaration param : parameters) {
IVariableBinding binding = param.resolveBinding();
if (binding == null || binding.getType() == null) {
return false;
}
}
if (resultingCollections == null) {
return true;
}
AssignToVariableAssistProposal fieldProposal = new AssignToVariableAssistProposal(context.getCompilationUnit(), parameters, IProposalRelevance.ASSIGN_ALL_PARAMS_TO_NEW_FIELDS);
resultingCollections.add(fieldProposal);
return true;
}
use of org.eclipse.jdt.ls.core.internal.corrections.proposals.AssignToVariableAssistProposal in project eclipse.jdt.ls by eclipse.
the class QuickAssistProcessor method getAssignParamToFieldProposals.
private static boolean getAssignParamToFieldProposals(IInvocationContext context, ASTNode node, Collection<ChangeCorrectionProposal> resultingCollections) {
node = ASTNodes.getNormalizedNode(node);
ASTNode parent = node.getParent();
if (!(parent instanceof SingleVariableDeclaration) || !(parent.getParent() instanceof MethodDeclaration)) {
return false;
}
SingleVariableDeclaration paramDecl = (SingleVariableDeclaration) parent;
IVariableBinding binding = paramDecl.resolveBinding();
MethodDeclaration methodDecl = (MethodDeclaration) parent.getParent();
if (binding == null || methodDecl.getBody() == null) {
return false;
}
ITypeBinding typeBinding = binding.getType();
if (typeBinding == null) {
return false;
}
if (resultingCollections == null) {
return true;
}
ITypeBinding parentType = Bindings.getBindingOfParentType(node);
if (parentType != null) {
if (parentType.isInterface()) {
return false;
}
// assign to existing fields
CompilationUnit root = context.getASTRoot();
IVariableBinding[] declaredFields = parentType.getDeclaredFields();
boolean isStaticContext = ASTResolving.isInStaticContext(node);
for (int i = 0; i < declaredFields.length; i++) {
IVariableBinding curr = declaredFields[i];
if (isStaticContext == Modifier.isStatic(curr.getModifiers()) && typeBinding.isAssignmentCompatible(curr.getType())) {
ASTNode fieldDeclFrag = root.findDeclaringNode(curr);
if (fieldDeclFrag instanceof VariableDeclarationFragment) {
VariableDeclarationFragment fragment = (VariableDeclarationFragment) fieldDeclFrag;
if (fragment.getInitializer() == null) {
resultingCollections.add(new AssignToVariableAssistProposal(context.getCompilationUnit(), paramDecl, fragment, typeBinding, IProposalRelevance.ASSIGN_PARAM_TO_EXISTING_FIELD));
}
}
}
}
}
AssignToVariableAssistProposal fieldProposal = new AssignToVariableAssistProposal(context.getCompilationUnit(), paramDecl, null, typeBinding, IProposalRelevance.ASSIGN_PARAM_TO_NEW_FIELD);
resultingCollections.add(fieldProposal);
return true;
}
use of org.eclipse.jdt.ls.core.internal.corrections.proposals.AssignToVariableAssistProposal in project eclipse.jdt.ls by eclipse.
the class RefactorProposalUtility method getAssignVariableProposal.
public static CUCorrectionProposal getAssignVariableProposal(CodeActionParams params, IInvocationContext context, boolean problemsAtLocation, Map formatterOptions, boolean returnAsCommand, IProblemLocationCore[] locations) throws CoreException {
ASTNode node = context.getCoveringNode();
Statement statement = ASTResolving.findParentStatement(node);
if (!(statement instanceof ExpressionStatement)) {
return null;
}
ExpressionStatement expressionStatement = (ExpressionStatement) statement;
Expression expression = expressionStatement.getExpression();
if (expression.getNodeType() == ASTNode.ASSIGNMENT) {
return null;
}
ITypeBinding typeBinding = expression.resolveTypeBinding();
typeBinding = Bindings.normalizeTypeBinding(typeBinding);
if (typeBinding == null) {
return null;
}
if (containsMatchingProblem(locations, IProblem.UnusedObjectAllocation)) {
return null;
}
final ICompilationUnit cu = context.getCompilationUnit();
int relevance;
if (context.getSelectionLength() == 0) {
relevance = IProposalRelevance.EXTRACT_LOCAL_ZERO_SELECTION;
} else if (problemsAtLocation) {
relevance = IProposalRelevance.EXTRACT_LOCAL_ERROR;
} else {
relevance = IProposalRelevance.EXTRACT_LOCAL;
}
if (returnAsCommand) {
return new AssignToVariableAssistCommandProposal(cu, JavaCodeActionKind.REFACTOR_ASSIGN_VARIABLE, AssignToVariableAssistProposal.LOCAL, expressionStatement, typeBinding, relevance, APPLY_REFACTORING_COMMAND_ID, Arrays.asList(ASSIGN_VARIABLE_COMMAND, params));
} else {
return new AssignToVariableAssistProposal(cu, JavaCodeActionKind.REFACTOR_ASSIGN_VARIABLE, AssignToVariableAssistProposal.LOCAL, expressionStatement, typeBinding, relevance);
}
}
Aggregations