Example usage for org.aspectj.lang.reflect FieldSignature getFieldType

List of usage examples for org.aspectj.lang.reflect FieldSignature getFieldType

Introduction

In this page you can find the example usage for org.aspectj.lang.reflect FieldSignature getFieldType.

Prototype

public Class getFieldType();

Source Link

Usage

From source file:org.kaleidofoundry.core.context.AnnotationContexInjectorAspect.java

License:Apache License

/**
 * @param jp/*  w ww .j a  v a  2 s. c  o  m*/
 * @param esjp
 * @param thisJoinPoint
 * @param annotation
 * @return <code>true / false</code> to enable poincut
 * @throws Throwable
 */
// track field with ProceedingJoinPoint and annotation information with @annotation(annotation)
@SuppressWarnings("unchecked")
@Around("trackRuntimeContextField(jp, esjp) && @annotation(annotation)")
public Object trackRuntimeContextFieldToInject(final JoinPoint jp, final JoinPoint.EnclosingStaticPart esjp,
        final ProceedingJoinPoint thisJoinPoint, final Context annotation) throws Throwable {
    if (thisJoinPoint.getSignature() instanceof FieldSignature) {
        final FieldSignature fs = (FieldSignature) thisJoinPoint.getSignature();
        final Object target = thisJoinPoint.getTarget();
        final Field field = fs.getField();
        field.setAccessible(true);
        final Object currentValue = field.get(target);
        if (currentValue == null) {
            final RuntimeContext<?> runtimeContext = RuntimeContext.createFrom(annotation, fs.getName(),
                    fs.getFieldType());
            field.set(target, runtimeContext);
            return runtimeContext;
        } else {
            return thisJoinPoint.proceed();
        }
    } else {
        throw new IllegalStateException("aspect advise handle only field, please check your pointcut");
    }
}

From source file:org.kaleidofoundry.core.context.AnnotationContexInjectorAspect.java

License:Apache License

/**
 * @param jp// w  ww  .j  a  v a  2s.  com
 * @param esjp
 * @param thisJoinPoint
 * @param annotation
 * @return <code>true / false</code> if join point have been processed
 * @throws Throwable
 */
// track field with ProceedingJoinPoint and annotation information with @annotation(annotation)
@Around("trackAgregatedRuntimeContextField(jp, esjp) && @annotation(annotation)")
@Task(comment = "check and handle reflection exception ", labels = TaskLabel.Enhancement)
public Object trackAgregatedRuntimeContextFieldToInject(final JoinPoint jp,
        final JoinPoint.EnclosingStaticPart esjp, final ProceedingJoinPoint thisJoinPoint,
        final Context annotation) throws Throwable {

    debugJoinPoint(LOGGER, jp);

    if (thisJoinPoint.getSignature() instanceof FieldSignature) {
        final FieldSignature annotatedFieldSignature = (FieldSignature) thisJoinPoint.getSignature();
        final Field annotatedField = annotatedFieldSignature.getField();
        final Boolean annotatedFieldInjected = _$injectedFieldMap.get(annotatedField);

        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("\t<joinpoint.field.{}.injected>\t{}", annotatedField.getName(),
                    annotatedFieldInjected == null ? Boolean.FALSE : annotatedFieldInjected.booleanValue());
        }

        // does the field to inject have already been injected
        if (annotatedFieldInjected == null || !annotatedFieldInjected.booleanValue()) {
            // we need to access to its fields, in order to lookup for a RuntimeContext field
            annotatedField.setAccessible(true);
            final Object targetInstance = thisJoinPoint.getTarget();
            Object fieldValue = targetInstance != null ? annotatedField.get(targetInstance) : null;

            // process field initialize if null
            if (targetInstance != null && fieldValue == null) {

                // does the plugin interface have a provider specify
                if (annotatedField.getType().isAnnotationPresent(Provider.class)) {

                    // create provider using annotation meta-information
                    final Provider providerInfo = annotatedField.getType().getAnnotation(Provider.class);
                    final Constructor<? extends ProviderService<?>> providerConstructor = providerInfo.value()
                            .getConstructor(Class.class);
                    final ProviderService<?> fieldProviderInstance = providerConstructor
                            .newInstance(annotatedField.getType());

                    // invoke provides method with Context annotation meta-informations
                    final Method providesMethod = providerInfo.value().getMethod(
                            ProviderService.PROVIDES_METHOD, Context.class, String.class, Class.class);

                    try {
                        fieldValue = providesMethod.invoke(fieldProviderInstance, annotation,
                                annotatedField.getName(), annotatedField.getType());
                    } catch (final InvocationTargetException ite) {
                        // direct runtime exception like ContextException...
                        throw ite.getCause() != null ? ite.getCause()
                                : (ite.getTargetException() != null ? ite.getTargetException() : ite);
                    }
                    // set the field that was not yet injected
                    annotatedField.set(targetInstance, fieldValue);
                }

            }

            // if field instance have RuntimeContext not annotated @Context, that have not yet been initialize
            if (targetInstance != null && fieldValue != null) {

                // does a RuntimeContext field have been found
                boolean targetRuntimeContextFieldFound = false;

                // scan accessible field (private, protected, public)
                final Set<Field> contextFields = ReflectionHelper.getAllDeclaredFields(fieldValue.getClass(),
                        Modifier.PRIVATE, Modifier.PROTECTED, Modifier.PUBLIC);

                // for each RuntimeContext field, that are not annotated by @Context (to skip direct injection aspect)
                for (final Field cfield : contextFields) {

                    if (cfield.getType().isAssignableFrom(RuntimeContext.class)
                            && !cfield.isAnnotationPresent(Context.class)) {

                        RuntimeContext<?> currentRuntimeContext = null;
                        targetRuntimeContextFieldFound = true;
                        cfield.setAccessible(true);

                        currentRuntimeContext = (RuntimeContext<?>) cfield.get(fieldValue);

                        // if runtime context field have not yet been set
                        if (currentRuntimeContext == null
                                || !currentRuntimeContext.hasBeenInjectedByAnnotationProcessing()) {

                            // does the implementation is a declare plugin
                            final Plugin<?> interfacePlugin = PluginHelper.getInterfacePlugin(fieldValue);

                            // create a new instance of RuntimeContext<?>, using annotation information
                            final RuntimeContext<?> newRuntimeContext = RuntimeContext.createFrom(annotation,
                                    annotatedField.getName(),
                                    interfacePlugin != null ? interfacePlugin.getAnnotatedClass() : null);

                            // inject new RuntimeContext<?> field instance to the target instance
                            if (!Modifier.isFinal(cfield.getModifiers())) {
                                cfield.set(fieldValue, newRuntimeContext);
                            }
                            // re-copy runtimeContext information to the existing one
                            else {
                                if (currentRuntimeContext != null) {
                                    RuntimeContext.copyFrom(newRuntimeContext, currentRuntimeContext);
                                } else {
                                    // RuntimeContext field is final && null
                                    throw new ContextException("context.annotation.illegalfield",
                                            fieldValue.getClass().getName(), cfield.getName());
                                }
                            }

                            // mark instance has injected
                            _$injectedFieldMap.put(annotatedField, Boolean.TRUE);
                        }
                    }
                }

                // coherence checks
                if (!targetRuntimeContextFieldFound) {
                    throw new ContextException("context.annotation.illegaluse.noRuntimeContextField",
                            annotatedFieldSignature.getFieldType().getName() + "#" + annotatedField.getName(),
                            Context.class.getName());
                }

            }
        }

        return thisJoinPoint.proceed();

    } else {
        throw new IllegalStateException("aspect advise handle only field, please check your pointcut");
    }

}