1
0
Fork 0

Added final keyword for methods

closure/master
Mike Gerwitz 2011-05-15 13:30:14 -04:00
parent edac0e1667
commit 4c74f2a1f2
2 changed files with 75 additions and 2 deletions

View File

@ -75,7 +75,11 @@ exports.buildMethod = function(
// TODO: We can improve performance by not scanning each one individually // TODO: We can improve performance by not scanning each one individually
// every time this method is called // every time this method is called
var prev_data = scanMembers( members, name, base ), var prev_data = scanMembers( members, name, base ),
prev = ( prev_data ) ? prev_data.member : null; prev = ( prev_data ) ? prev_data.member : null,
prev_keywords = ( prev && prev.___$$keywords$$ )
? prev.___$$keywords$$
: {}
;
// do not permit private abstract methods (doesn't make sense, since // do not permit private abstract methods (doesn't make sense, since
// they cannot be inherited/overridden) // they cannot be inherited/overridden)
@ -97,6 +101,12 @@ exports.buildMethod = function(
); );
} }
// final methods cannot be overridden
if ( prev_keywords[ 'final' ] )
{
throw TypeError( "Cannot override final method '" + name + "'" );
}
// do not allow overriding concrete methods with abstract // do not allow overriding concrete methods with abstract
if ( keywords[ 'abstract' ] && !( util.isAbstractMethod( prev ) ) ) if ( keywords[ 'abstract' ] && !( util.isAbstractMethod( prev ) ) )
{ {

View File

@ -0,0 +1,63 @@
/**
* Tests final members
*
* Copyright (C) 2010 Mike Gerwitz
*
* This file is part of ease.js.
*
* ease.js is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author Mike Gerwitz
* @package test
*/
var common = require( './common' ),
assert = require( 'assert' ),
builder = common.require( 'class_builder' ),
fallback = common.require( 'util' ).definePropertyFallback()
;
/**
* Methods declared as final should not be able to be overridden by subtypes.
* Simple as that.
*/
( function testFinalMethodsCannotBeOverridenBySubtypes()
{
var Foo = builder.build(
{
'final public foo': function() {},
} );
try
{
// attempt to override (should fail)
builder.build( Foo,
{
'public foo': function() {},
} );
}
catch ( e )
{
assert.ok(
e.message.search( 'foo' ) !== -1,
"Final error message contains name of method"
);
return;
}
assert.fail( 'Should not be able to override final methods' );
} )();