Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkalyan prasad2018-03-16 10:29:49 +0000
committerKalyan Prasad Tatavarthi2018-03-16 10:35:23 +0000
commit6a8f68da1bbbed60ccca77d6348268f1b6a85acb (patch)
treefa8822cfd0e98878f5b2c2528f0555b254a55ff9
parent73485ef25af465b71a52bb65710dcc1acf51cce3 (diff)
downloadeclipse.jdt.ui-6a8f68da1bbbed60ccca77d6348268f1b6a85acb.tar.gz
eclipse.jdt.ui-6a8f68da1bbbed60ccca77d6348268f1b6a85acb.tar.xz
eclipse.jdt.ui-6a8f68da1bbbed60ccca77d6348268f1b6a85acb.zip
Bug 531040 - [10][quick assist] Convert type to 'var'
Change-Id: Id30c1c7dbfebe22c93ab969f94927bb5333b77eb Signed-off-by: kalyan prasad <kalyan_prasad@in.ibm.com>
-rw-r--r--org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickAssistProcessor.java92
-rw-r--r--org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/TypeChangeCorrectionProposal.java51
2 files changed, 138 insertions, 5 deletions
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickAssistProcessor.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickAssistProcessor.java
index cd3d8e6687..353a711500 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickAssistProcessor.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickAssistProcessor.java
@@ -321,6 +321,7 @@ public class QuickAssistProcessor implements IQuickAssistProcessor {
getConvertStringConcatenationProposals(context, resultingCollections);
getMissingCaseStatementProposals(context, coveringNode, resultingCollections);
getConvertVarTypeToResolvedTypeProposals(context, coveringNode, resultingCollections);
+ getConvertResolvedTypeToVarTypeProposals(context, coveringNode, resultingCollections);
}
return resultingCollections.toArray(new IJavaCompletionProposal[resultingCollections.size()]);
}
@@ -3634,6 +3635,10 @@ public class QuickAssistProcessor implements IQuickAssistProcessor {
}
private static boolean getConvertVarTypeToResolvedTypeProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> proposals) {
+ CompilationUnit astRoot= context.getASTRoot();
+ if (!JavaModelUtil.is10OrHigher(astRoot.getJavaElement().getJavaProject()))
+ return false;
+
if (!(node instanceof SimpleName))
return false;
@@ -3646,7 +3651,6 @@ public class QuickAssistProcessor implements IQuickAssistProcessor {
return false;
ICompilationUnit cu= context.getCompilationUnit();
- CompilationUnit astRoot= context.getASTRoot();
ASTNode declaration= astRoot.findDeclaringNode(varBinding);
ITypeBinding typeBinding= varBinding.getType();
Type type= null;
@@ -3675,6 +3679,92 @@ public class QuickAssistProcessor implements IQuickAssistProcessor {
}
+ private static boolean getConvertResolvedTypeToVarTypeProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> proposals) {
+ CompilationUnit astRoot= context.getASTRoot();
+ if (!JavaModelUtil.is10OrHigher(astRoot.getJavaElement().getJavaProject()))
+ return false;
+
+ if (!(node instanceof SimpleName))
+ return false;
+
+ SimpleName name= (SimpleName) node;
+ IBinding binding= name.resolveBinding();
+ if (!(binding instanceof IVariableBinding))
+ return false;
+ IVariableBinding varBinding= (IVariableBinding) binding;
+ if (varBinding.isField() || varBinding.isParameter())
+ return false;
+
+ ICompilationUnit cu= context.getCompilationUnit();
+ ASTNode declaration= astRoot.findDeclaringNode(varBinding);
+ ITypeBinding typeBinding= varBinding.getType();
+ ITypeBinding expTypeBinding= null;
+ Type type= null;
+ Expression exp= null;
+
+ if (declaration instanceof SingleVariableDeclaration) {
+ SingleVariableDeclaration svDecl= (SingleVariableDeclaration) declaration;
+ exp= svDecl.getInitializer();
+ if (exp != null) {
+ expTypeBinding= exp.resolveTypeBinding();
+ } else {
+ ASTNode parent= svDecl.getParent();
+ if (parent instanceof EnhancedForStatement) {
+ EnhancedForStatement efStmt= (EnhancedForStatement) parent;
+ exp= efStmt.getExpression();
+ if (exp != null) {
+ ITypeBinding expBinding= exp.resolveTypeBinding();
+ if (expBinding != null) {
+ if (expBinding.isArray()) {
+ expTypeBinding= expBinding.getElementType();
+ } else {
+ ITypeBinding iterable= Bindings.findTypeInHierarchy(expBinding, "java.lang.Iterable"); //$NON-NLS-1$
+ if (iterable != null) {
+ ITypeBinding[] typeArguments= iterable.getTypeArguments();
+ if (typeArguments.length == 1) {
+ expTypeBinding= typeArguments[0];
+ expTypeBinding= Bindings.normalizeForDeclarationUse(expTypeBinding, context.getASTRoot().getAST());
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ type= svDecl.getType();
+ } else if (declaration instanceof VariableDeclarationFragment) {
+ ASTNode parent= declaration.getParent();
+ VariableDeclarationFragment vdFrag= (VariableDeclarationFragment) declaration;
+ exp= vdFrag.getInitializer();
+ if (exp != null) {
+ expTypeBinding= exp.resolveTypeBinding();
+ }
+ if (parent instanceof VariableDeclarationStatement) {
+ VariableDeclarationStatement vdStmt= (VariableDeclarationStatement) parent;
+ type= vdStmt.getType();
+ } else if (parent instanceof VariableDeclarationExpression) {
+ VariableDeclarationExpression vdExpr= (VariableDeclarationExpression) parent;
+ type= vdExpr.getType();
+ }
+ }
+
+ if (type == null || typeBinding == null) {
+ return false;
+ }
+ if (expTypeBinding == null || !expTypeBinding.equals(typeBinding)) {
+ if (expTypeBinding == null || !expTypeBinding.isEqualTo(typeBinding)) {
+ return false;
+ }
+ }
+ if (exp == null || exp instanceof ArrayInitializer
+ || exp instanceof LambdaExpression || exp instanceof MethodReference) {
+ return false;
+ }
+ if (!type.isVar()) {
+ proposals.add(new TypeChangeCorrectionProposal(cu, varBinding, astRoot, typeBinding, IProposalRelevance.CHANGE_VARIABLE));
+ }
+ return true;
+ }
private static boolean getConvertLocalToFieldProposal(IInvocationContext context, final ASTNode node, Collection<ICommandAccess> proposals) throws CoreException {
if (!(node instanceof SimpleName))
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/TypeChangeCorrectionProposal.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/TypeChangeCorrectionProposal.java
index 677c31092a..492b3b5af0 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/TypeChangeCorrectionProposal.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/proposals/TypeChangeCorrectionProposal.java
@@ -5,6 +5,10 @@
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
+ * This is an implementation of an early-draft specification developed under the Java
+ * Community Process (JCP) and is made available for testing and evaluation purposes
+ * only. The code is not compatible with any specification of the JCP.
+ *
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
@@ -66,27 +70,58 @@ public class TypeChangeCorrectionProposal extends LinkedCorrectionProposal {
private final IBinding fBinding;
private final CompilationUnit fAstRoot;
private final ITypeBinding fNewType;
+ private final ITypeBinding fOldType;
private final ITypeBinding[] fTypeProposals;
private final TypeLocation fTypeLocation;
+ private final boolean fIsNewTypeVar;
+ private static String VAR_TYPE= "var"; //$NON-NLS-1$
public TypeChangeCorrectionProposal(ICompilationUnit targetCU, IBinding binding, CompilationUnit astRoot, ITypeBinding newType, boolean offerSuperTypeProposals, int relevance) {
+ this(targetCU, binding, astRoot, newType, false, offerSuperTypeProposals, relevance);
+ }
+
+ //This needs to be used to convert a given type to var type.
+ public TypeChangeCorrectionProposal(ICompilationUnit targetCU, IBinding binding, CompilationUnit astRoot, ITypeBinding oldType, int relevance) {
+ this(targetCU, binding, astRoot, oldType, true, false, relevance);
+ }
+
+ private TypeChangeCorrectionProposal(ICompilationUnit targetCU, IBinding binding, CompilationUnit astRoot, ITypeBinding newType, boolean isNewTypeVar, boolean offerSuperTypeProposals,
+ int relevance) {
super("", targetCU, null, relevance, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE)); //$NON-NLS-1$
Assert.isTrue(binding != null && (binding.getKind() == IBinding.METHOD || binding.getKind() == IBinding.VARIABLE) && Bindings.isDeclarationBinding(binding));
fBinding= binding; // must be generic method or (generic) variable
fAstRoot= astRoot;
+ fIsNewTypeVar= isNewTypeVar;
if (offerSuperTypeProposals) {
fTypeProposals= ASTResolving.getRelaxingTypes(astRoot.getAST(), newType);
sortTypes(fTypeProposals);
- fNewType= fTypeProposals[0];
+ if (!fIsNewTypeVar) {
+ fNewType= fTypeProposals[0];
+ fOldType= null;
+ } else {
+ fOldType= newType;
+ fNewType= null;
+ }
} else {
- fNewType= newType;
+ if (!fIsNewTypeVar) {
+ fNewType= newType;
+ fOldType= null;
+ } else {
+ fOldType= newType;
+ fNewType= null;
+ }
fTypeProposals= null;
}
- String typeName= BindingLabelProvider.getBindingLabel(fNewType, JavaElementLabels.ALL_DEFAULT);
+ String typeName;
+ if (isNewTypeVar) {
+ typeName= VAR_TYPE;
+ } else {
+ typeName= BindingLabelProvider.getBindingLabel(fNewType, JavaElementLabels.ALL_DEFAULT);
+ }
if (binding.getKind() == IBinding.VARIABLE) {
IVariableBinding varBinding= (IVariableBinding) binding;
String[] args= { BasicElementLabels.getJavaElementName(varBinding.getName()), BasicElementLabels.getJavaElementName(typeName)};
@@ -124,7 +159,12 @@ public class TypeChangeCorrectionProposal extends LinkedCorrectionProposal {
ImportRewrite imports= createImportRewrite(newRoot);
ImportRewriteContext context= new ContextSensitiveImportRewriteContext(newRoot, declNode.getStartPosition(), imports);
- Type type= imports.addImport(fNewType, ast, context, fTypeLocation);
+ Type type;
+ if (fIsNewTypeVar) {
+ type= fAstRoot.getAST().newSimpleType(fAstRoot.getAST().newName(VAR_TYPE));
+ } else {
+ type= imports.addImport(fNewType, ast, context, fTypeLocation);
+ }
if (declNode instanceof MethodDeclaration) {
MethodDeclaration methodDecl= (MethodDeclaration) declNode;
@@ -190,6 +230,9 @@ public class TypeChangeCorrectionProposal extends LinkedCorrectionProposal {
} else {
rewrite.set(varDecl, VariableDeclarationStatement.TYPE_PROPERTY, type, null);
DimensionRewrite.removeAllChildren(declNode, VariableDeclarationFragment.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
+ if (fIsNewTypeVar) {
+ TypeAnnotationRewrite.removePureTypeAnnotations(parent, VariableDeclarationStatement.MODIFIERS2_PROPERTY, rewrite, null);
+ }
}
} else if (parent instanceof VariableDeclarationExpression) {
VariableDeclarationExpression varDecl= (VariableDeclarationExpression) parent;

Back to the top