All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sampullara.mustache.ObjectHandler7 Maven / Gradle / Ivy

package com.sampullara.mustache;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import static com.sampullara.mustache.ObjectHandler6.getField;
import static com.sampullara.mustache.ObjectHandler6.getMethod;

/**
 * Implementation of Handle Object for Java VMs that include MethodHandles.
 * 

* User: sam * Date: 7/24/11 * Time: 2:56 PM */ public class ObjectHandler7 implements ObjectHandler { private static Logger logger = Logger.getLogger(Mustache.class.getName()); private static ClassValue> cache = new ClassValue>() { @Override protected Map computeValue(Class type) { return new ConcurrentHashMap<>(); } }; private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup(); // Need something to stand for unable to find field or method private static final MethodHandle NOHANDLE = MethodHandles.dropArguments( MethodHandles.constant(Object.class, Object.class), 0, Object.class); public Object handleObject(Object parent, Scope scope, String name) { Object value = null; Class aClass = parent.getClass(); Map handleMap = cache.get(aClass); MethodHandle handle = handleMap.get(name); if (handle == NOHANDLE) return null; try { if (handle == null) { try { Field field = getField(name, aClass); handleMap.put(name, handle = MethodHandles.lookup().unreflectGetter(field)); } catch (IllegalAccessException | NoSuchFieldException e) { // Not set } } if (handle == null) { try { Method method = getMethod(name, aClass); handleMap.put(name, handle = LOOKUP.unreflect(method)); } catch (IllegalAccessException | NoSuchMethodException e) { try { Method method = getMethod(name, aClass, Scope.class); handleMap.put(name, handle = LOOKUP.unreflect(method)); } catch (IllegalAccessException | NoSuchMethodException e1) { String propertyname = name.substring(0, 1).toUpperCase() + (name.length() > 1 ? name.substring(1) : ""); try { Method method = getMethod("get" + propertyname, aClass); handleMap.put(name, handle = LOOKUP.unreflect(method)); } catch (IllegalAccessException | NoSuchMethodException e2) { try { Method method = getMethod("is" + propertyname, aClass); handleMap.put(name, handle = LOOKUP.unreflect(method)); } catch (IllegalAccessException | NoSuchMethodException e3) { // Not set } } } } } if (handle != null) { if (handle.type().parameterCount() == 1) { value = handle.invoke(parent); } else { value = handle.invoke(parent, scope); } } if (value == null) { if (handle != null && handle.type().returnType().isAssignableFrom(Iterable.class)) { value = Scope.EMPTY; } else { value = Scope.NULL; } } } catch (Throwable e) { // Might be nice for debugging but annoying in practice logger.log(Level.WARNING, "Failed to get value for " + name, e); } if (handle == null) { handleMap.put(name, NOHANDLE); } return value; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy