This discussion is archived
2 Replies Latest reply: Oct 19, 2011 10:54 PM by 882653 RSS

Accessing fields of class in another package (Reflection)

882653 Newbie
Currently Being Moderated
Hi,

Since I didn't find a section named "Reflection" in the forums, I guessed this would be the most appropriate place to post my question.

I am trying to implement a function called setField(field, value) in my classes using Reflection. I included this function in a base class, so that its child classes could also use this function.

The base class looks like:
public class Base {

    private Integer x;

    public void setField( String fieldName, Object value) {
        Class cls = this.getClass();
        
        while (cls.getSuperclass() != null) {
            System.out.println(cls.getName());
            
            try {
                Field field = cls.getDeclaredField(fieldName);
                field.set(this, value);
                break;
            } catch (IllegalArgumentException ex) {
                Logger.getLogger(JPT.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(JPT.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NoSuchFieldException ex) {
                Logger.getLogger(JPT.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SecurityException ex) {
                Logger.getLogger(JPT.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            // if field not found in this class, check the superclass
            cls = cls.getSuperclass();
        }
    }
    
    public Integer getX() {
        return x;
    }

    public void setX(Integer x) {
        this.x = x;
    }
}
And then I have a child class:
public class Child extends Base {

    protected Integer y;

    public Integer getY() {
        return y;
    }

    public void setY(Integer y) {
        this.y = y;
    }
}
So, basically I need to get the following type of functionality to work, i.e. modify a field in the child class using the function available from the base class:
Child child = new Child();

child.setField("y", new Integer(20));

System.out.println(child.getY());
This works when both the base and the child classes are in the same package, but when they are on different packages, I get an error that looks like:
java.lang.IllegalAccessException: Class teletutor.core.test1.Base can not access a member of class teletutor.core.test2.Child with modifiers "protected"
     at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:95)
     at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:261)
     at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:253)
     at java.lang.reflect.Field.doSecurityCheck(Field.java:983)
     at java.lang.reflect.Field.getFieldAccessor(Field.java:927)
     at java.lang.reflect.Field.set(Field.java:680)
     at teletutor.core.test1.Base.setField(JPT.java:27)
I hope I have made my problem clear. Please suggest how I can achieve the desired effect.

Thanks in advance.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points