<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Recent changes to Variables</title><link>https://sourceforge.net/p/classmaker/wiki/Variables/</link><description>Recent changes to Variables</description><atom:link href="https://sourceforge.net/p/classmaker/wiki/Variables/feed" rel="self"/><language>en</language><lastBuildDate>Mon, 01 Oct 2012 00:12:04 -0000</lastBuildDate><atom:link href="https://sourceforge.net/p/classmaker/wiki/Variables/feed" rel="self" type="application/rss+xml"/><item><title>WikiPage Variables modified by Donald Strong</title><link>https://sourceforge.net/p/classmaker/wiki/Variables/</link><description>[Home]

----

Language features
=================

Variables
---------

_Local variables_

The &lt;code&gt;ClassMaker&lt;/code&gt; methods to declare and access local variables are:

&lt;pre&gt;
      public Type Declare(String name, Class javaClass, int modifiers) throws ClassMakerException
      public Type Get(String name) throws ClassMakerException
      public Type Set(String name, Type type) throws ClassMakerException
&lt;/pre&gt;

The methods are overloaded to handle local variables, member variables and static variables.

&lt;table border="1" width="100%"&gt;
&lt;tr&gt;&lt;td&gt;Java code&lt;/td&gt;&lt;td&gt;ClassMaker code&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;pre&gt;
      {
        int a;
        int b;
        a     = 2
        b     = a
      {
&lt;/pre&gt;&lt;/td&gt;
&lt;td&gt;&lt;pre&gt;
      Begin();
        Declare("a", int.class, 0);
        Declare("b", int.class, 0);
        Eval(Set("a", Literal(2)));
        Eval(Set("b", Get("a")));
      End();
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;

_Formal variables_

&lt;code&gt;ClassMaker&lt;/code&gt; uses the same methods to declare and access formal parameters.

&lt;pre&gt;
      public Type Declare(String name, Class javaClass, int modifiers) throws ClassMakerException
      public Type Get(String name) throws ClassMakerException
      public Type Set(String name, Type type) throws ClassMakerException
&lt;/pre&gt;

The only difference is that the &lt;code&gt;Declare&lt;/code&gt; method is called between the
&lt;code&gt;Method&lt;/code&gt; and &lt;code&gt;Begin&lt;/code&gt; methods.

&lt;table border="1" width="100%"&gt;
&lt;tr&gt;&lt;td&gt;Java code&lt;/td&gt;&lt;td&gt;ClassMaker code&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;pre&gt;
      void exec(int a, int b)
      {
        int c;
        c = a + b;
        a = c;
      }
&lt;/pre&gt;&lt;/td&gt;
&lt;td&gt;&lt;pre&gt;
      Method("exec", void.class, 0);
        Declare("a", int.class, 0);
        Declare("b", int.class, 0);
      Begin();
        Declare("c", int.class, 0);
        Eval(Set("c", Add(Get("a"), Get("b")));
        Eval(Set("a", Get("c")));
      End();
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;

_Member variables_

&lt;code&gt;ClassMaker&lt;/code&gt; uses the following methods to declare and access member variables.

&lt;pre&gt;
      public Type Declare(String name, Class javaClass, int modifiers) throws ClassMakerException
      public Type Get(Type classType, String fieldName) throws ClassMakerException
      public Type Set(Type classType, String name, Type type) throws ClassMakerException
&lt;/pre&gt;

The &lt;code&gt;Declare&lt;/code&gt; method is the same as that used for local variables and method parameters
but it is used outside any method.

The &lt;code&gt;Get&lt;/code&gt; and &lt;code&gt;Set&lt;/code&gt; methods take an extra parameter which is a reference to the
appropriate Object.

To access a variable in the current instance, the &lt;code&gt;this&lt;/code&gt; reference
must be provided. &lt;code&gt;ClasMaker&lt;/code&gt; provides a special method &lt;code&gt;This()&lt;/code&gt; to push a reference
to the current instance onto the stack.

To access a variable in another class instance, a reference to the appropriate object must
be pushed onto the stack. This is usually provided by one of the &lt;code&gt;Get&lt;/code&gt; methods.

&lt;table border="1" width="100%"&gt;
&lt;tr&gt;&lt;td&gt;Java code&lt;/td&gt;&lt;td&gt;ClassMaker code&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;pre&gt;
        public    int a;
        protected int b;
        private   int c;
&amp;nbsp;
        public void exec(MyClass other)
&amp;nbsp;
        {
          c = 2;  // implicit this reference
          b = c;  // implicit this reference
          a = other.a;
          other.a = c;
        }
&lt;/pre&gt;&lt;/td&gt;
&lt;td&gt;&lt;pre&gt;
        Declare("a", int.class, ACC_PUBLIC);
        Declare("b", int.class, ACC_PROTECTED);
        Declare("c", int.class, ACC_PRIVATE);
&amp;nbsp;
        Method("exec", void.class, ACC_PUBLIC);
          Declare("other", getClassType(), 0);
        Begin();
          Eval(Set(This(), "c", Literal(2)));
          Eval(Set(This(), "b", Get(This(), "c")));
          Eval(Set(This(), "a", Get(Get("other"), "a")));
          Eval(Set(Get("other"), "a", Get(This(), "c")));
        End();
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;

The other object is of type &lt;code&gt;MyClass&lt;/code&gt;, which is assumed to be the class being generated.
This &lt;code&gt;Type&lt;/code&gt; of the class being generated is available through the &lt;code&gt;getClassType()&lt;/code&gt;
method of the &lt;code&gt;ClassMaker&lt;/code&gt; instance.

_Static variables_

Static variables are declared and accessed in &lt;code&gt;ClassMaker&lt;/code&gt; using the following methods.

&lt;pre&gt;
      public Type Declare(String name, Class javaClass, int modifiers) throws ClassMakerException
      public Type Get(String className, String fieldName) throws ClassMakerException
      public Type Set(String className, String name, Type type) throws ClassMakerException
&lt;/pre&gt;

The &lt;code&gt;Declare&lt;/code&gt; method is the same as that used for all other declarations. For a variable
to be declared as static, the &lt;code&gt;Declare&lt;/code&gt; method must be called outside any method declaration
and the modifiers parameter must include the &lt;code&gt;ACC_STATIC&lt;/code&gt; bit-mask.

The &lt;code&gt;Get&lt;/code&gt; and &lt;code&gt;Set&lt;/code&gt; methods take the name of the class containing the static variable
instead of an object reference.

&lt;table border="1" width="100%"&gt;
&lt;tr&gt;&lt;td&gt;Java code&lt;/td&gt;&lt;td&gt;ClassMaker code&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;pre&gt;
        public static int s;
        &amp;nbsp;
        public void run()
        {
          int a;
          MyClass.s = a;
          a = MyClass.s;
        }
&lt;/pre&gt;&lt;/td&gt;
&lt;td&gt;&lt;pre&gt;
        Declare("s", int.class, ACC_PUBLIC | ACC_STATIC);
        &amp;nbsp;
        Method("exec", void.class, ACC_PUBLIC);
        Begin();
          Declare("a", int.class, 0);
          Set("MyClass", "s", Get("a"));
          Set("a", Get("MyClass", "s"));
        End();
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;

_Array variables_

    FIXME - Example of array variables
    
_Variable scope_

    FIXME - Variable scope is implemented so need an example

----

[Home]
</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Donald Strong</dc:creator><pubDate>Mon, 01 Oct 2012 00:12:04 -0000</pubDate><guid>https://sourceforge.neta97c5b8cf0573e270c040f75b80b5a7be5506bdc</guid></item></channel></rss>