OpenJava allows meta-programmer to extend the syntax of language in limited way. The easiest part of the syntax extension is adding new modifiers for class declaration or member declarations.
In this section, suppose an example of syntax extension for method declarations which explicitly describes the modified method should override the methed in superclasses. With this extension, base-level programmers can detect errors at compile-time. Otherwise, the regular Java compiler doesn't complain about it because it's proper method declaration defining a new method in the class.
In defining a new class which must override some methods in
superclasses, programmers would add the modifier overriding
to each method overriding the corresponding method in superclasses.
Here, we define a new class MyObject which surely overrides
the method toString() in the superclass java.lang.Object:
public class MyObject instantiates OverrideCheckerClass { public overriding String toString() { return "MyString"; } }
OpenJava compiler doesn't show any error message for this source code
as it overrides the supermethod properly. But the system should
show some error messages if the source code is as follows:
because there is no toStrung() method in the superclasses of the class MyObjectWithError.public class MyObjectWithError instantiates OverrideCheckerClass { public overriding String toStrung() { return "MyString"; } }
In this example, only the error checking is performed thus no translation
of source code are performed by the metaclass. Thus the system
should produce the source code in the regular Java like following for
the class MyObject:
public class MyObject { public String toString() { return "MyString"; } }
Only the difference from the original is the absense of the extended modifier overriding.
Extended modifiers are automatically removed by the
system in generating source code. Thus we do not have to care about
it here.
We should define such a metaclass OverrideCheckerClass
to override two methods. The first method:
is for the purpose of allowing the custom modifier overriding.public static boolean isRegisteredModifier(String)
And the second method is:
Though there are no translation, we must show alert in case of missing overriding.public void translateDefinition()
The source code following is a such implementation.
import openjava.mop.*; import openjava.ptree.*; public class OverrideCheckerClass instantiates Metaclass extends OJClass { private static final String OVERRIDING = "overriding"; public static boolean isRegisteredModifier( String keyword ) { if (keyword.equals( OVERRIDING )) return true; return OJClass.isRegisteredModifier( keyword ); } public void translateDefinition() throws MOPException { OJMethod[] methods = getDeclaredMethods(); for (int i = 0; i < methods.length; ++i) { if (! methods[i].getModifiers().has( OVERRIDING )) continue; String name = methods[i].getName(); OJClass[] ptypes = methods[i].getParameterTypes(); try { getSuperclass().getMethod( name, ptypes, this ); } catch (NoSuchMemberException e) { System.err.println( "warning: " + methods[i] + " doesn't " + "override any method in the superclasses." ); } } } }
In order to add a new modifier, we override the method isRegisteredModifier() and make it return true for the String object "overriding".