Lines 60-65
Link Here
|
60 |
import org.eclipse.jdt.core.dom.MethodDeclaration; |
60 |
import org.eclipse.jdt.core.dom.MethodDeclaration; |
61 |
import org.eclipse.jdt.core.dom.MethodInvocation; |
61 |
import org.eclipse.jdt.core.dom.MethodInvocation; |
62 |
import org.eclipse.jdt.core.dom.Name; |
62 |
import org.eclipse.jdt.core.dom.Name; |
|
|
63 |
import org.eclipse.jdt.core.dom.NumberLiteral; |
63 |
import org.eclipse.jdt.core.dom.ParenthesizedExpression; |
64 |
import org.eclipse.jdt.core.dom.ParenthesizedExpression; |
64 |
import org.eclipse.jdt.core.dom.PrefixExpression; |
65 |
import org.eclipse.jdt.core.dom.PrefixExpression; |
65 |
import org.eclipse.jdt.core.dom.PrimitiveType; |
66 |
import org.eclipse.jdt.core.dom.PrimitiveType; |
Lines 147-152
Link Here
|
147 |
|| getPullNegationUpProposals(context, coveredNodes, null) |
148 |
|| getPullNegationUpProposals(context, coveredNodes, null) |
148 |
|| getJoinIfListInIfElseIfProposals(context, coveringNode, coveredNodes, null) |
149 |
|| getJoinIfListInIfElseIfProposals(context, coveringNode, coveredNodes, null) |
149 |
|| getConvertSwitchToIfProposals(context, coveringNode, null) |
150 |
|| getConvertSwitchToIfProposals(context, coveringNode, null) |
|
|
151 |
|| getConvertBaseProposals(context, coveringNode, null) |
150 |
|| GetterSetterCorrectionSubProcessor.addGetterSetterProposal(context, coveringNode, null, null); |
152 |
|| GetterSetterCorrectionSubProcessor.addGetterSetterProposal(context, coveringNode, null, null); |
151 |
} |
153 |
} |
152 |
return false; |
154 |
return false; |
Lines 185-190
Link Here
|
185 |
getPullNegationUpProposals(context, coveredNodes, resultingCollections); |
187 |
getPullNegationUpProposals(context, coveredNodes, resultingCollections); |
186 |
getJoinIfListInIfElseIfProposals(context, coveringNode, coveredNodes, resultingCollections); |
188 |
getJoinIfListInIfElseIfProposals(context, coveringNode, coveredNodes, resultingCollections); |
187 |
getConvertSwitchToIfProposals(context, coveringNode, resultingCollections); |
189 |
getConvertSwitchToIfProposals(context, coveringNode, resultingCollections); |
|
|
190 |
getConvertBaseProposals(context, coveringNode, resultingCollections); |
188 |
GetterSetterCorrectionSubProcessor.addGetterSetterProposal(context, coveringNode, locations, resultingCollections); |
191 |
GetterSetterCorrectionSubProcessor.addGetterSetterProposal(context, coveringNode, locations, resultingCollections); |
189 |
} |
192 |
} |
190 |
|
193 |
|
Lines 2345-2348
Link Here
|
2345 |
} |
2348 |
} |
2346 |
return (Statement) rewrite.createMoveTarget(source); |
2349 |
return (Statement) rewrite.createMoveTarget(source); |
2347 |
} |
2350 |
} |
|
|
2351 |
|
2352 |
private static boolean getConvertBaseProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) { |
2353 |
//TODO: 0xCAFEBABE - quick assist on this throws exceptions -- range problems I suppose |
2354 |
//TODO: does not work with literals having underscore, maybe have to try with newer JDK |
2355 |
|
2356 |
if (!(covering instanceof NumberLiteral)) |
2357 |
return false; |
2358 |
|
2359 |
NumberLiteral numberLiteral= (NumberLiteral) covering; |
2360 |
String token= numberLiteral.getToken(); |
2361 |
if (token.indexOf('.') != -1) |
2362 |
return false; |
2363 |
|
2364 |
if(resultingCollections==null) |
2365 |
return true; |
2366 |
|
2367 |
AST ast= covering.getAST(); |
2368 |
Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE); |
2369 |
if (token.startsWith("0b")) { //$NON-NLS-1$ |
2370 |
//binary |
2371 |
String binaryString= token.substring(2); |
2372 |
int i= Integer.parseInt(binaryString, 2); |
2373 |
|
2374 |
convertToOctal(context, resultingCollections, numberLiteral, ast, image, i); |
2375 |
convertToHex(context, resultingCollections, numberLiteral, ast, image, i); |
2376 |
convertToDecimal(context, resultingCollections, numberLiteral, ast, image, i); |
2377 |
return true; |
2378 |
} else if (token.startsWith("0x")) { //$NON-NLS-1$ |
2379 |
//hex |
2380 |
String hexString= token.substring(2); |
2381 |
int i= Integer.parseInt(hexString, 16); |
2382 |
|
2383 |
convertToBinary(context, resultingCollections, numberLiteral, ast, image, i); |
2384 |
convertToOctal(context, resultingCollections, numberLiteral, ast, image, i); |
2385 |
convertToDecimal(context, resultingCollections, numberLiteral, ast, image, i); |
2386 |
return true; |
2387 |
} else if (token.startsWith("0")) { //$NON-NLS-1$ |
2388 |
//octal |
2389 |
String octalString= token.substring(1); |
2390 |
int i= Integer.parseInt(octalString, 8); |
2391 |
|
2392 |
convertToBinary(context, resultingCollections, numberLiteral, ast, image, i); |
2393 |
convertToHex(context, resultingCollections, numberLiteral, ast, image, i); |
2394 |
convertToDecimal(context, resultingCollections, numberLiteral, ast, image, i); |
2395 |
return true; |
2396 |
} else { |
2397 |
//decimal |
2398 |
int i= Integer.parseInt(token); |
2399 |
|
2400 |
convertToBinary(context, resultingCollections, numberLiteral, ast, image, i); |
2401 |
convertToOctal(context, resultingCollections, numberLiteral, ast, image, i); |
2402 |
convertToHex(context, resultingCollections, numberLiteral, ast, image, i); |
2403 |
return true; |
2404 |
} |
2405 |
} |
2406 |
|
2407 |
private static void convertToBinary(IInvocationContext context, Collection<ICommandAccess> resultingCollections, NumberLiteral numberLiteral, AST ast, Image image, int i) { |
2408 |
if(!JavaModelUtil.is70OrHigher(context.getCompilationUnit().getJavaProject())) |
2409 |
return; |
2410 |
ASTRewrite rewrite= ASTRewrite.create(ast); |
2411 |
NumberLiteral binary= ast.newNumberLiteral(); |
2412 |
binary.setToken("0b".concat(Integer.toBinaryString(i))); //$NON-NLS-1$ |
2413 |
rewrite.replace(numberLiteral, binary, null); |
2414 |
String label= "Convert to Binary"; |
2415 |
ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, -10, image); |
2416 |
resultingCollections.add(proposal); |
2417 |
} |
2418 |
|
2419 |
private static void convertToDecimal(IInvocationContext context, Collection<ICommandAccess> resultingCollections, NumberLiteral numberLiteral, AST ast, Image image, int i) { |
2420 |
ASTRewrite rewrite= ASTRewrite.create(ast); |
2421 |
NumberLiteral decimal= ast.newNumberLiteral(); |
2422 |
decimal.setToken(Integer.toString(i)); |
2423 |
rewrite.replace(numberLiteral, decimal, null); |
2424 |
String label= "Convert to Decimal"; |
2425 |
ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, -10, image); |
2426 |
resultingCollections.add(proposal); |
2427 |
} |
2428 |
|
2429 |
private static void convertToHex(IInvocationContext context, Collection<ICommandAccess> resultingCollections, NumberLiteral numberLiteral, AST ast, Image image, int i) { |
2430 |
ASTRewrite rewrite= ASTRewrite.create(ast); |
2431 |
NumberLiteral hex= ast.newNumberLiteral(); |
2432 |
hex.setToken("0x".concat(Integer.toHexString(i))); //$NON-NLS-1$ |
2433 |
rewrite.replace(numberLiteral, hex, null); |
2434 |
String label= "Convert to Hexadecimal"; |
2435 |
ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, -10, image); |
2436 |
resultingCollections.add(proposal); |
2437 |
} |
2438 |
|
2439 |
private static void convertToOctal(IInvocationContext context, Collection<ICommandAccess> resultingCollections, NumberLiteral numberLiteral, AST ast, Image image, int i) { |
2440 |
ASTRewrite rewrite= ASTRewrite.create(ast); |
2441 |
NumberLiteral octal= ast.newNumberLiteral(); |
2442 |
octal.setToken("0".concat(Integer.toOctalString(i))); //$NON-NLS-1$ |
2443 |
rewrite.replace(numberLiteral, octal, null); |
2444 |
String label= "Convert to Octal"; |
2445 |
ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, -10, image); |
2446 |
resultingCollections.add(proposal); |
2447 |
} |
2348 |
} |
2448 |
} |