Private bisa di"paksa" bisa dibaca/tulis dari luar koq.
http://tutorials.jenkov.com/java-reflection/private-fields-and-methods.html

Framework dependency injection yang inject to field juga biasanya gak
masalah walopun fieldnya private (di Guice sering gini koq, di EJB/JPA pun
kayaknya bisa).

<http://tutorials.jenkov.com/java-reflection/private-fields-and-methods.html>
-Kurniady

2010/5/2 Adelwin Handoyo <adel...@gmail.com>

>
>
> Aahh...
> Disini guna nya convention POJO.
> Field nya.. As in ID, NAMA, dll ituh tuh semua private...
> Sesuai dasar nya java... Private ituh kagak bisa di baca dari luar...
> Jadi walopun pake reflection pun kagak bisa di baca...
> Reflection yang Class.getFields() itu tuh Cuma bisa untuk retrieve public
> fields.
> Hahaha.. G juga pernah ketipu sama ginian...
>
> Untuk yang satu lagi tadi... PropertyUtils nya ituh dari package mana?
> Gue kagak nemu malah...
>
> As I said before.. Ini guna nya POJO Convention...
> Liat method ini
>
>
>     public static final String GETTER_METHOD_PREFIX = "get";    public
> static final String SETTER_METHOD_PREFIX = "set";    public static final
> String METHOD_SPLICE_BEGIN_INDEX = "3";
>     public static final String getFieldFromMethod(Method method) {
>        String retVal = new String();        retVal =
> method.getName().substring(Integer.parseInt(METHOD_SPLICE_BEGIN_INDEX));
>        return retVal;    }    public static final List extractFields(Class
> clazz) {        List retVal = new ArrayList();        Method[] methods =
> clazz.getMethods();        Method method;        String methodName;
>        for (int i = 0; i < methods.length; i++) {            method =
> methods[i];            if
> (method.getName().startsWith(GETTER_METHOD_PREFIX)) {
>                methodName = BeanUtil.getFieldFromMethod(method);
>                if (!retVal.contains(methodName)) {
>                    retVal.add(methodName);                                }
>                            }        }        return retVal;    }    public
> static final List<String> extractBeanFields(Class clazz) {        List
> retVal = BeanUtil.extractFields(clazz);        List objField =
> BeanUtil.extractFields(Object.class);        retVal.removeAll(objField);
>        return retVal;    }
> Ini g barusan buat... agak2 penasaran soal nya tadi.
> As you can see.. Bisa jalan sampe jdk1.4
> Kalo udah dapet list of string, ntar kita baru bisa begini.
>
> BeanWrapper student = BeanWrapperImpl(new Student());
> Object value = student.getPropertyValue(“name”);
> student.setPropertyValue(“name”, “Adelwin”);
>
> Tapi setelah g coba2 lagi...
> Ada cara yang lebih elegant...
>
>     public static final List getFields(Class clazz) throws
> InstantiationException, IllegalAccessException {        List fields = new
> ArrayList();        BeanWrapper clazzWrapper = new
> BeanWrapperImpl(clazz.newInstance());        PropertyDescriptor[]
> propertyDescriptors = clazzWrapper.getPropertyDescriptors();        for
> (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
>            fields.add(propertyDescriptor.getName());        }        return
> fields;    }
>
> *Adelwin Handoyo
> *- adel...@gmail.com -
> Sent from my Mac
>
>
> ------------------------------
> *From: *Deny Prasetyo <jas...@gmail.com>
> *Reply-To: *JUG-Indonesia <jug-indonesia@yahoogroups.com>
> *Date: *Sun, 02 May 2010 12:48:29 +0700
> *To: *JUG-Indonesia <jug-indonesia@yahoogroups.com>
> *Subject: *Re: [JUG-Indonesia] tanya baca property di POJO
>
>
>
>
> Pakai Reflection.
>
> http://java.sun.com/docs/books/tutorial/reflect/index.html
>
> On 5/2/2010 12:35 PM, Wiyanto Ngasinur wrote:
>
>
>
>
> PropertyUtils.getPropertyDescriptors([nama class])
>
>
>
>
> return nya PropertyDescriptor[], tinggal di loop n panggil getName()
>
>
>
>
>
>
>
> thanks,
>
> W
>
>
>
> 2010/5/2 ivan prasetia <iphan_zeke_...@yahoo.co.id>
>
>
>
>
>
>
>
>  I all..
>
> mau tanya bgm cara baca property2 di class pojo pakai class lain
> misal ada class Person berisi
> private String id;
> private String name;
> private String address;
>
> nah bagaimana cara mendapatkan property2 nya dr class lain
> misal ada method public List getPropertyPOJO(Class class)
> misal kita kasih parameternya memakai class Person
> List tersebut berisi property2 pojo nya id,name,address
>
> Thanks....
>
> Y! iphan_zeke_boy
>
>
>
>
>
>
>
>
>
>
>
>
>
> --
> Deny Prasetyo
> Janabadra University Lecturer
> http://www.janabadra.ac.id
> http://jasoet.wordpress.com
> www.twitter.com/jasoet <http://www.twitter.com/jasoet>
> YM: jasoet87
> -------
>
>
>
>
>   
>

Kirim email ke