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

[utils] Replace isJsInstanceOfJavaType method with use of instanceof #364

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading