Skip to content

Static method of object disappears after compilation #7225

@scabug

Description

@scabug

Using this very simple piece of code

    import java.util.Properties

    class MyProperties extends Properties

    object MyProperties {
    
        def get(): MyProperties = new MyProperties
    
        def anotherMethod(): MyProperties = new MyProperties
    
    }

the get() method is missing in the compiled code; a Java decompilation of the MyProperties class yields (scala signature omitted)

    import java.util.Properties;
    import scala.reflect.ScalaSignature;

    public class MyProperties extends Properties
    {
      public static MyProperties anotherMethod()
      {
        return MyProperties..MODULE$.anotherMethod();
      }
    }

If MyProperties does not extend java.util.Properties however, the get() method is generated.

java.util.Properties inherits the public V get(Object key) from java.util.Dictionary but that is a non static method with a different signature and this "works" in Java

    import java.util.Properties;

    public class MyPropertiesjava extends Properties {
  
        private static final long serialVersionUID = 1L;
	
        public static MyProperties get() {
    	
    	    return new MyProperties();
        }
    
        public static MyProperties antotherMethod() {
    	
    	    return new MyProperties();
        }
    }

A suggested workaround using type (author Régis Jean-Gilles) is stated below, but there does not seem to be a JVM related reason no to generate the get method

    import java.util.Properties

    type MyProperties = MyPropertiesImpl

    class MyPropertiesImpl extends Properties

    object MyProperties {    
        def get(): MyProperties = new MyPropertiesImpl
        def anotherMethod(): MyProperties = new MyPropertiesImpl
    }

(sorry for the lack of rendered formatting)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions