简体   繁体   English

Eclipse无法解析抽象类类型的复合组件属性

[英]Eclipse cannot resolve composite component attribute of abstract class type

The code seems to work even though Eclipse is marking the line #{cc.attrs.bolt.spec.size} in the bolt_component.xhtml as the property cannot be resolved for spec.size 即使Eclipse在#{cc.attrs.bolt.spec.size}标记#{cc.attrs.bolt.spec.size}行,该代码似乎仍然有效,因为无法为spec.size解析该属性。

bolt_component.xhtml bolt_component.xhtml

<composite:attribute name="bolt" type="model.bolts.Bolt" />
#{cc.attrs.bolt.spec.size}

my abstract class 我的抽象课

public abstract class Bolt implements Serializable {
protected BoltSpec spec;
    I have setSpec()
    but not a getSpec()

and the concrete class 和具体的阶级

public class BoltHexHead extends Bolt implements Serializable {

private static final long serialVersionUID = 1L;
private BoltSpecHexHead spec;

public BoltSpecHexHead getSpec() {
    return spec;
}
public void setSpec(BoltSpecHexHead spec) {
    this.spec = spec;
    super.setSpec(spec);
}

I realize why Eclipse is flagging because I don't have a getSpec() but when I add getSpec in Bolt I get a null pointer in my controller which uses the HexHeadBolt .getSpec() turns out to be null when it should not be. 我意识到为什么Eclipse会标记为因为我没有getSpec(),但是当我在Bolt中添加getSpec时,我在使用HexHeadBolt的控制器中得到了一个空指针。如果不应该,则.getSpec()变为null。

if (analysisHexHeadWithNutOperational.getBolt().getSpec().getSize() == null) {

I've tried multitude of combinations of getters setters private protected, etc... 我已经尝试了多种由私有保护的getters setters组合,等等。

Is the flagging by Eclipse a bug? Eclipse的标记是否是错误? Or do I have my inheritance set up incorrectly I'm using Luna 还是我使用Luna的继承设置不正确

EDIT 编辑

added screenshot showing even with cleaner DRY code, using parameterized property, Eclipses still flags "size" cannot be resolved, but only in the custom component 添加的屏幕快照显示即使使用更干净的DRY代码,使用参数化属性,Eclipse仍然标记“大小”无法解析,而只能在自定义组件中解析 在此处输入图片说明

EDIT2 EDIT2

by adding spec to cc interface I can get Eclipse to stop flagging 通过将spec添加到cc接口,我可以使Eclipse停止标记

faclet faclet

<stk:bolt_component boltTypes="#{data.hexHeadBoltTypes}"
         bolt="#{hex_head_nut_operational.boltAnalysis.bolt}"
         spec="#{hex_head_nut_operational.boltAnalysis.bolt.spec}" />

cc CC

composite:interface>
    <composite:attribute name="boltTypes" /> 
    <composite:attribute name="bolt" />
    <composite:attribute name="spec" />

I gather you're having trouble figuring out how to use an abstract bolt with a generic abstract bolt spec. 我认为您在弄清楚如何将抽象螺栓与通用抽象螺栓规范一起使用时遇到了麻烦。 In a good abstract class with common properties (and getters/setters) it should not have been necessary to repeat the getter/setter in every subclass and delegate to super . 在具有通用属性(和getter / setter)的良好抽象类中,不必在每个子类中重复getter / setter并将其委托给super The mistake in your getSpec() attempt was likely that you didn't delegate to super . 尝试getSpec()出错,可能是您没有委托给super Moreover, the in subclass repeated spec property is unnecessary. 此外,in子类中的重复spec属性是不必要的。

public BoltSpecHexHead getSpec() {
    return (BoltSpecHexHead) super.getSpec();
}

public void setSpec(BoltSpecHexHead spec) {
    super.setSpec(spec);
}

Actually, this is not DRY . 实际上,这不是DRY If the property of the abstract class is in turn also abstract, and you want to avoid casting hell, then you'd better make it a parameterized type. 如果抽象类的属性又是抽象的,并且您希望避免产生地狱,那么最好将其设置为参数化类型。

public abstract class BoltSpec {

    // ...

}

public abstract class Bolt<S extends BoltSpec> {

    private S spec;

    public S getSpec() {
        return spec;
    }

    public void setSpec(S spec) {
        this.spec = spec;
    }

}

public class BoltSpecHexHead extends BoltSpec {

    // ...

}

public class BoltHexHead extends Bolt<BoltSpecHexHead> {

    // ... (note: no getSpec()/setSpec() necessary!)

}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM