From e9d699a7a597840ddf2a3d94a913af03d0eb0d49 Mon Sep 17 00:00:00 2001 From: Florian Hotze Date: Wed, 24 Jul 2024 16:52:29 +0200 Subject: [PATCH] [utils] Replace `isJsInstanceOfJavaType` method with use of instanceof (#364) The instanceof operator also works for Java types, so there is no need to have this method. Signed-off-by: Florian Hotze --- src/helpers.js | 7 +++---- src/utils.js | 22 ---------------------- test/utils.spec.js | 36 ------------------------------------ types/utils.d.ts | 10 ---------- types/utils.d.ts.map | 2 +- 5 files changed, 4 insertions(+), 73 deletions(-) diff --git a/src/helpers.js b/src/helpers.js index 320bcf9a8..bfd081ff3 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -1,6 +1,5 @@ // Helper functions used internally across the library -const utils = require('./utils'); const javaZDT = Java.type('java.time.ZonedDateTime'); const javaDuration = Java.type('java.time.Duration'); const javaInstant = Java.type('java.time.Instant'); @@ -91,7 +90,7 @@ function _isQuantity (o) { function _isZonedDateTime (o) { if (typeof o !== 'object') return false; return ((o.constructor && o.constructor.name === 'ZonedDateTime') || - (!utils.isJsInstanceOfJavaType(o, javaZDT) && typeof o.withZoneSameInstant === 'function' && typeof o.withZoneSameLocal === 'function') + (!(o instanceof javaZDT) && typeof o.withZoneSameInstant === 'function' && typeof o.withZoneSameLocal === 'function') ); } @@ -108,7 +107,7 @@ function _isZonedDateTime (o) { function _isDuration (o) { if (typeof o !== 'object') return false; return ((o.constructor && o.constructor.name === 'Duration') || - (!utils.isJsInstanceOfJavaType(o, javaDuration) && typeof o.plusDuration === 'function' && typeof o.minusDuration === 'function') + (!(o instanceof javaDuration) && typeof o.plusDuration === 'function' && typeof o.minusDuration === 'function') ); } @@ -125,7 +124,7 @@ function _isDuration (o) { function _isInstant (o) { if (typeof o !== 'object') return false; return ((o.constructor && o.constructor.name === 'Instant') || - (!utils.isJsInstanceOfJavaType(o, javaInstant) && typeof o.minusMicros === 'function' && typeof o.plusMicros === 'function') + (!(o instanceof javaInstant) && typeof o.minusMicros === 'function' && typeof o.plusMicros === 'function') ); } diff --git a/src/utils.js b/src/utils.js index 91df5fb7f..63f7a8bbb 100644 --- a/src/utils.js +++ b/src/utils.js @@ -194,27 +194,6 @@ function dumpObject (obj, dumpProps = false) { } } -/** - * Checks whether an object is instance of a Java class. - * - * @memberOf utils - * @param {*} instance object - * @param {JavaClass} type Java class ({@link https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Class.html}) - * @returns {boolean} whether it is an instance of a Java class - * @throws error if type is not a java class - */ -function isJsInstanceOfJavaType (instance, type) { - if (!Java.isType(type)) { - throw Error('type is not a Java type'); - } - - if (instance === null || instance === undefined || !instance.getClass || !instance.getClass()) { - return false; - } - - return Java.typeName(type) === instance.getClass().getName(); -} - /** * Convert Java Instant to JS-Joda Instant. * @@ -266,7 +245,6 @@ module.exports = { javaMapToJsObj, randomUUID, dumpObject, - isJsInstanceOfJavaType, javaInstantToJsInstant, javaZDTToJsZDT, javaZDTToJsZDTWithDefaultZoneSystem, diff --git a/test/utils.spec.js b/test/utils.spec.js index 309e08ba5..f5be46733 100644 --- a/test/utils.spec.js +++ b/test/utils.spec.js @@ -7,7 +7,6 @@ const { javaListToJsArray, javaSetToJsArray, javaSetToJsSet, - isJsInstanceOfJavaType, javaInstantToJsInstant, javaZDTToJsZDT } = require('../src/utils'); @@ -89,41 +88,6 @@ describe('utils.js', () => { }); }); - describe('isJsInstanceOfJavaType', () => { - it('throws error when type is not a Java type.', () => { - const notAJavaType = {}; - jest.spyOn(Java, 'isType').mockImplementation(() => false); - expect(() => isJsInstanceOfJavaType('', notAJavaType)).toThrow( - 'type is not a Java type' - ); - expect(Java.isType).toHaveBeenCalledWith(notAJavaType); - }); - - it('returns false if instance or type is null or undefined.', () => { - jest.spyOn(Java, 'isType').mockImplementation(() => true); - expect(isJsInstanceOfJavaType(null, {})).toBe(false); - expect(isJsInstanceOfJavaType(undefined, {})).toBe(false); - expect(isJsInstanceOfJavaType({ getClass: () => { return null; } }, {})).toBe(false); - expect(isJsInstanceOfJavaType({ getClass: () => { return undefined; } }, {})).toBe(false); - }); - - it('delegates to Java.typeName(type) and instance.getClass().getName()', () => { - const getNameMock = jest.fn(() => 'java.lang.Object'); - const instance = { - getClass: () => { - return { - getName: getNameMock - }; - } - }; - const type = {}; - jest.spyOn(Java, 'isType').mockImplementation(() => true); - jest.spyOn(Java, 'typeName').mockImplementation(() => 'java.lang.Object'); - isJsInstanceOfJavaType(instance, type); - expect(getNameMock).toHaveBeenCalled(); - }); - }); - describe('javaInstantToJsInstant', () => { it('returns JS-Joda Instant', () => { expect(javaInstantToJsInstant(new Instant())).toBeInstanceOf(time.Instant); diff --git a/types/utils.d.ts b/types/utils.d.ts index 041423187..5f27ac463 100644 --- a/types/utils.d.ts +++ b/types/utils.d.ts @@ -77,16 +77,6 @@ export function randomUUID(): string; * @param {boolean} [dumpProps=false] whether properties also should be dumped */ export function dumpObject(obj: any, dumpProps?: boolean): void; -/** - * Checks whether an object is instance of a Java class. - * - * @memberOf utils - * @param {*} instance object - * @param {JavaClass} type Java class ({@link https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Class.html}) - * @returns {boolean} whether it is an instance of a Java class - * @throws error if type is not a java class - */ -export function isJsInstanceOfJavaType(instance: any, type: JavaClass): boolean; /** * Convert Java Instant to JS-Joda Instant. * diff --git a/types/utils.d.ts.map b/types/utils.d.ts.map index d6da1b0ef..fc38f7cd3 100644 --- a/types/utils.d.ts.map +++ b/types/utils.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.js"],"names":[],"mappings":"AA4BA;;;;;;GAMG;AACH,uDAIC;AAED;;;;;;GAMG;AACH,sDAMC;AAED;;;;;;GAMG;AACH,wDAMC;AAED;;;;;;GAMG;AACH,yDAEC;AAED;;;;;;GAMG;AACH,sDAEC;AA4BD;;;;;;GAMG;AACH,uDAEC;AAnCD;;;;;;GAMG;AACH,8CAFa,IAAI,GAAG,EAAE,GAAG,CAAC,CAMzB;AAED;;;;;;GAMG;AACH,8CAFa,MAAM,CAMlB;AAaD;;;;;GAKG;AACH,8BAFa,MAAM,CAEyD;AAE5E;;;;;;GAMG;AACH,iDAFW,OAAO,QAmDjB;AAED;;;;;;;;GAQG;AACH,wEAHa,OAAO,CAanB;AAED;;;;;;GAMG;AACH,8DAFa,KAAK,OAAO,CAIxB;AAED;;;;;;GAMG;AACH,wDAFa,KAAK,aAAa,CAO9B;AAED;;;;;;GAMG;AACH,6EAFa,KAAK,aAAa,CAQ9B;AA7PD;;;;;;GAMG;AACH,uBAFU,MAAM,CAEmC"} \ No newline at end of file +{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.js"],"names":[],"mappings":"AA4BA;;;;;;GAMG;AACH,uDAIC;AAED;;;;;;GAMG;AACH,sDAMC;AAED;;;;;;GAMG;AACH,wDAMC;AAED;;;;;;GAMG;AACH,yDAEC;AAED;;;;;;GAMG;AACH,sDAEC;AA4BD;;;;;;GAMG;AACH,uDAEC;AAnCD;;;;;;GAMG;AACH,8CAFa,IAAI,GAAG,EAAE,GAAG,CAAC,CAMzB;AAED;;;;;;GAMG;AACH,8CAFa,MAAM,CAMlB;AAaD;;;;;GAKG;AACH,8BAFa,MAAM,CAEyD;AAE5E;;;;;;GAMG;AACH,iDAFW,OAAO,QAmDjB;AAED;;;;;;GAMG;AACH,8DAFa,KAAK,OAAO,CAIxB;AAED;;;;;;GAMG;AACH,wDAFa,KAAK,aAAa,CAO9B;AAED;;;;;;GAMG;AACH,6EAFa,KAAK,aAAa,CAQ9B;AAxOD;;;;;;GAMG;AACH,uBAFU,MAAM,CAEmC"} \ No newline at end of file