Issue2181

classification
Title: __tojava__ does not support cast to user derived class
Type: Severity: major
Components: Core Versions: Jython 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: brianray
Priority: Keywords:

Created on 2014-07-24.14:53:55 by brianray, last changed 2014-07-24.14:53:55 by brianray.

Messages
msg8886 (view) Author: Brian ray (brianray) Date: 2014-07-24.14:53:54
I get the following exception:

>     Exception in thread "main" java.lang.ClassCastException:
>     org.python.core.PySingleton cannot be cast to 
>     com.xxx.yyyy.Example.ExapleApiJavaType

This happens when I call 'return (ExampleApiJavaType) ExampleApiObject.__tojava__(ExampleApiJavaType.class)', full code here:

    package com.xxx.yyyy.example;
    
    import com.xxx.yyyy.example.ExampleApiJavaType;
    import org.python.core.PyObject;
    import org.python.core.PyString;
    import org.python.util.PythonInterpreter;
    
    public class ExampleApiFactory {
    
        private PyObject ExampleApiClass;
   
    
        public ExampleApiFactory() {
            PythonInterpreter interpreter = new PythonInterpreter();
            interpreter.exec("from ExampleAPI import ExampleAPI");
            ExampleApiClass = interpreter.get("ExampleAPI");
        }
    

    
        public ExampleApiJavaType create (String userid, String passw, String namespace, String URL, String api_level) {
    
            PyObject[] arguments = new PyObject[] {
                    new PyString(userid),
                    new PyString(passw),
                    new PyString(namespace),
                    new PyString(URL),
                    new PyString(api_level)
            };
    
            PyObject ExampleApiObject = ExampleApiClass.__call__(arguments);
    
            return (ExampleApiJavaType) ExampleApiObject.__tojava__(ExampleApiJavaType.class);
        }
    
    }

My type is here:

    // Java interface for a building object
    package com.xxx.yyyy.example;
    
    
    
    public interface ExampleApiJavaType{
    
        public void start_session();
        public void stop_session();
        public String get_related(String article_id);
        public String get_company(String tickerid);
        public void save_last_results_to_disk(String filename);
        public String get_article(String query_string);
    
    }


I used the example from the book http://www.jython.org/jythonbook/en/1.0/JythonAndJavaIntegration.html#one-to-one-jython-object-factories 

Using jython-standalone-2.7-b2 with jdk1.7.0_51

Attached is a complete example. (also found here https://github.com/brianray/embedded-jython)

Also see related case http://bugs.jython.org/issue1103
History
Date User Action Args
2014-07-24 14:53:55brianraycreate