2013-04-10 5 views
2

int, short, byte 또는 long 유형의 개체가 있는데 새로운 값을 지정해야합니다. Java에서 가능합니까? 그렇다면 어떻게?Java Reflection ~ 기본 유형의 내부 개체 값 설정

public static void set(Object obj, int value) throws Exception 
{ 
    Class<?> c = obj.getClass(); 
    if (c.equals(Integer.class)) 
    { 
     // ??? 
    } 
} 
+0

* obj instance *에 값을 설정 하시겠습니까, 아니면 객체의 * 속성 *을 설정 하시겠습니까? – gaborsch

+0

@GaborSch obj는 정수이므로 값을 설정하고 싶습니다. – Bitterblue

+0

int 또는 Integer를 원하십니까? 래퍼 클래스를 수정할 수는 없지만 Integer를 새로운 값으로 설정할 수 있습니다. 내 답변의 맨 아래에있는 예를 확인하십시오. – TofuBeer

답변

2

정수는 변경할 수 없습니다. Integer 인스턴스에 값을 설정할 수 없습니다.

마찬가지로, 기본 유형의 다른 래퍼 클래스도 변경할 수 없습니다.

+0

글쎄, 고마워! 이것은 내 문제를 해결합니다. 나는 들판을 돌아 다니며 가야 할 것입니다. – Bitterblue

+0

반갑습니다. 예, 필드를 통해 설정하거나 래퍼 객체를 도입해야합니다. – gaborsch

1

예, 어떤 원시 유형을 처리하는지 알고있는 한.

Class clazz = Class.forName("TheClass"); 
Field f = clazz.getDeclaredField("ThePrimitiveField"); 
Object obj; 
f.setBoolean(obj, true); 

이렇게하면 obj의 "ThePrimitiveField"필드가 변경됩니다. 당신이 유형을 모르는 경우 ...

Field f; 
Object obj; 
try { 
    f.setBoolean(obj, true); 
} catch (IllegalArgumentException ex) { 
    try { 
     f.setByte(obj, 16); 
    } catch (IllegalArgumentException ex) { 
     try { 
      f.setChar(obj, 'a'); 
      // etc 
     } 
    } 
} 
1

당신은 종류가 이렇게 알고있는 경우 : 당신이 종류를 알 수없는 경우

public class Main 
{ 
    public static void main(String[] args) 
     throws NoSuchFieldException, 
       IllegalArgumentException, 
       IllegalAccessException 
    { 
     Foo   fooA; 
     Foo   fooB; 
     final Class<?> clazz; 
     final Field field; 

     fooA = new Foo(); 
     fooB = new Foo(); 
     clazz = fooA.getClass(); 
     field = clazz.getDeclaredField("bar"); 

     System.out.println(fooA.getBar()); 
     System.out.println(fooB.getBar()); 
     field.setAccessible(true); // have to do this since bar is private 
     field.set(fooA, 42); 
     System.out.println(fooA.getBar()); 
     System.out.println(fooB.getBar()); 
    } 
} 

class Foo 
{ 
    private int bar; 

    public int getBar() 
    { 
     return (bar); 
    } 
} 

당신이 이런 식으로 뭔가를 할 수 있습니다

public class Main 
{ 
    public static void main(String[] args) 
     throws NoSuchFieldException, 
       IllegalArgumentException, 
       IllegalAccessException 
    { 
     Foo   fooA; 
     Foo   fooB; 
     final Class<?> clazz; 
     final Class<?> type; 
     final Field field; 

     fooA = new Foo(); 
     fooB = new Foo(); 
     clazz = fooA.getClass(); 
     field = clazz.getDeclaredField("bar"); 

     System.out.println(fooA.getBar()); 
     System.out.println(fooB.getBar()); 
     field.setAccessible(true); // have to do this since bar is private   
     type = field.getType(); 

     if(type.equals(int.class)) 
     { 
      field.set(fooA, 42); 
     } 
     else if(type.equals(byte.class)) 
     { 
      field.set(fooA, (byte)1); 
     } 
     else if(type.equals(char.class)) 
     { 
      field.set(fooA, 'A'); 
     } 

     System.out.println(fooA.getBar()); 
     System.out.println(fooB.getBar()); 
    } 
} 

class Foo 
{ 
    private char bar; 

    public char getBar() 
    { 
     return (bar); 
    } 
} 

, 당신은 래퍼 클래스 (정수, 문자 등 ..) 사용하려면이 추가 할 수 있습니다

else if(type.equals(Integer.class)) 
{ 
    field.set(fooA, new Integer(43)); 
} 
+0

예 감사합니다! 이것이 내 계획 B였습니다. 계획 A의 코드를 변경하고 싶지 않았기 때문에 여기에서 물었습니다. ^^ – Bitterblue