use of org.eclipse.jdt.ls.core.internal.corrections.proposals.TypeChangeCorrectionProposal in project eclipse.jdt.ls by eclipse.
the class RefactorProcessor method getConvertResolvedTypeToVarTypeProposal.
private static boolean getConvertResolvedTypeToVarTypeProposal(IInvocationContext context, ASTNode node, Collection<ChangeCorrectionProposal> proposals) {
CompilationUnit astRoot = context.getASTRoot();
IJavaElement root = astRoot.getJavaElement();
if (root == null) {
return false;
}
IJavaProject javaProject = root.getJavaProject();
if (javaProject == null) {
return false;
}
if (!JavaModelUtil.is10OrHigher(javaProject)) {
return false;
}
SimpleName name = getSimpleNameForVariable(node);
if (name == null) {
return false;
}
IBinding binding = name.resolveBinding();
if (!(binding instanceof IVariableBinding)) {
return false;
}
IVariableBinding varBinding = (IVariableBinding) binding;
if (varBinding.isField() || varBinding.isParameter()) {
return false;
}
ASTNode varDeclaration = astRoot.findDeclaringNode(varBinding);
if (varDeclaration == null) {
return false;
}
Type type = null;
Expression expression = null;
ITypeBinding typeBinding = varBinding.getType();
if (typeBinding == null) {
return false;
}
ITypeBinding expressionTypeBinding = null;
if (varDeclaration instanceof SingleVariableDeclaration) {
SingleVariableDeclaration svDecl = (SingleVariableDeclaration) varDeclaration;
type = svDecl.getType();
expression = svDecl.getInitializer();
if (expression != null) {
expressionTypeBinding = expression.resolveTypeBinding();
} else {
ASTNode parent = svDecl.getParent();
if (parent instanceof EnhancedForStatement) {
EnhancedForStatement efStmt = (EnhancedForStatement) parent;
expression = efStmt.getExpression();
if (expression != null) {
ITypeBinding expBinding = expression.resolveTypeBinding();
if (expBinding != null) {
if (expBinding.isArray()) {
expressionTypeBinding = expBinding.getElementType();
} else {
// $NON-NLS-1$
ITypeBinding iterable = Bindings.findTypeInHierarchy(expBinding, "java.lang.Iterable");
if (iterable != null) {
ITypeBinding[] typeArguments = iterable.getTypeArguments();
if (typeArguments.length == 1) {
expressionTypeBinding = typeArguments[0];
expressionTypeBinding = Bindings.normalizeForDeclarationUse(expressionTypeBinding, context.getASTRoot().getAST());
}
}
}
}
}
}
}
} else if (varDeclaration instanceof VariableDeclarationFragment) {
ASTNode parent = varDeclaration.getParent();
expression = ((VariableDeclarationFragment) varDeclaration).getInitializer();
if (expression != null) {
expressionTypeBinding = expression.resolveTypeBinding();
}
if (parent instanceof VariableDeclarationStatement) {
type = ((VariableDeclarationStatement) parent).getType();
} else if (parent instanceof VariableDeclarationExpression) {
VariableDeclarationExpression varDecl = (VariableDeclarationExpression) parent;
// cannot convert a VariableDeclarationExpression with multiple fragments to var.
if (varDecl.fragments().size() > 1) {
return false;
}
type = varDecl.getType();
}
}
if (type == null || type.isVar()) {
return false;
}
if (expression == null || expression instanceof ArrayInitializer || expression instanceof LambdaExpression || expression instanceof MethodReference) {
return false;
}
if (expressionTypeBinding == null || !expressionTypeBinding.isEqualTo(typeBinding)) {
return false;
}
TypeChangeCorrectionProposal proposal = new TypeChangeCorrectionProposal(context.getCompilationUnit(), varBinding, astRoot, typeBinding, IProposalRelevance.CHANGE_VARIABLE);
proposal.setKind(CodeActionKind.Refactor);
proposals.add(proposal);
return true;
}
use of org.eclipse.jdt.ls.core.internal.corrections.proposals.TypeChangeCorrectionProposal in project eclipse.jdt.ls by eclipse.
the class RefactorProcessor method getConvertVarTypeToResolvedTypeProposal.
private static boolean getConvertVarTypeToResolvedTypeProposal(IInvocationContext context, ASTNode node, Collection<ChangeCorrectionProposal> proposals) {
CompilationUnit astRoot = context.getASTRoot();
IJavaElement root = astRoot.getJavaElement();
if (root == null) {
return false;
}
IJavaProject javaProject = root.getJavaProject();
if (javaProject == null) {
return false;
}
if (!JavaModelUtil.is10OrHigher(javaProject)) {
return false;
}
SimpleName name = getSimpleNameForVariable(node);
if (name == null) {
return false;
}
IBinding binding = name.resolveBinding();
if (!(binding instanceof IVariableBinding)) {
return false;
}
IVariableBinding varBinding = (IVariableBinding) binding;
if (varBinding.isField() || varBinding.isParameter()) {
return false;
}
ASTNode varDeclaration = astRoot.findDeclaringNode(varBinding);
if (varDeclaration == null) {
return false;
}
ITypeBinding typeBinding = varBinding.getType();
if (typeBinding == null || typeBinding.isAnonymous() || typeBinding.isIntersectionType() || typeBinding.isWildcardType()) {
return false;
}
Type type = null;
if (varDeclaration instanceof SingleVariableDeclaration) {
type = ((SingleVariableDeclaration) varDeclaration).getType();
} else if (varDeclaration instanceof VariableDeclarationFragment) {
ASTNode parent = varDeclaration.getParent();
if (parent instanceof VariableDeclarationStatement) {
type = ((VariableDeclarationStatement) parent).getType();
} else if (parent instanceof VariableDeclarationExpression) {
type = ((VariableDeclarationExpression) parent).getType();
}
}
if (type == null || !type.isVar()) {
return false;
}
TypeChangeCorrectionProposal proposal = new TypeChangeCorrectionProposal(context.getCompilationUnit(), varBinding, astRoot, typeBinding, false, IProposalRelevance.CHANGE_VARIABLE);
proposal.setKind(CodeActionKind.Refactor);
proposals.add(proposal);
return true;
}
Aggregations