Skip to content

Commit

Permalink
[utils] Replace isJsInstanceOfJavaType method with use of instanceof (
Browse files Browse the repository at this point in the history
#364)

The instanceof operator also works for Java types, so there is no need
to have this method.

Signed-off-by: Florian Hotze <[email protected]>
  • Loading branch information
florian-h05 authored Jul 24, 2024
1 parent 488e5b1 commit e9d699a
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 73 deletions.
7 changes: 3 additions & 4 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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')
);
}

Expand All @@ -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')
);
}

Expand All @@ -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')
);
}

Expand Down
22 changes: 0 additions & 22 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -266,7 +245,6 @@ module.exports = {
javaMapToJsObj,
randomUUID,
dumpObject,
isJsInstanceOfJavaType,
javaInstantToJsInstant,
javaZDTToJsZDT,
javaZDTToJsZDTWithDefaultZoneSystem,
Expand Down
36 changes: 0 additions & 36 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const {
javaListToJsArray,
javaSetToJsArray,
javaSetToJsSet,
isJsInstanceOfJavaType,
javaInstantToJsInstant,
javaZDTToJsZDT
} = require('../src/utils');
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 0 additions & 10 deletions types/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion types/utils.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e9d699a

Please sign in to comment.