Skip to content

java.lang.ClassFormatError: Method foo in class CLibrary has illegal modifiers: 0x101 #9432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
michelou opened this issue Jul 25, 2020 · 4 comments
Labels
area:annotations area:refchecks good first issue Perfect for someone who wants to get started contributing help wanted itype:bug

Comments

@michelou
Copy link
Contributor

Minimized code

trait CLibrary {
  @native
  def foo(x: Int): Unit
}

object I9432 {
  def main(args: Array[String]): Unit = {
    new CLibrary {
      override def foo(x: Int): Unit = println(x)
    }.foo(1)
  }
}

Output

$ java -version
openjdk version "11.0.8" 2020-07-14

$ C:\opt\dotty-0.26.0-RC1\bin\dotc.bat -deprecation -feature -d target\classes src\main\scala\I9432.scala

$ C:\opt\dotty-0.26.0-RC1\bin\dotr.bat -classpath target\classes I9432
Exception in thread "main" java.lang.ClassFormatError: Method foo in class CLibrary has illegal modifiers: 0x101
        at java.base/java.lang.ClassLoader.defineClass1(Native Method)
        at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1017)
        at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
        at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:800)
        at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:698)
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:621)
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:579)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
        at I9432.main(I9432.scala)

$ javap -cp target\classes CLibrary
Compiled from "I9432.scala"
public interface CLibrary {
  public static void foo$(CLibrary, int);
  public native default void foo(int);
}

Expectation

Modifiers native and default (since Java 8) are mutual exclusive (see above javap output).

Source file src\main\scala\I9432.scala should not compile.

@smarter
Copy link
Member

smarter commented Jul 25, 2020

FYI, in scalac this fails with:

A trait cannot define a native method.

So we're just missing a check that leads to an error (which could be done in RefChecks, like it is in scala 2)

@smarter smarter added area:refchecks good first issue Perfect for someone who wants to get started contributing help wanted labels Jul 25, 2020
@michelou
Copy link
Contributor Author

Quick code revue gives:

@smarter In method checkWellFormed(sym: Symbol) most fail(..) invocations take a Message as argument; what is the rule for adding new error messages like this one (file messages.scala is a minefield..) ?!

NB. The above minimized code does not provide the whole picture of my original code example which makes use of the JNA library. In my case the Java version works fine and I still have issues with the native modifier and varargs when using Dotty.

@smarter
Copy link
Member

smarter commented Jul 29, 2020

We try to make new message case class for new error messages, but you can instead just pass a String (there's an implicit conversion from String to Message).

tudorv91 added a commit to tudorv91/dotty that referenced this issue Sep 14, 2020
@tudorv91
Copy link
Contributor

Hi @smarter @michelou . I have created a PR for this issue here: #9799 . Can you have a look?

smarter added a commit that referenced this issue Sep 15, 2020
Fix #9432: Fail compilation on @Native methods in traits
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:annotations area:refchecks good first issue Perfect for someone who wants to get started contributing help wanted itype:bug
Projects
None yet
Development

No branches or pull requests

3 participants