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

com.github.dxee.dject.lifecycle.JSR250PreDestroyLifecycleFeature Maven / Gradle / Ivy

Go to download

A collection of guice extensions, help to improve the developer experience of guice.

There is a newer version: 1.2.1
Show newest version
package com.github.dxee.dject.lifecycle;

import com.github.dxee.dject.lifecycle.impl.AbstractTypeVisitor;
import com.github.dxee.dject.lifecycle.impl.OneAnnotationLifecycleFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PreDestroy;
import java.lang.annotation.Annotation;

/**
 * Special LifecycleFeature to support @PreDestroy annotation processing and
 * java.lang.AutoCloseable detection.
 * @author bing.fan
 * 2018-06-08 20:03
 */
public final class JSR250PreDestroyLifecycleFeature extends OneAnnotationLifecycleFeature implements PreDestroyLifecycleFeature {

    @Override
    public PreDestroyTypeVisitor visitor() {
        return new PreDestroyTypeVisitor(this.annotationClazz);
    }

    private class PreDestroyTypeVisitor extends AbstractTypeVisitor {
        private final Logger LOGGER = LoggerFactory.getLogger(PreDestroyTypeVisitor.class);

        public PreDestroyTypeVisitor(Class annotationClazz) {
            super(annotationClazz);
        }

        @Override
        public void addMethodLifecycleAction(LifecycleAction lifecycleAction) {
            addLifecycleActionToLastOne(lifecycleAction);
        }

        @Override
        public boolean visit(final Class clazz) {
            boolean continueVisit = !clazz.isInterface();
            if (continueVisit && AutoCloseable.class.isAssignableFrom(clazz)) {
                AutoCloseableLifecycleAction closeableAction = new AutoCloseableLifecycleAction(
                        clazz.asSubclass(AutoCloseable.class));
                LOGGER.debug("adding action {}", closeableAction);
                addLifecycleActionToLastOne(closeableAction);
                continueVisit = false;
            }
            return continueVisit;
        }
    }


    @Override
    public Class annotationClazz() {
        return PreDestroy.class;
    }

    @Override
    public int priority() {
        return 3;
    }

    @Override
    public String toString() {
        return new StringBuilder().append("Predestroy @").append(this.annotationClazz==null ? "null" : this.annotationClazz.getSimpleName())
                .append(" with priority ").append(priority()).toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy