【问题标题】:Intellij reports code duplication while actually it's notIntellij 报告代码重复,但实际上并非如此
【发布时间】:2016-09-23 06:26:16
【问题描述】:

这是代码。方法 test 和 test2 中的代码不同,因为传递给 Test 构造函数的参数不同。实际上,如果我将任何参数更改为 null,intellij 就会停止报告重复项。有没有办法解决这个问题?

---- 更新--------

我通过 2 个函数做完全不同的事情,但 intellij 仍然报告重复

public class TestMain {

    public void test(int a)
    {
        System.out.println("haha");
        System.out.println("hahaa");
        TestMain testMain = new TestMain();
        new Test(testMain::test3);
        System.out.println("hahaaa");
    }

    public void test2(int a)
    {
        System.out.println("haha");
        System.out.println("hahaa");
        TestMain testMain = new TestMain();
        new Test(testMain::still_dup);
        System.out.println("hahaaa");
    }

    public void test3(int a) {
        System.out.println("abc");
    }

    public void still_dup(int a) {
        String b = "edf";
        b.toLowerCase();
    }

    public class Test {
        Test(handler h) {

        }
    }

    public interface handler<M> {
        void entitySelector(int a);
    }

    public static void main(String[] args) {
        TestMain test = new TestMain();
        test.test(1);
        System.out.println("-------");
        test.test2(2);
    }
}

【问题讨论】:

  • 你传递了两个不同的方法引用,但是这两个引用的方法做的事情完全相同。
  • @JBNizet 这不是重点。即使我传递了一个做完全不同的事情的函数,它也会报告重复。

标签: intellij-idea


【解决方案1】:

我认为解决此问题的最佳方法是用一种方法替换 testtest2。您不必区分要传递构造函数的内容,因为它是当前方法。这可能是报告代码重复的原因。这些方法可以替换为一个没有问题的方法。

【讨论】:

  • “单一方法”是什么意思?
  • 我的意思是用一种方法替换这两种方法。
  • 答案不再适用,因为问题已更新。 test 和 test2 中传递的方法引用发生了变化。
猜你喜欢
  • 2020-09-07
  • 1970-01-01
  • 2023-03-14
  • 2016-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-23
  • 1970-01-01
相关资源
最近更新 更多