Issue2181

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

Created on 2014-07-24.14:53:55 by brianray, last changed 2015-04-28.16:07:32 by zyasoft.

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
msg9917 (view) Author: Jim Baker (zyasoft) Date: 2015-04-20.21:09:41
This should have been just fixed, the root cause of #1795 is almost certainly a duplicate

Could you please try out the latest RC3 when it's published shortly (hopefully tonight!), or against trunk?
History
Date User Action Args
2015-04-28 16:07:32zyasoftsetstatus: pending -> closed
2015-04-20 21:09:41zyasoftsetstatus: open -> pending
resolution: duplicate
messages: + msg9917
nosy: + zyasoft
2014-07-24 14:53:55brianraycreate