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

de.quantummaid.httpmaid.guice.factories.SinglePublicConstructorModule Maven / Gradle / Ivy

/*
 * Copyright (c) 2020 Richard Hauswald - https://quantummaid.de/.
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 de.quantummaid.httpmaid.guice.factories;

import com.google.inject.AbstractModule;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

import java.lang.reflect.Constructor;
import java.util.List;

import static de.quantummaid.httpmaid.guice.UnclearHowToInstantiateException.unclearHowToInstantiateException;
import static java.lang.String.format;
import static java.lang.reflect.Modifier.isPublic;
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

@ToString
@EqualsAndHashCode(callSuper = true)
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
final class SinglePublicConstructorModule extends AbstractModule {
    private final Class type;
    private final Constructor constructor;

    static  AbstractModule singlePublicConstructorModule(final Class type) {
        final List> constructors = publicConstructors(type);
        if (constructors.size() == 1) {
            final Constructor constructor = constructors.get(0);
            return new SinglePublicConstructorModule<>(type, constructor);
        } else {
            final String constructorsString = constructors.stream()
                    .map(Constructor::toGenericString)
                    .collect(joining(", ", "[", "]"));
            throw unclearHowToInstantiateException(format(
                    "Can only bind classes that have exactly one public constructor. Class '%s' has the following constructors: %s",
                    type.getName(), constructorsString));
        }
    }

    @Override
    protected void configure() {
        bind(type).toConstructor(constructor);
    }

    @SuppressWarnings("unchecked")
    private static  List> publicConstructors(final Class type) {
        final Constructor[] constructors = (Constructor[]) type.getConstructors();
        return stream(constructors)
                .filter(constructor -> isPublic(constructor.getModifiers()))
                .collect(toList());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy