|
Lines 47-52
Link Here
|
| 47 |
|
47 |
|
| 48 |
import org.eclipse.jdt.internal.corext.dom.ASTNodes; |
48 |
import org.eclipse.jdt.internal.corext.dom.ASTNodes; |
| 49 |
import org.eclipse.jdt.internal.corext.dom.Bindings; |
49 |
import org.eclipse.jdt.internal.corext.dom.Bindings; |
|
|
50 |
import org.eclipse.jdt.internal.corext.util.JavaModelUtil; |
| 50 |
import org.eclipse.jdt.internal.corext.util.Messages; |
51 |
import org.eclipse.jdt.internal.corext.util.Messages; |
| 51 |
|
52 |
|
| 52 |
import org.eclipse.jdt.ui.JavaElementLabels; |
53 |
import org.eclipse.jdt.ui.JavaElementLabels; |
|
Lines 125-139
Link Here
|
| 125 |
return; |
126 |
return; |
| 126 |
} |
127 |
} |
| 127 |
|
128 |
|
|
|
129 |
ITypeBinding currBinding= nodeToCast.resolveTypeBinding(); |
| 130 |
|
| 128 |
if (!(nodeToCast instanceof ArrayInitializer)) { |
131 |
if (!(nodeToCast instanceof ArrayInitializer)) { |
| 129 |
ITypeBinding binding= nodeToCast.resolveTypeBinding(); |
132 |
ITypeBinding castFixType= null; |
| 130 |
if (binding == null || binding.isCastCompatible(castTypeBinding) || nodeToCast instanceof CastExpression) { |
133 |
if (currBinding == null || castTypeBinding.isCastCompatible(currBinding) || nodeToCast instanceof CastExpression) { |
| 131 |
proposals.add(createCastProposal(context, castTypeBinding, nodeToCast, 7)); |
134 |
castFixType= castTypeBinding; |
|
|
135 |
} else if (JavaModelUtil.is50OrHigher(cu.getJavaProject())) { |
| 136 |
ITypeBinding boxUnboxedTypeBinding= boxUnboxPrimitives(castTypeBinding, currBinding, ast); |
| 137 |
if (boxUnboxedTypeBinding != castTypeBinding && boxUnboxedTypeBinding.isCastCompatible(currBinding)) { |
| 138 |
castFixType= boxUnboxedTypeBinding; |
| 139 |
} |
| 140 |
} |
| 141 |
if (castFixType != null) { |
| 142 |
proposals.add(createCastProposal(context, castFixType, nodeToCast, 7)); |
| 132 |
} |
143 |
} |
| 133 |
} |
144 |
} |
| 134 |
|
145 |
|
| 135 |
ITypeBinding currBinding= nodeToCast.resolveTypeBinding(); |
|
|
| 136 |
|
| 137 |
boolean nullOrVoid= currBinding == null || "void".equals(currBinding.getName()); //$NON-NLS-1$ |
146 |
boolean nullOrVoid= currBinding == null || "void".equals(currBinding.getName()); //$NON-NLS-1$ |
| 138 |
|
147 |
|
| 139 |
// change method return statement to actual type |
148 |
// change method return statement to actual type |
|
Lines 185-191
Link Here
|
| 185 |
|
194 |
|
| 186 |
addChangeSenderTypeProposals(context, nodeToCast, castTypeBinding, false, 5, proposals); |
195 |
addChangeSenderTypeProposals(context, nodeToCast, castTypeBinding, false, 5, proposals); |
| 187 |
|
196 |
|
| 188 |
if (castTypeBinding == ast.resolveWellKnownType(CorrectionMessages.TypeMismatchSubProcessor_0) && currBinding != null && !currBinding.isPrimitive() && !Bindings.isVoidType(currBinding)) { |
197 |
if (castTypeBinding == ast.resolveWellKnownType("boolean") && currBinding != null && !currBinding.isPrimitive() && !Bindings.isVoidType(currBinding)) { //$NON-NLS-1$ |
| 189 |
String label= CorrectionMessages.TypeMismatchSubProcessor_insertnullcheck_description; |
198 |
String label= CorrectionMessages.TypeMismatchSubProcessor_insertnullcheck_description; |
| 190 |
Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE); |
199 |
Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE); |
| 191 |
ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); |
200 |
ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); |
|
Lines 201-206
Link Here
|
| 201 |
|
210 |
|
| 202 |
} |
211 |
} |
| 203 |
|
212 |
|
|
|
213 |
public static ITypeBinding boxUnboxPrimitives(ITypeBinding castType, ITypeBinding toCast, AST ast) { |
| 214 |
/* |
| 215 |
* e.g: |
| 216 |
* void m(toCast var) { |
| 217 |
* castType i= var; |
| 218 |
* } |
| 219 |
*/ |
| 220 |
if (castType.isPrimitive() && !toCast.isPrimitive()) { |
| 221 |
return Bindings.getBoxedTypeBinding(castType, ast); |
| 222 |
} else if (!castType.isPrimitive() && toCast.isPrimitive()) { |
| 223 |
return Bindings.getUnboxedTypeBinding(castType, ast); |
| 224 |
} else { |
| 225 |
return castType; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 204 |
public static void addChangeSenderTypeProposals(IInvocationContext context, Expression nodeToCast, ITypeBinding castTypeBinding, boolean isAssignedNode, int relevance, Collection proposals) throws JavaModelException { |
229 |
public static void addChangeSenderTypeProposals(IInvocationContext context, Expression nodeToCast, ITypeBinding castTypeBinding, boolean isAssignedNode, int relevance, Collection proposals) throws JavaModelException { |
| 205 |
IBinding callerBinding= Bindings.resolveExpressionBinding(nodeToCast, false); |
230 |
IBinding callerBinding= Bindings.resolveExpressionBinding(nodeToCast, false); |
| 206 |
|
231 |
|
|
Lines 264-272
Link Here
|
| 264 |
ICompilationUnit cu= context.getCompilationUnit(); |
289 |
ICompilationUnit cu= context.getCompilationUnit(); |
| 265 |
|
290 |
|
| 266 |
String label; |
291 |
String label; |
| 267 |
ITypeBinding toCastBinding= nodeToCast.resolveTypeBinding(); |
292 |
String castType= BindingLabelProvider.getBindingLabel(castTypeBinding, JavaElementLabels.ALL_DEFAULT); |
| 268 |
ITypeBinding resultingTypeBinding= CastCorrectionProposal.getBoxedTypeBindingIfNeeded(castTypeBinding, toCastBinding, nodeToCast.getAST()); |
|
|
| 269 |
String castType= BindingLabelProvider.getBindingLabel(resultingTypeBinding, JavaElementLabels.ALL_DEFAULT); |
| 270 |
if (nodeToCast.getNodeType() == ASTNode.CAST_EXPRESSION) { |
293 |
if (nodeToCast.getNodeType() == ASTNode.CAST_EXPRESSION) { |
| 271 |
label= Messages.format(CorrectionMessages.TypeMismatchSubProcessor_changecast_description, castType); |
294 |
label= Messages.format(CorrectionMessages.TypeMismatchSubProcessor_changecast_description, castType); |
| 272 |
} else { |
295 |
} else { |