[RESOLVIDO] Reflection: invoke retornando null

2 respostas
P

Pessoal, preciso obter o valor de uma List usando reflection, o código é este:

if (m.getReturnType().equals(List.class)) {
                        try {
                            l = (List) m.invoke(obj, (Object[]) null);
                        } catch (IllegalArgumentException ex) {
                            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (InvocationTargetException ex) {
                            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        list = true;
                    }

este if está num for…

problema: a lista está ‘l’, após o retorno do m.invoke, está null, mesmo eu tendo setado uma lista de string para o método em questão antes, para testes.

Tem alguma forma especifica pra se trabalhar com listas e reflection?

Obrigado.

2 Respostas

_fs
Isso aqui funciona, cara:
public class Scrap {
	
	public static void main(String[] args) throws Exception {
		System.out.println(Test.class.getMethod("getList").invoke(new Test()));
	}
	
	static class Test {
		public List<String> getList() {
			List<String> l = new ArrayList<String>();
			l.add("test");
			return l;
		}
	}
}

Talvez seu fluxo nao esteja correto.

P

Obrigado pela ajuda!

Olhei melhor o código e vi que tinha um pequeno erro em um if anterior ao loop.

Criado 30 de abril de 2010
Ultima resposta 3 de mai. de 2010
Respostas 2
Participantes 2