mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Project Coin numeric literals (InspectionGadgets)
This commit is contained in:
+7
-7
@@ -17,10 +17,7 @@ package com.siyeh.ig.numeric;
|
||||
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.JavaPsiFacade;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiElementFactory;
|
||||
import com.intellij.psi.PsiExpression;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.InspectionGadgetsFix;
|
||||
@@ -39,12 +36,15 @@ class ConvertOctalLiteralToDecimalFix
|
||||
protected void doFix(Project project, ProblemDescriptor descriptor)
|
||||
throws IncorrectOperationException {
|
||||
final PsiElement element = descriptor.getPsiElement();
|
||||
final String text = element.getText();
|
||||
final int number = Integer.parseInt(text, 8);
|
||||
if (!(element instanceof PsiLiteralExpression)) return;
|
||||
|
||||
final Object value = ((PsiLiteralExpression)element).getValue();
|
||||
if (value == null) return;
|
||||
|
||||
final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
|
||||
final PsiElementFactory factory = psiFacade.getElementFactory();
|
||||
final PsiExpression decimalNumber =
|
||||
factory.createExpressionFromText(Integer.toString(number),
|
||||
factory.createExpressionFromText(value.toString(),
|
||||
element);
|
||||
element.replace(decimalNumber);
|
||||
}
|
||||
|
||||
@@ -20,11 +20,12 @@ import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
import com.siyeh.ig.InspectionGadgetsFix;
|
||||
import org.intellij.lang.annotations.Pattern;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class OctalLiteralInspection extends BaseInspection {
|
||||
|
||||
@Pattern(VALID_ID_PATTERN)
|
||||
@Override
|
||||
@NotNull
|
||||
public String getID() {
|
||||
@@ -77,13 +78,17 @@ public class OctalLiteralInspection extends BaseInspection {
|
||||
return;
|
||||
}
|
||||
@NonNls final String text = literal.getText();
|
||||
if ("0".equals(text) || "0L".equals(text) || "0l".equals(text)) {
|
||||
if (text.length() == 1) {
|
||||
return;
|
||||
}
|
||||
if (text.charAt(0) != '0') {
|
||||
return;
|
||||
}
|
||||
if (text.startsWith("0x") || text.startsWith("0X")) {
|
||||
final char c1 = text.charAt(1);
|
||||
if (c1 != '_' && (c1 < '0' || c1 > '7')) {
|
||||
return;
|
||||
}
|
||||
if (literal.getValue() == null) {
|
||||
return;
|
||||
}
|
||||
registerError(literal);
|
||||
|
||||
@@ -44,7 +44,7 @@ class RemoveLeadingZeroFix extends InspectionGadgetsFix {
|
||||
return;
|
||||
}
|
||||
int index = 0;
|
||||
while (index < max && text.charAt(index) == '0') {
|
||||
while (index < max && (text.charAt(index) == '0' || text.charAt(index) == '_')) {
|
||||
index++;
|
||||
}
|
||||
final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
class C {
|
||||
int i = 511;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class C {
|
||||
int i = <caret>0777;
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
class C {
|
||||
int i = 511;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class C {
|
||||
int i = <caret>0_7_7_7;
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
class C {
|
||||
int i = 7_7_7;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class C {
|
||||
int i = <caret>0__7_7_7;
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
public class OctalRecognition {
|
||||
public void f() {
|
||||
int i1 = 0123;
|
||||
int i2 = 0_123;
|
||||
|
||||
int i3 = 0;
|
||||
int i4 = 7;
|
||||
int i5 = 0x0123;
|
||||
int i6 = 0b0101;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems>
|
||||
<problem>
|
||||
<file>OctalRecognition.java</file>
|
||||
<line>3</line>
|
||||
<problem_class>Octal integer</problem_class>
|
||||
<description>Octal integer <code>0123</code> #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>OctalRecognition.java</file>
|
||||
<line>4</line>
|
||||
<problem_class>Octal integer</problem_class>
|
||||
<description>Octal integer <code>0_123</code> #loc</description>
|
||||
</problem>
|
||||
</problems>
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2000-2011 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.siyeh.ig.numeric;
|
||||
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.fixes.bugs.IGQuickFixesTestCase;
|
||||
|
||||
public class OctalLiteralFixesTest extends IGQuickFixesTestCase {
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
myFixture.enableInspections(new OctalLiteralInspection());
|
||||
}
|
||||
|
||||
public void testConvertOctalToDecimal1() {
|
||||
doTest("ConvertToDecimal1", InspectionGadgetsBundle.message("convert.octal.literal.to.decimal.literal.quickfix"));
|
||||
}
|
||||
|
||||
public void testConvertOctalToDecimal2() {
|
||||
doTest("ConvertToDecimal2", InspectionGadgetsBundle.message("convert.octal.literal.to.decimal.literal.quickfix"));
|
||||
}
|
||||
|
||||
public void testRemoveLeadingZero() {
|
||||
doTest("RemoveLeadingZero", InspectionGadgetsBundle.message("remove.leading.zero.to.make.decimal.quickfix"));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getRelativePath() {
|
||||
return "numeric/octal";
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Copyright 2000-2011 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.siyeh.ig.numeric;
|
||||
|
||||
import com.IGInspectionTestCase;
|
||||
|
||||
public class OctalLiteralInspectionTest extends IGInspectionTestCase {
|
||||
public void test() throws Exception {
|
||||
doTest("com/siyeh/igtest/numeric/octal_inspection",
|
||||
new OctalLiteralInspection());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user