Я использую Freemarker и агент DCEVM + HotSwapManager. Это в основном позволяет мне выполнять горячую замену классов даже при добавлении / удалении методов.
Все работает как шарм, пока Freemarker не использует класс с горячей заменой в качестве модели. Он выбрасывает 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 во время выполнения не даст никакого эффекта.
BeansWrapper
, который действительно используется, он должен работать. НоremoveFromClassIntrospectionCache(Class clazz)
- это общедоступный метод, почему вы так его называете? Если это старый FreeMarker, сначала попробуйте обновить. - person ddekany   schedule 25.02.2015removeFromClassIntrospectionCache
меня озадачивает:...If the class will be still used, the cache entry will be silently re-created
- означает ли это, что когда кеш шаблона содержит сопоставление для рассматриваемого класса, самоанализ не получит его перезагруженную версию? Спасибо! - person plesatejvlk   schedule 26.02.2015beanInfo = java.beans.Introspector.getBeanInfo(aClass)
вместо прямого отражения, а затем перечислите методы с помощьюbeanInfo.getMethodDescriptors()
. Также свойства сbeanInfo.getPropertyDescriptors()
, потому что я предполагаю, что вы действительно получаете доступ к свойствам в шаблоне (еще не видели его). Возможно, проблема в том, что DCEVM не очищаетBeanIntrospector
кеш Java. Просто дикая догадка. - person ddekany   schedule 26.02.2015