OptionDescriptor.java revision 12651:6ef01bd40ce2
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.options;
24
25/**
26 * Describes the attributes of a static field {@linkplain Option option} and provides access to its
27 * {@linkplain OptionValue value}.
28 */
29public final class OptionDescriptor {
30
31    protected final String name;
32    protected final Class<?> type;
33    protected final String help;
34    protected final OptionValue<?> option;
35    protected final Class<?> declaringClass;
36    protected final String fieldName;
37
38    public static OptionDescriptor create(String name, Class<?> type, String help, Class<?> declaringClass, String fieldName, OptionValue<?> option) {
39        OptionDescriptor result = option.getDescriptor();
40        if (result == null) {
41            result = new OptionDescriptor(name, type, help, declaringClass, fieldName, option);
42            option.setDescriptor(result);
43        }
44        assert result.name.equals(name) && result.type == type && result.declaringClass == declaringClass && result.fieldName.equals(fieldName) && result.option == option;
45        return result;
46    }
47
48    private OptionDescriptor(String name, Class<?> type, String help, Class<?> declaringClass, String fieldName, OptionValue<?> option) {
49        this.name = name;
50        this.type = type;
51        this.help = help;
52        this.option = option;
53        this.declaringClass = declaringClass;
54        this.fieldName = fieldName;
55        assert !type.isPrimitive() : "must used boxed type instead of " + type;
56    }
57
58    /**
59     * Gets the type of values stored in the option. This will be the boxed type for a primitive
60     * option.
61     */
62    public Class<?> getType() {
63        return type;
64    }
65
66    /**
67     * Gets a descriptive help message for the option.
68     */
69    public String getHelp() {
70        return help;
71    }
72
73    /**
74     * Gets the name of the option. It's up to the client of this object how to use the name to get
75     * a user specified value for the option from the environment.
76     */
77    public String getName() {
78        return name;
79    }
80
81    /**
82     * Gets the boxed option value.
83     */
84    public OptionValue<?> getOptionValue() {
85        return option;
86    }
87
88    public Class<?> getDeclaringClass() {
89        return declaringClass;
90    }
91
92    public String getFieldName() {
93        return fieldName;
94    }
95
96    /**
97     * Gets a description of the location where this option is stored.
98     */
99    public String getLocation() {
100        return getDeclaringClass().getName() + "." + getFieldName();
101    }
102}
103