IntelliJ resolving incorrect base class contructor for implicit super() call ?

已回答

I'm new to IntelliJ but I recently switched from Eclipse and have noticed an issue with implicit super() calls.

I have an abstract base class that has several constructors, some which throw exceptions and one which doesn't:

public Base() { ... }
public Base(Arg one) throws SomeException { ... }
public Base(Arg one, Arg two) throws SomeException { ... }

One of the derived classes which declares some new fields has a constructor which doesn't use an explicit super() call:

public Derived(DerivedArg one, DerivedArg two) {
// No explicit super() call
this.one = one;
this.two = two;
}

IntelliJ seems to mark this constructor as an error: "Unhandled Exception: SomeException" even though Base() { ... } does not throw SomeException. Eclipse has never marked this as an error before and it still compiles without issue, but nevertheless it's highlighted as an error in the class and it goes away as soon as an explicit call to 'super()' is added.

Is it possible this is something to do with IntelliJ's 'inspection' feature, or maybe a bug?

0

Could you please attach a sample project to illustrate the problem? I can't reproduce it from the description above.

0
Avatar
Permanently deleted user

Hi Serge,

I'm not able to attach the original project unfortunately, but in trying to reproduce the problem in a test project I've discovered the real issue and provided a sample for you below.

The problem seems to be when the base class has a constructor that accepts a single varargs parameter (which throws the exception), and that constructor appears above the no args constructor (which doesn't throw the exception) in the class file. Reordering these gets rid of the error, so IntelliJ seems to match the implicit super() call to the constructor that appears first in the file.

That actually seems somewhat valid, so I'm not even sure if this is an issue even though Eclipse never highlighted it. It's an ambiguous constructor call and probably just comes down to bad code design really, but I'd appreciate your thoughts. I'm finding it difficult to determine from the Java spec how this case should be handled?

public class Base {

    String[] items;

    public Base(String ... items) throws Exception {
        this.items = items;
    }

    public Base() {
        this.items = null;
    }
}
public class Derived extends Base {

    public Derived() {
// Unhandled Exception error unless super() is explicitly called
    }
}

 

0

请先登录再写评论。