InstantiatingClassBasedListenerObjectCreator.java

/*
 * Copyright (C) 2012-2024 RRiBbit.org
 *
 * 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
 *
 * https://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.rribbit.creation;

import org.rribbit.Listener;
import org.rribbit.ListenerObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.util.Collection;

/**
 * This {@link ListenerObjectCreator} creates {@link ListenerObject}s from classes. Users can pass in {@link Class}es or packagenames and this class will scan the {@link Class}es
 * and create {@link ListenerObject}s for the public methods that are annotated with {@link Listener}. Note that public methods inherited from superclasses and superinterfaces will also be
 * scanned. This means that users must take care not to scan a method twice, once as a method of a class and once as a method of a superclass, by passing a class/interface and its
 * superclass/superinterface separately to this {@link ListenerObjectCreator}.
 * <p />
 * Please note that in Java, method annotations are NOT inherited. This means that, if you override/implement a method in a subclass or subinterface, and the overriding/implementing method
 * does not have the annotation, then that method will not inherit it. If a class or interface just inherits a method, without overriding it, then the annotation WILL exist.
 * <p />
 * To get the actual {@link Object}s that the listeners run on, this class simply attempts to instantiate an {@link Object} for each {@link Class} that it scans.
 * When there is no default constructor for a {@link Class}, the {@link Class} will simply be ignored.
 *
 * @author G.J. Schouten
 *
 */
public class InstantiatingClassBasedListenerObjectCreator extends AbstractClassBasedListenerObjectCreator {

	private static final Logger log = LoggerFactory.getLogger(InstantiatingClassBasedListenerObjectCreator.class);

	/**
	 * @param classes
	 *
	 * @see AbstractClassBasedListenerObjectCreator#AbstractClassBasedListenerObjectCreator(Class...)
	 */
	public InstantiatingClassBasedListenerObjectCreator(Class<?>... classes) {
		super(classes);
	}

	/**
	 * @param excludedClasses
	 * @param scanSubPackages
	 * @param packageNames
	 *
	 * @see AbstractClassBasedListenerObjectCreator#AbstractClassBasedListenerObjectCreator(Collection, boolean, String...)
	 */
	public InstantiatingClassBasedListenerObjectCreator(Collection<Class<?>> excludedClasses, boolean scanSubPackages, String... packageNames) {
		super(excludedClasses, scanSubPackages, packageNames);
	}

	@Override
	protected Object getTargetObjectForClass(Class<?> clazz) {

		try {
			log.debug("Attempting to construct instance of class '{}'", clazz.getName());
			return clazz.getConstructor().newInstance();
		} catch(InvocationTargetException e) {
			throw new RuntimeException("Exception thrown from constructor of Listener", e.getCause());
		} catch(Exception e) {
			log.debug("No suitable constructor found, returning null");
			return null;
		}
	}
}