question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invalid static method resolution

See original GitHub issue

I have a class hierarchy, where class Child extends class Parent, and they both define a method “newInstance”:

class Parent {
  static
  public Parent newInstance(){
    return new Parent();
  }
}

class Child extends Parent {
  static
  public Child newInstance(){
    return new Child();
  }
}

I’m trying to obtain a new Child instance by invoking the static method Child#newInstance(). However, I keep getting Parent instances (instead of Child instances), which indicates that PyJNIus has “redirected” my static method invocation to Parent#newInstance(). This is obviously incorrect behaviour.

Using PyJNIus version 1.2.0.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
tshirtmancommented, Feb 17, 2019

From what i gather (didn’t look at this code in some time), we use Class.getMethods() to populate our list of methods, https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getMethods() and if we have multiple signatures for the same name, we use JavaMultipleMethod, and use compute_score at runtime to determine which method to call depending on various parameters, but… https://github.com/kivy/pyjnius/blob/5f6aa738b2b7ae496ec43bfadbf9933ee640bb7b/jnius/jnius_utils.pxi#L340-L352

i think this should try to give less points the further in the class hierarchy of classes the method come from, because the previously mentioned getMethods method makes no promise on the order of the returned list, we might currently return any implementation, not necessarily the one the programmer expect.

0reactions
tshirtmancommented, Feb 18, 2019

Thank you for confirmation, i’ll try adding the test case later, so we can avoid regressions 😃, then merge.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Wrong static method resolution in Visual C++ - Stack Overflow
The compiler is clearly taking the wrong method as I have a dedicated variant for long long . What is the reason for...
Read more >
Java Error: Non-static Variable/Method X Cannot be Referenced
Attempting to access a non-static variable/method from a static context without a class instance creates ambiguity.
Read more >
Invalid static method call detected PHP-E1003 - DeepSource
The method you are trying to call is a private static method from a parent class. Bad practice. class Output { public static...
Read more >
Can You Override Static Method in Java? Method ... - Java67
This means static methods are resolved even before objects are created, that's why it's not possible to override static methods in Java.
Read more >
Static Keyword - Manual - PHP
Because static methods are callable without an instance of the object created, the pseudo-variable $this is not available inside methods declared as static....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found