Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/share/options/OptionDefinition.java
41155 views
/*1* Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package vm.share.options;2324import java.util.Map;25import java.util.HashMap;26import java.lang.reflect.Field;27import nsk.share.TestBug;2829/**30* This is a "value" class for holding information about the defined options.31*/32public final class OptionDefinition {33private static final Map<String, OptionObjectFactory> factories = new HashMap<String, OptionObjectFactory>();34private String prefix;35private String name;36private String description;37private String defaultValue;38private Class<? extends OptionObjectFactory> factory;39private Field field;40private Object owner;4142public OptionDefinition(String prefix, String name, String description, String defaultValue, Class<? extends OptionObjectFactory> factory, Field field, Object owner) {43this.prefix = prefix;44this.name = name;45this.description = description;46this.defaultValue = defaultValue;47this.factory = factory;48this.field = field;49this.owner = owner;50}5152public String getPrefix() {53return prefix;54}5556public String getName() {57return name;58}5960public String getFullName() {61if (prefix != null)62return prefix + "." + name;63else64return name;65}6667public Field getField() {68return field;69}7071public Object getOwner() {72return owner;73}7475public String getDescription() {76if (hasFactory())77if (description == null)78return getFactory().getDescription();79return description;80}8182public String getDefaultValue() {83if (hasFactory())84if (defaultValue == null)85return getFactory().getDefaultValue();86return defaultValue;87}8889public boolean hasFactory() {90return factory != null;91}9293public String getPlaceHolder() {94if (hasFactory())95return getFactory().getPlaceholder();96else97return getField().getType().toString();98}99100public synchronized OptionObjectFactory getFactory() {101if (factory == null)102throw new TestBug("Called getFactory() on OptionDefinition with unset factory");103OptionObjectFactory factory = factories.get(this.factory);104if (factory == null) {105try {106factory = this.factory.newInstance();107} catch (InstantiationException ex) {108throw new TestBug("Failed to instantiate factory " + this.factory, ex);109} catch (IllegalAccessException ex) {110throw new TestBug("Failed to instantiate factory " + this.factory, ex);111}112}113return factory;114}115116public String toString() {117return "Option " + name + " field " + field + " object " + owner;118}119}120121122