我创建了这个代理包装器来处理方法的调用。似乎可以正常工作,我只是想知道是否有人可以检查并告诉我是否可以改进某些地方,为什么?
干杯!
stackover抱怨我需要写更多,所以这只是填充物...大声笑
视频提供了一种强大的方式来帮助您证明自己的观点。单击“在线视频”时,可以粘贴要添加的视频的嵌入代码。您也可以输入关键字以在线搜索最适合您的文档的视频。 为了使您的文档看起来专业制作,Word提供了相互补充的页眉,页脚,封面和文本框设计。例如,您可以添加匹配的封面,页眉和侧边栏。单击插入,然后从不同的库中选择所需的元素。 主题和样式还有助于使文档保持一致。当您单击设计并选择一个新的主题时,图片,图表和SmartArt图形将更改以匹配您的新主题。应用样式时,标题会更改以匹配新主题。 通过在您需要的地方显示新按钮来节省Word中的时间。要更改图片在文档中的放置方式,请单击它,然后在其旁边显示一个用于布局选项的按钮。在表上工作时,单击要在其中添加行或列的位置,然后单击加号。 在新的“阅读”视图中阅读也更容易。您可以折叠文档的一部分,然后将重点放在所需的文本上。如果您需要在到达终点之前停止阅读,Word会记住您离开的地方-即使在其他设备上。
import java.lang.reflect.Method;
public class ProxyWrapper
{
Class<?> c = null;
Object o = null;
String namespace = null;
String className = null;
Thread tCleanup;
HouseKeeping houseKeeping = null;
Boolean closing = false;
long idle_start = System.currentTimeMillis();
int timeout = 10000;
public ProxyWrapper()
{
CreateHouseKeeping();
}
public ProxyWrapper(String namespace, String className) throws ClassNotFoundException, InstantiationException, IllegalAccessException, Exception
{
msg("new ProxyWrapper");
this.namespace = namespace;
this.className = className;
msg("calling INIT");
INIT();
CreateHouseKeeping();
}
private void CreateHouseKeeping()
{
msg("creating housekeeping");
houseKeeping = new HouseKeeping();
houseKeeping.SetParent(this);
tCleanup = new Thread(houseKeeping);
tCleanup.start();
}
public void msg(Object e)
{
if(e.getClass() == String.class)
{
System.out.println((String)e);
}
if(e.getClass() == Exception.class)
{
System.out.println(((Exception)e).toString());
}
}
public int GetTimeout()
{
return timeout;
}
public long GetIdleTime()
{
return idle_start;
}
public void SetIdleTime(long value)
{
idle_start = value;
}
public Boolean GetClosing()
{
return closing;
}
public void SetClosing(Boolean value)
{
closing = value;
}
private void INIT()throws ClassNotFoundException, InstantiationException, IllegalAccessException, Exception
{
if(namespace == null || namespace.length()==0)
throw new Exception("Namespace property not set");
if(className == null || className.length()==0)
throw new Exception("Classname property not set");
c = Class.forName(namespace+"."+className);
o= c.newInstance();
}
private class HouseKeeping implements Runnable
{
ProxyWrapper parent = null;
public HouseKeeping()
{
}
public void SetParent(ProxyWrapper parent)
{
this.parent = parent;
}
@Override
public void run()
{
long t = 0;
while (!parent.GetClosing())
{
try
{
t = (System.currentTimeMillis() - parent.GetIdleTime());
msg("Housekeeping, checking idle session: " + t);
Thread.sleep(200);
if( t > parent.GetTimeout() )
{
msg("Session idle expired, finilizing");
parent.SetIdleTime(System.currentTimeMillis());
parent.finalize();
//break;
}
}
catch (Throwable e)
{
msg(e);
}
}
}
}
public Object Invoke(String method, Object[] args) throws Exception
{
//Method m = null;
Method[] methods = null;
try
{
if(method==null ||method.length()==0)
throw new Exception("Method name is null/empty");
msg("Invoking method " + method);
if(o==null)
INIT();
SetIdleTime(System.currentTimeMillis());
//m = o.getClass().getMethod(method);
methods = c.getMethods();
for (Method m : methods) {
msg(m.getName());
if (method.equals(m.getName())) {
return m.invoke(null, args);
}
}
throw new ClassNotFoundException("Method not found " + method);
}
finally
{
methods = null;
}
}
@Override
public void finalize() throws Throwable
{
try
{
msg("entering finalize");
closing = true;
c = null;
o = null;
}
catch(Throwable t)
{
msg(t);
throw t;
}
finally
{
msg("leaving finalize");
super.finalize();
}
}
}