64

I have a public abstract class and I'm trying to use the getClass() method, as I will need info from the class extending my abstract class. An example is this:

public String getName() {
    return getClass().getSimpleName();
}

However, IntelliJ reports this:

Ambiguous method call. Both
getClass    ()    in Object and
getClass    ()    in Object match.

The code runs fine, but having tens of error warnings in my IDE is kinda in my way. It disrupts my work flow with a lot of false positives.

Why are these errors shown, and what can I do to not see them?

JJD
  • 50,076
  • 60
  • 203
  • 339
Matsemann
  • 21,083
  • 19
  • 56
  • 89
  • 12
    looks like a bug in IntelliJ – MRalwasser Apr 30 '12 at 15:31
  • 2
    Call me old-fashioned, but I don't see why you'd need to know the concrete class. Virtual methods should do the job. – Igor F. Apr 30 '12 at 15:34
  • @IgorF. Ideally, yes. As I said, my example could easily be changed. But not all of my methods have easy counterparts. That's not the issue, anyway. – Matsemann Apr 30 '12 at 15:48
  • @MRalwasser after your comment, I googled around. It is actually a bug in IntelliJ. http://youtrack.jetbrains.com/issue/IDEA-71363 and http://youtrack.jetbrains.com/issue/IDEA-79680 and I also found some more. – Matsemann Apr 30 '12 at 15:52
  • [Here](https://code.google.com/p/android/issues/detail?can=2&start=0&num=100&q=getClass%20ambiguous%20method%20call&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars&groupby=&sort=&id=70135) is the *Android Studio* bug report. – Phil Jul 16 '14 at 15:00

5 Answers5

93

Casting my getClass() call to Object like this

((Object) this).getClass()

solves the problem (with non abstract classes) for me. It's not great, but it's working.

Also, manipulating your Android SDKs from the project settings and removing all JDK jars from your Android SDK resolves the error. Of course you'll have to reference it within your project to utilize the fix.

Ben Weiss
  • 17,182
  • 6
  • 67
  • 87
43

The code is fine, but it is an error in IntelliJ.

Error report, another one.

There are even some more error reports with different variations of this issue. As duffymo pointed out in comments, it can also be because there are different versions of the JDK in the classpath.

Matsemann
  • 21,083
  • 19
  • 56
  • 89
  • 1
    Nope, the site says it's caused by having several JDKs in your CLASSPATH. Cleaning that up fixes the problem. Poor housekeeping on your part. – duffymo Apr 09 '13 at 23:35
  • @duffymo If you read the link that fixed the issue for *some people*, still others having the issue in other cases. And for most people it only happened after an update, suggesting that it could work properly before. – Matsemann Apr 10 '13 at 08:13
  • @Budda See keyboardsurfer's answer. As duffymo pointed out, some have had the problem because of different JDK versions in classpath. – Matsemann Apr 10 '13 at 09:31
  • 6
    It's worth noting this is just an introspection error, compiling and running the project still works fine. – Chris.Jenkins Sep 12 '13 at 10:41
5

Another working workaround is to generate a helper method to get the Class:

public Class<?> type() {
    return super.getClass();
}

or a static reusable util:

public static final Class<?> type(Object object) {
    return object.getClass();
}
pablisco
  • 14,027
  • 4
  • 48
  • 70
3

I noticed that it depends on the Android version you are referencing whether the error occurs or not. I used to define a dependency for Android 4.1.1.4 in the pom.xml linking to Maven Central.

<dependency>
    <groupId>com.google.android</groupId>
    <artifactId>android</artifactId>
    <version>4.1.1.4</version>
    <scope>provided</scope>
</dependency>

Meanwhile, I provide the needed dependencies using maven-android-sdk-deployer.

<dependency>
    <groupId>android</groupId>
    <artifactId>android</artifactId>
    <version>4.3_r1</version>
    <scope>provided</scope>
</dependency>

Then getClass() produces the error.

JJD
  • 50,076
  • 60
  • 203
  • 339
1

This can happen if you have an Android project with Maven, and you import into IntelliJ using the Maven Android Platform as the project SDK. The problem is that both Maven Android Platform and the Android maven dependency jar includes the java.lang.Object class.

The workaround is goint to Project structure -> Platform Settings -> SDK -> Maven Android Platform -> Classpath. It will list all the jars which are in JDK actually. Remove all of them, so only the two Android dependency remains (res and annotations.jar).

EDIT: This problem has been already reported to IntelliJ issue tracker long time ago.

WonderCsabo
  • 11,947
  • 13
  • 63
  • 105