/*
* Copyright (C) 2004 Roman Krylov
* rkrylov@mail.ru
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package org.JWrapper;
import java.lang.*;
import java.lang.reflect.*;
import java.util.*;
public class JBeanAccessAdapter extends JAccessAdapter
{
protected Object source;
protected Method setter;
protected Method getter;
protected boolean absoluteRW = true;
public JBeanAccessAdapter(Object _source,
String _entity_name,
boolean _RW)
{
source = _source;
try
{
getter = _source.getClass().getMethod("get"+_entity_name,null);
value_class = getter.getReturnType();
RW=_RW;
try
{
setter =
_source.getClass().getMethod("set"+_entity_name,new Class[]{value_class});
} catch (Exception ex)
{ if(!_RW) { absoluteRW = false; RW = false; } }
} catch(Exception ex)
{
System.out.println("JBeanAccessAdapter:"+ex.toString());
ex.printStackTrace();
}
this.setRW(RW);
}
public JBeanAccessAdapter(Object _source,
String _entity_name)
{
source = _source;
try
{
getter = _source.getClass().getMethod("get"+_entity_name,null);
value_class = getter.getReturnType();
try
{
setter =
_source.getClass().getMethod("set"+_entity_name,new Class[]{value_class});
} catch (Exception ex)
{ absoluteRW = false; RW = false;}
} catch(Exception ex)
{
System.out.println("JBeanAccessAdapter:"+ex.toString());
ex.printStackTrace();
}
}
public void setRW(boolean rw)
{
if(!absoluteRW==false)
{ RW = rw; }
}
public T get() throws Exception
{
T value = null;
value = (T) getter.invoke(source, null);
return value;
}
protected void _set(T obj) throws Exception
{
if(RW)
{ setter.invoke(source, new Object[]{obj}); }
}
}
|