How to cast variables in python with jcc - java

In java it is possible to cast an object onto a class.
An good example is found here
Object aSentenceObject = "This is just a regular sentence";
String aSentenceString = (String)aSentenceObject;
I have a program that needs to integrate some java with python. I am trying to do this via the JCC library. The problem that I am encountering is that with JCC, all of the java classes are loaded into the imported library that I created with JCC. So I can create an instance of the base class by passing the necessary argument to the constructor of the java class.
obj = javaLibrary.BaseClass('foo')
However, in my code I need to be able to cast this object onto a a more “specific” type of Object.
How can I accomplish this in python with JCC? It seems like it may be impossible because python is dynamically typed, but that is why I am asking this question.

All comments above valid, but to be specific for your case:
casted_obj = Object.cast_(obj)

Related

In Java, detect if an Object is a Kotlin array

Sorry if this is very specific...
I am looking at Javapoet's implementation of AnnotationSpec.get(Annotation). It recursively clones an annotation so that you can copy it to another element. In my case, I am trying to copy an #OpenApi annotation from one method to another.
The issue is caused by the annotations being written in kotlin. Anywhere the annotation needs a class, it is using KClass<*>. Javapoet only handles Java types, like Class<?>. From Java, I can say if (o instanceof Class || o instance of KClass) no problem.
However, there's also code that says o.getClass().isArray() but, from what I can tell, kotlin annotations use Array<*> for arrays, so that check is failing. The actual type appears to be com.sun.proxy.$Proxy54 when I inspect it, but I have no idea what that is.
How do you detect if an object is an kotlin array from Java? Can this be converted to a Java array? Is there some universal way to make kotlin annotations appear as java annotations using Class and built-in arrays and so on?
Kotlin arrays and Java arrays are the same thing at the VM level. Array<*> is Kotlin's syntax for arrays, but the objects are the same thing. .isArray should work.
The proxy objects are typical for what you get when you call getClass on annotation objects in Java or Kotlin. You likely need to use annotationType instead of getClass.

How to share an object state between java and prolog with JPL?

I want to create an object in java:
MyObject obj = new MyObject ();
and I want to pass it to prolog with a jpl query.
How can I accomplish java to prolog object passing?
I know that I could use jpl_new in a prolog file like this:
execMethod :-
jpl_new('my_package.MyObject', [], Object),
jpl_call(Object, myMethod, [], _ ).
But, I want to avoid the jpl_new call and just use the jpl_call with the java object obj.
And converserly,
How can I accomplish prolog to java object passing?
I mean passing to java, objects created with a jpl_new call.
In other words, I want to share an object state between java and prolog.
To access a Prolog knowledge base from within Java, you can use JPL Queries. Let's look at a simple, trivial example below:
% Knowledge base (Prolog)
foo(x,bar).
all_foo(X,Y) :- foo(X,Y).
In java, we could then write:
String query = "all_foo(x,Y)";
System.out.println("First solution: " + Query.oneSolution(query).get("Y"));
which would return 'bar' as answer in Y.
Vice versa -as you showed in your question- JPL can be used when we want to access Java functionality from within a Prolog file.
Firstly, looking at the docs of jpl_call/4, we see that its first arguments can be:
a type, class object or classname (for static methods of the denoted class, or for static or instance methods of java.lang.Class)
a class instance or array (for static or instance methods)
So you are free in how to pass your class information to jpl_call/4 to execute certain methods.
Subsequently, you can access your Java model rather than executing logic by using jpl_get/3. An example below is shown where we bind the Prolog variable Colour to a reference of a field of a Java car object held in the static final .colour field of the example.class.car class.
jpl_get('example.class.car', colour, Colour)
More generally:
jpl_get(+Class_or_Object, +Field, -Datum)
Hope this helped.
Good luck!

Can reflection bind a class to a new name?

I am trying to let users of my program rename classes and methods in the class. The new names can be read in and configured at run time and they will call these classes and methods in a scripting language using Java Script Engine. I need a way to bind their new names to the real names of the classes and methods. I have been looking at Reflection but I do not think this can provide me with the capability I need, or is this even possible?
Ex:
public class RealName {
public void printHello() {
System.out.println("Hello");
}
}
Then in maybe Paython say
obj = new NewName()
obj.hello()
Tell me if this is impossible please!
You can not change the method names, but you can bind an instance of an object to a given name and inject that into the context of the scripting language.
This would only work for that instance of the class, not for instantiating new instances.
If you really want this you may be able to generate sub classes with the new name and method names in the target scripting language and inject them into to the context to get the effect you are looking for.
Having said all that I can't really come up with a good reason to do any of this.
To answer my question from what I've found no you cannot use reflection to bind a class to a new name. In fact I found no easy way to do dynamic renaming.
What I did to overcome this was to write code from a string to a file, save that file with extension .java, compile that file, then use it with reflection or better yet use it inside a script using the Java ScriptEngine API (that way you can avoid the ugly reflection code and actually have everything dynamic and on the fly).
Here's a starting point for creating the file,
Dynamic in-memory compilation
And here's something for scripting Java,
Scripting for Java

getting the argument names of a function like netbeans does

Ok, this could be a tricky one. For a code generating tool I need to know methods and arguments of a class. The method name and argument types are the easy ones - just using reflection. But the argument name - and I need the real argument name - is a tricky one because this information is in the javadoc. In my case I use Netbeans 8 and I am pretty sure if Netbeans can get the arguments name I can too. Does anyone know how to read the javadoc to get the argument names of a method?
PS I know this question will pop up. I need the real argument names because the generated code provides an api and it is not very helpful for a developper to use an api where the api methods are something like set_a1, set_a2, and so on.
Indeed, this is tricky, and will involve a considerable effort if you intend to find a general solution that works for arbitrary (third-party) classes and arbitrary Java versions.
However, under certain conditions, there may be a simple solution:
If you can compile the classes on your onw, and if you can use Java 8, then you can use the Method Parameter Reflection infrastructure that was added in Java 8. When compiling the classes with javac -parameters ..., then the parameter names are added to the class file, and can be obtained from the method by calling getParameters on the Method object, and then Parameter#getName()
Parameter parameters[] = method.getParameters();
String name = parameters[0].getName();
...

Rhino - Passing a javascript object to java

Im quite new to Rhino and my question is around how to achieve the following,
Say I have a javascript object that follows something like the following that I can consume within java.
var myObject = new Object();
myObject.string1 = "Hello";
myObject.string2 = "World";
myObject.int1 = 1;
But how do I consume this within java if its dynamic. For .e.g. if you decide to add few more members dynamically to this object within javascript. Is this doable ? My guess is the class defined within java will have to take all the possible members into account to do this ?
Hopefully I have explained what im trying to achieve correctly.
JavaScript objects, when you access them in Java, are all essentially the same class: ScriptableObject which implements the Scriptable interface (GitHub source). There are a few other classes for functions and other specialized objects.
The Scriptable interface includes methods like get, has, and put that correspond roughly to myObject.string1, myObject.hasOwnProperty("string1"), and myObject.string1 = "Hello" in JavaScript. The ScriptableObject class adds some other useful methods for defining properties, etc.
Consider using a library like GSON for converting a javascript object to JAVA.
https://code.google.com/p/google-gson/
you can convert a javascript object to JSON using JSON.stringify
and then use GSON or another such library to generate a Java object.

Categories

Resources