我正在使用Freemarker和DCEVM + HotSwapManager代理。即使在添加/删除方法时,这基本上也允许我热交换类。
一切都像魅力一样,直到Freemarker使用hotswapped类作为模型。它正在抛出freemarker.ext.beans.InvalidPropertyException:即使反射显示该方法存在(在调试会话期间检查),我也没有这样的bean属性。
我正在使用
final Method clearInfoMethod = beanWrapper.getClass().getDeclaredMethod("removeIntrospectionInfo", Class.class);
clearInfoMethod.setAccessible(true);
clearInfoMethod.invoke(clazz);
清除缓存,但它不起作用。我甚至尝试获取classCache成员字段并使用反射清除它,但它也不起作用。
我做错了什么? 我只需要强制freemarker抛弃他已经获得的模型类/类的任何内省。
有什么办法吗?
更新
Application.java
// Application.java
public class Application
{
public static final String TEMPLATE_PATH = "TemplatePath";
public static final String DEFAULT_TEMPLATE_PATH = "./";
private static Application INSTANCE;
private Configuration freemarkerConfiguration;
private BeansWrapper beanWrapper;
public static void main(String[] args)
{
final Application application = new Application();
INSTANCE = application;
try
{
application.run(args);
}
catch (InterruptedException e)
{
System.out.println("Exiting");
}
catch (IOException e)
{
System.out.println("IO Error");
e.printStackTrace();
}
}
public Configuration getFreemarkerConfiguration()
{
return freemarkerConfiguration;
}
public static Application getInstance()
{
return INSTANCE;
}
private void run(String[] args) throws InterruptedException, IOException
{
final String templatePath = System.getProperty(TEMPLATE_PATH) != null
? System.getProperty(TEMPLATE_PATH)
: DEFAULT_TEMPLATE_PATH;
final Configuration configuration = new Configuration();
freemarkerConfiguration = configuration;
beanWrapper = new BeansWrapper();
beanWrapper.setUseCache(false);
configuration.setObjectWrapper(beanWrapper);
try
{
final File templateDir = new File(templatePath);
configuration.setTemplateLoader(new FileTemplateLoader(templateDir));
}
catch (IOException e)
{
throw new RuntimeException(e);
}
final RunnerImpl runner = new RunnerImpl();
try
{
runner.run(args);
}
catch (RuntimeException e)
{
e.printStackTrace();
}
}
public BeansWrapper getBeanWrapper()
{
return beanWrapper;
}
}
RunnerImpl.java
// RunnerImpl.java
public class RunnerImpl implements Runner
{
@Override
public void run(String[] args) throws InterruptedException
{
long counter = 0;
while(true)
{
++counter;
System.out.printf("Run %d\n", counter);
// Application.getInstance().getFreemarkerConfiguration().setObjectWrapper(new BeansWrapper());
Application.getInstance().getBeanWrapper().clearClassIntrospecitonCache();
final Worker worker = new Worker();
worker.doWork();
Thread.sleep(1000);
}
}
Worker.java
// Worker.java
public class Worker
{
void doWork()
{
final Application application = Application.getInstance();
final Configuration freemarkerConfiguration = application.getFreemarkerConfiguration();
try
{
final Template template = freemarkerConfiguration.getTemplate("test.ftl");
final Model model = new Model();
final PrintWriter printWriter = new PrintWriter(System.out);
printObjectInto(model);
System.out.println("-----TEMPLATE MACRO PROCESSING-----");
template.process(model, printWriter);
System.out.println();
System.out.println("-----END OF PROCESSING------");
System.out.println();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (TemplateException e)
{
e.printStackTrace();
}
}
private void printObjectInto(Object o)
{
final Class<?> aClass = o.getClass();
final Method[] methods = aClass.getDeclaredMethods();
for (final Method method : methods)
{
System.out.println(String.format("Method name: %s, public: %s", method.getName(), Modifier.isPublic(method.getModifiers())));
}
}
}
Model.java
// Model.java
public class Model
{
public String getMessage()
{
return "Hello";
}
public String getAnotherMessage()
{
return "Hello World!";
}
}
此示例根本不起作用。即使在运行期间更改BeansWrapper也没有任何效果。
答案 0 :(得分:3)
BeansWrapper
(和DefaultObjectWrapper
等)内省缓存依赖于java.beans.Introspector.getBeanInfo(aClass)
,而不是反射。 (那是因为它将对象视为JavaBeans。)java.beans.Introspector
有自己的内部缓存,因此它可以返回陈旧信息,在这种情况下BeansWrapper
将根据陈旧信息重新创建自己的类内省数据。从java.beans.Introspector
的缓存开始,它实际上是正确的,因为它建立在Java中的类是不可变的假设之上。如果有什么东西打破了这个基本规则,它应该确保java.beans.Introspector
的缓存被清除(以及许多其他缓存......),否则它不仅仅会破坏FreeMarker。例如,在JRebel,他们付出了很多努力来清除所有类型的缓存。我猜DCEVM没有相应的资源。那么,似乎你必须自己打电话给Introspector.flushCaches()
。
更新:有一段时间(Java 7,可能是6)java.beans.Introspector
每个线程组有一个缓存,因此您可以从所有线程组调用flushCaches()
。这一切实际上都是实施细节,原则上可以随时改变。遗憾的是,Introspector.flushCaches()
的JavaDoc并没有警告你......