aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/builder/xml/dom/chains/DomBuilderCreator.java
blob: b62c1a01fa3e3af5e51f2e26adfa40aa07745801 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.builder.xml.dom.chains;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * Utility class for instantiating a builder using reflection.
 *
 * @author Tony Vaagenes
 */
public class DomBuilderCreator {

    public static <T> T create(Class<T> builderClass, Object... parameters) {
        try {
            return getConstructor(builderClass).newInstance(parameters);
        } catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @SuppressWarnings("unchecked")
    private static <T> Constructor<T> getConstructor(Class<T> builderClass) {
        Constructor<?>[] constructors = builderClass.getConstructors();
        assert(constructors.length == 1);
        return (Constructor<T>) constructors[0];
    }

}