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

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

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

import com.artemis.*;
import com.artemis.annotations.AspectDescriptor;
import com.artemis.utils.reflect.Annotation;
import com.artemis.utils.reflect.Field;

import java.util.IdentityHashMap;

import static com.artemis.Aspect.all;

/**
 * 

Resolves the following aspect-related types:

*
    *
  • {@link com.artemis.Aspect}
  • *
  • {@link com.artemis.Aspect.Builder}
  • *
  • {@link com.artemis.EntitySubscription}
  • *
  • {@link com.artemis.EntityTransmuter}
  • *
* * @author Snorre E. Brekke * @author Adrian Papari */ public class AspectFieldResolver implements FieldResolver { private World world; private IdentityHashMap fields = new IdentityHashMap(); @Override public void initialize(World world) { this.world = world; } @Override @SuppressWarnings("unchecked") public Object resolve(Class fieldType, Field field) { Aspect.Builder aspect = aspect(field); if (aspect == null) return null; Class type = field.getType(); if (Aspect.class == type) { return world.getAspectSubscriptionManager().get(aspect).getAspect(); } else if (Aspect.Builder.class == type) { return aspect; } else if (EntityTransmuter.class == type) { return new EntityTransmuter(world, aspect); } else if (EntitySubscription.class == type) { return world.getAspectSubscriptionManager().get(aspect); } else if (Archetype.class == type) { return new ArchetypeBuilder() .add(descriptor(field).all()) .build(world); } return null; } private Aspect.Builder aspect(Field field) { if (!fields.containsKey(field)) { AspectDescriptor descriptor = descriptor(field); if (descriptor != null) { fields.put(field, toAspect(descriptor)); } else { fields.put(field, null); } } return fields.get(field); } private AspectDescriptor descriptor(Field field) { Annotation anno = field.getDeclaredAnnotation(AspectDescriptor.class); return (anno != null) ? anno.getAnnotation(AspectDescriptor.class) : null; } private Aspect.Builder toAspect(AspectDescriptor ad) { return all(ad.all()).one(ad.one()).exclude(ad.exclude()); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy