From adb7e088b7c919e804b3324a9d745a40f05eccaf Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Thu, 9 Jun 2011 22:21:26 -0400 Subject: [PATCH] [#19] Cannot declare virtual static methods --- lib/member_builder.js | 9 +++++++++ test/test-member_builder-method.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/lib/member_builder.js b/lib/member_builder.js index af03593..1f87d7d 100644 --- a/lib/member_builder.js +++ b/lib/member_builder.js @@ -145,6 +145,15 @@ function validateMethod( keywords, prev_data, value, name ) ); } + // virtual static does not make sense, as static methods cannot be + // overridden + if ( keywords[ 'virtual' ] && ( keywords[ 'static' ] ) ) + { + throw TypeError( + "Cannot declare static method '" + name + "' as virtual" + ); + } + // search for any previous instances of this member if ( prev ) { diff --git a/test/test-member_builder-method.js b/test/test-member_builder-method.js index 58b7621..30a6f54 100644 --- a/test/test-member_builder-method.js +++ b/test/test-member_builder-method.js @@ -182,6 +182,34 @@ mb_common.assertCommon(); } )(); +/** + * Static methods cannot realistically be declared as virtual; it doesn't make + * sense. Virtual implies that the method may be overridden, but static methods + * cannot be overridden. Only hidden. + */ +( function testCannotDeclareStaticMethodsAsVirtual() +{ + mb_common.value = function() {}; + + try + { + // attempt to build a virtual static method (should throw exception) + mb_common.buildMemberQuick( { 'static': true, 'virtual': true } ); + } + catch ( e ) + { + assert.ok( + e.message.search( mb_common.name ) !== -1, + "Method name should be provided in virtual static error message" + ); + + return; + } + + assert.fail( "Should not be permitted to declare a virtual static method" ); +} )(); + + /** * To ensure interfaces of subtypes remain compatible with that of their * supertypes, the parameter lists must match and build upon each other.