summaryrefslogtreecommitdiff
path: root/tutorial/examples/capsule
diff options
context:
space:
mode:
Diffstat (limited to 'tutorial/examples/capsule')
-rw-r--r--tutorial/examples/capsule/CapsuleClass.oj130
-rw-r--r--tutorial/examples/capsule/Point.oj22
-rw-r--r--tutorial/examples/capsule/PointUser.oj17
-rw-r--r--tutorial/examples/capsule/Test.oj47
-rw-r--r--tutorial/examples/capsule/TestUser.oj38
-rw-r--r--tutorial/examples/capsule/TestUser2.oj8
6 files changed, 262 insertions, 0 deletions
diff --git a/tutorial/examples/capsule/CapsuleClass.oj b/tutorial/examples/capsule/CapsuleClass.oj
new file mode 100644
index 0000000..9d54fe3
--- /dev/null
+++ b/tutorial/examples/capsule/CapsuleClass.oj
@@ -0,0 +1,130 @@
+/*
+ * CapsuleClass.oj
+ *
+ * Apr 23, 1999 by Michiaki Tatsubori
+ */
+package examples.capsule;
+
+
+import java.lang.Object;
+import openjava.mop.*;
+import openjava.ptree.*;
+import openjava.syntax.*;
+
+
+/**
+ * The class <code>CapsuleClass</code> is a metaclass which
+ * hides all the fields in the class and provides reader and writer
+ * methods corresponding to each field. Furthermore, accesses
+ * to fields of the class is to be replaced with these methods.
+ */
+public class CapsuleClass instantiates Metaclass extends OJClass
+{
+ /** Overrides to translate definition */
+ public void translateDefinition() throws MOPException {
+ /* Detect public fields in superclass */
+ OJField[] inherited_flds = getSuperclass().getFields();
+ if (inherited_flds.length > 0) {
+ System.err.println( "WARNING: Leak Possibilities" );
+ for (int i = 0; i < inherited_flds.length; ++i)
+ System.err.println( inherited_flds[i].toString() );
+ }
+
+ /* Get all declared fields and hide it */
+ OJField[] fields = getDeclaredFields();
+ for (int i = 0; i < fields.length; ++i) {
+ OJModifier modif = fields[i].getModifiers();
+ if (! modif.isPrivate()) hideField( fields[i] );
+ }
+ }
+
+ private void hideField( OJField field ) throws MOPException {
+ System.err.println( "Hiding a field : " + field.toString() );
+ generateReaderMethod( field );
+ generateWriterMethod( field );
+ field.setModifiers( field.getModifiers().setPrivate() );
+ }
+
+ private void generateReaderMethod( OJField field ) throws MOPException {
+ StatementList body = makeStatementList(
+ "return " + field.getName() + ";"
+ );
+ OJMethod reader = new OJMethod( this,
+ field.getModifiers(), field.getType(),
+ fieldReaderName( field.getName() ),
+ (OJClass[]) null, null, body
+ );
+ addMethod( reader );
+ }
+
+ private void generateWriterMethod( OJField field ) throws MOPException {
+ OJMethod writer = new OJMethod( this,
+ field.getModifiers(), field.getType(),
+ fieldWriterName( field.getName() ),
+ new OJClass[]{ field.getType() }, null, null
+ );
+ String param = writer.getParameters()[0];
+ StatementList body = makeStatementList( writer.getEnvironment(),
+ "return " + field.getName() + "=" + param + ";"
+ );
+ writer.setBody( body );
+ addMethod( writer );
+ }
+
+ private static final String fieldReaderName( String name ) {
+ return "read_" + name;
+ }
+
+ private static final String fieldWriterName( String name ) {
+ return "write_" + name;
+ }
+
+ /* overrides */
+
+ /** Allows references to private fields */
+ public OJField resolveException( NoSuchMemberException e, String name )
+ throws NoSuchMemberException
+ {
+ try {
+ return getDeclaredField( name );
+ } catch ( NoSuchMemberException e2 ) {}
+ return super.resolveException( e, name );
+ }
+
+ /** Overrides to expand fields to be read */
+ public Expression expandFieldRead(
+ Environment env,
+ FieldAccess expr )
+ {
+ Expression ref = expr.getReferenceExpr();
+ String name = fieldReaderName( expr.getName() );
+ Expression result;
+ if (ref != null) {
+ result = new MethodCall( ref, name, null );
+ } else {
+ result = new MethodCall( expr.getReferenceType(), name, null );
+ }
+ System.err.println( "Patch FR : " + expr + "\tto\t" + result );
+ return result;
+ }
+
+ /** Overrides to expand fields to be written */
+ public Expression expandFieldWrite(
+ Environment env,
+ AssignmentExpression expr )
+ {
+ FieldAccess fldac = (FieldAccess) expr.getLeft();
+ ExpressionList args = new ExpressionList( expr.getRight() );
+ Expression ref = fldac.getReferenceExpr();
+ String name = fieldWriterName( fldac.getName() );
+ Expression result;
+ if (ref != null) {
+ result = new MethodCall( ref, name, args );
+ } else {
+ result = new MethodCall( fldac.getReferenceType(), name, args );
+ }
+ System.err.println( "Patch FW : " + expr + "\tto\t" + result );
+ return result;
+ }
+
+}
diff --git a/tutorial/examples/capsule/Point.oj b/tutorial/examples/capsule/Point.oj
new file mode 100644
index 0000000..a74f566
--- /dev/null
+++ b/tutorial/examples/capsule/Point.oj
@@ -0,0 +1,22 @@
+/*
+ * Point.oj
+ */
+package examples.capsule;
+
+
+public class Point instantiates CapsuleClass
+{
+ public int x, y;
+ protected String name;
+
+ public Point( String name, int x, int y ) {
+ this.x = x;
+ this.y = y;
+ this.name = name;
+ }
+
+ public Point( int x, int y ) {
+ this( "DefaultName", x, y );
+ }
+
+}
diff --git a/tutorial/examples/capsule/PointUser.oj b/tutorial/examples/capsule/PointUser.oj
new file mode 100644
index 0000000..f81a335
--- /dev/null
+++ b/tutorial/examples/capsule/PointUser.oj
@@ -0,0 +1,17 @@
+/*
+ * PointUser.oj
+ */
+package examples.capsule;
+
+
+public class PointUser
+{
+
+ public static void main( String[] args ) {
+ Point p = new Point( 1, 2 );
+ p.name = "MyFavorite";
+ int x = p.x + 1;
+ p.y = x * 2 + p.y;
+ }
+
+}
diff --git a/tutorial/examples/capsule/Test.oj b/tutorial/examples/capsule/Test.oj
new file mode 100644
index 0000000..4b25816
--- /dev/null
+++ b/tutorial/examples/capsule/Test.oj
@@ -0,0 +1,47 @@
+package examples.capsule;
+
+
+import java.awt.Panel;
+import java.util.*;
+import java.io.PrintStream;
+
+
+public class Test instantiates CapsuleClass
+{
+ private int iii = 0;
+
+ protected Test n = null;
+
+ public String str = "string";
+
+ public static String NAME = "Test";
+
+ public Test() {
+ super();
+ }
+
+ public static void main( String[] argv ) {
+ PrintStream out = System.out;
+ PrintStream error = java.lang.System.err;
+ out.println( "Hello" + " " + "World" );
+ Test n = new Test();
+ java.lang.System.err.println( "done. " );
+ Test test = new Test();
+ test.foo();
+ }
+
+ public Test( String str ) {
+ this.str = str;
+ n = null;
+ }
+
+ public int foo() {
+ return iii;
+ }
+
+ public String toString() {
+ if (n == null) return str;
+ return str + n;
+ }
+
+}
diff --git a/tutorial/examples/capsule/TestUser.oj b/tutorial/examples/capsule/TestUser.oj
new file mode 100644
index 0000000..1aa85b6
--- /dev/null
+++ b/tutorial/examples/capsule/TestUser.oj
@@ -0,0 +1,38 @@
+package examples.capsule;
+
+
+import java.awt.Panel;
+import java.util.*;
+import java.io.PrintStream;
+
+
+public class TestUser
+{
+ Test test;
+
+ public static void main( String[] argv ) {
+ TestUser user = new TestUser();
+ Test test = new Test();
+ System.out.println( test.str.toString() );
+ System.out.println( test.n.toString() );
+ test.foo();
+ }
+
+ private TestUser() {
+ this.test = new Test();
+ }
+
+ public void run() {
+ test.n = test;
+ java.lang.System.out.println( test.n = test );
+ System.out.println( (test.n = test).toString() );
+ }
+
+ public void print() {
+ System.out.println( test.n.str );
+ String s = ((String) Test.NAME).toString();
+ String sa = Test.NAME + "_ALPHA";
+ String ss = (Test.NAME).toString();
+ return;
+ }
+}
diff --git a/tutorial/examples/capsule/TestUser2.oj b/tutorial/examples/capsule/TestUser2.oj
new file mode 100644
index 0000000..931f279
--- /dev/null
+++ b/tutorial/examples/capsule/TestUser2.oj
@@ -0,0 +1,8 @@
+package examples.capsule;
+
+public class TestUser2
+{
+ public static void main( String[] argv ) {
+ Test.NAME.toString();
+ }
+}