私がしようとしているものの通常の形はこれです。
MyClassFacadeLocal cls = new MyClassFacadeLocal();
List allMyClass = cls.findAll();
Iterator it = allMyClass.iterator();
while(it.haxNext()) {
MyClass Obj = (MyClass)it.next();
out.println(obj.getTitle());
}
さて、私が作成している問題は、このいくつかのケースを処理できるグローバル メソッドです。このために、エンティティ クラス名、メソッド名、およびメソッドによって返されるリストを渡し.findAll()
ます。反射を使用してこれを解決する方法。私が試したことは本当に大まかで、もちろんうまくいきませんでした。
List allMyClass; //I will have passed this before
Iterator it = allMyClass.iterator();
while(it.hasNext()) {
try {
Class<?> c = Class.forName(this.getEntityClassName());
c.cast(it.next());
Method method = c.getDeclaredMethod("getTitle");
String title = method.invoke(c, null).toString();
} catch(Exception e) {
}
}
与えます:"object is not an instance of declaring class"
エラー。しかし、これは使用上の欠陥であると確信しています。