diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/WrongParameterPackage.java b/core/src/main/java/com/google/errorprone/bugpatterns/WrongParameterPackage.java deleted file mode 100644 index 84ce9456549..00000000000 --- a/core/src/main/java/com/google/errorprone/bugpatterns/WrongParameterPackage.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Copyright 2012 The Error Prone Authors. - * - * 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.google.errorprone.bugpatterns; - -import static com.google.errorprone.BugPattern.SeverityLevel.ERROR; - -import com.google.errorprone.BugPattern; -import com.google.errorprone.VisitorState; -import com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher; -import com.google.errorprone.fixes.SuggestedFix; -import com.google.errorprone.matchers.Description; -import com.google.errorprone.util.ASTHelpers; -import com.sun.source.tree.MethodTree; -import com.sun.source.tree.VariableTree; -import com.sun.tools.javac.code.Symbol; -import com.sun.tools.javac.code.Symbol.ClassSymbol; -import com.sun.tools.javac.code.Symbol.MethodSymbol; -import com.sun.tools.javac.code.Symbol.TypeSymbol; -import com.sun.tools.javac.code.Type; -import com.sun.tools.javac.util.Name; -import javax.lang.model.element.ElementKind; - -/** @author scottjohnson@google.com (Scott Johnson) */ -@BugPattern( - name = "ParameterPackage", - summary = "Method parameter has wrong package", - severity = ERROR) -public class WrongParameterPackage extends BugChecker implements MethodTreeMatcher { - - private MethodSymbol supermethod; - - @Override - public Description matchMethod(MethodTree tree, VisitorState state) { - MethodSymbol method = ASTHelpers.getSymbol(tree); - if (method == null) { - return Description.NO_MATCH; - } - ClassSymbol classSym = method.enclClass(); - if (classSym == null) { - return Description.NO_MATCH; - } - TypeSymbol superClass = classSym.getSuperclass().tsym; - if (superClass == null) { - return Description.NO_MATCH; - } - - for (Symbol s : superClass.members().getSymbols()) { - if (s.name.contentEquals(method.name) && s.getKind() == ElementKind.METHOD) { - MethodSymbol supermethod = (MethodSymbol) s; - - // if this method actually overrides the supermethod, then it's correct and not a match. - if (method.overrides(supermethod, superClass, state.getTypes(), /* checkResult= */ true)) { - return Description.NO_MATCH; - } - - // if this doesn't have the right number of parameters, look at other ones. - if (supermethod.params().size() != method.params().size()) { - continue; - } - - for (int x = 0; x < method.params().size(); x++) { - Type methodParamType = method.params().get(x).type; - Type supermethodParamType = supermethod.params().get(x).type; - if (methodParamType.tsym.name.contentEquals(supermethodParamType.tsym.name) - && !state.getTypes().isSameType(methodParamType, supermethodParamType)) { - this.supermethod = supermethod; - return describe(tree, state); - } - } - } - } - return Description.NO_MATCH; - } - - public Description describe(MethodTree tree, VisitorState state) { - SuggestedFix.Builder builder = null; - - MethodSymbol method = ASTHelpers.getSymbol(tree); - - if (supermethod == null) { - throw new IllegalStateException("Matching supermethod was not found"); - } - - for (int x = 0; x < method.params().size(); x++) { - Type methodParamType = method.params().get(x).type; - Type supermethodParamType = supermethod.params().get(x).type; - if (methodParamType.tsym.name.contentEquals(supermethodParamType.tsym.name) - && !state.getTypes().isSameType(methodParamType, supermethodParamType)) { - VariableTree param = tree.getParameters().get(x); - - // TODO(b/158870945): Name is most likely more qualified than necessary. - Name replacement = supermethodParamType.tsym.getQualifiedName(); - if (builder == null) { - builder = SuggestedFix.builder(); - } - builder.replace(param, replacement + " " + param.getName()); - } - } - - return (builder != null) ? describeMatch(tree, builder.build()) : describeMatch(tree); - } -} diff --git a/core/src/main/java/com/google/errorprone/scanner/BuiltInCheckerSuppliers.java b/core/src/main/java/com/google/errorprone/scanner/BuiltInCheckerSuppliers.java index fa3bc312882..65334396792 100644 --- a/core/src/main/java/com/google/errorprone/scanner/BuiltInCheckerSuppliers.java +++ b/core/src/main/java/com/google/errorprone/scanner/BuiltInCheckerSuppliers.java @@ -365,7 +365,6 @@ import com.google.errorprone.bugpatterns.WildcardImport; import com.google.errorprone.bugpatterns.WithSignatureDiscouraged; import com.google.errorprone.bugpatterns.WrongOneof; -import com.google.errorprone.bugpatterns.WrongParameterPackage; import com.google.errorprone.bugpatterns.XorPower; import com.google.errorprone.bugpatterns.android.BinderIdentityRestoredDangerously; import com.google.errorprone.bugpatterns.android.BundleDeserializationCast; @@ -1035,8 +1034,7 @@ public static ScannerSupplier errorChecks() { UrlInSee.class, UseEnumSwitch.class, VarChecker.class, - WildcardImport.class, - WrongParameterPackage.class + WildcardImport.class // end ); diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/WrongParameterPackageTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/WrongParameterPackageTest.java deleted file mode 100644 index b1b6a8fa3e6..00000000000 --- a/core/src/test/java/com/google/errorprone/bugpatterns/WrongParameterPackageTest.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 2012 The Error Prone Authors. - * - * 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.google.errorprone.bugpatterns; - -import com.google.errorprone.CompilationTestHelper; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** @author scottjohnson@google.com (Scott Johnson) */ -@RunWith(JUnit4.class) -public class WrongParameterPackageTest { - - private final CompilationTestHelper compilationHelper = - CompilationTestHelper.newInstance(WrongParameterPackage.class, getClass()); - - @Test - public void testPositiveCase() { - compilationHelper - .addSourceFile("WrongParameterPackageNegativeCases.java") // used as a dependency - .addSourceFile("WrongParameterPackagePositiveCases.java") - .doTest(); - } - - @Test - public void testNegativeCase() { - compilationHelper.addSourceFile("WrongParameterPackageNegativeCases.java").doTest(); - } - - // regression test for https://github.com/google/error-prone/issues/330 - @Test - public void testNPE() { - compilationHelper - .addSourceLines( - "foo/Bar.java", "package foo;", "public interface Bar {", " void bar();", "}") - .doTest(); - } - - // regression test for https://github.com/google/error-prone/issues/356 - @Test - public void testCompleteParams() { - compilationHelper - .addSourceLines( - "Test.java", - "package org.gaul.mypackage;", - "import java.io.IOException;", - "import java.io.InputStream;", - "class MyInputStream extends InputStream {", - " @Override", - " public int read() throws IOException {", - " return 0;", - " }", - "}") - .doTest(); - } -} diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/testdata/WrongParameterPackageNegativeCases.java b/core/src/test/java/com/google/errorprone/bugpatterns/testdata/WrongParameterPackageNegativeCases.java deleted file mode 100644 index b9f7d31c1d6..00000000000 --- a/core/src/test/java/com/google/errorprone/bugpatterns/testdata/WrongParameterPackageNegativeCases.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright 2013 The Error Prone Authors. - * - * 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.google.errorprone.bugpatterns.testdata; - -/** @author scottjohnson@google.com (Scott Johnson) */ -public class WrongParameterPackageNegativeCases { - - public void testParameter(Integer x) {} - - public void testParameter(Integer x, Integer y) {} - - public void testParameter2(Integer x, Integer y) {} - - /** Test overrides */ - public static class Subclass extends WrongParameterPackageNegativeCases { - - @Override - public void testParameter(Integer x) {} - - @Override - public void testParameter(Integer x, Integer y) {} - - public void testParameter(Boolean x, Integer y) {} - - public void testParameter(Boolean x) {} - - @Override - public void testParameter2(WrongParameterPackageNegativeCases.Integer x, Integer y) {} - - @SuppressWarnings("ParameterPackage") - public void testParameter(java.lang.Integer x) {} - } - - /** Ambiguous Integer class */ - public static class Integer {} -} diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/testdata/WrongParameterPackagePositiveCases.java b/core/src/test/java/com/google/errorprone/bugpatterns/testdata/WrongParameterPackagePositiveCases.java deleted file mode 100644 index f6606dbd91a..00000000000 --- a/core/src/test/java/com/google/errorprone/bugpatterns/testdata/WrongParameterPackagePositiveCases.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2013 The Error Prone Authors. - * - * 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.google.errorprone.bugpatterns.testdata; - -/** @author scottjohnson@google.com (Scott Johnson) */ -public class WrongParameterPackagePositiveCases { - - public void testParameter(WrongParameterPackageNegativeCases.Integer x) {} - - public void testParameter(Integer x, Integer y) {} - - public void testParameter2(java.lang.Integer x, Integer y) {} - - public void testParameter3(Integer x, Integer y) {} - - /** Test overrides */ - public static class Subclass extends WrongParameterPackagePositiveCases { - - // BUG: Diagnostic contains: public void - // testParameter(com.google.errorprone.bugpatterns.testdata.WrongParameterPackageNegativeCases.Integer x) {} - public void testParameter(Integer x) {} - - // BUG: Diagnostic contains: public void - // testParameter(com.google.errorprone.bugpatterns.testdata.WrongParameterPackagePositiveCases.Integer x, com.google.errorprone.bugpatterns.testdata.WrongParameterPackagePositiveCases.Integer y) {} - public void testParameter(WrongParameterPackageNegativeCases.Integer x, Integer y) {} - - // BUG: Diagnostic contains: public void testParameter2(java.lang.Integer x, - // com.google.errorprone.bugpatterns.testdata.WrongParameterPackagePositiveCases.Integer y) {} - public void testParameter2(WrongParameterPackageNegativeCases.Integer x, java.lang.Integer y) {} - - // BUG: Diagnostic contains: public void - // testParameter3(com.google.errorprone.bugpatterns.testdata.WrongParameterPackagePositiveCases.Integer x, com.google.errorprone.bugpatterns.testdata.WrongParameterPackagePositiveCases.Integer y) {} - public void testParameter3(java.lang.Integer x, java.lang.Integer y) {} - - /** Ambiguous Integer class */ - public static class Integer {} - } - - /** Ambiguous Integer class */ - public static class Integer {} -} diff --git a/docs/bugpattern/ParameterPackage.md b/docs/bugpattern/ParameterPackage.md deleted file mode 100644 index e9858261967..00000000000 --- a/docs/bugpattern/ParameterPackage.md +++ /dev/null @@ -1,4 +0,0 @@ -Method does not override method in superclass due to wrong package for -parameter. For example, defining a method void foo(alpha.Foo x) when the -superclass contains a method void foo(beta.Foo x). The defined method was -probably meant to override the superclass method.