new "Use of 'clone()' or 'Cloneable'" inspection

This commit is contained in:
Bas Leijdekkers
2015-01-17 17:00:42 +01:00
parent 7acaf1bd4f
commit cf1ea54a25
6 changed files with 173 additions and 0 deletions
@@ -578,6 +578,10 @@
key="non.public.clone.display.name" groupBundle="messages.InspectionsBundle"
groupKey="group.names.cloning.issues" enabledByDefault="false" level="WARNING"
implementationClass="com.siyeh.ig.cloneable.NonPublicCloneInspection"/>
<localInspection language="JAVA" shortName="UseOfClone" bundle="com.siyeh.InspectionGadgetsBundle"
key="use.of.clone.display.name" groupBundle="messages.InspectionsBundle"
groupKey="group.names.cloning.issues" enabledByDefault="false" level="WARNING"
implementationClass="com.siyeh.ig.cloneable.UseOfCloneInspection"/>
<!--group.names.control.flow.issues-->
<localInspection language="JAVA" shortName="BreakStatement" bundle="com.siyeh.InspectionGadgetsBundle" key="break.statement.display.name"
@@ -2140,3 +2140,7 @@ clone.returns.class.type.display.name='clone()' should have return type equal to
clone.returns.class.type.problem.descriptor=''clone()'' should have return type ''{0}'' #loc
clone.returns.class.type.quickfix=Change return type to ''{0}''
clone.returns.class.type.family.quickfix=Change return type to class type
use.of.clone.display.name=Use of 'clone()' or 'Cloneable'
use.of.clone.call.problem.descriptor=Call to <code>#ref()</code>
use.of.clone.call.method.problem.descriptor=Implementation of <code>#ref()</code>
use.of.clone.reference.problem.descriptor=Use of <code>#ref</code>
@@ -0,0 +1,100 @@
/*
* Copyright 2000-2015 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.cloneable;
import com.intellij.psi.*;
import com.siyeh.HardcodedMethodConstants;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.CloneUtils;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
/**
* @author Bas Leijdekkers
*/
public class UseOfCloneInspection extends BaseInspection {
@Nls
@NotNull
@Override
public String getDisplayName() {
return InspectionGadgetsBundle.message("use.of.clone.display.name");
}
@NotNull
@Override
protected String buildErrorString(Object... infos) {
final Object errorElement = infos[0];
if (errorElement instanceof PsiMethodCallExpression) {
return InspectionGadgetsBundle.message("use.of.clone.call.problem.descriptor");
}
else if (errorElement instanceof PsiMethod) {
return InspectionGadgetsBundle.message("use.of.clone.call.method.problem.descriptor");
}
else {
return InspectionGadgetsBundle.message("use.of.clone.reference.problem.descriptor");
}
}
@Override
public BaseInspectionVisitor buildVisitor() {
return new UseOfCloneVisitor();
}
private static class UseOfCloneVisitor extends BaseInspectionVisitor {
@Override
public void visitMethodCallExpression(PsiMethodCallExpression expression) {
final PsiReferenceExpression methodExpression = expression.getMethodExpression();
final String referenceName = methodExpression.getReferenceName();
if (!HardcodedMethodConstants.CLONE.equals(referenceName)) {
return;
}
final PsiExpressionList argumentList = expression.getArgumentList();
final PsiExpression[] arguments = argumentList.getExpressions();
if (arguments.length != 0) {
return;
}
final PsiExpression qualifierExpression = methodExpression.getQualifierExpression();
if (qualifierExpression != null) {
final PsiType type = qualifierExpression.getType();
if (type instanceof PsiArrayType) {
return;
}
}
registerMethodCallError(expression, expression);
}
@Override
public void visitReferenceElement(PsiJavaCodeReferenceElement reference) {
final String qualifiedName = reference.getQualifiedName();
if (!CommonClassNames.JAVA_LANG_CLONEABLE.equals(qualifiedName)) {
return;
}
registerError(reference, reference);
}
@Override
public void visitMethod(PsiMethod method) {
if (!CloneUtils.isClone(method) || CloneUtils.onlyThrowsException(method)) {
return;
}
registerMethodError(method, method);
}
}
}
@@ -0,0 +1,11 @@
<html>
<body>
Reports calls to and implementations of the <b>clone()</b> method and uses of <b>java.lang.Cloneable</b>.
Some coding standards prohibit <b>clone()</b> usage, and recommend using a copy constructor or <b>static</b> factory method.
Calls to <b>clone()</b> on arrays are ignored, because that is a common, correct, efficient and compact way to copy an array.
<!-- tooltip end -->
<p>
<small>New in 14.1</small>
</body>
</html>
@@ -0,0 +1,18 @@
package use_of_clone;
class UseOfClone implements <warning descr="Use of 'Cloneable'">Cloneable</warning> {
void f(int[] is) {
System.out.println(is.clone()); // don't warn when cloning an array
}
public UseOfClone <warning descr="Implementation of 'clone()'">clone</warning>() {
return this;
}
void g(UseOfClone o) {
o.<warning descr="Call to 'clone()'">clone</warning>();
}
}
interface I extends <warning descr="Use of 'Cloneable'">Cloneable</warning> {
}
@@ -0,0 +1,36 @@
/*
* Copyright 2000-2015 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.cloneable;
import com.intellij.codeInspection.InspectionProfileEntry;
import com.siyeh.ig.LightInspectionTestCase;
import org.jetbrains.annotations.Nullable;
/**
* @author Bas Leijdekkers.
*/
public class UseOfCloneInspectionTest extends LightInspectionTestCase {
public void testUseOfClone() {
doTest();
}
@Nullable
@Override
protected InspectionProfileEntry getInspection() {
return new UseOfCloneInspection();
}
}