Last updated June 30, 2009 13:37, by Joshua
Feedicon  

Using JythonObjectFactory

The following is a list of steps required to use the JythonObjectFactory implementation out-of-the-box.

1. Add jython.jar to your Java project classpath

2. Add PlyJy.jar to your Java project classpath

3. Ensure your Jython classes implement Java interfaces and are included in your sys.path so that the PythonInterpreter can find them

4. Use the factory to coerce your Jython objects!

Example

In this example, we are using a Jython class stored in a module named Automobile.py. We will implement a Java interface that contains public declarations for all of the methods from our Automobile class that we want to use in the Java application.

Java interface name: AutomobileType.java

public interface AutomobileType {
    public String getType();
    public void setType(String value);
    public String getColor();
    public void setColor(String value);
    public String getDoc();


}

Automobile.py

from myjavaprogram.interfaces import AutomobileType

class Automobile(AutomobileType, object):
    ''' Bean to hold automobile objects '''

    def __init__(self):
        print 'Initializing Now'
        pass

    def setType(self, value):
        self.__type = value
        print 'setting object: ' + value

    def getType(self):
        print 'getting object'
        return self.__type

    type = property(fget=getType,
                    fset=setType,
                    doc="The Type of the Automobile.")

    def setColor(self, value):
        self.__color = value

    def getColor(self):
        return self.__color

    def getDoc(self):
        return self.__doc__



    color = property(fget=getColor,
                     fset=setColor,
                     doc="Color of Automobile.")

Using the Code!


import org.plyjy.factory.JythonObjectFactory;

...

JythonObjectFactory factory = JythonObjectFactory.getInstance();
AutomobileType automobile = (AutomobileType) factory.createObject(
                AutomobileType.class, "Automobile");
automobile.setType("Sport");
automobile.setColor("red");
// Use the automobile object!

  • Mysql
  • Glassfish
  • Jruby
  • Rails
  • Nblogo
Terms of Use; Privacy Policy;
© 2013, Oracle Corporation and/or its affiliates
(revision 20140418.2d69abc)
 
 
Close
loading
Please Confirm
Close