1/*
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package org.graalvm.compiler.core.test;
24
25import java.lang.annotation.RetentionPolicy;
26import java.lang.reflect.Field;
27import java.lang.reflect.Modifier;
28import java.lang.reflect.ParameterizedType;
29import java.lang.reflect.Type;
30import java.util.Iterator;
31import java.util.Map;
32import java.util.Properties;
33
34import org.graalvm.compiler.options.Option;
35import org.graalvm.compiler.options.OptionDescriptor;
36import org.graalvm.compiler.options.OptionDescriptors;
37import org.graalvm.compiler.options.OptionKey;
38import org.graalvm.compiler.options.OptionValues;
39import org.graalvm.util.EconomicMap;
40import org.graalvm.util.MapCursor;
41
42/**
43 * An implementation of {@link OptionDescriptor} that uses reflection to create descriptors from a
44 * list of field name and help text pairs. We cannot use the {@link Option} annotation as it has a
45 * {@link RetentionPolicy#SOURCE} retention policy.
46 *
47 * This class is useful for working with {@link OptionKey} and {@link OptionValues} but without
48 * having to rely on {@link Option} and its associated annotation processor.
49 */
50public class ReflectionOptionDescriptors implements OptionDescriptors {
51
52    /**
53     * Extracts name/value entries from a set of properties based on a given name prefix.
54     *
55     * @param properties the properties set to extract from
56     * @param prefix entries whose names start with this prefix are extracted
57     * @param stripPrefix specifies whether to remove the prefix from the names in the returned map
58     */
59    public static EconomicMap<String, String> extractEntries(Properties properties, String prefix, boolean stripPrefix) {
60        EconomicMap<String, String> matches = EconomicMap.create();
61        for (Map.Entry<Object, Object> e : properties.entrySet()) {
62            String name = (String) e.getKey();
63            if (name.startsWith(prefix)) {
64                String value = (String) e.getValue();
65                if (stripPrefix) {
66                    name = name.substring(prefix.length());
67                }
68                matches.put(name, value);
69            }
70        }
71        return matches;
72    }
73
74    private final EconomicMap<String, OptionDescriptor> descriptors = EconomicMap.create();
75
76    public ReflectionOptionDescriptors(Class<?> declaringClass, String... fieldsAndHelp) {
77        assert fieldsAndHelp.length % 2 == 0;
78        for (int i = 0; i < fieldsAndHelp.length; i += 2) {
79            String fieldName = fieldsAndHelp[i];
80            String help = fieldsAndHelp[i + 1];
81            addOption(declaringClass, fieldName, help);
82        }
83    }
84
85    public ReflectionOptionDescriptors(Class<?> declaringClass, EconomicMap<String, String> fieldsAndHelp) {
86        MapCursor<String, String> cursor = fieldsAndHelp.getEntries();
87        while (cursor.advance()) {
88            String fieldName = cursor.getKey();
89            String help = cursor.getValue();
90            addOption(declaringClass, fieldName, help);
91        }
92    }
93
94    private void addOption(Class<?> declaringClass, String fieldName, String help) {
95        try {
96            Field f = declaringClass.getDeclaredField(fieldName);
97            if (!OptionKey.class.isAssignableFrom(f.getType())) {
98                throw new IllegalArgumentException(String.format("Option field must be of type %s: %s", OptionKey.class.getName(), f));
99            }
100            if (!Modifier.isStatic(f.getModifiers())) {
101                throw new IllegalArgumentException(String.format("Option field must be static: %s", f));
102            }
103            f.setAccessible(true);
104            Type declaredType = f.getAnnotatedType().getType();
105            if (!(declaredType instanceof ParameterizedType)) {
106                throw new IllegalArgumentException(String.format("Option field must have a parameterized type: %s", f));
107            }
108            ParameterizedType pt = (ParameterizedType) declaredType;
109            Type[] actualTypeArguments = pt.getActualTypeArguments();
110            assert actualTypeArguments.length == 1;
111            Class<?> optionType = (Class<?>) actualTypeArguments[0];
112            descriptors.put(fieldName, OptionDescriptor.create(fieldName, optionType, help, declaringClass, fieldName, (OptionKey<?>) f.get(null)));
113        } catch (IllegalAccessException | NoSuchFieldException e) {
114            throw new IllegalArgumentException(e);
115        }
116    }
117
118    @Override
119    public Iterator<OptionDescriptor> iterator() {
120        return descriptors.getValues().iterator();
121    }
122
123    @Override
124    public OptionDescriptor get(String value) {
125        return descriptors.get(value);
126    }
127}
128