Change templator to also access private fields

This commit is contained in:
Ziver Koc 2021-07-06 21:57:48 +02:00
parent e515fd7f87
commit dbed0b91db

View file

@ -84,7 +84,7 @@ import java.util.logging.Logger;
* @author Ziver koc * @author Ziver koc
*/ */
public class Templator { public class Templator {
private static final Logger log = LogUtil.getLogger(); private static final Logger logger = LogUtil.getLogger();
private HashMap<String,Object> data; private HashMap<String,Object> data;
private TemplateEntity tmplRoot; private TemplateEntity tmplRoot;
@ -136,11 +136,11 @@ public class Templator {
public String compile() { public String compile() {
if (file != null && lastModified != file.lastModified()) { if (file != null && lastModified != file.lastModified()) {
try { try {
log.info("Template file(" + file.getName() + ") changed. Regenerating template..."); logger.info("Template file(" + file.getName() + ") changed. Regenerating template...");
parseTemplate(FileUtil.getContent(file)); parseTemplate(FileUtil.getContent(file));
this.lastModified = file.lastModified(); this.lastModified = file.lastModified();
} catch(IOException e) { } catch(IOException e) {
log.log(Level.WARNING, "Unable to regenerate template", e); logger.log(Level.WARNING, "Unable to regenerate template", e);
} }
} }
@ -194,7 +194,7 @@ public class Templator {
// Is this tag closing the parent? // Is this tag closing the parent?
if (parentTag != null && tagName.endsWith(parentTag.substring(1))) if (parentTag != null && tagName.endsWith(parentTag.substring(1)))
return root; return root;
log.severe("Closing non-opened tag: {{" + tagName + "}}"); logger.severe("Closing non-opened tag: {{" + tagName + "}}");
root.add(new TemplateStaticString("{{" + tagName + "}}")); root.add(new TemplateStaticString("{{" + tagName + "}}"));
break; break;
case '!': // Comment case '!': // Comment
@ -218,7 +218,7 @@ public class Templator {
if (parentTag != null) { if (parentTag != null) {
root = new TemplateNode(root); root = new TemplateNode(root);
String tagName = "{{" + parentTag + "}}"; String tagName = "{{" + parentTag + "}}";
log.severe("Missing closure of tag: " + tagName); logger.severe("Missing closure of tag: " + tagName);
root.addFirst(new TemplateStaticString(tagName)); root.addFirst(new TemplateStaticString(tagName));
} }
return root; return root;
@ -412,7 +412,7 @@ public class Templator {
else { else {
// Using a loop as the direct lookup throws a exception if no field was found // Using a loop as the direct lookup throws a exception if no field was found
// So this is probably a bit faster // So this is probably a bit faster
for (Field field : obj.getClass().getFields()) { // Only look for public fields for (Field field : obj.getClass().getDeclaredFields()) { // Only look for public fields
if (field.getName().equals(attrib)) { if (field.getName().equals(attrib)) {
field.setAccessible(true); field.setAccessible(true);
return field.get(obj); return field.get(obj);
@ -420,7 +420,7 @@ public class Templator {
} }
} }
} catch (IllegalAccessException | InvocationTargetException e) { } catch (IllegalAccessException | InvocationTargetException e) {
log.log(Level.WARNING, null, e); logger.log(Level.WARNING, null, e);
} }
return null; return null;
} }