Skip to content

Instantiating an imported Java class #344

@msalman-abid

Description

@msalman-abid

I've been facing difficulty with accessing methods of an exported Java class within the scope of the Python guest language.

For context, I have a class in Java named Point.java which has the following code:

Point.java

class Point {
    int x;
    int y;

    Point(int x, int y) {
        this(x, y, false);
    }

    static final double EPS = 0.0001;

    Point(double r, double a/* ngle */, boolean polar) {
        double x = polar ? r * Math.cos(a) : r;
        double y = polar ? r * Math.sin(a) : a;
        this.x = (int) x;
        this.y = (int) y;
        if (Math.abs(this.x - x) >= EPS || Math.abs(this.y - y) >= EPS) {
            throw new IllegalArgumentException("Likely not integers!");
        }
    }

    int getX() {
        return x;
    }

    int getY() {
        return y;
    }

    public String toString() {
        return "(" + x + ", " + y + ")";
    }
}

Now, I have another file named Segment.java (which works in conjunction with Segment.py) to create instances of the Point class within Python. Some code for both of these files is shared below:

Segment.java

class Segment {
    private static Value clz;

    private Value obj;

    static {
        try {
            File file = new File("subtask-1.5/src/main/python", "Segment.py");
            Source source = Source.newBuilder("python", file).build();
            assert source != null;

            // build context to get EPS
            Context context = Context.newBuilder("python")
                    .allowAllAccess(true)
                    .option("python.PythonPath", "subtask-1.5/src/main/python")
                    .build();

            // add Point class to context before evaluating Segment.py
            context.getPolyglotBindings().putMember("Point", Point.class);

            context.eval(source);

            clz = context.getBindings("python").getMember("Segment");

        } catch (Exception e) {
            System.out.println("[-] " + e);
        }
    }

    Segment(Point p1, Point p2) {
        try {
            obj = clz.execute(p1, p2);
        } catch (PolyglotException e) {
            System.out.println("[-] " + e);
            throw new IllegalArgumentException("Points must differ!");
        }
    }
}

Segment.py

(This also has other code, but it is not relevant to the scope of this issue)

x = polyglot.import_value("Point")

x(1,2)
# Result: invalid instantiation of foreign object

I tried to replicate examples from the documentation on this python API found here, but I'm not certain how to access the constructor or other methods of this imported class.

The output from the dir() function in Python lists all available methods on the object, but I cannot figure out if I can call a constructor using any of these. Can you please tell me what I'm missing?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions