You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi. I am using JPype + some scala package (third-party app, I know almost nothing about scala) for pretty a long time. But it was always a pain to deal with scala stuff, since it does everything itself and in a very strange way. So I just took Krakatau decompiler, written snippets in scala (mostly taken from StackOverflow, but modified and simplified), decompiled them and tried to incorporate the generated code into my app.
It worked and with a lot of pain and debugging I got a working app. Untill the upstream (or maybe scala authors) has changed something in the app and I started getting very nasty errors, like
class scala.collection.convert.Wrappers$IterableWrapper cannot be cast to class java.lang.String (scala.collection.convert.Wrappers$IterableWrapper is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap').
Could you:
modularize the parts related to Scala into a separate package.
document your understanding how the conversion of java types into scala ones work and how to fix the typical bugs?
Here is my code
importrefromJAbsimportSelectedJVMInitializer, ClassPathT, ClassesImportSpecT#abstraction layer, allows to use not only with JPype, but also with GraalVMfromcollectionsimportOrderedDict, defaultdictimportsysclassScalaJVMInitializer:
__slots__= ("ji",)
def__init__(self, classPathz: ClassPathT, classes2import: ClassesImportSpecT) ->None:
self.__class__.ji.__set__(self, SelectedJVMInitializer(classPathz, classes2import))
self.loadScala()
def__getattr__(self, k):
returngetattr(self.ji, k)
def__setattr__(self, k, v):
setattr(self.ji, k, v)
defloadScala(self) ->None:
self.ji.loadClasses(( # this method of imports classes into ji, if tuple, the secind item is name"scala.concurrent.Await",
"scala.collection.mutable.Seq",
"scala.collection.JavaConverters",
("scala.Predef$", "scalaPredef"),
("scala.collection.Seq$", "scalaCollSeq"),
"java.util.Arrays"
))
self.scalaPredef=getattr(self.scalaPredef, "MODULE$")
self.scalaCollSeq=getattr(self.scalaCollSeq, "MODULE$")
defscalaSeq(self, it):
print("it", it)
coll=self.scalaCollSeq.apply(self.scalaPredef.wrapRefArray(list(it)))
coll=coll.to(self.scalaCollSeq.canBuildFrom())
print("coll", coll)
print("coll.__class__.__name__", coll.__class__.__name__)
returncoll#return self.scalaCollSeq.apply(self.scalaPredef.wrapRefArray(list(it)))#return self.scalaPredef.wrapRefArray(list(it))#return self.JavaConverters.collectionAsScalaIterable(self.Arrays.asList(list(it))).toSeq()scalaTupleRx=re.compile("^_(\\d+)$")
@classmethoddefscalaDetuple(cls, t):
res= [None] *t.productArity()
fornindir(t):
m=cls.scalaTupleRx.match(n)
ifmisnotNone:
res[int(m.group(1)) -1] =getattr(t, n)()
returntuple(res)
@staticmethoddefgetSomeKindOfImmutableObjectTemplate(cls):
c=max(cls.class_.getConstructors(), key=lambdact: len(ct.getParameters()))
returnOrderedDict([(str(p.getName()), None) forpinc.getParameters()])
@classmethoddefscalaSomeKindOfImmutableObjectToDict(cls, o, template=None):
iftemplate:
d=type(template)(template)
else:
d=cls.getSomeKindOfImmutableObjectTemplate(o.__class__)
forpind.keys():
d[p] =getattr(o, p)()
returnd@classmethoddefmergeScalaSomeKindOfImmutableObjects(cls, defaultOne, *args, subMerge=None):
res=cls.getSomeKindOfImmutableObjectTemplate(defaultOne.__class__)
ifsubMergeisNone:
subMerge= {}
scheduledSubMerges=defaultdict(list)
defaultOne=cls.scalaSomeKindOfImmutableObjectToDict(defaultOne, res)
res=type(defaultOne)(defaultOne)
fori, oinenumerate(args):
ifnotisinstance(o, dict):
o=cls.scalaSomeKindOfImmutableObjectToDict(o, res)
fork, vino.items():
#print(i, k, v)ifkinsubMerge:
scheduledSubMerges[k].append(v)
else:
vIsNone=visNonedefaultOneIsNone=defaultOne[k] isNoneif (vIsNone!=defaultOneIsNone) and (vIsNoneordefaultOneIsNoneorv!=defaultOne[k]):
res[k] =v#print(res)subMerged=type(res)()
forkinscheduledSubMerges:
subMerged[k] =subMerge[k](*scheduledSubMerges[k])
#print(subMerged)res.update(subMerged)
vals=list(res.values())
#print(vals)returnargs[0].__class__(*vals)
The text was updated successfully, but these errors were encountered:
Hi. I am using JPype + some scala package (third-party app, I know almost nothing about scala) for pretty a long time. But it was always a pain to deal with scala stuff, since it does everything itself and in a very strange way. So I just took Krakatau decompiler, written snippets in scala (mostly taken from StackOverflow, but modified and simplified), decompiled them and tried to incorporate the generated code into my app.
It worked and with a lot of pain and debugging I got a working app. Untill the upstream (or maybe scala authors) has changed something in the app and I started getting very nasty errors, like
class scala.collection.convert.Wrappers$IterableWrapper cannot be cast to class java.lang.String (scala.collection.convert.Wrappers$IterableWrapper is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')
.Could you:
Here is my code
The text was updated successfully, but these errors were encountered: