Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Not possible to implement a java.util.Comparator with AdapterFabric #571

Open
vvdleun opened this issue Nov 14, 2020 · 3 comments
Open

Not possible to implement a java.util.Comparator with AdapterFabric #571

vvdleun opened this issue Nov 14, 2020 · 3 comments

Comments

@vvdleun
Copy link

vvdleun commented Nov 14, 2020

Hello,

This could be classified as simply "works as designed", but wanted to submit it nevertheless:

This code compiles in Golo 3.3.0....

module demo

function main = |args| {
	let conf = map[
		["interfaces", ["java.util.Comparator"]],
		["implements", map[
			["compare", |this, o1, o2| -> Integer.compare(o1: size(), o2: size())]
		]]
	]

	let foo = list[[1], [1, 2, 3], [1, 2]]
		
	Collections.sort(foo, AdapterFabric(): maker(conf): newInstance())
} 

...but when running, it throws the exception:

Exception in thread "main" org.eclipse.golo.runtime.adapters.AdapterDefinitionProblem: There is no implementation or override for: public abstract boolean java.util.Comparator.equals(j
ava.lang.Object)
        at org.eclipse.golo.runtime.adapters.AdapterDefinition.checkMethodsToBeImplemented(AdapterDefinition.java:181)
        at org.eclipse.golo.runtime.adapters.AdapterDefinition.validate(AdapterDefinition.java:91)
        at gololang.AdapterFabric.maker(AdapterFabric.java:158)
        at java.base/java.lang.invoke.MethodHandle.invokeWithArguments(MethodHandle.java:710)
        at org.eclipse.golo.runtime.MethodInvocationSupport.fallback(MethodInvocationSupport.java:282)
        at demo.main(bug.golo:14)
        at org.eclipse.golo.cli.command.spi.CliCommand.callRun(CliCommand.java:39)
        at org.eclipse.golo.cli.command.GoloGoloCommand.execute(GoloGoloCommand.java:57)
        at org.eclipse.golo.cli.Main.main(Main.java:69)

When adding something like: ["equals", |this, o| -> false] to the conf implements map however, it throws:

Exception in thread "main" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
        at org.eclipse.golo.runtime.adapters.JavaBytecodeAdapterGenerator.generateIntoDefinitionClassloader(JavaBytecodeAdapterGenerator.java:82)
        at gololang.AdapterFabric.maker(AdapterFabric.java:159)
        at java.base/java.lang.invoke.MethodHandle.invokeWithArguments(MethodHandle.java:710)
        at org.eclipse.golo.runtime.MethodInvocationSupport.fallback(MethodInvocationSupport.java:282)
        at demo.main(bug.golo:13)
        at org.eclipse.golo.cli.command.spi.CliCommand.callRun(CliCommand.java:39)
        at org.eclipse.golo.cli.command.GoloGoloCommand.execute(GoloGoloCommand.java:57)
        at org.eclipse.golo.cli.Main.main(Main.java:69)
Caused by: java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.eclipse.golo.runtime.adapters.JavaBytecodeAdapterGenerator.generateIntoDefinitionClassloader(JavaBytecodeAdapterGenerator.java:80)
        ... 7 more
Caused by: java.lang.ClassFormatError: Duplicate method name "equals" with signature "(Ljava.lang.Object;)Z" in class file $Golo$Adapter$0
        at java.base/java.lang.ClassLoader.defineClass1(Native Method)
        at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
        at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:877)
        ... 12 more

PS: Very nice to see development on Golo again!

@vvdleun
Copy link
Author

vvdleun commented Nov 14, 2020

A workaround seems to be:

let comparator = asInterfaceInstance(java.util.Comparator.class, |o1, o2| -> Integer.compare(o1: size(), o2: size()))
Collections.sort(foo, comparator)

@jponge
Copy link
Contributor

jponge commented Nov 15, 2020

Good catch!

@yloiseau
Copy link
Contributor

yloiseau commented Nov 15, 2020 via email

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants