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

com.artemis.injection.InjectionCache Maven / Gradle / Ivy

There is a newer version: 2.3.0
Show newest version
package com.artemis.injection;

import com.artemis.BaseSystem;
import com.artemis.ComponentMapper;
import com.artemis.EntityFactory;
import com.artemis.Manager;
import com.artemis.annotations.Mapper;
import com.artemis.annotations.Wire;
import com.artemis.utils.reflect.ClassReflection;
import com.artemis.utils.reflect.Field;
import com.artemis.utils.reflect.ReflectionException;

import java.util.HashMap;
import java.util.Map;

/**
 * Date: 31/7/2015
 * Time: 17:13 PM
 *
 * @author Snorre E. Brekke
 */
public class InjectionCache {
    private static final Map, CachedClass> classCache = new HashMap, CachedClass>();
    private static final Map, ClassType> fieldClassTypeCache = new HashMap, ClassType>();
    private static final Map namedWireCache = new HashMap();
    private static final Map> genericsCache = new HashMap>();

    public CachedClass getCachedClass(Class clazz) throws ReflectionException {
        CachedClass cachedClass = classCache.get(clazz);
        if (cachedClass == null) {
            cachedClass = new CachedClass(clazz);
            cachedClass.wireType = ClassReflection.isAnnotationPresent(clazz, Wire.class) ?
                                   WireType.WIRE :
                                   WireType.IGNORED;

            if (cachedClass.wireType == WireType.WIRE) {
                Wire wireAnnotation = ClassReflection.getAnnotation(clazz, Wire.class);
                cachedClass.wireAnnotation = wireAnnotation;
                cachedClass.failOnNull = wireAnnotation.failOnNull();
                cachedClass.injectInherited = wireAnnotation.injectInherited();
            }
            classCache.put(clazz, cachedClass);
        }
        return cachedClass;
    }


    public CachedField getCachedField(Field field) {
        CachedField cachedField = namedWireCache.get(field);
        if (cachedField == null) {
            if (field.isAnnotationPresent(Wire.class)) {
                final Wire wire = field.getAnnotation(Wire.class);
                cachedField = new CachedField(field, WireType.WIRE, wire.name());
            }
            else if(field.isAnnotationPresent(Mapper.class)) {
                cachedField = new CachedField(field, WireType.MAPPER, null);
            }else {
                cachedField = new CachedField(field, WireType.IGNORED, null);
            }
            namedWireCache.put(field, cachedField);
        }
        return cachedField;
    }


    public ClassType getFieldClassType(Class fieldType) {
        ClassType injectionType = fieldClassTypeCache.get(fieldType);
        if (injectionType == null) {
            if (ClassReflection.isAssignableFrom(ComponentMapper.class, fieldType)) {
                injectionType = ClassType.MAPPER;
            } else if(ClassReflection.isAssignableFrom(BaseSystem.class, fieldType)) {
                injectionType = ClassType.SYSTEM;
            } else if(ClassReflection.isAssignableFrom(Manager.class, fieldType)) {
                injectionType = ClassType.MANAGER;
            } else if(ClassReflection.isAssignableFrom(EntityFactory.class, fieldType)){
                injectionType = ClassType.FACTORY;
            } else {
                injectionType = ClassType.CUSTOM;
            }
            fieldClassTypeCache.put(fieldType, injectionType);
        }
        return injectionType;
    }

    public Class getGenericType(Field field){
        Class genericsType = genericsCache.get(field);
        if (genericsType == null) {
            genericsType = field.getElementType(0);
            genericsCache.put(field, genericsType);
        }
        return genericsType;
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy