简体   繁体   English

类“…”必须声明为“抽象”。 Checkstyle

[英]Class '…' must be declared as 'abstract'. Checkstyle

I have this warning on most of my classes and not sure why is that. 我在大多数班上都收到过此警告,但不确定为什么会这样。 This happens on both public normal classes and final classes which have private constructors, some no constructor at all. 这在具有私有构造函数的公共普通类和最终类上都会发生,有些根本没有构造函数。 I tried changing my private class methods to protected, doesn't help. 我试图将私有类方法更改为protected,但无济于事。
Any suggestions on how to turn this off? 关于如何关闭此功能的任何建议?

Here's a class example 这是一个课堂例子

public final class PlanBenefitManagerAssembler {

    private static final Logger LOGGER = Logger.getLogger(PlanBenefitManagerAssembler.class);

    /**
     * No Instance of the this class is allowed.
     */
    private PlanBenefitManagerAssembler() {

    }

    public static List<BenefitDecisionDetailsBean> assembleBenefitDecisionDetailsBean(
            List<BenefitDetails> benefitDecisionDetailsList, int relationalSequenceNumber) {

        LOGGER.debug("Enter assembleBenefitDecisionDetailsBean");
        List<BenefitDecisionDetailsBean> benefitDecisionDetailsBeanList = new ArrayList<BenefitDecisionDetailsBean>();

        for (BenefitDetails benefitDecisionDetails : benefitDecisionDetailsList) {
            BenefitDecisionDetailsBean benefitDecisionDetailsBean = new BenefitDecisionDetailsBean();
            benefitDecisionDetailsBean.setBenefitTypeCode(benefitDecisionDetails.getBenefitTypeCode());
            benefitDecisionDetailsBean.setRelationSequenceNumber(relationalSequenceNumber);
            benefitDecisionDetailsBean.setBenefitStatusDescription(
                    benefitDecisionDetails.getBenefitStatusDescription());
            benefitDecisionDetailsBean.setBenefitStatusCode(benefitDecisionDetails.getBenefitStatusCode());
            benefitDecisionDetailsBean.setBenefitUnderwritingStatusCode(
                    benefitDecisionDetails.getBenefitUnderwritingStatusCode());
            benefitDecisionDetailsBean.setBenefitUnderwritingStatusDescription(
                    benefitDecisionDetails.getBenefitUnderwritingStatusDescription());
            benefitDecisionDetailsBean.setBenefitChangeReasonCode(
                    String.valueOf(benefitDecisionDetails.getBenefitChangeReasonCode()));
            benefitDecisionDetailsBean.setBenefitChangeReasonDescription(
                    benefitDecisionDetails.getBenefitChangeReasonDescription());
            benefitDecisionDetailsBean.setComponentNumber(benefitDecisionDetails.getBenefitNumber());

            benefitDecisionDetailsBean.setBenefitVisible(benefitDecisionDetails.isExplicitBenefitDecisionRequired());

            benefitDecisionDetailsBean.setModelChanged(false);

            // * Set BenefitLoading and BenefitExclusion
            List<ExclusionDetailsBean> exclusionDetailsBeanList =
                    PlanBenefitManagerAssembler.assembleExclusionDetailsList(benefitDecisionDetails
                            .getBenefitExclusionsDetailsList().getBenefitExclusionsDetailsList());

            List<LoadingDetailsBean> loadingDetailsBeanList =
                    PlanBenefitManagerAssembler.assembleLoadingDetailsList(benefitDecisionDetails
                            .getBenefitLoadingsDetailsList().getBenefitLoadingsDetailsList());

            benefitDecisionDetailsBean.setExclusionDetailsBeanList(exclusionDetailsBeanList);
            benefitDecisionDetailsBean.setLoadingDetailsBeanList(loadingDetailsBeanList);

            benefitDecisionDetailsBeanList.add(benefitDecisionDetailsBean);         
        }

        LOGGER.debug("Exit assembleBenefitDecisionDetailsBean");

        return benefitDecisionDetailsBeanList;
    }
}

You can always turn the warnings off, but they generally are here for a reason :) 您可以随时关闭警告,但通常出于某种原因它们在这里:)

  • Do you intend to make them abstract classes ? 您打算使它们成为抽象类吗? If so, declare them that way. 如果是这样,请以这种方式声明它们。
  • Will you need to instantiate them at some point ? 您是否需要在某个时刻实例化它们? If so, add a public constructor. 如果是这样,请添加一个公共构造函数。

I'm pretty sure this will solve your problem. 我很确定这可以解决您的问题。

When Checkstyle produces a warning the warning text should include a short rule name which will allow you to look up the exact rule that is being triggered. 当Checkstyle产生警告时,警告文本应包含简短的规则名称,这将使您可以查找所触发的确切规则。 "DesignForExtension", for example. 例如,“ DesignForExtension”。

Given the rule name, you can look up more detail on what it means in the Checkstyle documentation: http://checkstyle.sourceforge.net/availablechecks.html 给定规则名称后,您可以在Checkstyle文档中查找有关其含义的更多详细信息: http : //checkstyle.sourceforge.net/availablechecks.html

Post the full details of the rule being triggered and someone might be able to help. 发布正在触发的规则的全部详细信息,也许有人可以提供帮助。

On sourceforge it says that the AbstractClassName rule uses the following regex: sourceforge上,它说AbstractClassName规则使用以下正则表达式:

^Abstract.*$|^.*Factory$

This causes classes with a name starting with 'Abstract' or ending with 'Factory' to be flagged. 这将导致标记以“ Abstract”开头或以“ Factory”结尾的类。 I get the 'Abstract..' part of that, but why should all '..Factory' classes be abstract? 我得到了“抽象..”部分,但是为什么所有的“ ..Factory”类都应该是抽象的? Sometimes I create factories which use dependencies to do their work so I need an instance to inject into. 有时,我创建使用依赖项来完成工作的工厂,因此需要注入实例。

This however does not explain your case. 但是,这不能解释您的情况。 I tried your example class and did not get any Checkstyle warning (I am using the Eclipse Checkstyle Plug-in version 5.3.0.201012121300). 我尝试了您的示例类,但未收到任何Checkstyle警告(我正在使用Eclipse Checkstyle插件版本5.3.0.201012121300)。

Are you sure you are getting the AbstractClassName warning for this class? 您确定会收到此类的AbstractClassName警告吗? Which version of Checkstyle are you using? 您正在使用哪个版本的Checkstyle?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 必须将类声明为抽象或实现抽象方法 - Class must be declared as abstract or implement abstract method 必须将类声明为abstract或实现抽象方法错误 - Class must either be declared abstract or implement abstract method error 类必须声明为抽象或实现抽象方法错误 - Class must be either declared abstract or implement abstract method error 类必须声明为abstract或实现抽象方法:Intellij错误? - Class must either be declared abstract or implement abstract method: Intellij error? 循环的AsyncTask类必须声明为抽象或实现抽象方法 - Looping AsyncTask Class must either be declared abstract or implement abstract method RxAndroid:类必须声明为抽象或实现抽象方法错误 - RxAndroid: Class must either be declared abstract or implement abstract method error 类必须声明为抽象或实现抽象方法toArray - Class must either be declared abstract or implement abstract method toArray onBindViewHolder必须声明为抽象 - onBindViewHolder must be declared abstract 类“从FirebaseAdapter派生的匿名类”必须在适配器中声明为抽象 - Class 'Anonymous class derived from FirebaseAdapter' must be declared abstract in Adapter 类&#39;从PlaceSelectedListener派生的匿名类&#39;必须声明为抽象或实现抽象方法 - class 'Anonymous class derived from PlaceSelectedListener' must either be declared abstract or implement abstract method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM