Black listing synchronized keyword for groovy scripts -
there application users can provide custom groovy scripts. can write own functions in scripts. want restrict people using 'synchronized' keyword other keywords in these scripts. example should not possible write function below.
public synchronized void test() { }
i creating compilerconfiguration , using secureastcustomizer. adding org.codehaus.groovy.syntax.types.keyword_synchronized list of black listed tokens doesn't seem job. (if add org.codehaus.groovy.syntax.types.plus it's preventing usage of '+' within scripts.. doesn't seem job synchronized)
any ideas on how achieve ...
you can this:
import org.codehaus.groovy.control.compilerconfiguration import org.codehaus.groovy.syntax.syntaxexception import org.codehaus.groovy.ast.classnode import org.codehaus.groovy.control.sourceunit import org.codehaus.groovy.classgen.generatorcontext class synchronizedremover extends org.codehaus.groovy.control.customizers.compilationcustomizer { synchronizedremover() { super(org.codehaus.groovy.control.compilephase.conversion) } void call(final sourceunit source, final generatorcontext context, final classnode classnode) { classnode.methods.each { mn -> if (mn.modifiers & 0x0020) { // 0x0020 synchronized source.adderror(new syntaxexception("synchronized not allowed", mn.linenumber, mn.columnnumber)) } } } } def config = new compilerconfiguration() config.addcompilationcustomizers(new synchronizedremover()) def shell = new groovyshell(config) shell.evaluate ''' class foo { public synchronized void foo() { println 'bar' } } '''
the idea create compilation customizer checks generated classes , each method, add error if synchronized modifier present. synchronized block inside methods, can use secureastcustomizer
custom statement checker.
Comments
Post a Comment