倭マン's BLOG

くだらない日々の日記書いてます。 たまにプログラミング関連の記事書いてます。 書いてます。

果たしてそれが(どれが)プロパティなのか? (5) :インデックスを介してプロパティ値にアクセスする場合

今回はインデックスを介してプロパティ値にアクセスするメソッドが宣言されている場合です。 まぁ、java.util.List みたいなものを想像していただければよいかと。

class Hoge{
    Object getFoo(int i){...}
    void setFoo(int i, Object value){...}
}

みたいなものです。

対象とするオブジェクトの定義


プロパティを取得するオブジェクトの定義。

Java オブジェクト

分類は大雑把に言って

  • 4つの各アクセス修飾子
  • アクセッサが getXxxx() か isXxxx() か
  • アクセッサ (getter) メソッドのみ (Read Only)
  • ミューテータ (setter) メソッドのみ (Write Only)

でしています:

public class IndexJavaVean {

    private Object getPrivate(int i){return null;}
    private void setPrivate(int i, Object value){}

    Object getPackage(int i){return null;}
    void setPackage(int i, Object value){}

    protected Object getProtected(int i){return null;}
    protected void setProtected(int i, Object value){}

    public Object getPublic(int i){return null;}
    public void setPublic(int i, Object value){}

    //********** Boolean **********
    private boolean isPrivateBoolean(int i){return false;}
    private void setPrivateBoolean(int i, boolean value){}

    boolean isPackageBoolean(int i){return false;}
    void setPackageBoolean(int i, boolean value){}

    protected boolean isProtectedBoolean(int i){return false;}
    protected void setProtectedBoolean(int i, boolean value){}

    public boolean isPublicBoolean(int i){return false;}
    public void setPublicBoolean(int i, boolean value){}

    //********** Read Only **********
    private Object getPrivateReadOnly(int i){return null;}
    Object getPackageReadOnly(int i){return null;}
    protected Object getProtectedReadOnly(int i){return null;}
    public Object getPublicReadOnly(int i){return null;}

    private boolean isPrivateReadOnlyBoolean(int i){return false;}
    boolean isPackageReadOnlyBoolean(int i){return false;}
    protected boolean isProtectedReadOnlyBoolean(int i){return false;}
    public boolean isPublicReadOnlyBoolean(int i){return false;}

    //********** Write Only **********
    private void setPrivateWriteOnly(int i, Object value){}
    void setPackageWriteOnly(int i, Object value){}
    protected void setProtectedWriteOnly(int i, Object value){}
    public void setPublicWriteOnly(int i, Object value){}
}

Groovy オブジェクト

Java の場合と大体同じ:

class IndexGroovyVean {

    private Object getPrivate(index){null}
    private void setPrivate(index, value){}

    protected Object getProtected(index){null}
    protected void setProtected(index, value){}

    Object getPublic(index){null}
    void setPublic(index, value){}

    //********** Boolean **********
    private boolean isPrivateBoolean(index){null}
    private void setPrivateBoolean(index, value){}

    protected boolean isProtectedBoolean(index){null}
    protected void setProtectedBoolean(index, value){}

    boolean isPublicBoolean(index){null}
    void setPublicBoolean(index, value){}

    //********** Read Only **********
    private Object getPrivateReadOnly(index){null}
    protected Object getProtectedReadOnly(index){null}
    Object getPublicReadOnly(index){null}

    private boolean isPrivateReadOnlyBoolean(index){null}
    protected boolean isProtectedReadOnlyBoolean(index){null}
    boolean isPublicReadOnlyBoolean(index){null}

    //********** Write Only **********
    private void setPrivateWriteOnly(index, value){}
    protected void setProtectedWriteOnly(index, value){}
    void setPublicWriteOnly(index, value){}
}

プロパティ


では、「プロパティかどうか」の結果です。

  • 」はプロパティとして認識される
  • ×」はプロパティとして認識されない

です。

Java

()内は boolean 型の場合です。 この関連の記事では常にそうします。 Object ( boolean )

スコープ Read/Write GroovyBeans Inspector JavaBeans MetaClass
private Read/Write
Read Only
Write Only
× ( × )
× ( × )
×
× ( × )
× ( × )
×
× ( × )
× ( × )
×
× ( × )
× ( × )
×
package Read/Write
Read Only
Write Only
× ( × )
× ( × )
×
× ( × )
× ( × )
×
× ( × )
× ( × )
×
× ( × )
× ( × )
×
protected Read/Write
Read Only
Write Only
× ( × )
× ( × )
×
× ( × )
× ( × )
×
× ( × )
× ( × )
×
× ( × )
× ( × )
×
public Read/Write
Read Only
Write Only
× ( × )
× ( × )
×
× ( × )
× ( × )
×
☆ ( ☆ )
☆ ( × )
× ( × )
× ( × )
×

Groovy

スコープ Read/Write GroovyBeans Inspector JavaBeans MetaClass
private Read/Write
Read Only
Write Only
× ( × )
× ( × )
×
× ( × )
× ( × )
×
× ( × )
× ( × )
×
× ( × )
× ( × )
×
protected Read/Write
Read Only
Write Only
× ( × )
× ( × )
×
× ( × )
× ( × )
×
× ( × )
× ( × )
×
× ( × )
× ( × )
×
public Read/Write
Read Only
Write Only
× ( × )
× ( × )
×
× ( × )
× ( × )
×
× ( × )
× ( × )
×
× ( × )
× ( × )
×

まとめ

ほとんど無理です。 唯一、Java オブジェクトを JavaBean として扱うときは OK。
Groovyイン・アクション

Groovyイン・アクション