凪瀬 Blog
Programming SHOT BAR

目次

Blog 利用状況
  • 投稿数 - 260
  • 記事 - 0
  • コメント - 46945
  • トラックバック - 192
ニュース
広告
  • Java開発者募集中
  • 経歴不問
  • 腕に自信のある方
  • 富山市内
  • (株)凪瀬アーキテクツ
アクセサリ
  • あわせて読みたい
凪瀬悠輝(なぎせ ゆうき)
  • Java技術者
  • お茶好き。カクテル好き。
  • 所属は(株)凪瀬アーキテクツ
  • Twitter:@nagise

書庫

日記カテゴリ

 

ふと訪れたblogで 共変戻り値(covariant return types)を話題にしていたのですが、 「コレ、JavaSE5.0からだったっけ?1.4だったような」、と思って調べるとやっぱり5.0でした orz

調べるとかつのりさんところ( 共変戻り値)で書いているなぁ…。 自分の中でなぜか1.4だと思い込んでいる様子。この記憶はどこからやってきたのか…。

このあたりを見て、そういえばジェネリクスとリフレクションの話題があったなぁと思いだしたのでひとつ小噺を。

ジェネリクスの具象化とメソッドシグニチャ

さて、気を取り直して、この共変なオーバーライドですが、ジェネリクスの実装にはなくてはならないものです。

public interface Sample<T> {
    T hoge();
    void piyo(T t);
}

といったジェネリクス型パラメータを持つinterfaceがあったとして

public class SampleImpl implements Sample<String>{
    public String hoge() {
        return null;
    }
    public void piyo(String t) {
    }
}

というようにimplements時にStringとすると、hoge()の戻り型はStringクラスになるわけです。 これは共変な戻り値になっているわけですね。

ジェネリクスのメソッドをリフレクションしたら

さて、先ほどのコード、piyo()がオーバーロードになっているのがわかりますか?

インターフェースの段階ではジェネリクス型パラメータのTが引数とされていました。 これは実際的にはObject型となってしまうのですが、そうするとSampleImplのpiyo(String)は オーバーライドではなくオーバーロードになってしまいます。

SampleImpl impl = new SampleImpl();
Method m = impl.getClass().getMethod("piyo", Object.class);
m.invoke(impl, "param");

ところが、上記のようにリフレクションでpiyo(Object)を呼び出すと、ちゃんとpiyo(String)が実行されます。 これはどうしたことか!?

作られるclassの中をのぞいてみると面白いことがわかります。

// Method descriptor #19 (Ljava/lang/String;)V
// Stack: 0, Locals: 2
public void piyo(java.lang.String t);
// Method descriptor #22 (Ljava/lang/Object;)V
// Stack: 2, Locals: 2
public bridge synthetic void piyo(java.lang.Object arg0);

引数がStringのものと、Objectのものとふたつ作られているんですね。 "bridge synthetic"という修飾がついています。 この元となったinterfaceのメソッドシグネチャ(メソッドを特定するためのメソッド名と引数型の組み合わせのこと)と同等のシグネチャで宣言されるメソッドが ブリッジとなってpiyo(String)を呼び出しているのです。

このため、ジェネリクスをimplementsする際に具現化していても、interfaceや抽象クラスで宣言された メソッドシグニチャを使ってMethodを取得してinvokeすることができるのです。

投稿日時 : 2008年6月16日 15:07
コメント
  • # Hi there just wanted to give you a quick heads up. The words in your content seem to be running off the screen in Chrome. I'm not sure if this is a format issue or something to do with browser compatibility but I figured I'd post to let you know. The lay
    Hi there just wanted to give you a quick heads up.
    Posted @ 2019/04/05 21:44
    Hi there just wanted to give you a quick heads up.
    The words in your content seem to be running off the screen in Chrome.
    I'm not sure if this is a format issue or something to do with
    browser compatibility but I figured I'd post to let you know.
    The layout look great though! Hope you get the problem solved
    soon. Kudos
  • # Hi all, here every person is sharing such know-how, therefore it's good to read this webpage, and I used to go to see this blog everyday.
    Hi all, here every person is sharing such know-how
    Posted @ 2019/04/06 11:55
    Hi all, here every person is sharing such know-how, therefore it's good to
    read this webpage, and I used to go to see this blog everyday.
  • # Spot on with this write-up, I absolutely believe this site needs a lot more attention. I'll probably be returning to see more, thanks for the information!
    Spot on with this write-up, I absolutely believe t
    Posted @ 2019/06/07 20:01
    Spot on with this write-up, I absolutely believe
    this site needs a lot more attention. I'll probably
    be returning to see more, thanks for the information!
  • # Magnificent goods from you, man. I've understand your stuff previous to and you are just extremely fantastic. I actually like what you have acquired here, certainly like what you are saying and the way in which you say it. You make it entertaining and yo
    Magnificent goods from you, man. I've understand y
    Posted @ 2019/09/06 21:11
    Magnificent goods from you, man. I've understand your stuff previous to and you are just
    extremely fantastic. I actually like what you have acquired
    here, certainly like what you are saying and the way in which
    you say it. You make it entertaining and you still care for to keep it sensible.
    I can't wait to read much more from you. This is really a tremendous site.
  • # Magnificent goods from you, man. I've understand your stuff previous to and you are just extremely fantastic. I actually like what you have acquired here, certainly like what you are saying and the way in which you say it. You make it entertaining and yo
    Magnificent goods from you, man. I've understand y
    Posted @ 2019/09/06 21:12
    Magnificent goods from you, man. I've understand your stuff previous to and you are just
    extremely fantastic. I actually like what you have acquired
    here, certainly like what you are saying and the way in which
    you say it. You make it entertaining and you still care for to keep it sensible.
    I can't wait to read much more from you. This is really a tremendous site.
  • # Magnificent goods from you, man. I've understand your stuff previous to and you are just extremely fantastic. I actually like what you have acquired here, certainly like what you are saying and the way in which you say it. You make it entertaining and yo
    Magnificent goods from you, man. I've understand y
    Posted @ 2019/09/06 21:13
    Magnificent goods from you, man. I've understand your stuff previous to and you are just
    extremely fantastic. I actually like what you have acquired
    here, certainly like what you are saying and the way in which
    you say it. You make it entertaining and you still care for to keep it sensible.
    I can't wait to read much more from you. This is really a tremendous site.
  • # Magnificent goods from you, man. I've understand your stuff previous to and you are just extremely fantastic. I actually like what you have acquired here, certainly like what you are saying and the way in which you say it. You make it entertaining and yo
    Magnificent goods from you, man. I've understand y
    Posted @ 2019/09/06 21:14
    Magnificent goods from you, man. I've understand your stuff previous to and you are just
    extremely fantastic. I actually like what you have acquired
    here, certainly like what you are saying and the way in which
    you say it. You make it entertaining and you still care for to keep it sensible.
    I can't wait to read much more from you. This is really a tremendous site.
  • # My programmer is trying to persuade me to move to .net from PHP. I have always disliked the idea because of the costs. But he's tryiong none the less. I've been using Movable-type on a number of websites for about a year and am anxious about switching
    My programmer is trying to persuade me to move to
    Posted @ 2019/09/17 3:06
    My programmer is trying to persuade me to move to
    .net from PHP. I have always disliked the idea because of the costs.
    But he's tryiong none the less. I've been using Movable-type on a number of websites for about a year and am anxious about switching to another platform.
    I have heard fantastic things about blogengine.net. Is there a way I can import all my wordpress
    content into it? Any help would be really appreciated!
タイトル
名前
Url
コメント