0

リフレクションを使用して、メソッドに渡された変数を特定するにはどうすればよいですか?

public class ExampleClass {

  // class member variables
  ArrayList<String> strArrayOne;
  ArrayLIst<String> strArrayTwo;

  //constructor
  public ExampleClass()[
    strArrayOne = new ArrayList<String>();
    strArrayTwo = new ArrayList<String>();
  } 

   // create instance of nested class passing in the required ArrayList in constructor
   NestedInnerClass testInstance = new NestedInnerClass(strArrayOne);

  // nested inner class
  public class NestedInnerClass{

    // class member variable
    ArrayList<String> memberArray = new ArrayList<String>();

    // constructor
    public NestedInnerClass(ArrayList<String> inputArray){
      memberArray = inputArray;

      // put code here to determine with reflection which of the
      // two outer class variables is being passed in strArrayOne or strArrayTwo?
    }

  } // end nested inner class

} // end outer class
4

1 に答える 1

1

そのためにリフレクションは必要ありません。渡された配列とそれを囲むクラスのフィールドが等しいことを確認するだけで十分です。

if (Arrays.equals(inputArray, ExampleClass.this.strArrayOne)) {
  // first one has been passed
}

if (Arrays.equals(inputArray, ExampleClass.this.strArrayTwo)) {
  // second one has been passed
}
于 2013-07-11T13:08:04.573 に答える