简体   繁体   中英

Javap Exception in thread “main” java.lang.NoSuchMethodError

I have old class which throws this exception.

com.SomeClass.createEmail(Ljava/lang/String;)Lorg/apache/commons/mail/Email;

After decompiling the old class using Cavaj I have identical source code as in current file. So I disassembled the classes and the only difference is in the return type.

Old:

127  invokevirtual com.SomeClass.createEmail(java.lang.String) : org.apache.commons.mail.Email [68]

New:

126  invokestatic com.SomeCalss.createEmail(java.lang.String) : com.NewEmail [68]

NewEmail is extending Email. So I guess this return type is the problem even though new return type extends old class. So on JVM machine code level extending is not working :)?

Each Java method has a signature, which also contains the return type.

Thus

com.SomeClass1.createEmail(Ljava/lang/String;)Lorg/apache/commons/mail/Email;

and

com.SomeClass2.createEmail(Ljava/lang/String;)Lcom/NewEmail;

are different.

It's possible to use inheritance, but this does not change the signature of the invoked method. So if

  • someClass2 is derived from someClass1 and
  • com.NewEmail is derived from org.apache.commons.mail.Email
  • and the variable use to invoke createEmail is an instance of someClass2

then the new method is called.

Example:

public class Mail {
}
public class NewMail extends Mail{
}
public class C1 {
    public Mail send() {
        return new Mail();
    }
}
public class C2 extends C1 {
    @Override
    public NewMail send() {
        return new NewMail();
    }
}
public class Main {
    public static void main(String[] args) {
        C1 c = new C2();
        Mail m = c.send();
        System.out.println(m);
    }
}

will print something like

NewMail@64726693

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM