diff options
author | Kenny Ballou <kballou@devnulllabs.io> | 2018-11-19 22:59:50 -0700 |
---|---|---|
committer | Kenny Ballou <kballou@devnulllabs.io> | 2018-11-19 22:59:50 -0700 |
commit | ea3e1b949dcbdc09518f17eee0bcf21d41d76896 (patch) | |
tree | 7ec7a7fb4df67815a9b7bb0e4d95d67c4050e2a2 /tutorial/examples/mixin | |
download | openjava-ea3e1b949dcbdc09518f17eee0bcf21d41d76896.tar.gz openjava-ea3e1b949dcbdc09518f17eee0bcf21d41d76896.tar.xz |
OJ (aka OpenJava) modernization/mirroring
Signed-off-by: Kenny Ballou <kballou@devnulllabs.io>
Diffstat (limited to 'tutorial/examples/mixin')
-rw-r--r-- | tutorial/examples/mixin/0UNDERCONSTRUCTION | 0 | ||||
-rw-r--r-- | tutorial/examples/mixin/MixinedClass.java | 124 | ||||
-rw-r--r-- | tutorial/examples/mixin/MixinedClass.oj | 115 | ||||
-rw-r--r-- | tutorial/examples/mixin/Textbox.java | 23 | ||||
-rw-r--r-- | tutorial/examples/mixin/Textbox.oj | 17 | ||||
-rw-r--r-- | tutorial/examples/mixin/TextboxWithUndo.java | 22 | ||||
-rw-r--r-- | tutorial/examples/mixin/TextboxWithUndo.oj | 8 | ||||
-rw-r--r-- | tutorial/examples/mixin/Undo.java | 8 | ||||
-rw-r--r-- | tutorial/examples/mixin/Undo.oj | 19 |
9 files changed, 336 insertions, 0 deletions
diff --git a/tutorial/examples/mixin/0UNDERCONSTRUCTION b/tutorial/examples/mixin/0UNDERCONSTRUCTION new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tutorial/examples/mixin/0UNDERCONSTRUCTION diff --git a/tutorial/examples/mixin/MixinedClass.java b/tutorial/examples/mixin/MixinedClass.java new file mode 100644 index 0000000..c281fb9 --- /dev/null +++ b/tutorial/examples/mixin/MixinedClass.java @@ -0,0 +1,124 @@ +/* + * This code was generated by ojc. + */ +/* + * MixinedClass.oj + * + * An OpenJava example for incorporating mixin mechanism. + * + * @author Michiaki Tatsubori + * @version %VERSION% %DATE% + * @see java.lang.Object + * + * COPYRIGHT 1999 by Michiaki Tatsubori, ALL RIGHTS RESERVED. + */ +package examples.mixin; + + +import java.lang.Object; +import openjava.mop.*; +import openjava.ptree.*; +import openjava.syntax.*; + + +/** + * The metaclass <code>MixinedClass</code> supports classes + * using mixin. + * <p> + * For example, the class <code>MixinedClass</code>: + * <pre> + * public class TextboxWithUndo instantiates MixinedClass + * extends Textbox mixs Undo + * { + * } + * </pre> + * would be automatically implemented. + * <p> + * + * @author Michiaki Tatsubori + * @version 1.0 + * @since $Id: MixinedClass.java,v 1.2 2003/02/19 02:55:01 tatsubori Exp $ + * @see java.lang.Object + */ +public class MixinedClass extends OJClass +{ + + public static final String KEY_MIXES = "mixes"; + + /* overrides for translation */ + public void translateDefinition() + throws MOPException + { + OJClass[] mixinClasses = getMixins(); + for (int i = 0; i < mixinClasses.length; ++i) { + incorporateMixin( mixinClasses[i] ); + } + } + + public void incorporateMixin( OJClass mixinClazz ) + throws MOPException + { + addInterface( mixinClazz ); + OJMethod[] mixeds = mixinClazz.getMethods(); + for (int i = 0; i < mixeds.length; ++i) { + addMethod( makeEmptyMethod( mixeds[i] ) ); + } + } + + /** + * Generates a empty method with specified name. + * + * @param name generating method's name. + * @param forwarded method to which the generated method forwards. + * @return a generated empty method. + */ + private OJMethod makeEmptyMethod( OJMethod implementee ) + throws MOPException + { + StatementList body = new StatementList(); + if (implementee.getReturnType() == OJSystem.VOID) { + body.add( new ReturnStatement() ); + } else { + body.add( new ReturnStatement( Literal.constantNull() ) ); + } + return new OJMethod( this, implementee.getModifiers().remove( OJModifier.ABSTRACT ), implementee.getReturnType(), implementee.getName(), implementee.getParameterTypes(), implementee.getExceptionTypes(), body ); + } + + /* extended information */ + private OJClass[] getMixins() + throws MOPException + { + ObjectList suffix = (ObjectList) getSuffix( KEY_MIXES ); + OJClass[] result = new OJClass[suffix.size()]; + for (int i = 0; i < result.length; ++i) { + result[i] = OJClass.forName( suffix.get( i ).toString() ); + } + return result; + } + + /* override to extend syntax */ + public static boolean isRegisteredKeyword( String keyword ) + { + return keyword.equals( KEY_MIXES ); + } + + /* override to extend syntax */ + public static SyntaxRule getDeclSuffixRule( String keyword ) + { + if (keyword.equals( KEY_MIXES )) { + return new DefaultListRule( new TypeNameRule(), TokenID.COMMA ); + } + return null; + } + + public MixinedClass( openjava.mop.Environment oj_param0, openjava.mop.OJClass oj_param1, openjava.ptree.ClassDeclaration oj_param2 ) + { + super( oj_param0, oj_param1, oj_param2 ); + } + + public MixinedClass( java.lang.Class oj_param0, openjava.mop.MetaInfo oj_param1 ) + { + super( oj_param0, oj_param1 ); + } + +} diff --git a/tutorial/examples/mixin/MixinedClass.oj b/tutorial/examples/mixin/MixinedClass.oj new file mode 100644 index 0000000..8883948 --- /dev/null +++ b/tutorial/examples/mixin/MixinedClass.oj @@ -0,0 +1,115 @@ +/*
+ * MixinedClass.oj
+ *
+ * An OpenJava example for incorporating mixin mechanism.
+ *
+ * @author Michiaki Tatsubori
+ * @version %VERSION% %DATE%
+ * @see java.lang.Object
+ *
+ * COPYRIGHT 1999 by Michiaki Tatsubori, ALL RIGHTS RESERVED.
+ */
+package examples.mixin;
+
+
+import java.lang.Object;
+import openjava.mop.*;
+import openjava.ptree.*;
+import openjava.syntax.*;
+
+
+/**
+ * The metaclass <code>MixinedClass</code> supports classes
+ * using mixin.
+ * <p>
+ * For example, the class <code>MixinedClass</code>:
+ * <pre>
+ * public class TextboxWithUndo instantiates MixinedClass
+ * extends Textbox mixs Undo
+ * {
+ * }
+ * </pre>
+ * would be automatically implemented.
+ * <p>
+ *
+ * @author Michiaki Tatsubori
+ * @version 1.0
+ * @since %SOFTWARE% 1.0
+ * @see java.lang.Object
+ */
+public class MixinedClass instantiates Metaclass extends OJClass
+{
+
+ public static final String KEY_MIXES = "mixes";
+
+ /* overrides for translation */
+ public void translateDefinition() throws MOPException {
+ OJClass[] mixinClasses = getMixins();
+
+ for (int i = 0; i < mixinClasses.length; ++i) {
+ incorporateMixin(mixinClasses[i]);
+ }
+ }
+
+ public void incorporateMixin(OJClass mixinClazz) throws MOPException {
+ /* implicit forwarding to the same signature's */
+ addInterface(mixinClazz);
+ OJMethod[] mixeds = mixinClazz.getMethods();
+ for (int i = 0; i < mixeds.length; ++i) {
+ /* generate a forwarding method with forwarded's name */
+ addMethod(makeEmptyMethod(mixeds[i]));
+ }
+ }
+
+ /**
+ * Generates a empty method with specified name.
+ *
+ * @param name generating method's name.
+ * @param forwarded method to which the generated method forwards.
+ * @return a generated empty method.
+ */
+ private OJMethod makeEmptyMethod(OJMethod implementee)
+ throws MOPException
+ {
+ /* generates a return statement */
+ StatementList body = new StatementList();
+ if (implementee.getReturnType() == OJSystem.VOID) {
+ body.add(new ReturnStatement());
+ } else {
+ body.add(new ReturnStatement(Literal.constantNull()));
+ }
+
+ /* generates a new method without body */
+ return new OJMethod( this,
+ implementee.getModifiers().remove(OJModifier.ABSTRACT),
+ implementee.getReturnType(), implementee.getName(),
+ implementee.getParameterTypes(), implementee.getExceptionTypes(),
+ body );
+ }
+
+ /* extended information */
+
+ private OJClass[] getMixins() throws MOPException {
+ ObjectList suffix = (ObjectList) getSuffix(KEY_MIXES);
+ OJClass[] result = new OJClass[suffix.size()];
+ for (int i = 0; i < result.length; ++i) {
+ result[i] = OJClass.forName(suffix.get(i).toString());
+ }
+ return result;
+ }
+
+ /* override to extend syntax */
+ public static boolean isRegisteredKeyword( String keyword ) {
+ return keyword.equals( KEY_MIXES );
+ }
+
+ /* override to extend syntax */
+ public static SyntaxRule getDeclSuffixRule( String keyword ) {
+ if (keyword.equals( KEY_MIXES )) {
+ return new DefaultListRule(new TypeNameRule(), TokenID.COMMA);
+ }
+ return null;
+ }
+
+}
+
diff --git a/tutorial/examples/mixin/Textbox.java b/tutorial/examples/mixin/Textbox.java new file mode 100644 index 0000000..5b0e06b --- /dev/null +++ b/tutorial/examples/mixin/Textbox.java @@ -0,0 +1,23 @@ +/* + * This code was generated by ojc. + */ +package examples.mixin; + + +public class Textbox extends java.awt.Component +{ + + String text = null; + + /* ..... */ + public String getText() + { + return text; + } + + public void setText( String s ) + { + text = s; + } + +} diff --git a/tutorial/examples/mixin/Textbox.oj b/tutorial/examples/mixin/Textbox.oj new file mode 100644 index 0000000..a3313d0 --- /dev/null +++ b/tutorial/examples/mixin/Textbox.oj @@ -0,0 +1,17 @@ +package examples.mixin;
+
+
+public class Textbox extends java.awt.Component
+{
+ String text = null;
+
+ /* ..... */
+
+ public String getText() {
+ return text;
+ }
+
+ public void setText(String s) {
+ text = s;
+ }
+}
diff --git a/tutorial/examples/mixin/TextboxWithUndo.java b/tutorial/examples/mixin/TextboxWithUndo.java new file mode 100644 index 0000000..9772e62 --- /dev/null +++ b/tutorial/examples/mixin/TextboxWithUndo.java @@ -0,0 +1,22 @@ +/* + * This code was generated by ojc. + */ +package examples.mixin; + + +public class TextboxWithUndo extends Textbox implements examples.mixin.Undo +{ + + + public void setText( java.lang.String oj_param0 ) + { + return; + } + + + public void undo() + { + return; + } + +} diff --git a/tutorial/examples/mixin/TextboxWithUndo.oj b/tutorial/examples/mixin/TextboxWithUndo.oj new file mode 100644 index 0000000..cc04f88 --- /dev/null +++ b/tutorial/examples/mixin/TextboxWithUndo.oj @@ -0,0 +1,8 @@ +package examples.mixin;
+
+
+public class TextboxWithUndo instantiates MixinedClass
+ extends Textbox
+ mixes Undo
+{
+}
diff --git a/tutorial/examples/mixin/Undo.java b/tutorial/examples/mixin/Undo.java new file mode 100644 index 0000000..a7f6bfb --- /dev/null +++ b/tutorial/examples/mixin/Undo.java @@ -0,0 +1,8 @@ +package examples.mixin; + + +public interface Undo +{ + public void setText(String s); + public void undo(); +} diff --git a/tutorial/examples/mixin/Undo.oj b/tutorial/examples/mixin/Undo.oj new file mode 100644 index 0000000..66ad9a9 --- /dev/null +++ b/tutorial/examples/mixin/Undo.oj @@ -0,0 +1,19 @@ +package examples.mixin;
+
+
+public class Undo
+{
+ inherited String getText();
+ inherited void setText(String s);
+
+ String lastText;
+
+ public void setText(String s) {
+ lastText = getText();
+ super.setText(s);
+ }
+
+ public void undo() {
+ super.setText(lastText);
+ }
+}
|