diff options
Diffstat (limited to 'plugins/InspectionGadgets')
70 files changed, 1058 insertions, 641 deletions
diff --git a/plugins/InspectionGadgets/src/META-INF/plugin.xml b/plugins/InspectionGadgets/src/META-INF/plugin.xml index 1df5b70f00e4..3e5977f50329 100644 --- a/plugins/InspectionGadgets/src/META-INF/plugin.xml +++ b/plugins/InspectionGadgets/src/META-INF/plugin.xml @@ -455,7 +455,7 @@ level="WARNING" implementationClass="com.siyeh.ig.classlayout.MarkerInterfaceInspection"/> <localInspection language="JAVA" shortName="MissingDeprecatedAnnotation" bundle="com.siyeh.InspectionGadgetsBundle" key="missing.deprecated.annotation.display.name" groupBundle="messages.InspectionsBundle" - groupKey="group.names.class.structure" enabledByDefault="false" level="WARNING" + groupKey="group.names.class.structure" enabledByDefault="false" level="WARNING" runForWholeFile="true" implementationClass="com.siyeh.ig.classlayout.MissingDeprecatedAnnotationInspection"/> <localInspection language="JAVA" suppressId="override" shortName="MissingOverrideAnnotation" bundle="com.siyeh.InspectionGadgetsBundle" key="missing.override.annotation.display.name" groupBundle="messages.InspectionsBundle" @@ -1197,7 +1197,7 @@ key="html.tag.can.be.javadoc.tag.display.name" groupBundle="messages.InspectionsBundle" groupKey="group.names.javadoc.issues" enabledByDefault="false" level="WARNING" implementationClass="com.siyeh.ig.javadoc.HtmlTagCanBeJavadocTagInspection"/> - <localInspection language="JAVA" shortName="PackageDotHtmlMayBePackageInfo" bundle="com.siyeh.InspectionGadgetsBundle" + <localInspection language="XML" shortName="PackageDotHtmlMayBePackageInfo" bundle="com.siyeh.InspectionGadgetsBundle" key="package.dot.html.may.be.package.info.display.name" groupBundle="messages.InspectionsBundle" groupKey="group.names.javadoc.issues" enabledByDefault="false" level="WARNING" implementationClass="com.siyeh.ig.javadoc.PackageDotHtmlMayBePackageInfoInspection"/> diff --git a/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties b/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties index 723ac434de4c..712c06aba4a2 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties +++ b/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties @@ -7,7 +7,6 @@ class.references.subclass.display.name=Class references one of its subclasses class.references.subclass.problem.descriptor=Class ''{0}'' references subclass <code>#ref</code> #loc class.references.subclass.problem.descriptor.anonymous=Anonymous class references subclass <code>#ref</code> #loc collection.declared.by.class.display.name=Collection declared by class, not interface -#{0} - name of the interface collection.declared.by.class.problem.descriptor=Declaration of <code>#ref</code> should probably be weakened to ''{0}'' #loc collection.declared.by.class.ignore.locals.option=Ignore local variables collection.declared.by.class.ignore.private.members.option=Ignore private fields and methods @@ -184,13 +183,11 @@ unnecessary.code.block.display.name=Unnecessary code block unnecessary.code.block.unwrap.quickfix=Unwrap block redundant.local.variable.display.name=Redundant local variable redundant.local.variable.ignore.option=Ignore immediately returned or thrown variables -redundant.local.variable.annotation.option=Ignore variables which have an annotation (JDK 5.0 only) +redundant.local.variable.annotation.option=Ignore variables which have an annotation static.collection.display.name=Static collection static.collection.problem.descriptor=Static collection <code>#ref</code> #loc static.collection.ignore.option=Ignore weak static collections or maps -#field of type java.lang.StringBuffer stringbuffer.field.display.name=StringBuffer field -#{0} - concrete field type stringbuffer.field.problem.descriptor=''{0}'' field <code>#ref</code> #loc gc.call.display.name=Calls to 'System.gc()' or 'Runtime.gc()' gc.call.problem.descriptor=<code>#ref</code> should not be called in production code #loc @@ -503,6 +500,7 @@ inner.class.too.deeply.nested.nesting.limit.option=Nesting limit: inner.class.too.deeply.nested.problem.descriptor=<code>#ref</code> is too deeply nested (nesting level = {0}) #loc too.many.constructors.display.name=Class with too many constructors too.many.constructors.count.limit.option=Constructor count limit: +too.many.constructors.ignore.deprecated.option=Ignore deprecated constructors too.many.constructors.problem.descriptor=<code>#ref</code> has too many constructors (constructor count = {0}) #loc too.many.fields.display.name=Class with too many fields too.many.fields.count.limit.option=Field count limit: @@ -610,7 +608,7 @@ call.to.simple.setter.in.class.display.name=Call to simple setter from within cl comparison.to.nan.display.name=Comparison to Double.NaN or Float.NaN instance.method.naming.convention.display.name=Instance method naming convention unnecessary.semicolon.display.name=Unnecessary semicolon -fallthru.in.switch.statement.display.name=Fallthrough in 'switch' statement +fallthru.in.switch.statement.display.name=Fall-through in 'switch' statement call.to.native.method.while.locked.display.name=Call to a native method while locked switch.statement.display.name='switch' statement custom.classloader.display.name=Custom ClassLoader @@ -874,7 +872,7 @@ divide.by.zero.problem.descriptor=Division by zero #loc non.reproducible.math.call.problem.descriptor=<code>Math.#ref()</code> may produce non-reproducible results #loc constant.math.call.problem.descriptor=Constant call to <code>#ref()</code> can be simplified #loc floating.point.equality.problem.descriptor=<code>#ref</code>: floating point values compared for exact equality #loc -fallthru.in.switch.statement.problem.descriptor=<code>#ref</code> fallthrough in 'switch' statement #loc +fallthru.in.switch.statement.problem.descriptor=<code>#ref</code> fall-through in 'switch' statement #loc switch.statements.without.default.problem.descriptor=<code>#ref</code> statement without 'default' branch #loc default.not.last.case.in.switch.problem.descriptor=<code>#ref</code> branch not last case in 'switch' statement #loc loop.statements.that.dont.loop.problem.descriptor=<code>#ref</code> statement does not loop #loc @@ -1401,7 +1399,6 @@ non.boolean.method.name.must.not.start.with.question.display.name=Non-boolean me non.boolean.method.name.must.not.start.with.question.problem.descriptor=Non-boolean method name <code>#ref</code> starts with a question word #loc boolean.constructor.simplify.quickfix=Simplify unnecessary.temporary.on.conversion.from.string.problem.descriptor=<code>#ref</code> #loc can be simplified to ''{0}'' -# 0 - replacement exception unnecessary.temporary.on.conversion.from.string.fix.name=Replace with ''{0}'' only.report.qualified.static.usages.option=Only report qualified static access from a static context unqualified,static.usage.only.report.static.usages.option=Only report static access from a non-static context diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/InspectionComparator.java b/plugins/InspectionGadgets/src/com/siyeh/ig/InspectionComparator.java deleted file mode 100644 index e792c0e1e743..000000000000 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/InspectionComparator.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 2003-2005 Dave Griffith - * - * 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; - -import com.intellij.codeInspection.InspectionProfileEntry; - -import java.util.Comparator; - -class InspectionComparator - implements Comparator<Class<? extends InspectionProfileEntry>> { - - public int compare(Class<? extends InspectionProfileEntry> class1, - Class<? extends InspectionProfileEntry> class2) { - final InspectionProfileEntry inspection1; - final InspectionProfileEntry inspection2; - try { - inspection1 = class1.newInstance(); - inspection2 = class2.newInstance(); - } - catch (InstantiationException ignore) { - return -1; - } - catch (IllegalAccessException ignore) { - return -1; - } - final String groupName1 = inspection1.getGroupDisplayName(); - final String groupName2 = inspection2.getGroupDisplayName(); - final int groupNameComparison = groupName1.compareTo(groupName2); - if (groupNameComparison != 0) { - return groupNameComparison; - } - String displayName1 = inspection1.getDisplayName(); - String displayName2 = inspection2.getDisplayName(); - displayName1 = displayName1.toUpperCase(); - displayName2 = displayName2.toUpperCase(); - displayName1 = stripQuotes(displayName1); - displayName2 = stripQuotes(displayName2); - return displayName1.compareTo(displayName2); - } - - private static String stripQuotes(String str) { - if (str.indexOf((int)'\'') < 0 && str.indexOf((int)'"') < 0) { - return str; - } - final int length = str.length(); - final StringBuffer buffer = new StringBuffer(length); - for (int i = 0; i < length; i++) { - final char ch = str.charAt(i); - if (ch != '"' && ch != '\'') { - buffer.append(ch); - } - } - return buffer.toString(); - } -}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/EmptyStatementBodyInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/EmptyStatementBodyInspection.java index a1717c97b5f6..4297498b096a 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/EmptyStatementBodyInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/EmptyStatementBodyInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2007 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2013 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,7 +29,7 @@ public class EmptyStatementBodyInspection extends BaseInspection { /** * @noinspection PublicField */ - public boolean m_reportEmptyBlocks = false; + public boolean m_reportEmptyBlocks = true; @NotNull public String getID() { @@ -38,14 +38,12 @@ public class EmptyStatementBodyInspection extends BaseInspection { @NotNull public String getDisplayName() { - return InspectionGadgetsBundle.message( - "statement.with.empty.body.display.name"); + return InspectionGadgetsBundle.message("statement.with.empty.body.display.name"); } @NotNull public String buildErrorString(Object... infos) { - return InspectionGadgetsBundle.message( - "statement.with.empty.body.problem.descriptor"); + return InspectionGadgetsBundle.message("statement.with.empty.body.problem.descriptor"); } public boolean isEnabledByDefault() { @@ -53,10 +51,8 @@ public class EmptyStatementBodyInspection extends BaseInspection { } public JComponent createOptionsPanel() { - return new SingleCheckboxOptionsPanel( - InspectionGadgetsBundle.message( - "statement.with.empty.body.include.option"), - this, "m_reportEmptyBlocks"); + return new SingleCheckboxOptionsPanel(InspectionGadgetsBundle.message("statement.with.empty.body.include.option"), + this, "m_reportEmptyBlocks"); } public BaseInspectionVisitor buildVisitor() { @@ -66,50 +62,31 @@ public class EmptyStatementBodyInspection extends BaseInspection { private class EmptyStatementVisitor extends BaseInspectionVisitor { @Override - public void visitDoWhileStatement( - @NotNull PsiDoWhileStatement statement) { + public void visitDoWhileStatement(@NotNull PsiDoWhileStatement statement) { super.visitDoWhileStatement(statement); - if (JspPsiUtil.isInJspFile(statement.getContainingFile())) { - return; - } - final PsiStatement body = statement.getBody(); - if (body == null || !isEmpty(body)) { - return; - } - registerStatementError(statement); + checkLoopStatement(statement); } @Override public void visitWhileStatement(@NotNull PsiWhileStatement statement) { super.visitWhileStatement(statement); - if (JspPsiUtil.isInJspFile(statement.getContainingFile())) { - return; - } - final PsiStatement body = statement.getBody(); - if (body == null || !isEmpty(body)) { - return; - } - registerStatementError(statement); + checkLoopStatement(statement); } @Override public void visitForStatement(@NotNull PsiForStatement statement) { super.visitForStatement(statement); - if (JspPsiUtil.isInJspFile(statement.getContainingFile())) { - return; - } - final PsiStatement body = statement.getBody(); - if (body == null || !isEmpty(body)) { - return; - } - registerStatementError(statement); + checkLoopStatement(statement); } @Override - public void visitForeachStatement( - @NotNull PsiForeachStatement statement) { + public void visitForeachStatement(@NotNull PsiForeachStatement statement) { super.visitForeachStatement(statement); - if (JspPsiUtil.isInJspFile(statement.getContainingFile())) { + checkLoopStatement(statement); + } + + private void checkLoopStatement(PsiLoopStatement statement) { + if (JspPsiUtil.isInJspFile(statement)) { return; } final PsiStatement body = statement.getBody(); @@ -122,7 +99,7 @@ public class EmptyStatementBodyInspection extends BaseInspection { @Override public void visitIfStatement(@NotNull PsiIfStatement statement) { super.visitIfStatement(statement); - if (JspPsiUtil.isInJspFile(statement.getContainingFile())) { + if (JspPsiUtil.isInJspFile(statement)) { return; } final PsiStatement thenBranch = statement.getThenBranch(); @@ -140,26 +117,41 @@ public class EmptyStatementBodyInspection extends BaseInspection { } } + @Override + public void visitSwitchStatement(PsiSwitchStatement statement) { + super.visitSwitchStatement(statement); + if (JspPsiUtil.isInJspFile(statement)) { + return; + } + final PsiCodeBlock body = statement.getBody(); + if (body == null || !isEmpty(body)) { + return; + } + registerStatementError(statement); + } + private boolean isEmpty(PsiElement body) { if (body instanceof PsiEmptyStatement) { return true; } - else if (m_reportEmptyBlocks && - body instanceof PsiBlockStatement) { + else if (body instanceof PsiBlockStatement) { final PsiBlockStatement block = (PsiBlockStatement)body; - final PsiCodeBlock codeBlock = block.getCodeBlock(); - return codeBlockIsEmpty(codeBlock); + return isEmpty(block.getCodeBlock()); } - else if (body instanceof PsiCodeBlock) { + else if (m_reportEmptyBlocks && body instanceof PsiCodeBlock) { final PsiCodeBlock codeBlock = (PsiCodeBlock)body; - return codeBlockIsEmpty(codeBlock); + final PsiStatement[] statements = codeBlock.getStatements(); + if (statements.length == 0) { + return true; + } + for (PsiStatement statement : statements) { + if (!isEmpty(statement)) { + return false; + } + } + return true; } return false; } - - private boolean codeBlockIsEmpty(PsiCodeBlock codeBlock) { - final PsiStatement[] statements = codeBlock.getStatements(); - return statements.length == 0; - } } }
\ No newline at end of file diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/classlayout/MissingDeprecatedAnnotationInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/classlayout/MissingDeprecatedAnnotationInspection.java index 4af986fe28ff..dcaa6c0fe205 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/classlayout/MissingDeprecatedAnnotationInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/classlayout/MissingDeprecatedAnnotationInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2010 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2013 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,7 +21,6 @@ import com.intellij.psi.*; import com.intellij.psi.javadoc.PsiDocComment; import com.intellij.psi.javadoc.PsiDocTag; import com.intellij.psi.util.PsiUtil; -import com.intellij.util.IncorrectOperationException; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; @@ -33,15 +32,18 @@ public class MissingDeprecatedAnnotationInspection extends BaseInspection { @Override @NotNull public String getDisplayName() { - return InspectionGadgetsBundle.message( - "missing.deprecated.annotation.display.name"); + return InspectionGadgetsBundle.message("missing.deprecated.annotation.display.name"); } @Override @NotNull protected String buildErrorString(Object... infos) { - return InspectionGadgetsBundle.message( - "missing.deprecated.annotation.problem.descriptor"); + return InspectionGadgetsBundle.message("missing.deprecated.annotation.problem.descriptor"); + } + + @Override + public boolean runForWholeFile() { + return true; } @Override @@ -49,27 +51,22 @@ public class MissingDeprecatedAnnotationInspection extends BaseInspection { return new MissingDeprecatedAnnotationFix(); } - private static class MissingDeprecatedAnnotationFix - extends InspectionGadgetsFix { + private static class MissingDeprecatedAnnotationFix extends InspectionGadgetsFix { @NotNull public String getName() { - return InspectionGadgetsBundle.message( - "missing.deprecated.annotation.add.quickfix"); + return InspectionGadgetsBundle.message("missing.deprecated.annotation.add.quickfix"); } @Override - public void doFix(Project project, ProblemDescriptor descriptor) - throws IncorrectOperationException { + public void doFix(Project project, ProblemDescriptor descriptor) { final PsiElement identifier = descriptor.getPsiElement(); - final PsiModifierListOwner parent = - (PsiModifierListOwner)identifier.getParent(); - assert parent != null; - final PsiElementFactory factory = - JavaPsiFacade.getElementFactory(project); - final PsiAnnotation annotation = - factory.createAnnotationFromText("@java.lang.Deprecated", - parent); + final PsiModifierListOwner parent = (PsiModifierListOwner)identifier.getParent(); + if (parent == null) { + return; + } + final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); + final PsiAnnotation annotation = factory.createAnnotationFromText("@java.lang.Deprecated", parent); final PsiModifierList modifierList = parent.getModifierList(); if (modifierList == null) { return; @@ -83,8 +80,7 @@ public class MissingDeprecatedAnnotationInspection extends BaseInspection { return new MissingDeprecatedAnnotationVisitor(); } - private static class MissingDeprecatedAnnotationVisitor - extends BaseInspectionVisitor { + private static class MissingDeprecatedAnnotationVisitor extends BaseInspectionVisitor { @Override public void visitClass(@NotNull PsiClass aClass) { @@ -92,10 +88,7 @@ public class MissingDeprecatedAnnotationInspection extends BaseInspection { if (!PsiUtil.isLanguageLevel5OrHigher(aClass)) { return; } - if (!hasDeprecatedComment(aClass)) { - return; - } - if (hasDeprecatedAnnotation(aClass)) { + if (!hasDeprecatedComment(aClass) || hasDeprecatedAnnotation(aClass)) { return; } registerClassError(aClass); @@ -103,16 +96,13 @@ public class MissingDeprecatedAnnotationInspection extends BaseInspection { @Override public void visitMethod(@NotNull PsiMethod method) { - if (method.getNameIdentifier() == null) { - return; - } if (!PsiUtil.isLanguageLevel5OrHigher(method)) { return; } - if (!hasDeprecatedComment(method)) { + if (method.getNameIdentifier() == null) { return; } - if (hasDeprecatedAnnotation(method)) { + if (!hasDeprecatedComment(method) || hasDeprecatedAnnotation(method)) { return; } registerMethodError(method); @@ -123,29 +113,22 @@ public class MissingDeprecatedAnnotationInspection extends BaseInspection { if (!PsiUtil.isLanguageLevel5OrHigher(field)) { return; } - if (!hasDeprecatedComment(field)) { - return; - } - if (hasDeprecatedAnnotation(field)) { + if (!hasDeprecatedComment(field) || hasDeprecatedAnnotation(field)) { return; } registerFieldError(field); } - private static boolean hasDeprecatedAnnotation( - PsiModifierListOwner element) { + private static boolean hasDeprecatedAnnotation(PsiModifierListOwner element) { final PsiModifierList modifierList = element.getModifierList(); if (modifierList == null) { return false; } - final PsiAnnotation annotation = - modifierList.findAnnotation( - CommonClassNames.JAVA_LANG_DEPRECATED); + final PsiAnnotation annotation = modifierList.findAnnotation(CommonClassNames.JAVA_LANG_DEPRECATED); return annotation != null; } - private static boolean hasDeprecatedComment( - PsiDocCommentOwner element) { + private static boolean hasDeprecatedComment(PsiDocCommentOwner element) { final PsiDocComment comment = element.getDocComment(); if (comment == null) { return false; diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/classlayout/NonFinalFieldInEnumInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/classlayout/NonFinalFieldInEnumInspection.java index 967ab5f5c944..f292af39ff21 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/classlayout/NonFinalFieldInEnumInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/classlayout/NonFinalFieldInEnumInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * Copyright 2000-2013 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. @@ -21,8 +21,11 @@ import com.intellij.psi.PsiModifier; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; +import com.siyeh.ig.InspectionGadgetsFix; +import com.siyeh.ig.fixes.MakeFieldFinalFix; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * @author Bas Leijdekkers @@ -43,6 +46,12 @@ public class NonFinalFieldInEnumInspection extends BaseInspection { return InspectionGadgetsBundle.message("non.final.field.in.enum.problem.descriptor", enumClass.getName()); } + @Nullable + protected InspectionGadgetsFix buildFix(Object... infos) { + final PsiField field = (PsiField)infos[1]; + return MakeFieldFinalFix.buildFix(field); + } + @Override public BaseInspectionVisitor buildVisitor() { return new NonFinalFieldInEnumVisitor(); @@ -60,7 +69,7 @@ public class NonFinalFieldInEnumInspection extends BaseInspection { if (field.hasModifierProperty(PsiModifier.FINAL)) { return; } - registerFieldError(field, containingClass); + registerFieldError(field, containingClass, field); } } } diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/classlayout/PublicConstructorInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/classlayout/PublicConstructorInspection.java index dcdd38ca5934..9ef67e948788 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/classlayout/PublicConstructorInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/classlayout/PublicConstructorInspection.java @@ -58,6 +58,11 @@ public class PublicConstructorInspection extends BaseInspection { } } + @Override + protected boolean buildQuickFixesOnlyForOnTheFlyErrors() { + return true; + } + @Nullable @Override protected InspectionGadgetsFix buildFix(Object... infos) { @@ -103,7 +108,11 @@ public class PublicConstructorInspection extends BaseInspection { if (!method.hasModifierProperty(PsiModifier.PUBLIC)) { return; } - if (SerializationUtils.isExternalizable(method.getContainingClass())) { + final PsiClass aClass = method.getContainingClass(); + if (aClass == null || aClass.hasModifierProperty(PsiModifier.ABSTRACT)) { + return; + } + if (SerializationUtils.isExternalizable(aClass)) { final PsiParameterList parameterList = method.getParameterList(); if (parameterList.getParametersCount() == 0) { return; @@ -118,7 +127,7 @@ public class PublicConstructorInspection extends BaseInspection { if (aClass.isInterface() || aClass.isEnum()) { return; } - if (!aClass.hasModifierProperty(PsiModifier.PUBLIC)) { + if (!aClass.hasModifierProperty(PsiModifier.PUBLIC) || aClass.hasModifierProperty(PsiModifier.ABSTRACT)) { return; } final PsiMethod[] constructors = aClass.getConstructors(); diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/classmetrics/ConstructorCountInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/classmetrics/ConstructorCountInspection.java index 2e6ac57bac66..39f61b3d4636 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/classmetrics/ConstructorCountInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/classmetrics/ConstructorCountInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2007 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2013 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,14 +16,23 @@ package com.siyeh.ig.classmetrics; import com.intellij.psi.PsiClass; +import com.intellij.psi.PsiMethod; +import com.intellij.util.ui.CheckBox; +import com.intellij.util.ui.GridBag; +import com.intellij.util.ui.UIUtil; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspectionVisitor; import org.jetbrains.annotations.NotNull; +import javax.swing.*; +import java.awt.*; + public class ConstructorCountInspection extends ClassMetricInspection { private static final int CONSTRUCTOR_COUNT_LIMIT = 5; + public boolean ignoreDeprecatedConstructors = false; + @NotNull public String getID() { return "ClassWithTooManyConstructors"; @@ -31,8 +40,7 @@ public class ConstructorCountInspection extends ClassMetricInspection { @NotNull public String getDisplayName() { - return InspectionGadgetsBundle.message( - "too.many.constructors.display.name"); + return InspectionGadgetsBundle.message("too.many.constructors.display.name"); } protected int getDefaultLimit() { @@ -40,15 +48,30 @@ public class ConstructorCountInspection extends ClassMetricInspection { } protected String getConfigurationLabel() { - return InspectionGadgetsBundle.message( - "too.many.constructors.count.limit.option"); + return InspectionGadgetsBundle.message("too.many.constructors.count.limit.option"); + } + + @Override + public JComponent createOptionsPanel() { + final JLabel label = new JLabel(getConfigurationLabel()); + final JFormattedTextField valueField = prepareNumberEditor("m_limit"); + final CheckBox includeCheckBox = + new CheckBox(InspectionGadgetsBundle.message("too.many.constructors.ignore.deprecated.option"), this, "ignoreDeprecatedConstructors"); + + final GridBag bag = new GridBag(); + bag.setDefaultInsets(0, 0, 0, UIUtil.DEFAULT_HGAP); + bag.setDefaultAnchor(GridBagConstraints.WEST); + final JPanel panel = new JPanel(new GridBagLayout()); + panel.add(label, bag.nextLine().next()); + panel.add(valueField, bag.next().weightx(1.0)); + panel.add(includeCheckBox, bag.nextLine().next().coverLine().weighty(1.0).anchor(GridBagConstraints.NORTHWEST)); + return panel; } @NotNull public String buildErrorString(Object... infos) { final Integer count = (Integer)infos[0]; - return InspectionGadgetsBundle.message( - "too.many.constructors.problem.descriptor", count); + return InspectionGadgetsBundle.message("too.many.constructors.problem.descriptor", count); } public BaseInspectionVisitor buildVisitor() { @@ -59,7 +82,6 @@ public class ConstructorCountInspection extends ClassMetricInspection { @Override public void visitClass(@NotNull PsiClass aClass) { - // note: no call to super final int constructorCount = calculateTotalConstructorCount(aClass); if (constructorCount <= getLimit()) { return; @@ -68,7 +90,17 @@ public class ConstructorCountInspection extends ClassMetricInspection { } private int calculateTotalConstructorCount(PsiClass aClass) { - return aClass.getConstructors().length; + final PsiMethod[] constructors = aClass.getConstructors(); + if (!ignoreDeprecatedConstructors) { + return constructors.length; + } + int count = 0; + for (PsiMethod constructor : constructors) { + if (!constructor.isDeprecated()) { + count++; + } + } + return count; } } }
\ No newline at end of file diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/FallthruInSwitchStatementInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/FallthruInSwitchStatementInspection.java index 8cf2330b59ff..b98a32051bb3 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/FallthruInSwitchStatementInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/FallthruInSwitchStatementInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2009 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2013 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,8 +16,10 @@ package com.siyeh.ig.controlflow; import com.intellij.codeInspection.ProblemDescriptor; +import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel; import com.intellij.openapi.project.Project; import com.intellij.psi.*; +import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.IncorrectOperationException; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; @@ -27,12 +29,14 @@ import com.siyeh.ig.psiutils.ControlFlowUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import javax.swing.*; +import java.util.regex.Pattern; + public class FallthruInSwitchStatementInspection extends BaseInspection { @NotNull public String getDisplayName() { - return InspectionGadgetsBundle.message( - "fallthru.in.switch.statement.display.name"); + return InspectionGadgetsBundle.message("fallthru.in.switch.statement.display.name"); } @NotNull @@ -42,8 +46,7 @@ public class FallthruInSwitchStatementInspection extends BaseInspection { @NotNull protected String buildErrorString(Object... infos) { - return InspectionGadgetsBundle.message( - "fallthru.in.switch.statement.problem.descriptor"); + return InspectionGadgetsBundle.message("fallthru.in.switch.statement.problem.descriptor"); } @Nullable @@ -55,52 +58,51 @@ public class FallthruInSwitchStatementInspection extends BaseInspection { return new FallthroughInSwitchStatementVisitor(); } - private static class FallthruInSwitchStatementFix - extends InspectionGadgetsFix { + private static class FallthruInSwitchStatementFix extends InspectionGadgetsFix { @NotNull public String getName() { - return InspectionGadgetsBundle.message( - "fallthru.in.switch.statement.quickfix"); + return InspectionGadgetsBundle.message("fallthru.in.switch.statement.quickfix"); } - protected void doFix(Project project, ProblemDescriptor descriptor) - throws IncorrectOperationException { - final PsiSwitchLabelStatement labelStatement = - (PsiSwitchLabelStatement)descriptor.getPsiElement(); + protected void doFix(Project project, ProblemDescriptor descriptor) { + final PsiSwitchLabelStatement labelStatement = (PsiSwitchLabelStatement)descriptor.getPsiElement(); final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); final PsiElementFactory factory = psiFacade.getElementFactory(); - final PsiStatement breakStatement = - factory.createStatementFromText("break;", labelStatement); + final PsiStatement breakStatement = factory.createStatementFromText("break;", labelStatement); final PsiElement parent = labelStatement.getParent(); parent.addBefore(breakStatement, labelStatement); } } - private static class FallthroughInSwitchStatementVisitor - extends BaseInspectionVisitor { + private static class FallthroughInSwitchStatementVisitor extends BaseInspectionVisitor { + + private static final Pattern commentPattern = Pattern.compile("(?i)falls?\\s*thro?u"); @Override - public void visitSwitchStatement( - @NotNull PsiSwitchStatement statement) { + public void visitSwitchStatement(@NotNull PsiSwitchStatement statement) { super.visitSwitchStatement(statement); final PsiCodeBlock body = statement.getBody(); if (body == null) { return; } - boolean switchLabelValid = true; final PsiStatement[] statements = body.getStatements(); - for (final PsiStatement child : statements) { - if (child instanceof PsiSwitchLabelStatement) { - if (!switchLabelValid) { - registerError(child); + for (int i = 1; i < statements.length; i++) { + final PsiStatement child = statements[i]; + if (!(child instanceof PsiSwitchLabelStatement)) { + continue; + } + final PsiElement previousSibling = PsiTreeUtil.skipSiblingsBackward(child, PsiWhiteSpace.class); + if (previousSibling instanceof PsiComment) { + final PsiComment comment = (PsiComment)previousSibling; + final String commentText = comment.getText(); + if (commentPattern.matcher(commentText).find()) { + continue; } - switchLabelValid = true; } - else { - switchLabelValid = - !ControlFlowUtils.statementMayCompleteNormally( - child); + final PsiStatement previousStatement = PsiTreeUtil.getPrevSiblingOfType(child, PsiStatement.class); + if (ControlFlowUtils.statementMayCompleteNormally(previousStatement)) { + registerError(child); } } } diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/LocalVariableAccessVisitor.java b/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/LocalVariableAccessVisitor.java deleted file mode 100644 index 5641dd92435c..000000000000 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/LocalVariableAccessVisitor.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2003-2006 Dave Griffith, Bas Leijdekkers - * - * 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.controlflow; - -import com.intellij.psi.*; -import org.jetbrains.annotations.NotNull; - -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; - -public class LocalVariableAccessVisitor extends JavaRecursiveElementVisitor { - - private final Set<PsiElement> m_accesssedVariables = - new HashSet<PsiElement>(2); - - @Override - public void visitReferenceExpression(@NotNull PsiReferenceExpression ref) { - super.visitReferenceExpression(ref); - final PsiExpression qualifier = ref.getQualifierExpression(); - if (qualifier != null && !(qualifier instanceof PsiThisExpression)) { - return; - } - final PsiElement element = ref.resolve(); - if (!(element instanceof PsiLocalVariable)) { - return; - } - m_accesssedVariables.add(element); - } - - public Set<PsiElement> getAccessedVariables() { - return Collections.unmodifiableSet(m_accesssedVariables); - } -}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/SwitchStatementDensityInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/SwitchStatementDensityInspection.java index 0aaaa86f8a7b..4ffd374c3cd2 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/SwitchStatementDensityInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/SwitchStatementDensityInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2011 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2013 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,10 +15,7 @@ */ package com.siyeh.ig.controlflow; -import com.intellij.psi.JavaRecursiveElementVisitor; -import com.intellij.psi.PsiCodeBlock; -import com.intellij.psi.PsiStatement; -import com.intellij.psi.PsiSwitchStatement; +import com.intellij.psi.*; import com.intellij.codeInspection.ui.SingleIntegerFieldOptionsPanel; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; @@ -31,34 +28,26 @@ import javax.swing.*; public class SwitchStatementDensityInspection extends BaseInspection { private static final int DEFAULT_DENSITY_LIMIT = 20; - /** - * this is public for the DefaultJDOMExternalizer thingy - * - * @noinspection PublicField - */ + + @SuppressWarnings("PublicField") public int m_limit = DEFAULT_DENSITY_LIMIT; @Override @NotNull public String getDisplayName() { - return InspectionGadgetsBundle.message( - "switch.statement.density.display.name"); + return InspectionGadgetsBundle.message("switch.statement.density.display.name"); } @Override public JComponent createOptionsPanel() { - return new SingleIntegerFieldOptionsPanel( - InspectionGadgetsBundle.message( - "switch.statement.density.min.option"), this, - "m_limit"); + return new SingleIntegerFieldOptionsPanel(InspectionGadgetsBundle.message("switch.statement.density.min.option"), this, "m_limit"); } @Override @NotNull protected String buildErrorString(Object... infos) { final Integer intDensity = (Integer)infos[0]; - return InspectionGadgetsBundle.message( - "switch.statement.density.problem.descriptor", intDensity); + return InspectionGadgetsBundle.message("switch.statement.density.problem.descriptor", intDensity); } @Override @@ -66,17 +55,19 @@ public class SwitchStatementDensityInspection extends BaseInspection { return new SwitchStatementDensityVisitor(); } - private class SwitchStatementDensityVisitor - extends BaseInspectionVisitor { + private class SwitchStatementDensityVisitor extends BaseInspectionVisitor { @Override - public void visitSwitchStatement( - @NotNull PsiSwitchStatement statement) { + public void visitSwitchStatement(@NotNull PsiSwitchStatement statement) { final PsiCodeBlock body = statement.getBody(); if (body == null) { return; } - final double density = calculateDensity(statement); + final int branchCount = SwitchUtils.calculateBranchCount(statement); + if (branchCount == 0) { + return; + } + final double density = calculateDensity(body, branchCount); final int intDensity = (int)(density * 100.0); if (intDensity > m_limit) { return; @@ -84,32 +75,28 @@ public class SwitchStatementDensityInspection extends BaseInspection { registerStatementError(statement, Integer.valueOf(intDensity)); } - private double calculateDensity(PsiSwitchStatement statement) { - final PsiCodeBlock body = statement.getBody(); - if (body == null) { - return -1.0; - } - final int numBranches = SwitchUtils.calculateBranchCount(statement); + private double calculateDensity(@NotNull PsiCodeBlock body, int branchCount) { final StatementCountVisitor visitor = new StatementCountVisitor(); body.accept(visitor); - final int numStatements = visitor.getNumStatements(); - return (double)numBranches / (double)numStatements; + return (double)branchCount / (double)visitor.getStatementCount(); } } - private static class StatementCountVisitor - extends JavaRecursiveElementVisitor { + private static class StatementCountVisitor extends JavaRecursiveElementVisitor { - private int numStatements = 0; + private int statementCount = 0; @Override - public void visitStatement(@NotNull PsiStatement psiStatement) { - super.visitStatement(psiStatement); - numStatements++; + public void visitStatement(@NotNull PsiStatement statement) { + super.visitStatement(statement); + if (statement instanceof PsiSwitchLabelStatement || statement instanceof PsiBreakStatement) { + return; + } + statementCount++; } - public int getNumStatements() { - return numStatements; + public int getStatementCount() { + return statementCount; } } }
\ No newline at end of file diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/SwitchStatementWithConfusingDeclarationInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/SwitchStatementWithConfusingDeclarationInspection.java index 9e985f116e09..98669f9bb982 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/SwitchStatementWithConfusingDeclarationInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/SwitchStatementWithConfusingDeclarationInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2007 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2013 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,8 +24,7 @@ import org.jetbrains.annotations.NotNull; import java.util.HashSet; import java.util.Set; -public class SwitchStatementWithConfusingDeclarationInspection - extends BaseInspection { +public class SwitchStatementWithConfusingDeclarationInspection extends BaseInspection { @NotNull public String getID() { @@ -34,66 +33,72 @@ public class SwitchStatementWithConfusingDeclarationInspection @NotNull public String getDisplayName() { - return InspectionGadgetsBundle.message( - "switch.statement.with.confusing.declaration.display.name"); + return InspectionGadgetsBundle.message("switch.statement.with.confusing.declaration.display.name"); } @NotNull protected String buildErrorString(Object... infos) { - return InspectionGadgetsBundle.message( - "switch.statement.with.confusing.declaration.problem.descriptor"); + return InspectionGadgetsBundle.message("switch.statement.with.confusing.declaration.problem.descriptor"); } public BaseInspectionVisitor buildVisitor() { return new SwitchStatementWithConfusingDeclarationVisitor(); } - private static class SwitchStatementWithConfusingDeclarationVisitor - extends BaseInspectionVisitor { + private static class SwitchStatementWithConfusingDeclarationVisitor extends BaseInspectionVisitor { @Override - public void visitSwitchStatement( - @NotNull PsiSwitchStatement statement) { + public void visitSwitchStatement(@NotNull PsiSwitchStatement statement) { final PsiCodeBlock body = statement.getBody(); if (body == null) { return; } - final Set<PsiLocalVariable> variablesInPreviousBranches = - new HashSet<PsiLocalVariable>(10); - final Set<PsiLocalVariable> variablesInCurrentBranch = - new HashSet<PsiLocalVariable>(10); + final Set<PsiLocalVariable> variablesInPreviousBranches = new HashSet<PsiLocalVariable>(5); + final Set<PsiLocalVariable> variablesInCurrentBranch = new HashSet<PsiLocalVariable>(5); final PsiStatement[] statements = body.getStatements(); + final LocalVariableAccessVisitor visitor = new LocalVariableAccessVisitor(variablesInPreviousBranches); for (final PsiStatement child : statements) { if (child instanceof PsiDeclarationStatement) { - final PsiDeclarationStatement declaration = - (PsiDeclarationStatement)child; - final PsiElement[] declaredElements = - declaration.getDeclaredElements(); + final PsiDeclarationStatement declarationStatement = (PsiDeclarationStatement)child; + final PsiElement[] declaredElements = declarationStatement.getDeclaredElements(); for (final PsiElement declaredElement : declaredElements) { if (declaredElement instanceof PsiLocalVariable) { - final PsiLocalVariable localVar = - (PsiLocalVariable)declaredElement; - variablesInCurrentBranch.add(localVar); + final PsiLocalVariable localVariable = (PsiLocalVariable)declaredElement; + variablesInCurrentBranch.add(localVariable); } } } - if (child instanceof PsiBreakStatement) { - variablesInPreviousBranches.addAll( - variablesInCurrentBranch); + else if (child instanceof PsiBreakStatement) { + variablesInPreviousBranches.addAll(variablesInCurrentBranch); variablesInCurrentBranch.clear(); } - final LocalVariableAccessVisitor visitor = - new LocalVariableAccessVisitor(); child.accept(visitor); - final Set<PsiElement> accessedVariables = - visitor.getAccessedVariables(); - for (Object accessedVariable : accessedVariables) { - final PsiLocalVariable localVar = - (PsiLocalVariable)accessedVariable; - if (variablesInPreviousBranches.contains(localVar)) { - variablesInPreviousBranches.remove(localVar); - registerVariableError(localVar); - } + } + } + + class LocalVariableAccessVisitor extends JavaRecursiveElementVisitor { + + private final Set<PsiLocalVariable> myVariablesInPreviousBranches; + + public LocalVariableAccessVisitor(Set<PsiLocalVariable> variablesInPreviousBranches) { + myVariablesInPreviousBranches = variablesInPreviousBranches; + } + + @Override + public void visitReferenceExpression(@NotNull PsiReferenceExpression referenceExpression) { + super.visitReferenceExpression(referenceExpression); + final PsiExpression qualifier = referenceExpression.getQualifierExpression(); + if (qualifier != null) { + return; + } + final PsiElement element = referenceExpression.resolve(); + if (!(element instanceof PsiLocalVariable)) { + return; + } + final PsiLocalVariable accessedVariable = (PsiLocalVariable)element; + if (myVariablesInPreviousBranches.contains(accessedVariable)) { + myVariablesInPreviousBranches.remove(accessedVariable); + registerVariableError(accessedVariable); } } } diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/SwitchStatementWithTooFewBranchesInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/SwitchStatementWithTooFewBranchesInspection.java index 8fb901160aca..173e2b510e70 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/SwitchStatementWithTooFewBranchesInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/SwitchStatementWithTooFewBranchesInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2007 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2013 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,9 +15,9 @@ */ package com.siyeh.ig.controlflow; +import com.intellij.codeInspection.ui.SingleIntegerFieldOptionsPanel; import com.intellij.psi.PsiCodeBlock; import com.intellij.psi.PsiSwitchStatement; -import com.intellij.codeInspection.ui.SingleIntegerFieldOptionsPanel; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; @@ -26,53 +26,45 @@ import org.jetbrains.annotations.NotNull; import javax.swing.*; -public class SwitchStatementWithTooFewBranchesInspection - extends BaseInspection { +public class SwitchStatementWithTooFewBranchesInspection extends BaseInspection { private static final int DEFAULT_BRANCH_LIMIT = 2; - /** - * this is public for the DefaultJDOMExternalizer thingy - * - * @noinspection PublicField - */ + + @SuppressWarnings("PublicField") public int m_limit = DEFAULT_BRANCH_LIMIT; @NotNull public String getDisplayName() { - return InspectionGadgetsBundle.message( - "switch.statement.with.too.few.branches.display.name"); + return InspectionGadgetsBundle.message("switch.statement.with.too.few.branches.display.name"); } public JComponent createOptionsPanel() { - return new SingleIntegerFieldOptionsPanel( - InspectionGadgetsBundle.message( - "switch.statement.with.too.few.branches.min.option"), - this, "m_limit"); + return new SingleIntegerFieldOptionsPanel(InspectionGadgetsBundle.message("switch.statement.with.too.few.branches.min.option"), + this, "m_limit"); } @NotNull protected String buildErrorString(Object... infos) { final Integer branchCount = (Integer)infos[0]; - return InspectionGadgetsBundle.message( - "switch.statement.with.too.few.branches.problem.descriptor", - branchCount); + return InspectionGadgetsBundle.message("switch.statement.with.too.few.branches.problem.descriptor", branchCount); } public BaseInspectionVisitor buildVisitor() { return new SwitchStatementWithTooFewBranchesVisitor(); } - private class SwitchStatementWithTooFewBranchesVisitor - extends BaseInspectionVisitor { + private class SwitchStatementWithTooFewBranchesVisitor extends BaseInspectionVisitor { @Override - public void visitSwitchStatement( - @NotNull PsiSwitchStatement statement) { + public void visitSwitchStatement(@NotNull PsiSwitchStatement statement) { final PsiCodeBlock body = statement.getBody(); if (body == null) { return; } final int branchCount = SwitchUtils.calculateBranchCount(statement); + if (branchCount == 0) { + return; // // do not warn when no switch branches are present at all + } if (branchCount >= m_limit) { return; } diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/SwitchStatementsWithoutDefaultInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/SwitchStatementsWithoutDefaultInspection.java index f950d6646bf0..7d581b1d7979 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/SwitchStatementsWithoutDefaultInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/SwitchStatementsWithoutDefaultInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2007 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2013 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,8 +15,8 @@ */ package com.siyeh.ig.controlflow; -import com.intellij.psi.*; import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel; +import com.intellij.psi.*; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; @@ -24,18 +24,14 @@ import org.jetbrains.annotations.NotNull; import javax.swing.*; -public class SwitchStatementsWithoutDefaultInspection - extends BaseInspection { +public class SwitchStatementsWithoutDefaultInspection extends BaseInspection { - /** - * @noinspection PublicField - */ + @SuppressWarnings("PublicField") public boolean m_ignoreFullyCoveredEnums = true; @NotNull public String getDisplayName() { - return InspectionGadgetsBundle.message( - "switch.statements.without.default.display.name"); + return InspectionGadgetsBundle.message("switch.statements.without.default.display.name"); } @NotNull @@ -45,13 +41,11 @@ public class SwitchStatementsWithoutDefaultInspection @NotNull protected String buildErrorString(Object... infos) { - return InspectionGadgetsBundle.message( - "switch.statements.without.default.problem.descriptor"); + return InspectionGadgetsBundle.message("switch.statements.without.default.problem.descriptor"); } public JComponent createOptionsPanel() { - return new SingleCheckboxOptionsPanel(InspectionGadgetsBundle.message( - "switch.statement.without.default.ignore.option"), + return new SingleCheckboxOptionsPanel(InspectionGadgetsBundle.message("switch.statement.without.default.ignore.option"), this, "m_ignoreFullyCoveredEnums"); } @@ -59,57 +53,53 @@ public class SwitchStatementsWithoutDefaultInspection return new SwitchStatementsWithoutDefaultVisitor(); } - private class SwitchStatementsWithoutDefaultVisitor - extends BaseInspectionVisitor { + private class SwitchStatementsWithoutDefaultVisitor extends BaseInspectionVisitor { @Override - public void visitSwitchStatement( - @NotNull PsiSwitchStatement statement) { + public void visitSwitchStatement(@NotNull PsiSwitchStatement statement) { super.visitSwitchStatement(statement); if (switchStatementHasDefault(statement)) { return; } - if (m_ignoreFullyCoveredEnums && - switchStatementIsFullyCoveredEnum(statement)) { + if (m_ignoreFullyCoveredEnums && switchStatementIsFullyCoveredEnum(statement)) { return; } registerStatementError(statement); } - private boolean switchStatementHasDefault( - PsiSwitchStatement statement) { + private boolean switchStatementHasDefault(PsiSwitchStatement statement) { final PsiCodeBlock body = statement.getBody(); if (body == null) { - return false; + return true; // do not warn about incomplete code } final PsiStatement[] statements = body.getStatements(); + if (statements.length == 0) { + return true; // do not warn when no switch branches are present at all + } for (final PsiStatement child : statements) { - if (child instanceof PsiSwitchLabelStatement && - ((PsiSwitchLabelStatement)child).isDefaultCase()) { + if (!(child instanceof PsiSwitchLabelStatement)) { + continue; + } + final PsiSwitchLabelStatement switchLabelStatement = (PsiSwitchLabelStatement)child; + if (switchLabelStatement.isDefaultCase()) { return true; } } return false; } - private boolean switchStatementIsFullyCoveredEnum( - PsiSwitchStatement statement) { + private boolean switchStatementIsFullyCoveredEnum(PsiSwitchStatement statement) { final PsiExpression expression = statement.getExpression(); if (expression == null) { return false; } final PsiType type = expression.getType(); - if (type == null) { - return false; - } if (!(type instanceof PsiClassType)) { return false; } - final PsiClass aClass = ((PsiClassType)type).resolve(); - if (aClass == null) { - return false; - } - if (!aClass.isEnum()) { + final PsiClassType classType = (PsiClassType)type; + final PsiClass aClass = classType.resolve(); + if (aClass == null || !aClass.isEnum()) { return false; } final PsiCodeBlock body = statement.getBody(); diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/dataflow/UnnecessaryLocalVariableInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/dataflow/UnnecessaryLocalVariableInspection.java index 223a21920e55..483e9f3ebc35 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/dataflow/UnnecessaryLocalVariableInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/dataflow/UnnecessaryLocalVariableInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2010 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2013 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -45,19 +45,15 @@ public class UnnecessaryLocalVariableInspection extends BaseInspection { @Override @NotNull public String getDisplayName() { - return InspectionGadgetsBundle.message( - "redundant.local.variable.display.name"); + return InspectionGadgetsBundle.message("redundant.local.variable.display.name"); } @Override public JComponent createOptionsPanel() { - final MultipleCheckboxOptionsPanel optionsPanel = - new MultipleCheckboxOptionsPanel(this); - optionsPanel.addCheckbox(InspectionGadgetsBundle.message( - "redundant.local.variable.ignore.option"), + final MultipleCheckboxOptionsPanel optionsPanel = new MultipleCheckboxOptionsPanel(this); + optionsPanel.addCheckbox(InspectionGadgetsBundle.message("redundant.local.variable.ignore.option"), "m_ignoreImmediatelyReturnedVariables"); - optionsPanel.addCheckbox(InspectionGadgetsBundle.message( - "redundant.local.variable.annotation.option"), + optionsPanel.addCheckbox(InspectionGadgetsBundle.message("redundant.local.variable.annotation.option"), "m_ignoreAnnotatedVariables"); return optionsPanel; } @@ -70,8 +66,7 @@ public class UnnecessaryLocalVariableInspection extends BaseInspection { @Override @NotNull public String buildErrorString(Object... infos) { - return InspectionGadgetsBundle.message( - "unnecessary.local.variable.problem.descriptor"); + return InspectionGadgetsBundle.message("unnecessary.local.variable.problem.descriptor"); } @Override @@ -89,15 +84,12 @@ public class UnnecessaryLocalVariableInspection extends BaseInspection { return new UnnecessaryLocalVariableVisitor(); } - private class UnnecessaryLocalVariableVisitor - extends BaseInspectionVisitor { + private class UnnecessaryLocalVariableVisitor extends BaseInspectionVisitor { @SuppressWarnings({"IfStatementWithIdenticalBranches"}) @Override - public void visitLocalVariable( - @NotNull PsiLocalVariable variable) { + public void visitLocalVariable(@NotNull PsiLocalVariable variable) { super.visitLocalVariable(variable); - if (m_ignoreAnnotatedVariables) { final PsiModifierList list = variable.getModifierList(); if (list != null && list.getAnnotations().length > 0) { @@ -107,12 +99,10 @@ public class UnnecessaryLocalVariableInspection extends BaseInspection { if (isCopyVariable(variable)) { registerVariableError(variable); } - else if (!m_ignoreImmediatelyReturnedVariables && - isImmediatelyReturned(variable)) { + else if (!m_ignoreImmediatelyReturnedVariables && isImmediatelyReturned(variable)) { registerVariableError(variable); } - else if (!m_ignoreImmediatelyReturnedVariables && - isImmediatelyThrown(variable)) { + else if (!m_ignoreImmediatelyReturnedVariables && isImmediatelyThrown(variable)) { registerVariableError(variable); } else if (isImmediatelyAssigned(variable)) { @@ -128,142 +118,138 @@ public class UnnecessaryLocalVariableInspection extends BaseInspection { if (!(initializer instanceof PsiReferenceExpression)) { return false; } - final PsiReferenceExpression reference = - (PsiReferenceExpression)initializer; + final PsiReferenceExpression reference = (PsiReferenceExpression)initializer; final PsiElement referent = reference.resolve(); if (referent == null) { return false; } - if (!(referent instanceof PsiLocalVariable || - referent instanceof PsiParameter)) { + if (!(referent instanceof PsiLocalVariable || referent instanceof PsiParameter)) { return false; } - final PsiCodeBlock containingScope = - PsiTreeUtil.getParentOfType(variable, PsiCodeBlock.class); + final PsiCodeBlock containingScope = PsiTreeUtil.getParentOfType(variable, PsiCodeBlock.class); if (containingScope == null) { return false; } if (!variable.hasModifierProperty(PsiModifier.FINAL) && - VariableAccessUtils.variableIsAssigned(variable, - containingScope, false)) { + VariableAccessUtils.variableIsAssigned(variable, containingScope, false)) { return false; } final PsiVariable initialization = (PsiVariable)referent; if (!initialization.hasModifierProperty(PsiModifier.FINAL) && - VariableAccessUtils.variableIsAssigned(initialization, - containingScope, false)) { + VariableAccessUtils.variableIsAssigned(initialization, containingScope, false)) { return false; } - if (!initialization.hasModifierProperty(PsiModifier.FINAL) - && variable.hasModifierProperty(PsiModifier.FINAL)) { - if (VariableAccessUtils.variableIsUsedInInnerClass(variable, - containingScope)) { + if (!initialization.hasModifierProperty(PsiModifier.FINAL) && variable.hasModifierProperty(PsiModifier.FINAL)) { + if (VariableAccessUtils.variableIsUsedInInnerClass(variable, containingScope)) { return false; } } - if (TypeConversionUtil.boxingConversionApplicable(variable.getType(), ((PsiVariable)referent).getType())) return false; - return true; + return !TypeConversionUtil.boxingConversionApplicable(variable.getType(), initialization.getType()); } private boolean isImmediatelyReturned(PsiVariable variable) { - final PsiCodeBlock containingScope = - PsiTreeUtil.getParentOfType(variable, PsiCodeBlock.class); + final PsiCodeBlock containingScope = PsiTreeUtil.getParentOfType(variable, PsiCodeBlock.class, true, PsiClass.class); if (containingScope == null) { return false; } - final PsiDeclarationStatement declarationStatement = - PsiTreeUtil.getParentOfType(variable, - PsiDeclarationStatement.class); - if (declarationStatement == null) { + final PsiElement parent = variable.getParent(); + if (!(parent instanceof PsiDeclarationStatement)) { return false; } + final PsiDeclarationStatement declarationStatement = (PsiDeclarationStatement)parent; PsiStatement nextStatement = null; final PsiStatement[] statements = containingScope.getStatements(); - for (int i = 0; i < statements.length - 1; i++) { + for (int i = 0; i < (statements.length - 1); i++) { if (statements[i].equals(declarationStatement)) { nextStatement = statements[i + 1]; + break; } } if (!(nextStatement instanceof PsiReturnStatement)) { return false; } - final PsiReturnStatement returnStatement = - (PsiReturnStatement)nextStatement; + final PsiReturnStatement returnStatement = (PsiReturnStatement)nextStatement; final PsiExpression returnValue = returnStatement.getReturnValue(); if (!(returnValue instanceof PsiReferenceExpression)) { return false; } - final PsiElement referent = ((PsiReference)returnValue).resolve(); - return !(referent == null || !referent.equals(variable)); + final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)returnValue; + final PsiElement referent = referenceExpression.resolve(); + if (referent == null || !referent.equals(variable)) { + return false; + } + if (isVariableUsedInFollowingDeclarations(variable, declarationStatement)) { + return false; + } + return true; } private boolean isImmediatelyThrown(PsiVariable variable) { - final PsiCodeBlock containingScope = - PsiTreeUtil.getParentOfType(variable, PsiCodeBlock.class); + final PsiCodeBlock containingScope = PsiTreeUtil.getParentOfType(variable, PsiCodeBlock.class, true, PsiClass.class); if (containingScope == null) { return false; } - final PsiDeclarationStatement declarationStatement = - PsiTreeUtil.getParentOfType(variable, - PsiDeclarationStatement.class); - if (declarationStatement == null) { + final PsiElement parent = variable.getParent(); + if (!(parent instanceof PsiDeclarationStatement)) { return false; } + final PsiDeclarationStatement declarationStatement = (PsiDeclarationStatement)parent; PsiStatement nextStatement = null; final PsiStatement[] statements = containingScope.getStatements(); - for (int i = 0; i < statements.length - 1; i++) { + for (int i = 0; i < (statements.length - 1); i++) { if (statements[i].equals(declarationStatement)) { nextStatement = statements[i + 1]; + break; } } if (!(nextStatement instanceof PsiThrowStatement)) { return false; } - final PsiThrowStatement throwStatement = - (PsiThrowStatement)nextStatement; + final PsiThrowStatement throwStatement = (PsiThrowStatement)nextStatement; final PsiExpression returnValue = throwStatement.getException(); if (!(returnValue instanceof PsiReferenceExpression)) { return false; } final PsiElement referent = ((PsiReference)returnValue).resolve(); - return !(referent == null || !referent.equals(variable)); + if (referent == null || !referent.equals(variable)) { + return false; + } + if (isVariableUsedInFollowingDeclarations(variable, declarationStatement)) { + return false; + } + return true; } private boolean isImmediatelyAssigned(PsiVariable variable) { - final PsiCodeBlock containingScope = - PsiTreeUtil.getParentOfType(variable, PsiCodeBlock.class); + final PsiCodeBlock containingScope = PsiTreeUtil.getParentOfType(variable, PsiCodeBlock.class, true, PsiClass.class); if (containingScope == null) { return false; } - final PsiDeclarationStatement declarationStatement = - PsiTreeUtil.getParentOfType(variable, - PsiDeclarationStatement.class); - if (declarationStatement == null) { + final PsiElement parent = variable.getParent(); + if (!(parent instanceof PsiDeclarationStatement)) { return false; } + final PsiDeclarationStatement declarationStatement = (PsiDeclarationStatement)parent; PsiStatement nextStatement = null; int followingStatementNumber = 0; final PsiStatement[] statements = containingScope.getStatements(); - for (int i = 0; i < statements.length - 1; i++) { + for (int i = 0; i < (statements.length - 1); i++) { if (statements[i].equals(declarationStatement)) { nextStatement = statements[i + 1]; followingStatementNumber = i + 2; + break; } } if (!(nextStatement instanceof PsiExpressionStatement)) { return false; } - final PsiExpressionStatement expressionStatement = - (PsiExpressionStatement)nextStatement; - final PsiExpression expression = - expressionStatement.getExpression(); + final PsiExpressionStatement expressionStatement = (PsiExpressionStatement)nextStatement; + final PsiExpression expression = expressionStatement.getExpression(); if (!(expression instanceof PsiAssignmentExpression)) { return false; } - final PsiAssignmentExpression assignmentExpression = - (PsiAssignmentExpression)expression; - final IElementType tokenType = - assignmentExpression.getOperationTokenType(); + final PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression)expression; + final IElementType tokenType = assignmentExpression.getOperationTokenType(); if (tokenType != JavaTokenType.EQ) { return false; } @@ -271,8 +257,7 @@ public class UnnecessaryLocalVariableInspection extends BaseInspection { if (!(rhs instanceof PsiReferenceExpression)) { return false; } - final PsiReferenceExpression reference = - (PsiReferenceExpression)rhs; + final PsiReferenceExpression reference = (PsiReferenceExpression)rhs; final PsiElement referent = reference.resolve(); if (referent == null || !referent.equals(variable)) { return false; @@ -281,55 +266,52 @@ public class UnnecessaryLocalVariableInspection extends BaseInspection { if (lhs instanceof PsiArrayAccessExpression) { return false; } - if (VariableAccessUtils.variableIsUsed(variable, lhs)) { + if (isVariableUsedInFollowingDeclarations(variable, declarationStatement)) { return false; } for (int i = followingStatementNumber; i < statements.length; i++) { - if (VariableAccessUtils.variableIsUsed(variable, - statements[i])) { + if (VariableAccessUtils.variableIsUsed(variable, statements[i])) { return false; } } return true; } - private boolean isImmediatelyAssignedAsDeclaration( - PsiVariable variable) { - final PsiCodeBlock containingScope = - PsiTreeUtil.getParentOfType(variable, PsiCodeBlock.class); + private boolean isImmediatelyAssignedAsDeclaration(PsiVariable variable) { + final PsiCodeBlock containingScope = PsiTreeUtil.getParentOfType(variable, PsiCodeBlock.class, true, PsiClass.class); if (containingScope == null) { return false; } - final PsiDeclarationStatement declarationStatement = - PsiTreeUtil.getParentOfType(variable, - PsiDeclarationStatement.class); - if (declarationStatement == null) { + final PsiElement parent = variable.getParent(); + if (!(parent instanceof PsiDeclarationStatement)) { return false; } + final PsiDeclarationStatement declarationStatement = (PsiDeclarationStatement)parent; PsiStatement nextStatement = null; int followingStatementNumber = 0; final PsiStatement[] statements = containingScope.getStatements(); - for (int i = 0; i < statements.length - 1; i++) { + for (int i = 0; i < (statements.length - 1); i++) { if (statements[i].equals(declarationStatement)) { nextStatement = statements[i + 1]; followingStatementNumber = i + 2; + break; } } - if (nextStatement instanceof PsiDeclarationStatement) { boolean referenceFound = false; - for (PsiElement declaration : ((PsiDeclarationStatement)nextStatement).getDeclaredElements()) { + final PsiDeclarationStatement nextDeclarationStatement = (PsiDeclarationStatement)nextStatement; + for (PsiElement declaration : nextDeclarationStatement.getDeclaredElements()) { if (!(declaration instanceof PsiVariable)) { continue; } - final PsiExpression initializer = ((PsiVariable)declaration).getInitializer(); - if (!referenceFound) { - if (initializer instanceof PsiReferenceExpression) { - final PsiElement referent = ((PsiReference)initializer).resolve(); - if (variable.equals(referent)) { - referenceFound = true; - continue; - } + final PsiVariable nextVariable = (PsiVariable)declaration; + final PsiExpression initializer = nextVariable.getInitializer(); + if (!referenceFound && initializer instanceof PsiReferenceExpression) { + final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)initializer; + final PsiElement referent = referenceExpression.resolve(); + if (variable.equals(referent)) { + referenceFound = true; + continue; } } if (VariableAccessUtils.variableIsUsed(variable, initializer)) { @@ -349,13 +331,12 @@ public class UnnecessaryLocalVariableInspection extends BaseInspection { boolean referenceFound = false; for (PsiResourceVariable resourceVariable : resourceList.getResourceVariables()) { final PsiExpression initializer = resourceVariable.getInitializer(); - if (!referenceFound) { - if (initializer instanceof PsiReferenceExpression) { - final PsiElement referent = ((PsiReferenceExpression)initializer).resolve(); - if (variable.equals(referent)) { - referenceFound = true; - continue; - } + if (!referenceFound && initializer instanceof PsiReferenceExpression) { + final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)initializer; + final PsiElement referent = referenceExpression.resolve(); + if (variable.equals(referent)) { + referenceFound = true; + continue; } } if (VariableAccessUtils.variableIsUsed(variable, initializer)) { @@ -378,14 +359,33 @@ public class UnnecessaryLocalVariableInspection extends BaseInspection { else { return false; } - + if (isVariableUsedInFollowingDeclarations(variable, declarationStatement)) { + return false; + } for (int i = followingStatementNumber; i < statements.length; i++) { - if (VariableAccessUtils.variableIsUsed(variable, - statements[i])) { + if (VariableAccessUtils.variableIsUsed(variable, statements[i])) { return false; } } return true; } + + private boolean isVariableUsedInFollowingDeclarations(PsiVariable variable, PsiDeclarationStatement declarationStatement) { + final PsiElement[] declaredElements = declarationStatement.getDeclaredElements(); + if (declaredElements.length == 1) { + return false; + } + boolean check = false; + for (PsiElement declaredElement : declaredElements) { + if (!check && variable.equals(declaredElement)) { + check = true; + } else { + if (VariableAccessUtils.variableIsUsed(variable, declaredElement)) { + return true; + } + } + } + return false; + } } }
\ No newline at end of file diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/errorhandling/ThrowsRuntimeExceptionInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/errorhandling/ThrowsRuntimeExceptionInspection.java index 46794e51ee75..e09265b74513 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/errorhandling/ThrowsRuntimeExceptionInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/errorhandling/ThrowsRuntimeExceptionInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2012 Bas Leijdekkers + * Copyright 2011-2013 Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,8 @@ package com.siyeh.ig.errorhandling; import com.intellij.codeInspection.ProblemDescriptor; import com.intellij.lang.LanguageDocumentation; import com.intellij.lang.documentation.CodeDocumentationProvider; +import com.intellij.lang.documentation.CompositeDocumentationProvider; +import com.intellij.lang.documentation.DocumentationProvider; import com.intellij.openapi.project.Project; import com.intellij.psi.*; import com.intellij.psi.javadoc.PsiDocComment; @@ -91,9 +93,20 @@ public class ThrowsRuntimeExceptionInspection extends BaseInspection { else { final PsiDocComment docComment = factory.createDocCommentFromText("/** */"); final PsiComment resultComment = (PsiComment)method.addBefore(docComment, method.getModifierList()); - final CodeDocumentationProvider documentationProvider = - (CodeDocumentationProvider)LanguageDocumentation.INSTANCE.forLanguage(method.getLanguage()); - final String commentStub = documentationProvider.generateDocumentationContentStub(resultComment); + final DocumentationProvider documentationProvider = LanguageDocumentation.INSTANCE.forLanguage(method.getLanguage()); + final CodeDocumentationProvider codeDocumentationProvider; + if (documentationProvider instanceof CodeDocumentationProvider) { + codeDocumentationProvider = (CodeDocumentationProvider)documentationProvider; + } else if (documentationProvider instanceof CompositeDocumentationProvider) { + final CompositeDocumentationProvider compositeDocumentationProvider = (CompositeDocumentationProvider)documentationProvider; + codeDocumentationProvider = compositeDocumentationProvider.getFirstCodeDocumentationProvider(); + if (codeDocumentationProvider == null) { + return; + } + } else { + return; + } + final String commentStub = codeDocumentationProvider.generateDocumentationContentStub(resultComment); final PsiDocComment newComment = factory.createDocCommentFromText("/**\n" + commentStub + "*/"); resultComment.replace(newComment); } diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/javadoc/PackageDotHtmlMayBePackageInfoInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/javadoc/PackageDotHtmlMayBePackageInfoInspection.java index 1b388fff38f5..b6ec3b92ea1d 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/javadoc/PackageDotHtmlMayBePackageInfoInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/javadoc/PackageDotHtmlMayBePackageInfoInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 Bas Leijdekkers + * Copyright 2011-2013 Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,12 +17,11 @@ package com.siyeh.ig.javadoc; import com.intellij.codeInspection.ProblemDescriptor; import com.intellij.ide.DataManager; -import com.intellij.ide.IdeView; import com.intellij.openapi.actionSystem.DataContext; -import com.intellij.openapi.actionSystem.DataKeys; import com.intellij.openapi.command.CommandProcessor; import com.intellij.openapi.command.UndoConfirmationPolicy; import com.intellij.openapi.command.WriteCommandAction; +import com.intellij.openapi.fileEditor.FileEditorManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.roots.ProjectFileIndex; import com.intellij.openapi.roots.ProjectRootManager; @@ -35,7 +34,6 @@ import com.intellij.psi.javadoc.PsiDocComment; import com.intellij.psi.xml.XmlFile; import com.intellij.psi.xml.XmlTag; import com.intellij.psi.xml.XmlTagValue; -import com.intellij.util.IncorrectOperationException; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; @@ -51,19 +49,16 @@ public class PackageDotHtmlMayBePackageInfoInspection extends BaseInspection { @NotNull @Override public String getDisplayName() { - return InspectionGadgetsBundle.message( - "package.dot.html.may.be.package.info.display.name"); + return InspectionGadgetsBundle.message("package.dot.html.may.be.package.info.display.name"); } @NotNull @Override protected String buildErrorString(Object... infos) { if (((Boolean)infos[1]).booleanValue()) { - return InspectionGadgetsBundle.message( - "package.dot.html.may.be.package.info.exists.problem.descriptor"); + return InspectionGadgetsBundle.message("package.dot.html.may.be.package.info.exists.problem.descriptor"); } - return InspectionGadgetsBundle.message( - "package.dot.html.may.be.package.info.problem.descriptor"); + return InspectionGadgetsBundle.message("package.dot.html.may.be.package.info.problem.descriptor"); } @Override @@ -80,22 +75,17 @@ public class PackageDotHtmlMayBePackageInfoInspection extends BaseInspection { @NotNull public String getName() { - return InspectionGadgetsBundle.message( - "package.dot.html.may.be.package.info.delete.quickfix"); + return InspectionGadgetsBundle.message("package.dot.html.may.be.package.info.delete.quickfix"); } @Override - protected void doFix(Project project, ProblemDescriptor descriptor) - throws IncorrectOperationException { + protected void doFix(Project project, ProblemDescriptor descriptor) { final PsiElement element = descriptor.getPsiElement(); if (!(element instanceof XmlFile)) { return; } final XmlFile xmlFile = (XmlFile)element; - - new WriteCommandAction.Simple(project, - InspectionGadgetsBundle.message( - "package.dot.html.delete.command"), xmlFile) { + new WriteCommandAction.Simple(project, InspectionGadgetsBundle.message("package.dot.html.delete.command"), xmlFile) { @Override protected void run() throws Throwable { element.delete(); @@ -109,8 +99,7 @@ public class PackageDotHtmlMayBePackageInfoInspection extends BaseInspection { } } - private static class PackageDotHtmlMayBePackageInfoFix - extends InspectionGadgetsFix { + private static class PackageDotHtmlMayBePackageInfoFix extends InspectionGadgetsFix { private final String aPackage; @@ -120,14 +109,11 @@ public class PackageDotHtmlMayBePackageInfoInspection extends BaseInspection { @NotNull public String getName() { - return InspectionGadgetsBundle.message( - "package.dot.html.may.be.package.info.convert.quickfix"); + return InspectionGadgetsBundle.message("package.dot.html.may.be.package.info.convert.quickfix"); } @Override - protected void doFix(final Project project, - ProblemDescriptor descriptor) - throws IncorrectOperationException { + protected void doFix(final Project project, ProblemDescriptor descriptor) { final PsiElement element = descriptor.getPsiElement(); if (!(element instanceof XmlFile)) { return; @@ -141,24 +127,18 @@ public class PackageDotHtmlMayBePackageInfoInspection extends BaseInspection { if (file != null) { return; } - new WriteCommandAction.Simple(project, - InspectionGadgetsBundle.message( - "package.dot.html.convert.command"), file) { + new WriteCommandAction.Simple(project, InspectionGadgetsBundle.message("package.dot.html.convert.command"), file) { @Override protected void run() throws Throwable { - final PsiJavaFile file = (PsiJavaFile) - directory.createFile("package-info.java"); - CommandProcessor.getInstance().addAffectedFiles(project, - file.getVirtualFile()); - final PsiElementFactory elementFactory = - JavaPsiFacade.getElementFactory(project); + final PsiJavaFile file = (PsiJavaFile)directory.createFile("package-info.java"); + CommandProcessor.getInstance().addAffectedFiles(project, file.getVirtualFile()); + final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project); String packageInfoText = getPackageInfoText(xmlFile); if (packageInfoText == null) { packageInfoText = xmlFile.getText(); } final StringBuilder commentText = new StringBuilder("/**\n"); - final String[] lines = - StringUtil.splitByLines(packageInfoText); + final String[] lines = StringUtil.splitByLines(packageInfoText); boolean appended = false; for (String line : lines) { if (!appended && line.length() == 0) { @@ -169,12 +149,9 @@ public class PackageDotHtmlMayBePackageInfoInspection extends BaseInspection { appended = true; } commentText.append("*/"); - final PsiDocComment comment = - elementFactory.createDocCommentFromText( - commentText.toString()); + final PsiDocComment comment = elementFactory.createDocCommentFromText(commentText.toString()); if (aPackage.length() > 0) { - final PsiPackageStatement packageStatement = - elementFactory.createPackageStatement(aPackage); + final PsiPackageStatement packageStatement = elementFactory.createPackageStatement(aPackage); final PsiElement addedElement = file.add(packageStatement); file.addBefore(comment, addedElement); } @@ -185,18 +162,16 @@ public class PackageDotHtmlMayBePackageInfoInspection extends BaseInspection { if (!isOnTheFly()) { return; } - final AsyncResult<DataContext> dataContextFromFocus = - DataManager.getInstance().getDataContextFromFocus(); + final AsyncResult<DataContext> dataContextFromFocus = DataManager.getInstance().getDataContextFromFocus(); dataContextFromFocus.doWhenDone( new AsyncResult.Handler<DataContext>() { public void run(DataContext dataContext) { - final IdeView ideView = - DataKeys.IDE_VIEW.getData( - dataContext); - if (ideView == null) { + final FileEditorManager editorManager = FileEditorManager.getInstance(project); + final VirtualFile virtualFile = file.getVirtualFile(); + if (virtualFile == null) { return; } - ideView.selectElement(file); + editorManager.openFile(virtualFile, true); } } ); @@ -236,8 +211,7 @@ public class PackageDotHtmlMayBePackageInfoInspection extends BaseInspection { return new PackageDotHtmlMayBePackageInfoVisitor(); } - private static class PackageDotHtmlMayBePackageInfoVisitor - extends BaseInspectionVisitor { + private static class PackageDotHtmlMayBePackageInfoVisitor extends BaseInspectionVisitor { @Override public void visitFile(PsiFile file) { @@ -249,8 +223,7 @@ public class PackageDotHtmlMayBePackageInfoInspection extends BaseInspection { if (!"package.html".equals(fileName)) { return; } - final PsiDirectory directory = - file.getContainingDirectory(); + final PsiDirectory directory = file.getContainingDirectory(); if (directory == null) { return; } @@ -258,18 +231,15 @@ public class PackageDotHtmlMayBePackageInfoInspection extends BaseInspection { if (aPackage == null) { return; } - final boolean exists = - directory.findFile("package-info.java") != null; + final boolean exists = directory.findFile("package-info.java") != null; registerError(file, aPackage, Boolean.valueOf(exists)); } public static String getPackage(@NotNull PsiDirectory directory) { final VirtualFile virtualFile = directory.getVirtualFile(); final Project project = directory.getProject(); - final ProjectRootManager projectRootManager = - ProjectRootManager.getInstance(project); - final ProjectFileIndex fileIndex = - projectRootManager.getFileIndex(); + final ProjectRootManager projectRootManager = ProjectRootManager.getInstance(project); + final ProjectFileIndex fileIndex = projectRootManager.getFileIndex(); return fileIndex.getPackageNameByDirectory(virtualFile); } } diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/performance/BooleanConstructorInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/performance/BooleanConstructorInspection.java index 9207a48198af..fab1858e325d 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/performance/BooleanConstructorInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/performance/BooleanConstructorInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2012 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2013 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -80,8 +80,13 @@ public class BooleanConstructorInspection extends BaseInspection { public void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException { final PsiNewExpression expression = (PsiNewExpression)descriptor.getPsiElement(); final PsiExpressionList argumentList = expression.getArgumentList(); - assert argumentList != null; + if (argumentList == null) { + return; + } final PsiExpression[] arguments = argumentList.getExpressions(); + if (arguments.length != 1) { + return; + } final PsiExpression argument = arguments[0]; final String text = argument.getText(); final LanguageLevel languageLevel = PsiUtil.getLanguageLevel(expression); @@ -119,6 +124,7 @@ public class BooleanConstructorInspection extends BaseInspection { replaceExpression(expression, newExpression); } + @NonNls private static String buildText(PsiExpression argument, boolean useValueOf) { final String text = argument.getText(); final PsiType argumentType = argument.getType(); @@ -152,6 +158,14 @@ public class BooleanConstructorInspection extends BaseInspection { return; } } + final PsiExpressionList argumentList = expression.getArgumentList(); + if (argumentList == null) { + return; + } + final PsiExpression[] expressions = argumentList.getExpressions(); + if (expressions.length != 1) { + return; + } registerError(expression); } } diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/psiutils/ControlFlowUtils.java b/plugins/InspectionGadgets/src/com/siyeh/ig/psiutils/ControlFlowUtils.java index 9b8c58dadbcf..bb76ee23d292 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/psiutils/ControlFlowUtils.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/psiutils/ControlFlowUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2012 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2013 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package com.siyeh.ig.psiutils; import com.intellij.psi.*; +import com.intellij.psi.impl.source.jsp.jspJava.JspTemplateStatement; import com.intellij.psi.util.PsiTreeUtil; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; @@ -25,8 +26,7 @@ public class ControlFlowUtils { private ControlFlowUtils() {} - public static boolean statementMayCompleteNormally( - @Nullable PsiStatement statement) { + public static boolean statementMayCompleteNormally(@Nullable PsiStatement statement) { if (statement == null) { return true; } @@ -35,7 +35,8 @@ public class ControlFlowUtils { return false; } else if (statement instanceof PsiExpressionListStatement || statement instanceof PsiEmptyStatement || - statement instanceof PsiAssertStatement || statement instanceof PsiDeclarationStatement) { + statement instanceof PsiAssertStatement || statement instanceof PsiDeclarationStatement || + statement instanceof PsiSwitchLabelStatement) { return true; } else if (statement instanceof PsiExpressionStatement) { @@ -92,8 +93,11 @@ public class ControlFlowUtils { else if (statement instanceof PsiSwitchStatement) { return switchStatementMayCompleteNormally((PsiSwitchStatement)statement); } + else if (statement instanceof JspTemplateStatement) { + return true; + } else { - // unknown statement type + assert false : "unknown statement type: " + statement.getClass(); return true; } } diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/psiutils/ImportUtils.java b/plugins/InspectionGadgets/src/com/siyeh/ig/psiutils/ImportUtils.java index a561dff73c13..4fc8a0b97d3e 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/psiutils/ImportUtils.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/psiutils/ImportUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2012 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2013 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -137,7 +137,7 @@ public class ImportUtils { if (hasOnDemandImportConflict(fqName, file, true)) { return false; } - if (containsReferenceToConflictingClass(file, fqName)) { + if (containsConflictingReference(file, fqName)) { return false; } if (containsConflictingClass(fqName, file)) { @@ -266,7 +266,7 @@ public class ImportUtils { if (qualifiedClassName == null || fqName.equals(qualifiedClassName)) { continue; } - return containsReferenceToConflictingClass(file, qualifiedClassName); + return containsConflictingReference(file, qualifiedClassName); } } return hasJavaLangImportConflict(fqName, file); @@ -565,10 +565,10 @@ public class ImportUtils { /** * @return true, if the element contains a reference to a different class than fullyQualifiedName but which has the same class name */ - public static boolean containsReferenceToConflictingClass(PsiElement element, String fullyQualifiedName) { + public static boolean containsConflictingReference(PsiElement element, String fullyQualifiedName) { final ConflictingClassReferenceVisitor visitor = new ConflictingClassReferenceVisitor(fullyQualifiedName); element.accept(visitor); - return visitor.isReferenceFound(); + return visitor.isConflictingReferenceFound(); } private static class ConflictingClassReferenceVisitor extends JavaRecursiveElementVisitor { @@ -588,7 +588,7 @@ public class ImportUtils { if (referenceFound) { return; } - final String text = reference.getText(); + final String text = StringUtils.stripAngleBrackets(reference.getText()); if (text.indexOf((int)'.') >= 0 || !name.equals(text)) { return; } @@ -606,7 +606,7 @@ public class ImportUtils { referenceFound = true; } - public boolean isReferenceFound() { + public boolean isConflictingReferenceFound() { return referenceFound; } } diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/psiutils/UtilityClassUtil.java b/plugins/InspectionGadgets/src/com/siyeh/ig/psiutils/UtilityClassUtil.java index f31a46aeb342..fc60bc19b19d 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/psiutils/UtilityClassUtil.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/psiutils/UtilityClassUtil.java @@ -26,7 +26,7 @@ public class UtilityClassUtil { return isUtilityClass(aClass, true); } - public static boolean isUtilityClass(@NotNull PsiClass aClass, boolean checkExtends) { + public static boolean isUtilityClass(@NotNull PsiClass aClass, boolean fullCheck) { if (aClass.isInterface() || aClass.isEnum() || aClass.isAnnotationType()) { return false; } @@ -34,7 +34,7 @@ public class UtilityClassUtil { return false; } final PsiReferenceList extendsList = aClass.getExtendsList(); - if (checkExtends && extendsList != null && extendsList.getReferenceElements().length > 0) { + if (fullCheck && extendsList != null && extendsList.getReferenceElements().length > 0) { return false; } final PsiReferenceList implementsList = aClass.getImplementsList(); @@ -50,7 +50,7 @@ public class UtilityClassUtil { if (!allFieldsStatic(fields)) { return false; } - return staticMethodCount != 0 || fields.length != 0; + return (!fullCheck || staticMethodCount != 0) || fields.length != 0; } private static boolean allFieldsStatic(PsiField[] fields) { diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/FallthruInSwitchStatement.html b/plugins/InspectionGadgets/src/inspectionDescriptions/FallthruInSwitchStatement.html index 947e3bbbb157..88479905d550 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/FallthruInSwitchStatement.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/FallthruInSwitchStatement.html @@ -1,10 +1,12 @@ <html> <body> -Reports 'fallthrough' in a <b>switch</b> statement. -'Fallthrough' is defined to occur when a series of executable statements after a <b>switch</b> label is not guaranteed -to transfer control before the next <b>switch</b> label. In that case, control 'falls through' to the statements after +Reports 'fall-through' in a <b>switch</b> statement. +Fall-through is defined to occur when a series of executable statements after a <b>switch</b> label is not guaranteed +to transfer control before the next <b>switch</b> label. In that case, control falls through to the statements after that <b>switch</b> label, even though the <b>switch</b> expression does not equal the value of the fallen-through label. While occasionally intended, this construction is confusing, and is often the result of a typo. +<p> +This inspection ignores any fall-through commented with a text matching the regex pattern <code>(?i)falls?\s*thro?u</code>. <!-- tooltip end --> <p> <small>Powered by InspectionGadgets</small> diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/IOResource.html b/plugins/InspectionGadgets/src/inspectionDescriptions/IOResource.html index a0894221d9d6..c21230964abd 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/IOResource.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/IOResource.html @@ -1,7 +1,6 @@ <html> <body> -Reports any I/O resource which is not opened in front of a <b>try</b> -block and closed in the corresponding <b>finally</b> block. Such resources may +Reports any I/O resource which is not safely closed in the <b>finally</b> block. Such resources may be inadvertently leaked if an exception is thrown before the resource is closed. I/O resources checked by this inspection include <b>java.io.InputStream</b>, <b>java.io.OutputStream</b>, diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/UnnecessaryLocalVariable.html b/plugins/InspectionGadgets/src/inspectionDescriptions/UnnecessaryLocalVariable.html index 0a73b6644436..9e7551224265 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/UnnecessaryLocalVariable.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/UnnecessaryLocalVariable.html @@ -7,10 +7,12 @@ variable and then not used, and local variables which always have the same value local variable or parameter. <!-- tooltip end --> <p> -Use the checkbox below to have this inspection ignore variables which are immediately +Use the first checkbox below to have this inspection ignore variables which are immediately returned or thrown. Some coding styles suggest using such variables for clarity and ease of debugging. <p> +Use the second checkbox below to have this inspection ignore variable which are annotated. +<p> <small>Powered by InspectionGadgets</small> </body> </html>
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/errorhandling/throws_runtime_exception/MoveThrows.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/errorhandling/throws_runtime_exception/MoveThrows.after.java new file mode 100644 index 000000000000..2a8a561991d8 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/errorhandling/throws_runtime_exception/MoveThrows.after.java @@ -0,0 +1,6 @@ +class C { + /** + * @throws RuntimeException + */ + void foo() {} +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/errorhandling/throws_runtime_exception/MoveThrowsExistingComment.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/errorhandling/throws_runtime_exception/MoveThrowsExistingComment.after.java new file mode 100644 index 000000000000..3727d489d70f --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/errorhandling/throws_runtime_exception/MoveThrowsExistingComment.after.java @@ -0,0 +1,8 @@ +class C { + + /** + * bla bla bla + * @throws RuntimeException + */ + void foo() {} +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/errorhandling/throws_runtime_exception/MoveThrowsExistingComment.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/errorhandling/throws_runtime_exception/MoveThrowsExistingComment.java new file mode 100644 index 000000000000..e1a66f77fcee --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/errorhandling/throws_runtime_exception/MoveThrowsExistingComment.java @@ -0,0 +1,7 @@ +class C { + + /** + * bla bla bla + */ + void foo() throws <caret>RuntimeException {} +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/errorhandling/throws_runtime_exception/RemoveThrows.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/errorhandling/throws_runtime_exception/RemoveThrows.after.java new file mode 100644 index 000000000000..a9706e21c8db --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/errorhandling/throws_runtime_exception/RemoveThrows.after.java @@ -0,0 +1,3 @@ +class C { + void foo() throws Exception {} +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/errorhandling/throws_runtime_exception/RemoveThrows.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/errorhandling/throws_runtime_exception/RemoveThrows.java new file mode 100644 index 000000000000..4d59f12ab08d --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/errorhandling/throws_runtime_exception/RemoveThrows.java @@ -0,0 +1,3 @@ +class C { + void foo() throws RuntimeExcept<caret>ion, Exception {} +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/errorhandling/throws_runtime_exception/moveThrows.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/errorhandling/throws_runtime_exception/moveThrows.java new file mode 100644 index 000000000000..ea95fddcf155 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/errorhandling/throws_runtime_exception/moveThrows.java @@ -0,0 +1,3 @@ +class C { + void foo() throws RuntimeException<caret> {} +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/EmptyStatementBodyInspection.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/empty_statement_body/EmptyStatementBody.java index 4b1aca254417..38f7601b13ab 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/EmptyStatementBodyInspection.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/empty_statement_body/EmptyStatementBody.java @@ -1,8 +1,8 @@ -package com.siyeh.igtest.bugs; +package com.siyeh.igtest.bugs.empty_statement_body; -public class EmptyStatementBodyInspection +public class EmptyStatementBody { - private void foo() + private void foo(int j) { while(bar()); while(bar()){ @@ -29,6 +29,7 @@ public class EmptyStatementBodyInspection } else; + switch (j) {} } private boolean bar() diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/empty_statement_body/expected.xml b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/empty_statement_body/expected.xml new file mode 100644 index 000000000000..2132751ff25d --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/empty_statement_body/expected.xml @@ -0,0 +1,65 @@ +<?xml version="1.0" encoding="UTF-8"?> +<problems> + <problem> + <file>EmptyStatementBody.java</file> + <line>7</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Statement with empty body</problem_class> + <description><code>while</code> statement has empty body #loc</description> + </problem> + + <problem> + <file>EmptyStatementBody.java</file> + <line>8</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Statement with empty body</problem_class> + <description><code>while</code> statement has empty body #loc</description> + </problem> + + <problem> + <file>EmptyStatementBody.java</file> + <line>11</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Statement with empty body</problem_class> + <description><code>for</code> statement has empty body #loc</description> + </problem> + + <problem> + <file>EmptyStatementBody.java</file> + <line>12</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Statement with empty body</problem_class> + <description><code>for</code> statement has empty body #loc</description> + </problem> + + <problem> + <file>EmptyStatementBody.java</file> + <line>16</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Statement with empty body</problem_class> + <description><code>if</code> statement has empty body #loc</description> + </problem> + + <problem> + <file>EmptyStatementBody.java</file> + <line>17</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Statement with empty body</problem_class> + <description><code>if</code> statement has empty body #loc</description> + </problem> + + <problem> + <file>EmptyStatementBody.java</file> + <line>23</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Statement with empty body</problem_class> + <description><code>else</code> statement has empty body #loc</description> + </problem> + + <problem> + <file>EmptyStatementBody.java</file> + <line>30</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Statement with empty body</problem_class> + <description><code>else</code> statement has empty body #loc</description> + </problem> + + <problem> + <file>EmptyStatementBody.java</file> + <line>32</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Statement with empty body</problem_class> + <description><code>switch</code> statement has empty body #loc</description> + </problem> +</problems>
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/classlayout/public_constructor/PublicConstructor.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/classlayout/public_constructor/PublicConstructor.java index cfb0d7ffdf3a..35a177b86a7b 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/classlayout/public_constructor/PublicConstructor.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/classlayout/public_constructor/PublicConstructor.java @@ -6,4 +6,7 @@ abstract class X implements java.io.Externalizable { } class Y { public Y() {} +} +abstract class Z { + public Z() {} }
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/classmetrics/constructor_count/ConstructorCount.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/classmetrics/constructor_count/ConstructorCount.java new file mode 100644 index 000000000000..bc415dfc0d95 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/classmetrics/constructor_count/ConstructorCount.java @@ -0,0 +1,16 @@ +package com.siyeh.igtest.classmetrics.constructor_count; + +public class ConstructorCount { + + ConstructorCount() {} + ConstructorCount(String s) {} + + @Deprecated + ConstructorCount(int i) {} + + class A { + A() {} + A(int i) {} + A(String s) {} + } +} diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/classmetrics/constructor_count/expected.xml b/plugins/InspectionGadgets/test/com/siyeh/igtest/classmetrics/constructor_count/expected.xml new file mode 100644 index 000000000000..ebac7c232104 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/classmetrics/constructor_count/expected.xml @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="UTF-8"?> +<problems> + <problem> + <file>ConstructorCount.java</file> + <line>11</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Class with too many constructors</problem_class> + <description><code>A</code> has too many constructors (constructor count = 3) #loc</description> + </problem> +</problems>
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/FallthruInSwithInspection.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/fallthru_in_switch_statement/FallthruInSwitch.java index 94629ee3a6dd..ed8501e48045 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/FallthruInSwithInspection.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/fallthru_in_switch_statement/FallthruInSwitch.java @@ -1,10 +1,10 @@ -package com.siyeh.igtest.bugs; +package com.siyeh.igtest.controlflow.fallthru_in_switch_statement; -public class FallthruInSwithInspection +public class FallthruInSwitch { private int m_bar; - public FallthruInSwithInspection() + public FallthruInSwitch() { m_bar = 0; } @@ -15,6 +15,8 @@ public class FallthruInSwithInspection switch(bar) { case (3): + System.out.println(); + // Falls through case (4): System.out.println("3"); case (5): diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/fallthru_in_switch_statement/expected.xml b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/fallthru_in_switch_statement/expected.xml new file mode 100644 index 000000000000..fa39d92753c6 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/fallthru_in_switch_statement/expected.xml @@ -0,0 +1,18 @@ +<?xml version="1.0" encoding="UTF-8"?> +<problems> + + <problem> + <file>FallthruInSwitch.java</file> + <line>22</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Fall-through in 'switch' statement</problem_class> + <description><code>case (5):</code> fall-through in 'switch' statement #loc</description> + </problem> + + <problem> + <file>FallthruInSwitch.java</file> + <line>23</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Fall-through in 'switch' statement</problem_class> + <description><code>case (6):</code> fall-through in 'switch' statement #loc</description> + </problem> + +</problems>
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/SwitchStatementDensityInspection.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/switch_statement_density/SwitchStatementDensity.java index 338e69cb98be..d3d4ed73e00e 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/SwitchStatementDensityInspection.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/switch_statement_density/SwitchStatementDensity.java @@ -1,10 +1,10 @@ -package com.siyeh.igtest.bugs; +package com.siyeh.igtest.controlflow.switch_statement_density; -public class SwitchStatementDensityInspection +public class SwitchStatementDensity { private int m_bar; - public SwitchStatementDensityInspection() + public SwitchStatementDensity() { m_bar = 0; } @@ -28,6 +28,14 @@ public class SwitchStatementDensityInspection System.out.println("3"); System.out.println("3"); System.out.println("3"); + System.out.println("3"); + System.out.println("3"); + System.out.println("3"); + System.out.println("3"); + System.out.println("3"); + System.out.println("3"); + System.out.println("3"); + System.out.println("3"); break; case 6: System.out.println("4"); @@ -35,7 +43,7 @@ public class SwitchStatementDensityInspection default: break; } - + switch(bar) {} } } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/switch_statement_density/expected.xml b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/switch_statement_density/expected.xml new file mode 100644 index 000000000000..8cbf72395415 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/switch_statement_density/expected.xml @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="UTF-8"?> +<problems> + <problem> + <file>SwitchStatementDensity.java</file> + <line>15</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">'switch' statement with too low of a branch density</problem_class> + <description><code>switch</code> has too low of a branch density (19%) #loc</description> + </problem> +</problems>
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/confusing/SwitchStatementWithConfusingDeclaration.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/switch_statement_with_confusing_declaration/SwitchStatementWithConfusingDeclaration.java index bd4d476b85ca..7f175281b597 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/confusing/SwitchStatementWithConfusingDeclaration.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/switch_statement_with_confusing_declaration/SwitchStatementWithConfusingDeclaration.java @@ -1,4 +1,4 @@ -package com.siyeh.igtest.confusing; +package com.siyeh.igtest.controlflow.switch_statement_with_confusing_declaration; public class SwitchStatementWithConfusingDeclaration { diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/switch_statement_with_confusing_declaration/expected.xml b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/switch_statement_with_confusing_declaration/expected.xml new file mode 100644 index 000000000000..d92c4d5334ce --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/switch_statement_with_confusing_declaration/expected.xml @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="UTF-8"?> +<problems> + <problem> + <file>SwitchStatementWithConfusingDeclaration.java</file> + <line>10</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Local variable used and declared in different 'switch' branches</problem_class> + <description>Local variable <code>x</code> declared in one 'switch' branch and used in another #loc</description> + </problem> +</problems>
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/switch_statement_with_too_few_branches/SwitchStatementWithTooFewBranches.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/switch_statement_with_too_few_branches/SwitchStatementWithTooFewBranches.java new file mode 100644 index 000000000000..1bc89357dd89 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/switch_statement_with_too_few_branches/SwitchStatementWithTooFewBranches.java @@ -0,0 +1,22 @@ +package com.siyeh.igtest.controlflow.switch_statement_with_too_few_branches; + +class SwitchStatementWithTooFewBranches { + + void foo(int i) { + switch (i) {} + switch (i) { + case 1: + System.out.println(i); + } + switch(i) { + case 1: + break; + case 2: + break; + case 3: + break; + + } + switch (i) + } +} diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/switch_statement_with_too_few_branches/expected.xml b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/switch_statement_with_too_few_branches/expected.xml new file mode 100644 index 000000000000..c3237ad733b8 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/switch_statement_with_too_few_branches/expected.xml @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="UTF-8"?> +<problems> + <problem> + <file>SwitchStatementWithTooFewBranches.java</file> + <line>7</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">'switch' statement with too few branches</problem_class> + <description><code>switch</code> has too few branches (1), and should probably be replaced with an 'if' statement #loc</description> + </problem> +</problems> diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/SwitchStatementsWithoutDefaultInspection.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/switch_statements_without_default/SwitchStatementsWithoutDefault.java index f33bc24694c7..8c81c4091247 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/SwitchStatementsWithoutDefaultInspection.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/switch_statements_without_default/SwitchStatementsWithoutDefault.java @@ -1,10 +1,10 @@ -package com.siyeh.igtest.bugs; +package com.siyeh.igtest.controlflow.switch_statements_without_default; -public class SwitchStatementsWithoutDefaultInspection +public class SwitchStatementsWithoutDefault { private int m_bar; - public SwitchStatementsWithoutDefaultInspection() + public SwitchStatementsWithoutDefault() { m_bar = 0; } @@ -58,5 +58,10 @@ public class SwitchStatementsWithoutDefaultInspection case baz: break; } + switch (var) + } + + enum MyEnum { + foo, bar, baz; } } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/switch_statements_without_default/expected.xml b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/switch_statements_without_default/expected.xml new file mode 100644 index 000000000000..e3ec484499da --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/switch_statements_without_default/expected.xml @@ -0,0 +1,18 @@ +<?xml version="1.0" encoding="UTF-8"?> +<problems> + + <problem> + <file>SwitchStatementsWithoutDefault.java</file> + <line>32</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">'switch' statement without 'default' branch</problem_class> + <description><code>switch</code> statement without 'default' branch #loc</description> + </problem> + + <problem> + <file>SwitchStatementsWithoutDefault.java</file> + <line>55</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">'switch' statement without 'default' branch</problem_class> + <description><code>switch</code> statement without 'default' branch #loc</description> + </problem> + +</problems>
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/dataflow/unnecessary_local_vars/C.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/dataflow/unnecessary_local_vars/C.java index 4c70c60d9822..234c1ba884de 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/dataflow/unnecessary_local_vars/C.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/dataflow/unnecessary_local_vars/C.java @@ -25,4 +25,62 @@ class C { long ll = l; return (int) ll; } + + public int foo() { + int a = 2; + int b = a; + return b; + } + + public int bar() { + int b = 3; + return b; + } + + public int bar2() throws Exception{ + final Exception b = new Exception(); + throw b; + } + + public int baz() { + int a; + int b = 3; + a = b; + return a; + } + + public int bazoom() { + final int i = foo(); + bar(); + final int value = i; + System.out.println(value); + return 3; + } + + double time() { + double time = 0.0, dt = time - 1.0; + System.out.println(time); + return dt; + } + + double time2() { + double time = 0.0, dt = time - 1.0; + return time; + } + + void time3() { + double time = 0.0, dt = time - 1.0; + double time2 = time; + time2 += 1; + } + + void through() throws Exception { + Exception e2 = instance(), e3 = new RuntimeException(e2); + throw e2; + } + + Exception instance() { + return null; + } + }
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/dataflow/unnecessary_local_vars/expected.xml b/plugins/InspectionGadgets/test/com/siyeh/igtest/dataflow/unnecessary_local_vars/expected.xml index 0f4650b6bbb0..bf91c5c62e2c 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/dataflow/unnecessary_local_vars/expected.xml +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/dataflow/unnecessary_local_vars/expected.xml @@ -55,4 +55,48 @@ <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Redundant local variable</problem_class> <description>Local variable <code>r3</code> is redundant #loc</description> </problem> + + <problem> + <file>C.java</file> + <line>30</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Redundant local variable</problem_class> + <description>Local variable <code>a</code> is redundant #loc</description> + </problem> + + <problem> + <file>C.java</file> + <line>31</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Redundant local variable</problem_class> + <description>Local variable <code>b</code> is redundant #loc</description> + </problem> + + <problem> + <file>C.java</file> + <line>36</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Redundant local variable</problem_class> + <description>Local variable <code>b</code> is redundant #loc</description> + </problem> + + <problem> + <file>C.java</file> + <line>41</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Redundant local variable</problem_class> + <description>Local variable <code>b</code> is redundant #loc</description> + </problem> + + <problem> + <file>C.java</file> + <line>47</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Redundant local variable</problem_class> + <description>Local variable <code>b</code> is redundant #loc</description> + </problem> + + <problem> + <file>C.java</file> + <line>55</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Redundant local variable</problem_class> + <description>Local variable <code>value</code> is redundant #loc</description> + </problem> + + </problems>
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/inheritance/extends_utility_class/ExtendsUtilityClass.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/inheritance/extends_utility_class/ExtendsUtilityClass.java new file mode 100644 index 000000000000..553a464e3273 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/inheritance/extends_utility_class/ExtendsUtilityClass.java @@ -0,0 +1,27 @@ +/* + * Copyright 2000-2013 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.igtest.inheritance.extends_utility_class; + +public class ExtendsUtilityClass { +// all members are static + + public static void member() {} +} +class Extender extends ExtendsUtilityClass {} +class Extender2 extends ExtendsUtilityClass { + + public void nonStaticMethod() {} +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/inheritance/extends_utility_class/expected.xml b/plugins/InspectionGadgets/test/com/siyeh/igtest/inheritance/extends_utility_class/expected.xml new file mode 100644 index 000000000000..0511c7f95cb7 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/inheritance/extends_utility_class/expected.xml @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="UTF-8"?> +<problems> + <problem> + <file>ExtendsUtilityClass.java</file> + <line>24</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Class extends utility class</problem_class> + <description>Class <code>Extender2</code> extends utility class 'ExtendsUtilityClass' #loc</description> + </problem> +</problems>
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/javadoc/package_dot_html_may_be_package_info/expected.xml b/plugins/InspectionGadgets/test/com/siyeh/igtest/javadoc/package_dot_html_may_be_package_info/expected.xml new file mode 100644 index 000000000000..dd5618e37f02 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/javadoc/package_dot_html_may_be_package_info/expected.xml @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="UTF-8"?> +<problems> + <problem> + <file>package.html</file> + <line>1</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">'package.html' may be converted to 'package-info.java'</problem_class> + <description><code>package.html</code> may be converted to <code>package-info.java</code></description> + </problem> +</problems>
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/javadoc/package_dot_html_may_be_package_info/package.html b/plugins/InspectionGadgets/test/com/siyeh/igtest/javadoc/package_dot_html_may_be_package_info/package.html new file mode 100644 index 000000000000..b074131a521c --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/javadoc/package_dot_html_may_be_package_info/package.html @@ -0,0 +1,3 @@ +<html> +this is a test file +</html>
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_javadoc_link/UnnecessaryJavaDocLink.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/javadoc/unnecessary_javadoc_link/UnnecessaryJavaDocLink.java index 9a1882fbf974..cf67f8ec04c3 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_javadoc_link/UnnecessaryJavaDocLink.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/javadoc/unnecessary_javadoc_link/UnnecessaryJavaDocLink.java @@ -1,4 +1,4 @@ -package com.siyeh.igtest.style.unnecessary_javadoc_link; +package com.siyeh.igtest.javadoc.unnecessary_javadoc_link; public class UnnecessaryJavaDocLink { @@ -21,7 +21,7 @@ public class UnnecessaryJavaDocLink { } /** - * @see com.siyeh.igtest.style.unnecessary_javadoc_link.UnnecessaryJavaDocLink + * @see com.siyeh.igtest.javadoc.unnecessary_javadoc_link.UnnecessaryJavaDocLink * something */ void bar() {} diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_javadoc_link/expected.xml b/plugins/InspectionGadgets/test/com/siyeh/igtest/javadoc/unnecessary_javadoc_link/expected.xml index d8e1a99fad21..d8e1a99fad21 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_javadoc_link/expected.xml +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/javadoc/unnecessary_javadoc_link/expected.xml diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/jdk/auto_boxing/expected.xml b/plugins/InspectionGadgets/test/com/siyeh/igtest/jdk/auto_boxing/expected.xml index aa560f17e0e9..2f40755387dc 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/jdk/auto_boxing/expected.xml +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/jdk/auto_boxing/expected.xml @@ -17,13 +17,6 @@ <problem> <file>AutoBoxing.java</file> - <line>11</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Auto-boxing</problem_class> - <description>Auto-boxing <code>someNumber++</code> #loc</description> - </problem> - - <problem> - <file>AutoBoxing.java</file> <line>12</line> <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Auto-boxing</problem_class> <description>Auto-boxing <code>~someNumber</code> #loc</description> diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/performance/boolean_constructor/BooleanConstructor.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/performance/boolean_constructor/BooleanConstructor.java new file mode 100644 index 000000000000..f08d4ebecc2e --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/performance/boolean_constructor/BooleanConstructor.java @@ -0,0 +1,10 @@ +package com.siyeh.igtest.performance.boolean_constructor; + +public class BooleanConstructor { + + void foo(boolean b) { + Boolean b1 = new Boolean(); + Boolean b2 = new Boolean(b); + Boolean b3 = new Boolean(true); + } +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/performance/boolean_constructor/expected.xml b/plugins/InspectionGadgets/test/com/siyeh/igtest/performance/boolean_constructor/expected.xml new file mode 100644 index 000000000000..d7cc45f68716 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/performance/boolean_constructor/expected.xml @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> +<problems> + <problem> + <file>BooleanConstructor.java</file> + <line>7</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Boolean constructor call</problem_class> + <description>Boolean constructor call <code>new Boolean(b)</code> #loc</description> + </problem> + + <problem> + <file>BooleanConstructor.java</file> + <line>8</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Boolean constructor call</problem_class> + <description>Boolean constructor call <code>new Boolean(true)</code> #loc</description> + </problem> + +</problems>
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/verbose/UnnecessaryLocalVariableInspection.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/verbose/UnnecessaryLocalVariableInspection.java deleted file mode 100644 index 3102286c2a5a..000000000000 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/verbose/UnnecessaryLocalVariableInspection.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.siyeh.igtest.verbose; - -public class UnnecessaryLocalVariableInspection { - - public int foo() { - int a = 2; - int b = a; - return b; - } - - public int bar() { - int b = 3; - return b; - } - - public int bar2() throws Exception{ - final Exception b = new Exception(); - throw b; - } - - public int baz() { - int a; - int b = 3; - a = b; - return a; - } - - public int bazoom() { - final int i = foo(); - bar(); - final int value = i; - System.out.println(value); - return 3; - } -} diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/EmptyStatementBodyInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/EmptyStatementBodyInspectionTest.java new file mode 100644 index 000000000000..ad29f9e483b4 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/bugs/EmptyStatementBodyInspectionTest.java @@ -0,0 +1,12 @@ +package com.siyeh.ig.bugs; + +import com.siyeh.ig.IGInspectionTestCase; + +public class EmptyStatementBodyInspectionTest extends IGInspectionTestCase { + + public void test() throws Exception { + final EmptyStatementBodyInspection tool = new EmptyStatementBodyInspection(); + tool.m_reportEmptyBlocks = true; + doTest("com/siyeh/igtest/bugs/empty_statement_body", tool); + } +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/classmetrics/ConstructorCountInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/classmetrics/ConstructorCountInspectionTest.java new file mode 100644 index 000000000000..fb339a9317af --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/classmetrics/ConstructorCountInspectionTest.java @@ -0,0 +1,13 @@ +package com.siyeh.ig.classmetrics; + +import com.siyeh.ig.IGInspectionTestCase; + +public class ConstructorCountInspectionTest extends IGInspectionTestCase { + + public void test() throws Exception { + final ConstructorCountInspection tool = new ConstructorCountInspection(); + tool.m_limit = 2; + tool.ignoreDeprecatedConstructors = true; + doTest("com/siyeh/igtest/classmetrics/constructor_count", tool); + } +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/FallthruInSwitchStatementInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/FallthruInSwitchStatementInspectionTest.java new file mode 100644 index 000000000000..ce11a23803b9 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/FallthruInSwitchStatementInspectionTest.java @@ -0,0 +1,10 @@ +package com.siyeh.ig.controlflow; + +import com.siyeh.ig.IGInspectionTestCase; + +public class FallthruInSwitchStatementInspectionTest extends IGInspectionTestCase { + + public void test() throws Exception { + doTest("com/siyeh/igtest/controlflow/fallthru_in_switch_statement", new FallthruInSwitchStatementInspection()); + } +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/SwitchStatementDensityInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/SwitchStatementDensityInspectionTest.java new file mode 100644 index 000000000000..c0d7712f4d5e --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/SwitchStatementDensityInspectionTest.java @@ -0,0 +1,10 @@ +package com.siyeh.ig.controlflow; + +import com.siyeh.ig.IGInspectionTestCase; + +public class SwitchStatementDensityInspectionTest extends IGInspectionTestCase { + + public void test() throws Exception { + doTest("com/siyeh/igtest/controlflow/switch_statement_density", new SwitchStatementDensityInspection()); + } +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/SwitchStatementWithConfusingDeclarationInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/SwitchStatementWithConfusingDeclarationInspectionTest.java new file mode 100644 index 000000000000..0e32419eff53 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/SwitchStatementWithConfusingDeclarationInspectionTest.java @@ -0,0 +1,11 @@ +package com.siyeh.ig.controlflow; + +import com.siyeh.ig.IGInspectionTestCase; + +public class SwitchStatementWithConfusingDeclarationInspectionTest extends IGInspectionTestCase { + + public void test() throws Exception { + doTest("com/siyeh/igtest/controlflow/switch_statement_with_confusing_declaration", + new SwitchStatementWithConfusingDeclarationInspection()); + } +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/SwitchStatementWithTooFewBranchesInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/SwitchStatementWithTooFewBranchesInspectionTest.java new file mode 100644 index 000000000000..43964a565e31 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/SwitchStatementWithTooFewBranchesInspectionTest.java @@ -0,0 +1,10 @@ +package com.siyeh.ig.controlflow; + +import com.siyeh.ig.IGInspectionTestCase; + +public class SwitchStatementWithTooFewBranchesInspectionTest extends IGInspectionTestCase { + + public void test() throws Exception { + doTest("com/siyeh/igtest/controlflow/switch_statement_with_too_few_branches", new SwitchStatementWithTooFewBranchesInspection()); + } +} diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/SwitchStatementWithoutDefaultInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/SwitchStatementWithoutDefaultInspectionTest.java new file mode 100644 index 000000000000..c77592d9b354 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/SwitchStatementWithoutDefaultInspectionTest.java @@ -0,0 +1,11 @@ +package com.siyeh.ig.controlflow; + +import com.siyeh.ig.IGInspectionTestCase; + +public class SwitchStatementWithoutDefaultInspectionTest extends IGInspectionTestCase { + + public void test() throws Exception { + doTest("com/siyeh/igtest/controlflow/switch_statements_without_default", + new SwitchStatementsWithoutDefaultInspection()); + } +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/errorhandling/ThrowsRuntimeExceptionInspectionFixTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/errorhandling/ThrowsRuntimeExceptionInspectionFixTest.java new file mode 100644 index 000000000000..500d8244adad --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/errorhandling/ThrowsRuntimeExceptionInspectionFixTest.java @@ -0,0 +1,47 @@ +/* + * Copyright 2000-2012 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.fixes.errorhandling; + +import com.siyeh.InspectionGadgetsBundle; +import com.siyeh.ig.IGQuickFixesTestCase; +import com.siyeh.ig.errorhandling.CaughtExceptionImmediatelyRethrownInspection; +import com.siyeh.ig.errorhandling.EmptyFinallyBlockInspection; +import com.siyeh.ig.errorhandling.ThrowsRuntimeExceptionInspection; + +public class ThrowsRuntimeExceptionInspectionFixTest extends IGQuickFixesTestCase { + @Override + protected void setUp() throws Exception { + super.setUp(); + myFixture.enableInspections(new ThrowsRuntimeExceptionInspection()); + } + + @Override + protected String getRelativePath() { + return "errorhandling/throws_runtime_exception"; + } + + public void testMoveThrows() { + doTest(InspectionGadgetsBundle.message("throws.runtime.exception.move.quickfix", "RuntimeException")); + } + + public void testMoveThrowsExistingComment() { + doTest(InspectionGadgetsBundle.message("throws.runtime.exception.move.quickfix", "RuntimeException")); + } + + public void testRemoveThrows() { + doTest(InspectionGadgetsBundle.message("throws.runtime.exception.quickfix", "RuntimeException")); + } +} diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/inheritance/ExtendsUtilityClassInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/inheritance/ExtendsUtilityClassInspectionTest.java new file mode 100644 index 000000000000..3e41b435baad --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/inheritance/ExtendsUtilityClassInspectionTest.java @@ -0,0 +1,12 @@ +package com.siyeh.ig.inheritance; + +import com.siyeh.ig.IGInspectionTestCase; + +public class ExtendsUtilityClassInspectionTest extends IGInspectionTestCase { + + public void test() throws Exception { + final ExtendsUtilityClassInspection tool = new ExtendsUtilityClassInspection(); + tool.ignoreUtilityClasses = true; + doTest("com/siyeh/igtest/inheritance/extends_utility_class", tool); + } +} diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/inheritance/StaticInheritanceTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/inheritance/StaticInheritanceTest.java index 5d8ce0952d6f..146e2c0d084b 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/inheritance/StaticInheritanceTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/inheritance/StaticInheritanceTest.java @@ -24,6 +24,7 @@ import org.jetbrains.annotations.NotNull; * User: cdr */ public class StaticInheritanceTest extends LightQuickFixTestCase { + @NotNull @Override protected LocalInspectionTool[] configureLocalInspectionTools() { return new LocalInspectionTool[]{new StaticInheritanceInspection()}; diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/javadoc/PackageDotHtmlMayBePackageInfoInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/javadoc/PackageDotHtmlMayBePackageInfoInspectionTest.java new file mode 100644 index 000000000000..34550c020892 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/javadoc/PackageDotHtmlMayBePackageInfoInspectionTest.java @@ -0,0 +1,10 @@ +package com.siyeh.ig.javadoc; + +import com.siyeh.ig.IGInspectionTestCase; + +public class PackageDotHtmlMayBePackageInfoInspectionTest extends IGInspectionTestCase { + + public void test() throws Exception { + doTest("com/siyeh/igtest/javadoc/package_dot_html_may_be_package_info", new PackageDotHtmlMayBePackageInfoInspection()); + } +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/javadoc/UnnecessaryJavaDocLinkInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/javadoc/UnnecessaryJavaDocLinkInspectionTest.java index 7049fdca8236..c938cada9c77 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/javadoc/UnnecessaryJavaDocLinkInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/javadoc/UnnecessaryJavaDocLinkInspectionTest.java @@ -5,7 +5,6 @@ import com.siyeh.ig.IGInspectionTestCase; public class UnnecessaryJavaDocLinkInspectionTest extends IGInspectionTestCase { public void test() throws Exception { - doTest("com/siyeh/igtest/style/unnecessary_javadoc_link", - new UnnecessaryJavaDocLinkInspection()); + doTest("com/siyeh/igtest/javadoc/unnecessary_javadoc_link", new UnnecessaryJavaDocLinkInspection()); } }
\ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/performance/BooleanConstructorInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/performance/BooleanConstructorInspectionTest.java new file mode 100644 index 000000000000..e5d0fdce3ded --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/performance/BooleanConstructorInspectionTest.java @@ -0,0 +1,25 @@ +/* + * Copyright 2000-2013 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.performance; + +import com.siyeh.ig.IGInspectionTestCase; + +public class BooleanConstructorInspectionTest extends IGInspectionTestCase { + + public void test() throws Exception { + doTest("com/siyeh/igtest/performance/boolean_constructor", new BooleanConstructorInspection()); + } +}
\ No newline at end of file |