Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to instantiate a new object for FQN @aws-cdk/aws-ec2.VpcNetworkRef: null #680

Closed
dsilvasc opened this issue Sep 7, 2018 · 2 comments

Comments

@dsilvasc
Copy link

dsilvasc commented Sep 7, 2018

Hi, I'm using the JVM CDK and I'm running into a JsiiException when calling VpcNetworkRef.import_.

Minimal example in Scala:

import java.util

import scala.collection.JavaConverters._
import software.amazon.awscdk.{Construct, Environment, Stack, StackProps, App => CApp}
import software.amazon.awscdk.services.ec2.{
  AmazonLinuxImage,
  AutoScalingGroup,
  AutoScalingGroupProps,
  InstanceType,
  VpcNetworkId,
  VpcNetworkRef,
  VpcNetworkRefProps,
  VpcSubnetId
}

object SharedInfra {
  def vpc(parent: Construct): VpcNetworkRef = {
    val props = VpcNetworkRefProps.builder
      .withVpcId(new VpcNetworkId("test-vpc-id"))
      .withAvailabilityZones(util.Arrays.asList("us-east-1a"))
      .withPublicSubnetIds(util.Arrays.asList(new VpcSubnetId("test-public-id")))
      .withPrivateSubnetIds(util.Arrays.asList(new VpcSubnetId("test-private-id")))
      .build()
    VpcNetworkRef.import_(parent, "SharedInfraVPC", props)
  }
}

case class DemoStack(app: CApp, stackName: String, stackProps: StackProps, instanceType: String = "t2.medium")
    extends Stack(app, stackName, stackProps) {

  private val vpc = SharedInfra.vpc(this)

  new AutoScalingGroup(
    app,
    "Compute",
    AutoScalingGroupProps.builder
      .withInstanceType(new InstanceType(instanceType))
      .withMachineImage(new AmazonLinuxImage())
      .withVpc(vpc)
      .build()
  )
}

object DemoCDKSynthApp {
  def main(args: Array[String]): Unit = {
    val app = new software.amazon.awscdk.App
    val props = StackProps.builder.withEnv(Environment.builder.withRegion("us-east-1").build()).build()
    val stack = DemoStack(app, "demo-cdk", props)
    val output = app.synthesizeStack(stack.getName)
    println(output.getTemplate)
  }
}

Running it yields:

Exception in thread "main" software.amazon.jsii.JsiiException: Unable to instantiate a new object for FQN @aws-cdk/aws-ec2.VpcNetworkRef: null
	at software.amazon.jsii.JsiiEngine.createNative(JsiiEngine.java:252)
	at software.amazon.jsii.JsiiEngine.nativeFromObjRef(JsiiEngine.java:140)
	at software.amazon.jsii.JsiiObjectMapper.treeToValue(JsiiObjectMapper.java:111)
	at software.amazon.jsii.JsiiObject.jsiiStaticCall(JsiiObject.java:85)
	at software.amazon.awscdk.services.ec2.VpcNetworkRef.import_(VpcNetworkRef.java:14)
	at demo.cdk.SharedInfra$.vpc(DemoCDKApp.scala:63)
	at demo.cdk.DemoStack.<init>(DemoCDKApp.scala:70)
	at demo.cdk.DemoCDKSynthApp$.main(DemoCDKApp.scala:100)
	at demo.cdk.DemoCDKSynthApp.main(DemoCDKApp.scala)

The exception is InstantiationExceptionConstructorAccessorImpl and that line in JsiiEngine.java is:

                JsiiObject newObj = (JsiiObject)ctor.newInstance(InitializationMode.Jsii);

where ctor is protected software.amazon.awscdk.services.ec2.VpcNetworkRef(software.amazon.jsii.JsiiObject$InitializationMode).

The class VpcNetworkRef is abstract, so that might be the issue.

@rix0rrr
Copy link
Contributor

rix0rrr commented Sep 12, 2018

@mpiroc @costleya @eladb

@eladb
Copy link
Contributor

eladb commented Sep 12, 2018

@dsilvasc I think you are right. Seems like an issue with the fact that the method return type is abstract.

Tracking in aws/jsii#220

eladb pushed a commit to aws/jsii that referenced this issue Sep 12, 2018
Generate a $proxy class for all abstract classes
and use them when the return type is abstract,
similar to interface proxies.

This change also adds "abstract: true" to all
interface members, so the proxy generator can
treat interfaces and classes polymorphically.

Added a compliance test "returnAbstract" which
verifies this behavior.

Fixes #220
Related to #223 (.NET)
Related to aws/aws-cdk#680 (require jsii update)
eladb pushed a commit to aws/jsii that referenced this issue Sep 13, 2018
Generate a $proxy class for all abstract classes
and use them when the return type is abstract,
similar to interface proxies.

This change also adds "abstract: true" to all
interface members, so the proxy generator can
treat interfaces and classes polymorphically.

Added a compliance test "returnAbstract" which
verifies this behavior.

Fixes #220
Related to #223 (.NET)
Related to aws/aws-cdk#680 (require jsii update)
@eladb eladb closed this as completed Sep 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants