add implicit enum methods values() and valueOf()

This commit is contained in:
Dave Holoway
2020-06-22 15:47:26 +01:00
parent fbb275600a
commit 4f0e55a909
5 changed files with 54 additions and 7 deletions

View File

@@ -24,6 +24,39 @@ function generateShortSignature(scope_or_package_name, name) {
return pkgname ?`${pkgname.replace(/\./g, '/')}/${name}` : name;
}
/**
* @param {SourceType} enum_type
* @param {Map<string,CEIType>} typemap
*/
function createImplicitEnumMethods(enum_type, typemap) {
return [
new class extends Method {
constructor() {
super(enum_type, 'values', ['public','static'], '');
this._returnType = new ArrayType(enum_type, 1);
}
get returnType() {
return this._returnType;
}
},
new class extends Method {
constructor() {
super(enum_type, 'valueOf', ['public','static'], '');
this._parameters = [
new Parameter('name', typemap.get('java/lang/String'), false)
]
this._returnType = enum_type;
}
get parameters() {
return this._parameters;
}
get returnType() {
return this._returnType;
}
}
];
}
class SourceType extends CEIType {
/**
* @param {string} packageName
@@ -55,8 +88,10 @@ class SourceType extends CEIType {
this.implements_types = [];
/** @type {SourceConstructor[]} */
this.constructors = [];
/** @type {SourceMethod[]} */
this.methods = [];
/** @type {Method[]} */
this.methods = typeKind === 'enum'
? createImplicitEnumMethods(this, typemap)
: [];
/** @type {SourceField[]} */
this.fields = [];
/** @type {SourceInitialiser[]} */
@@ -65,6 +100,14 @@ class SourceType extends CEIType {
this.enumValues = [];
}
/**
* @returns {SourceMethod[]}
*/
get sourceMethods() {
// @ts-ignore
return this.methods.filter(m => m instanceof SourceMethod);// [...this.implicitMethods, ...this.sourceMethods];
}
/**
*
* @param {Token} ident
@@ -190,7 +233,11 @@ class SpecialisedSourceType extends CEIType {
};
});
this.methods = source_type.methods.map(m => {
this.methods = source_type.methods.map(method => {
if (!(method instanceof SourceMethod)) {
return method;
}
const m = method;
const type = this;
return new class extends Method {
constructor() {