finagle icon indicating copy to clipboard operation
finagle copied to clipboard

Inheriting MarshalledContext.Key in java has compile-time error

Open huangyoje opened this issue 5 years ago • 4 comments

Inheriting MarshalledContext.Key in java has compile-time error

Expected behavior

Inheriting MarshalledContext.Key in java as follows:

public class Test {
    static class Impl extends MarshalledContext.Key<String> {
        public Impl() {
            Contexts.broadcast().super("impl1");
        }
        @Override
        public Buf marshal(String value) {
            return null;
        }
        @Override
        public Try<String> tryUnmarshal(Buf buf) {
            return null;
        }
    }
    public static void main(String[] args) {
        System.out.println(new Impl());
    }
}

Expected this to work, there should no compile-time and run-time error.

Actual behavior

There is a compile-time error.

Error:(16, 33) java: constructor Key in class com.twitter.finagle.context.MarshalledContext.Key<A> cannot be applied to given types;
  required: com.twitter.finagle.context.MarshalledContext,java.lang.String
  found: java.lang.String
  reason: actual and formal argument lists differ in length

Steps to reproduce the behavior

Just run above codes.

huangyoje avatar Mar 18 '20 03:03 huangyoje

Hi @huangyoje, I tried running this code on both Java 8 and Java 11, both working. Can you double-check your code environment? Thanks!

yufangong avatar Apr 30 '20 01:04 yufangong

Hi, I uploaded the code to reproduce this issue, my code environment are as follows:

impl.zip

➜  tmp javac -version
javac 1.8.0_181
➜  tmp scalac -version
Scala compiler version 2.12.6 -- Copyright 2002-2018, LAMP/EPFL and Lightbend, Inc.
➜  tmp javac -J-Duser.language=en -cp util-core_2.12-20.1.0.jar:finagle-core_2.12-20.1.0.jar:. Impl.java
Impl.java:8: error: constructor Key in class MarshalledContext.Key<A> cannot be applied to given types;
         Contexts.broadcast().super("impl1");
                             ^
  required: MarshalledContext,String
  found: String
  reason: actual and formal argument lists differ in length
  where A is a type-variable:
    A extends Object declared in class MarshalledContext.Key
1 error

huangyoje avatar Apr 30 '20 04:04 huangyoje

@huangyoje got it. Unfortunately, we don't support building through javac with classpath, it's hard to tell what things are missing there. I'll suggest you build the system, using sbt or maven see if the problem remains.

yufangong avatar May 01 '20 16:05 yufangong

I found the same issue here. It seems scalac append implicit field on compiling and javac just does not have such a step - https://stackoverflow.com/questions/7117177/how-to-create-new-instance-of-scala-class-with-context-bound-via-java-reflection

It's possible to provide a friendly japi upstream?

tisonkun avatar Apr 03 '23 07:04 tisonkun