【发布时间】:2021-04-10 15:34:13
【问题描述】:
以下代码在代码行“CfnSecurityGroupIngress sgi = (CfnSecurityGroupIngress)obj;”上生成“software.amazon.jsii.JsiiObject cannot be cast to software.amazon.awscdk.services.ec2.CfnSecurityGroupIngress”错误
p> CfnSecurityGroup cfnSecurityGroup = (CfnSecurityGroup) resource_SecurityGroup.getCfnresource();
if (cfnSecurityGroup.getSecurityGroupIngress() != null) {
List<CfnSecurityGroupIngress> cfnSecurityGroupIngress = (List<CfnSecurityGroupIngress>) cfnSecurityGroup.getSecurityGroupIngress();
ListIterator<CfnSecurityGroupIngress> it = cfnSecurityGroupIngress.listIterator();
while (it.hasNext()) {
Object obj = it.next();
LOG.debug("obj: " + obj.toString());
CfnSecurityGroupIngress sgi = (CfnSecurityGroupIngress)obj;
LOG.debug("getFromPort" + sgi.getFromPort());
}
如何访问 SecurityGroupIngress 属性?
请注意,我已经确定 CfnSecurityGroup.IngressProperty 是存在于 cfnSecurityGroupIngress.listIterator() 中的接口类型。不过我不知道该怎么办。
Maven
<dependency>
<groupId>software.amazon.awscdk</groupId>
<artifactId>cdk-cloudformation-include</artifactId>
<version>1.79.0</version>
</dependency>
问候 联系方式
【问题讨论】:
-
输出obj的类会得到什么?
-
@chris 类 software.amazon.jsii.JsiiObject。知道如何确定类 software.amazon.jsii.JsiiObject 的 AWS 类型吗?
-
大概是JsiiObject。抱歉,我只是在这里推测我不知道的东西,但似乎答案在该迭代器返回的类的源代码中。如果 Javadoc 没有给您答案,也许值得快速反编译。我假设它不是开源的
-
@Chris 我想这一切都始于 cfnSecurityGroup.getSecurityGroupEgress() 返回一个对象的问题,我不知道该对象是什么类型。虽然它似乎转换为 (List
) -
是的,因为我从来没有接触过这些东西,所以我已经深入了解了。最好的猜测是 JsiiObject 是某种包装器,您必须对其进行询问才能弄清楚如何从中获取您想要的东西