1- import * as assert from 'assert' ;
21import * as spec from '@jsii/spec' ;
2+ import * as assert from 'assert' ;
33import * as clone from 'clone' ;
44import { toSnakeCase } from 'codemaker/lib/case-utils' ;
55import { createHash } from 'crypto' ;
@@ -1473,9 +1473,20 @@ class JavaGenerator extends Generator {
14731473 // so we should not emit these checks for primitive-only unions.
14741474 // Also, Java does not allow us to perform these checks if the types
14751475 // have no overlap (eg if a String instanceof Number).
1476- if ( type . includes ( 'java.lang.Object' ) && ( ! spec . isPrimitiveTypeReference ( prop . type ) || prop . type . primitive === spec . PrimitiveType . Any ) ) {
1476+ if (
1477+ type . includes ( 'java.lang.Object' ) &&
1478+ ( ! spec . isPrimitiveTypeReference ( prop . type ) ||
1479+ prop . type . primitive === spec . PrimitiveType . Any )
1480+ ) {
14771481 this . emitUnionParameterValdation ( [
1478- { name : 'value' , type : this . filterType ( prop . type , { covariant : prop . static , optional : prop . optional } , type ) } ,
1482+ {
1483+ name : 'value' ,
1484+ type : this . filterType (
1485+ prop . type ,
1486+ { covariant : prop . static , optional : prop . optional } ,
1487+ type ,
1488+ ) ,
1489+ } ,
14791490 ] ) ;
14801491 }
14811492 if ( prop . static ) {
@@ -1505,13 +1516,26 @@ class JavaGenerator extends Generator {
15051516 *
15061517 * @returns a type reference that matches the provided javaType.
15071518 */
1508- private filterType ( ref : spec . TypeReference , { covariant, optional } : { covariant ?: boolean , optional ?: boolean } , javaType : string ) : spec . TypeReference {
1519+ private filterType (
1520+ ref : spec . TypeReference ,
1521+ { covariant, optional } : { covariant ?: boolean ; optional ?: boolean } ,
1522+ javaType : string ,
1523+ ) : spec . TypeReference {
15091524 if ( ! spec . isUnionTypeReference ( ref ) ) {
15101525 // No filterning needed -- this isn't a type union!
15111526 return ref ;
15121527 }
1513- const types = ref . union . types . filter ( ( t ) => this . toDecoratedJavaType ( { optional, type : t } , { covariant } ) === javaType ) ;
1514- assert ( types . length > 0 , `No type found in ${ spec . describeTypeReference ( ref ) } has Java type ${ javaType } ` ) ;
1528+ const types = ref . union . types . filter (
1529+ ( t ) =>
1530+ this . toDecoratedJavaType ( { optional, type : t } , { covariant } ) ===
1531+ javaType ,
1532+ ) ;
1533+ assert (
1534+ types . length > 0 ,
1535+ `No type found in ${ spec . describeTypeReference (
1536+ ref ,
1537+ ) } has Java type ${ javaType } `,
1538+ ) ;
15151539 return { union : { types } } ;
15161540 }
15171541
@@ -1740,7 +1764,9 @@ class JavaGenerator extends Generator {
17401764 let emitAnd = false ;
17411765 const nestedCollectionUnionTypes = new Map < string , spec . TypeReference > ( ) ;
17421766 const typeRefs = type . union . types ;
1743- if ( typeRefs . length > 1 ) { this . code . indent ( 'if (' ) ; }
1767+ if ( typeRefs . length > 1 ) {
1768+ this . code . indent ( 'if (' ) ;
1769+ }
17441770 const checked = new Set < string > ( ) ;
17451771 for ( const typeRef of typeRefs ) {
17461772 const prefix = emitAnd ? '&&' : '' ;
@@ -1755,10 +1781,19 @@ class JavaGenerator extends Generator {
17551781 nestedCollectionUnionTypes . set ( javaType , typeRef ) ;
17561782 }
17571783 const test = `${ value } instanceof ${ javaRawType } ` ;
1758- if ( typeRefs . length > 1 ) { this . code . line ( `${ prefix } !(${ test } )` ) ; }
1784+ if ( typeRefs . length > 1 ) {
1785+ this . code . line ( `${ prefix } !(${ test } )` ) ;
1786+ }
17591787 emitAnd = true ;
17601788 }
1761- if ( typeRefs . length > 1 && typeRefs . some ( t => spec . isNamedTypeReference ( t ) && spec . isInterfaceType ( this . findType ( t . fqn ) ) ) ) {
1789+ if (
1790+ typeRefs . length > 1 &&
1791+ typeRefs . some (
1792+ ( t ) =>
1793+ spec . isNamedTypeReference ( t ) &&
1794+ spec . isInterfaceType ( this . findType ( t . fqn ) ) ,
1795+ )
1796+ ) {
17621797 // Only anonymous objects at runtime can be `JsiiObject`s.
17631798 this . code . line (
17641799 `&& !(${ value } .getClass().equals(software.amazon.jsii.JsiiObject.class))` ,
@@ -1786,12 +1821,13 @@ class JavaGenerator extends Generator {
17861821 }
17871822
17881823 for ( const [ javaType , typeRef ] of nestedCollectionUnionTypes ) {
1789- const varName = typeRefs . length > 1
1790- ? `__cast_${ createHash ( 'sha256' )
1791- . update ( value )
1792- . digest ( 'hex' )
1793- . slice ( 0 , 6 ) } `
1794- : value ;
1824+ const varName =
1825+ typeRefs . length > 1
1826+ ? `__cast_${ createHash ( 'sha256' )
1827+ . update ( value )
1828+ . digest ( 'hex' )
1829+ . slice ( 0 , 6 ) } `
1830+ : value ;
17951831 if ( typeRefs . length > 1 ) {
17961832 this . code . openBlock (
17971833 `if (${ value } instanceof ${ this . toJavaTypeNoGenerics ( typeRef ) } )` ,
0 commit comments