[stringtemplate-interest] it's 60% of time in ObjectModelAdaptor.getProperty()

Johan Stuyts j.stuyts at javathinker.com
Sun Feb 6 12:59:55 PST 2011


> here's the issue: what's the type in the cast? we don't know it.

Once you have the Member (Method or Attribute) object for the property,  
you can use the declaring class of that member for the cast. For example:

interface PropertyRetriever {
   Object getValue(Object o);
}

class DynGenForMethodNameOfPerson implements PropertyRetriever {
   Object getValue(Object o) {
     return ((Person)o).getName();
   }
}

class DynGenForAttrAgeOfPerson implements PropertyRetriever {
   Object getValue(Object o) {
     return ((Person)o).age;
   }
}

You have to insert references to the class (Person) and the member  
(getName()/age) in the byte array you use to define the retriever class.

You can then replace the code section that looks up and invokes/gets the  
member with (note: exception handling omitted):
...
// Look in cache for PropertyRetriever first
PropertyRetriever retriever = classAndPropertyToRetrieverCache.get(c,  
propertyName);
if ( retriever!=null ) {
   return retriver.getValue(o);
}
...

Please note that the cache uses the actual class of the object to retrieve  
the PropertyRetriever. To prevent multiple retriever classes being defined  
for the same declaring class and property (and filling up the PermGen  
space), you have to search the cache for the declaring class of the member  
when you have a cache miss:

Class c = Object.getClass();
Method method = c.getMetod(getGetterName(propertyName));
PropertyRetriever retriever =  
classAndPropertyToRetrieverCache.get(method.getDeclaringClass(),  
propertyName);
if ( retriever==null ) {
   Object newRetriever = generateRetrieverClassAndCreateInstance(method);
   PropertyRetriever retriever = (PropertyRetriever)newRetriever;
   classAndPropertyToRetrieverCache.put(method.getDeclaringClass(),  
propertyName, retriever);
}
classAndPropertyToRetrieverCache.put(c, propertyName, retriever);

Johan


More information about the stringtemplate-interest mailing list