freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddek...@apache.org
Subject [freemarker] 01/04: (Cleanup and javadoc improvements)
Date Sun, 12 Jan 2020 09:50:49 GMT
This is an automated email from the ASF dual-hosted git repository.

ddekany pushed a commit to branch 2.3-gae
in repository https://gitbox.apache.org/repos/asf/freemarker.git

commit 81d2fadd2641f89da9e590738f78e6df8a26b3be
Author: ddekany <ddekany@apache.org>
AuthorDate: Sun Jan 12 06:27:53 2020 +0100

    (Cleanup and javadoc improvements)
---
 .../java/freemarker/ext/beans/BeansWrapper.java    |  3 +--
 .../freemarker/ext/beans/BeansWrapperBuilder.java  |  9 ++++++---
 .../ext/beans/ClassBasedModelFactory.java          | 23 ++++++++++------------
 .../freemarker/ext/beans/MemberAccessPolicy.java   |  5 +++++
 .../java/freemarker/ext/beans/StaticModel.java     | 19 +++++++++---------
 5 files changed, 32 insertions(+), 27 deletions(-)

diff --git a/src/main/java/freemarker/ext/beans/BeansWrapper.java b/src/main/java/freemarker/ext/beans/BeansWrapper.java
index 2bda4b9..d3fb070 100644
--- a/src/main/java/freemarker/ext/beans/BeansWrapper.java
+++ b/src/main/java/freemarker/ext/beans/BeansWrapper.java
@@ -1546,8 +1546,7 @@ public class BeansWrapper implements RichObjectWrapper, WriteProtectable
{
     public TemplateHashModel getStaticModels() {
         return staticModels;
     }
-    
-    
+
     /**
      * Returns a hash model that represents the so-called class enum models.
      * Every class' enum model is itself a hash through which you can access
diff --git a/src/main/java/freemarker/ext/beans/BeansWrapperBuilder.java b/src/main/java/freemarker/ext/beans/BeansWrapperBuilder.java
index 047a9dd..6072a1e 100644
--- a/src/main/java/freemarker/ext/beans/BeansWrapperBuilder.java
+++ b/src/main/java/freemarker/ext/beans/BeansWrapperBuilder.java
@@ -75,9 +75,12 @@ import freemarker.template.Version;
  *     {@code freemarker.jar}-s (typically, in two Web Application's {@code WEB-INF/lib}
directories), those won't
  *     share their caches (as they don't share the same FreeMarker classes).
  *     Also, currently there's a separate cache for each permutation of the property values
that influence class
- *     introspection: {@link BeansWrapperBuilder#setExposeFields(boolean) expose_fields}
and
- *     {@link BeansWrapperBuilder#setExposureLevel(int) exposure_level}. So only {@link BeansWrapper}
where those
- *     properties are the same may share class introspection caches among each other.
+ *     introspection:
+ *     {@link BeansWrapperBuilder#setExposeFields(boolean) expose_fields}, and
+ *     {@link BeansWrapperBuilder#setExposureLevel(int) exposure_level}, and
+ *     {@link BeansWrapperBuilder#setMemberAccessPolicy(MemberAccessPolicy)}  member_access_policy}.
+ *     So only {@link BeansWrapper} where those properties are the same may share class introspection
caches among each
+ *     other.
  *   </li>
  *   <li><p>Model caches: These are local to a {@link BeansWrapper}. {@link BeansWrapperBuilder}
returns the same
  *     {@link BeansWrapper} instance for equivalent properties (unless the existing instance
was garbage collected
diff --git a/src/main/java/freemarker/ext/beans/ClassBasedModelFactory.java b/src/main/java/freemarker/ext/beans/ClassBasedModelFactory.java
index cc4a258..fd836d4 100644
--- a/src/main/java/freemarker/ext/beans/ClassBasedModelFactory.java
+++ b/src/main/java/freemarker/ext/beans/ClassBasedModelFactory.java
@@ -37,8 +37,8 @@ import freemarker.template.utility.ClassUtil;
 abstract class ClassBasedModelFactory implements TemplateHashModel {
     private final BeansWrapper wrapper;
     
-    private final Map/*<String,TemplateModel>*/ cache = new ConcurrentHashMap();
-    private final Set classIntrospectionsInProgress = new HashSet();
+    private final Map<String,TemplateModel> cache = new ConcurrentHashMap<String,TemplateModel>();
+    private final Set<String> classIntrospectionsInProgress = new HashSet<String>();
     
     protected ClassBasedModelFactory(BeansWrapper wrapper) {
         this.wrapper = wrapper;
@@ -59,7 +59,7 @@ abstract class ClassBasedModelFactory implements TemplateHashModel {
 
     private TemplateModel getInternal(String key) throws TemplateModelException, ClassNotFoundException
{
         {
-            TemplateModel model = (TemplateModel) cache.get(key);
+            TemplateModel model = cache.get(key);
             if (model != null) return model;
         }
 
@@ -67,19 +67,17 @@ abstract class ClassBasedModelFactory implements TemplateHashModel {
         int classIntrospectorClearingCounter;
         final Object sharedLock = wrapper.getSharedIntrospectionLock();
         synchronized (sharedLock) {
-            TemplateModel model = (TemplateModel) cache.get(key);
+            TemplateModel model = cache.get(key);
             if (model != null) return model;
             
-            while (model == null
-                    && classIntrospectionsInProgress.contains(key)) {
+            while (model == null && classIntrospectionsInProgress.contains(key))
{
                 // Another thread is already introspecting this class;
                 // waiting for its result.
                 try {
                     sharedLock.wait();
-                    model = (TemplateModel) cache.get(key);
+                    model = cache.get(key);
                 } catch (InterruptedException e) {
-                    throw new RuntimeException(
-                            "Class inrospection data lookup aborded: " + e);
+                    throw new RuntimeException("Class inrospection data lookup aborted: "
+ e);
                 }
             }
             if (model != null) return model;
@@ -93,7 +91,7 @@ abstract class ClassBasedModelFactory implements TemplateHashModel {
             classIntrospectorClearingCounter = classIntrospector.getClearingCounter();
         }
         try {
-            final Class clazz = ClassUtil.forName(key);
+            final Class<?> clazz = ClassUtil.forName(key);
             
             // This is called so that we trigger the
             // class-reloading detector. If clazz is a reloaded class,
@@ -129,7 +127,7 @@ abstract class ClassBasedModelFactory implements TemplateHashModel {
         }
     }
     
-    void removeFromCache(Class clazz) {
+    void removeFromCache(Class<?> clazz) {
         synchronized (wrapper.getSharedIntrospectionLock()) {
             cache.remove(clazz.getName());
         }
@@ -139,8 +137,7 @@ abstract class ClassBasedModelFactory implements TemplateHashModel {
         return false;
     }
     
-    protected abstract TemplateModel createModel(Class clazz) 
-    throws TemplateModelException;
+    protected abstract TemplateModel createModel(Class<?> clazz) throws TemplateModelException;
     
     protected BeansWrapper getWrapper() {
         return wrapper;
diff --git a/src/main/java/freemarker/ext/beans/MemberAccessPolicy.java b/src/main/java/freemarker/ext/beans/MemberAccessPolicy.java
index eacb97d..400f1ce 100644
--- a/src/main/java/freemarker/ext/beans/MemberAccessPolicy.java
+++ b/src/main/java/freemarker/ext/beans/MemberAccessPolicy.java
@@ -45,6 +45,11 @@ import freemarker.template.TemplateModel;
  * {@link ObjectWrapper} (from {@link Environment#getObjectWrapper()}), and so the {@link
MemberAccessPolicy} won't
  * affect those.
  *
+ * <p>The {@link MemberAccessPolicy} is only used during the class introspection phase
(which discovers the members of a
+ * type, and decides if, and how will they be exposed to templates), and the result of that
is cached. So, the speed of
+ * an {@link MemberAccessPolicy} implementation is usually not too important, as it won't
play a role during template
+ * execution.
+ *
  * <p>Implementations must be thread-safe, and instances generally should be singletons
on JVM level. FreeMarker
  * caches its class metadata in a global (static, JVM-scope) cache for shared use, and the
{@link MemberAccessPolicy}
  * used is part of the cache key. Thus {@link MemberAccessPolicy} instances used at different
places in the JVM
diff --git a/src/main/java/freemarker/ext/beans/StaticModel.java b/src/main/java/freemarker/ext/beans/StaticModel.java
index 1b9e0f5..99e9329 100644
--- a/src/main/java/freemarker/ext/beans/StaticModel.java
+++ b/src/main/java/freemarker/ext/beans/StaticModel.java
@@ -42,11 +42,11 @@ import freemarker.template.TemplateModelException;
  */
 final class StaticModel implements TemplateHashModelEx {
     private static final Logger LOG = Logger.getLogger("freemarker.beans");
-    private final Class clazz;
+    private final Class<?> clazz;
     private final BeansWrapper wrapper;
-    private final Map map = new HashMap();
+    private final Map<String, Object> map = new HashMap<String, Object>();
 
-    StaticModel(Class clazz, BeansWrapper wrapper) throws TemplateModelException {
+    StaticModel(Class<?> clazz, BeansWrapper wrapper) throws TemplateModelException
{
         this.clazz = clazz;
         this.wrapper = wrapper;
         populate();
@@ -107,22 +107,23 @@ final class StaticModel implements TemplateHashModelEx {
         }
 
         Field[] fields = clazz.getFields();
-        for (int i = 0; i < fields.length; ++i) {
-            Field field = fields[i];
+        for (Field field : fields) {
             int mod = field.getModifiers();
             if (Modifier.isPublic(mod) && Modifier.isStatic(mod)) {
-                if (Modifier.isFinal(mod))
+                if (Modifier.isFinal(mod)) {
                     try {
                         // public static final fields are evaluated once and
                         // stored in the map
                         map.put(field.getName(), wrapper.getOuterIdentity().wrap(field.get(null)));
                     } catch (IllegalAccessException e) {
                         // Intentionally ignored
-                    } else
+                    }
+                } else {
                     // This is a special flagging value: Field in the map means
                     // that this is a non-final field, and it must be evaluated
                     // on each get() call.
                     map.put(field.getName(), field);
+                }
             }
         }
         if (wrapper.getExposureLevel() < BeansWrapper.EXPOSE_PROPERTIES_ONLY) {
@@ -156,8 +157,8 @@ final class StaticModel implements TemplateHashModelEx {
                     }
                 }
             }
-            for (Iterator entries = map.entrySet().iterator(); entries.hasNext(); ) {
-                Map.Entry entry = (Map.Entry) entries.next();
+            for (Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
entries.hasNext(); ) {
+                Map.Entry<String, Object> entry = entries.next();
                 Object value = entry.getValue();
                 if (value instanceof Method) {
                     Method method = (Method) value;


Mime
View raw message