2011-02-08 4 views
0

ハードセクションのジェネリックを使用して値を設定しようとしている間に、引数型の不一致に問題があります。リフレクションを使用したJavaのプリミティブ型の動的明示的キャスト

public static void Main(String... args) { 

    int intValue = 1; 
    long longValue = 1l; 
    Foo foo = new Foo(); 

    // Easy 
    foo.setIntValue(intValue); 
    foo.setLongValue(longValue); 

    invokeSet(foo, "setIntValue",intValue); 
    invokeSet(foo, "setLongValue",longValue); 

    //Medium 
    foo.setLongValue(intValue); 
    invokeSet(foo, "setLongValue",intValue); 

    //Hard 
    foo.setIntValue((int)longValue); //How to implement this in generic way ? 
    invokeSet(foo, "setIntValue",longValue); 
} 

class Foo { 

int intValue = 0 
long llongValue = 0; 


    setIntValue(int i) { 
    this.intValue = i; 
    } 

    setLongValue(long l) { 
    this.longValue = l; 
    } 

}

事は、私は明示的なキャストを予想していたということでしょうか?

EDIT

が行われ、反射型のクラスなどを使用して動的な方法でそれを実行する可能性縮小プリミティブ変換を予想するすべての可能性はありますか? FYI

我々は、彼らがもはや原始的プリミティブ型に反射で作業しています。これを達成する

private static void invokeSet(Object bean, String methodName, Object value) throws Exception { 
    Method m = retriveMethod(bean, methodName); 
    m.invoke(bean,value); //More or less there is a type wrapper to change primitive to object class 
} 

EDIT2

一つの方法は、文字列に値を変更して、特定の種類の数の文字列コンストラクタを使用することである値を持つ文字列を渡します。 intlongキャスティング

int intValue = 0; 
long longValue = 0l; 

Integer intObject = i; 
Long longObject = l; 

    intValue = (int)longValue; 
    intOBject = new Integer(String.valueOf(longObject)); // intObject = (Integer) longValue; this is not allowed 

    intObject = longObject.intValue(); //Target to achieve with out writing bad code. 
+0

「一般的な方法で」とはどういう意味ですか?明示的なキャストがなければ、 'long 'を' int'に強制しようとしているので、コンパイラは正しく文句を言います。あなたは何をしたいのですか? –

+0

"invokeSet"とは何ですか?リフレクションを使用してBeanに値を設定する関数ですか?あなたはあなたの質問にもっと具体的になりますか? –

+0

@Oli反射を使用しています。私のエラー。 @Luciano、はい、反射を使ってBeanに値を設定する関数です。 –

答えて

3
/** 
    * Function that solve the problem with Numbers and narrowing primitive conversion. 
    * @param outputType - The type of output 
    * @param value - Number object to be narrowed. 
    */ 
    private static Number NarrovingNumberConversion(Class<? extends Number> outputType, Number value) { 

     if(value == null) { 
      return null; 
     } 
     if(Byte.class.equals(outputType)) { 
      return value.byteValue(); 
     } 
     if(Short.class.equals(outputType)) { 
      return value.shortValue(); 
     } 
     if(Integer.class.equals(outputType)) { 
      return value.intValue(); 
     } 
     if(Long.class.equals(outputType)) { 
      return value.longValue(); 
     } 
     if(Float.class.equals(outputType)) { 
      return value.floatValue(); 
     } 
     if(Double.class.equals(outputType)) { 
      return value.doubleValue(); 
     } 

     throw new TypeMismatchException(); 

    } 
4

は、したがって、それは暗黙的に行われることはありません精度の損失をもたらす可能性がnarrowing primitive conversionです。 (。定数式の例外を除いて、これは、このような状況には関係ありません)

+0

それは本当ですが、そのような状況がコード内で発生した場合は例外で終了するべきではありませんが、ナローイングプリミティブ変換を使用して番号を変換して処理してください。 –

+1

@Vashそれは実際にアプリケーションに依存します。確かに、Javaはオーバーフローまたはアンダーフローの例外をスローしませんが、アプリケーションで実行することをお勧めします。たとえば、私はオーバーフローに関する例外を投げない金融アプリケーションを信頼しません。 – biziclop

+0

私はコメントを編集したいと思っていましたが、書き留めるのではなく、遅くて一般的に私はこのコードをこのコードで書くべきでした。 –

1

あなたが発信者からのクラスにキャストを渡したい場合は、setIntValue()をオーバーロード:

setIntValue(long l) { 
    this.intValue = (int) l; 
} 

しかし、あなた以降」あなたの発信者からの絞り込みを隠して、すべての場合にこれが正しいことであることを確認してください。

+0

@ Yishai - 編集に感謝します。まだコーヒーが足りません:-) –

+1

私はこのコードをどの方法で使うのかわからないので、これをやりません。しかし、あなたの視点に感謝します。 –

関連する問題