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

org.jboss.weld.interceptor.builder.InterceptionModelImpl Maven / Gradle / Ivy

There is a newer version: 3.0.0.Alpha1
Show newest version
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
 * contributors by the @authors tag. See the copyright.txt in the
 * distribution for a full listing of individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jboss.weld.interceptor.builder;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.jboss.weld.interceptor.reader.TargetClassInterceptorMetadata;
import org.jboss.weld.interceptor.spi.metadata.InterceptorClassMetadata;
import org.jboss.weld.interceptor.spi.model.InterceptionModel;
import org.jboss.weld.interceptor.spi.model.InterceptionType;
import org.jboss.weld.util.collections.ImmutableMap;
import org.jboss.weld.util.collections.ImmutableSet;

/**
 * This impl is immutable provided the type of the intercepted entity is immutable as well.
 *
 * @author Marius Bogoevici
 * @author Marko Luksa
 * @author Martin Kouba
 *
 * @param  the type of the intercepted entity
 */
class InterceptionModelImpl implements InterceptionModel {

    private final Map>> globalInterceptors;

    private final Map>>> methodBoundInterceptors;

    private final Set methodsIgnoringGlobalInterceptors;

    private final Set> allInterceptors;

    private final boolean hasExternalNonConstructorInterceptors;

    private final TargetClassInterceptorMetadata targetClassInterceptorMetadata;

    InterceptionModelImpl(InterceptionModelBuilder builder) {
        this.hasExternalNonConstructorInterceptors = builder.isHasExternalNonConstructorInterceptors();
        this.globalInterceptors = ImmutableMap.>>copyOf(builder.getGlobalInterceptors());
        this.methodBoundInterceptors = ImmutableMap.>>>copyOf(builder.getMethodBoundInterceptors());
        this.methodsIgnoringGlobalInterceptors = ImmutableSet.copyOf(builder.getMethodsIgnoringGlobalInterceptors());
        this.allInterceptors = ImmutableSet.>copyOf(builder.getAllInterceptors());
        this.targetClassInterceptorMetadata = builder.getTargetClassInterceptorMetadata();
    }

    @Override
    public List> getInterceptors(InterceptionType interceptionType, Method method) {
        if (InterceptionType.AROUND_CONSTRUCT.equals(interceptionType)) {
            throw new IllegalStateException("Cannot use getInterceptors() for @AroundConstruct interceptor lookup. Use getConstructorInvocationInterceptors() instead.");
        }
        if (interceptionType.isLifecycleCallback() && method != null) {
            throw new IllegalArgumentException("On a lifecycle callback, the associated method must be null");
        }

        if (!interceptionType.isLifecycleCallback() && method == null) {
            throw new IllegalArgumentException("Around-invoke and around-timeout interceptors are defined for a given method");
        }

        if (interceptionType.isLifecycleCallback()) {
            if (globalInterceptors.containsKey(interceptionType)) {
                return globalInterceptors.get(interceptionType);
            }
        } else {
            ArrayList> returnedInterceptors = new ArrayList>();
            if (!methodsIgnoringGlobalInterceptors.contains(method) && globalInterceptors.containsKey(interceptionType)) {
                returnedInterceptors.addAll(globalInterceptors.get(interceptionType));
            }
            Map>> map = methodBoundInterceptors.get(interceptionType);
            if (map != null) {
                List> list = map.get(method);
                if (list != null) {
                    returnedInterceptors.addAll(list);
                }
            }
            return returnedInterceptors;
        }
        return Collections.emptyList();
    }

    @Override
    public Set> getAllInterceptors() {
        return Collections.unmodifiableSet(allInterceptors);
    }

    @Override
    public List> getConstructorInvocationInterceptors() {
        if (globalInterceptors.containsKey(InterceptionType.AROUND_CONSTRUCT)) {
            return globalInterceptors.get(InterceptionType.AROUND_CONSTRUCT);
        }
        return Collections.emptyList();
    }

    @Override
    public boolean hasExternalConstructorInterceptors() {
        return !getConstructorInvocationInterceptors().isEmpty();
    }

    @Override
    public boolean hasExternalNonConstructorInterceptors() {
        return hasExternalNonConstructorInterceptors;
    }

    @Override
    public boolean hasTargetClassInterceptors() {
        return targetClassInterceptorMetadata != null && targetClassInterceptorMetadata != TargetClassInterceptorMetadata.EMPTY_INSTANCE;
    }

    @Override
    public TargetClassInterceptorMetadata getTargetClassInterceptorMetadata() {
        return targetClassInterceptorMetadata;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy