From ea3e1b949dcbdc09518f17eee0bcf21d41d76896 Mon Sep 17 00:00:00 2001 From: Kenny Ballou Date: Mon, 19 Nov 2018 22:59:50 -0700 Subject: OJ (aka OpenJava) modernization/mirroring Signed-off-by: Kenny Ballou --- tutorial/examples/autoimp/AutoImplementerClass.oj | 64 +++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 tutorial/examples/autoimp/AutoImplementerClass.oj (limited to 'tutorial/examples/autoimp/AutoImplementerClass.oj') diff --git a/tutorial/examples/autoimp/AutoImplementerClass.oj b/tutorial/examples/autoimp/AutoImplementerClass.oj new file mode 100644 index 0000000..29ca2b2 --- /dev/null +++ b/tutorial/examples/autoimp/AutoImplementerClass.oj @@ -0,0 +1,64 @@ +/* + * AutoImplementerClass.oj + * + * Apr 29, 1999, by Michiaki Tatsubori + * Feb 2, 1999, by Michiaki Tatsubori + */ +package examples.autoimp; + + +import openjava.mop.*; +import openjava.ptree.*; +import openjava.syntax.*; + + +/** + * The metaclass AutoImprementerClass provides classes + * with a facility automatically implementing null methods for + * not implemented methods. + *

+ * + * @author Michiaki Tatsubori + * @version 1.0 + * @see openjava.mop.OJClass#translateDefinition() + * @see openjava.mop.OJClass#isRegisteredModifier() + */ +public class AutoImplementerClass instantiates Metaclass extends OJClass +{ + public void translateDefinition() throws MOPException { + OJMethod[] methods = getInheritedMethods(); + for (int i = 0; i < methods.length; ++i) { + if (! methods[i].getModifiers().isAbstract() + || hasDeclaredMethod( methods[i] )) continue; + addMethod( makeNullMethod( methods[i] ) ); + } + } + + private boolean hasDeclaredMethod( OJMethod m ) { + try { + getDeclaredMethod( m.getName(), m.getParameterTypes() ); + return true; + } catch ( NoSuchMemberException e ) { + return false; + } + } + + private OJMethod makeNullMethod( OJMethod m ) throws MOPException { + /* generates a new method without body */ + OJMethod result = new OJMethod( this, + m.getModifiers().remove( OJModifier.ABSTRACT ), + m.getReturnType(), m.getName(), m.getParameterTypes(), + m.getExceptionTypes(), null + ); + /* generates a return statement */ + StatementList body = new StatementList(); + if (m.getReturnType() == OJSystem.VOID) { + body.add( new ReturnStatement() ); + } else { + body.add( new ReturnStatement( Literal.constantNull() ) ); + } + result.setBody( body ); + return result; + } + +} -- cgit v1.2.1