2011-09-19 7 views
0

방금 ​​Method Reflection API를 사용하여 하나의 일반적인 방법을 만들었습니다. 이 메서드에서는 미립자 메서드 (getter 메서드/setter 메서드) 값을 얻으려고하지만이 작업을 수행하는 방법을 모른다. 저는 Abel이 Method Reflection API를 사용하여 모든 메소드 이름을 가져 왔지만 Abel은이 메소드의 가치를 얻지 못했습니다. 그러니 제발 도와주세요.리플렉션을 사용하여 POJO에서 가치를 얻는 방법?

여기

내 코드는 ......

/*
로 propertyNames 목록은 두 개의 레코드를 포함합니다. */

public <T>void fillList(List<String> propertyNames , T clas){ 

    try{ 
     for(Object entity : entities.keySet()){ 
      if(propertyNames != null && propertyNames.size() > 0){ 
       Method[] methodList = clas.getClass().getMethods(); 
       Method methodName; 
       for (String propertyName : propertyNames) { 
        for(Method method : methodList){ 
         if(method.getName().trim().startsWith("set")){ 
          if(propertyName.trim().equalsIgnoreCase(method.getName().substring(3, method.getName().trim().length()))){ 

           methodName = clas.getClass().getMethod(method.getName().trim(),new Class[]{Integer.class}); 

           System.out.println("HERE I GET METHOD NAME ::: " + methodName.getName()); 

           /* 
           * here one by one i am getting all the Setter methods name from the class . 
           * but i want a that Setter methods values. Not return type. 
           */ 
          } 
         } 
        } 
       } 
      } 
     } 

    }catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
+0

코드를 게시 할 수 있습니까? 귀하의 질문은 거의 이해가되지 않습니다. –

+0

내 코드를 추가하십시오. –

답변

0

당신이 "방법의 값"무엇을 의미합니까

and that records are two different EntityName(Variable Name) 

    ex. in my class i have declared two Entity(Variable) 
    private Integer productId; 
    private Integer bomBucket; 

    so propertyNames List is contain [productId , bomBucket] this two records. 

수행하면 메소드의 반환 유형을 의미, 또는 그것의 당신이 특정 인스턴스에서, 그 메소드를 호출 싶어 부모 클래스, 어쩌면 일부 argumetns, 그리고 반환 값을 얻을?

//get instance of the class that has your method 
    DsrProcessor dsrProcessor = DsrProcessor.class.newInstance(); 
    //get method object (by passing in it's name and the list of argument types he's accepting - echo(String): 
    Method m = DsrProcessor.class.getMethod("echo", String.class); 
    //use reflection to invoke the method passing the instance of it's class and a list of arguments : 
    String methodReturnValue = (String) m.invoke(dsrProcessor, "Hello"); 
    //do something with what the method returned 
    System.out.println(methodReturnValue);//prints: "echo:hello" 
0

당신은 또한 java.beans.Introspector 클래스를 사용하고 다음 중 하나를 할 수 있습니다 당신이 방법이 있다면이 경우에, 당신이 그것을 호출하기 전에 먼저 방법 객체 (참) setAccessible를 호출 할 필요가 공개되지 않습니다 그 getBeanInfo 메소드 모든 속성 설명자 java.beans.PropertyDescriptor을 참조하는 BeanInfo의 인스턴스를 반환합니다.

+0

당신의 대답보다하지만 나는 또한 PropertyDescriptor를 사용했지만 여전히 어떤 해결책도 얻지 못했습니다. –

관련 문제