aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/utils/internal/ReflectionUtil.java
blob: 5d3274f0461787f25573be37042460523d6c8cf5 (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.utils.internal;

import com.google.common.reflect.TypeToken;
import com.yahoo.config.ChangesRequiringRestart;
import com.yahoo.config.ConfigInstance;
import com.yahoo.vespa.config.ConfigKey;
import com.yahoo.vespa.model.ConfigProducer;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Utility class containing static methods for retrieving information about the config producer tree.
 *
 * @author Ulf Lilleengen
 * @author bjorncs
 * @author gjoranv
 */
public final class ReflectionUtil {

    private ReflectionUtil() {}

    public static Set<ConfigKey<?>> getAllConfigsProduced(Class<? extends ConfigProducer> producerClass, String configId) {
        // TypeToken is @Beta in guava, so consider implementing a simple recursive method instead.
        TypeToken<? extends ConfigProducer>.TypeSet interfaces = TypeToken.of(producerClass).getTypes().interfaces();
        return interfaces.rawTypes().stream()
                .filter(ReflectionUtil::isConcreteProducer)
                .map(i -> createConfigKeyFromInstance(i.getEnclosingClass(), configId))
                .collect(Collectors.toCollection(() -> new LinkedHashSet<>()));
    }

    /**
     * Determines if the config class contains the methods required for detecting config value changes
     * between two config instances.
     */
    public static boolean hasRestartMethods(Class<? extends ConfigInstance> configClass) {
        try {
            configClass.getDeclaredMethod("containsFieldsFlaggedWithRestart");
            configClass.getDeclaredMethod("getChangesRequiringRestart", configClass);
            return true;
        } catch (NoSuchMethodException e) {
            return false;
        }
    }

    /**
     * Determines if the config definition for the given config class contains key-values flagged with restart.
     */
    public static boolean containsFieldsFlaggedWithRestart(Class<? extends ConfigInstance> configClass)  {
        try {
            Method m = configClass.getDeclaredMethod("containsFieldsFlaggedWithRestart");
            m.setAccessible(true);
            return (boolean) m.invoke(null);
        } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Compares the config instances and lists any differences that will require service restart.
     *
     * @param from The previous config.
     * @param to The new config.
     * @return An object describing the difference.
     */
    public static ChangesRequiringRestart getChangesRequiringRestart(ConfigInstance from, ConfigInstance to) {
        Class<?> clazz = from.getClass();
        if (!clazz.equals(to.getClass())) {
            throw new IllegalArgumentException(String.format("%s != %s", clazz, to.getClass()));
        }
        try {
            Method m = clazz.getDeclaredMethod("getChangesRequiringRestart", clazz);
            m.setAccessible(true);
            return (ChangesRequiringRestart) m.invoke(from, to);
        }  catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static List<Class<?>> getAllSuperclasses(Class<?> cls) {
        var result = new ArrayList<Class<?>>();
        for(Class<?> superClass = cls.getSuperclass(); superClass != null; superClass = superClass.getSuperclass()) {
            result.add(superClass);
        }
        return result;
    }

    private static ConfigKey<?> createConfigKeyFromInstance(Class<?> configInstClass, String configId) {
        String defName = ConfigInstance.getDefName(configInstClass);
        String defNamespace = ConfigInstance.getDefNamespace(configInstClass);
        return new ConfigKey<>(defName, configId, defNamespace);
    }

    private static boolean isConcreteProducer(Class<?> producerInterface) {
        if (isRootConfigProducerInterface(producerInterface))  return false;

        boolean parentIsConfigInstance = false;
        for (Class<?> ifaceParent : producerInterface.getInterfaces()) {
            if (isConfigInstanceProducer(ifaceParent)) {
                parentIsConfigInstance = true;
            }
        }
        return (ConfigInstance.Producer.class.isAssignableFrom(producerInterface)
                && parentIsConfigInstance
                && !isConfigInstanceProducer(producerInterface));
    }

    private static boolean isConfigInstanceProducer(Class<?> clazz) {
        return clazz.getName().equals(ConfigInstance.Producer.class.getName());
    }

    private static boolean isRootConfigProducerInterface(Class<?> clazz) {
        return clazz.getCanonicalName().equals(ConfigProducer.class.getCanonicalName());
    }

}