Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/share/options/BasicOptionObjectFactory.java
41155 views
/*1* Copyright (c) 2008, 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*/22/*23* To change this template, choose Tools | Templates24* and open the template in the editor.25*/2627package vm.share.options;2829import java.util.HashMap;30import java.util.Map;31import nsk.share.TestBug;3233/**34* This class allows user to create an {@link ObjectFactory} implementation35* via @{@link vm.share.options.Factory} annotation. See the source code of36* {@link vm.share.options.test.BasicObjectFactoryUsageExample} and37* {@link vm.share.options.test.ExampleWithNonprimitiveOptions}.38* @see Factory39* @see FClass40* @see ObjectFactory41*/42public class BasicOptionObjectFactory<T> implements OptionObjectFactory<T>43{44public String getPlaceholder()45{46return getAnnotaion().placeholder_text();47}4849public String[] getPossibleValues()50{51return getTypesMap().keySet().toArray(new String[0]);52}5354public String getDescription()55{56return getAnnotaion().description().equals(Factory.defDescription)? null : getAnnotaion().description();57}5859public String getDefaultValue()60{61return getAnnotaion().default_value().equals(Factory.defDefaultValue)? null :62getAnnotaion().default_value();63}6465public String getParameterDescription(String key)66{67return getTypesMap().get(key).description();68}697071// shouldn't value be named key?7273public T getObject(String value)74{75try76{77@SuppressWarnings(value="unchecked")78T result = (T) getTypesMap().get(value).type().newInstance();79return result;80} catch (InstantiationException ex)81{82throw new TestBug("Error while trying to instantiate via " + this.getClass() + " for key " + value, ex);83} catch (IllegalAccessException ex)84{85throw new TestBug("Error while trying to instantiate via " + this.getClass() + " for key " + value, ex);86}87}8889protected Factory getAnnotaion()90{91if(!this.getClass().isAnnotationPresent(Factory.class))92throw new TestBug(" Found an unnotated BasicObjectFactory subclass.");93Factory factoryAnn = this.getClass().getAnnotation(Factory.class);94return factoryAnn;95}9697protected Map<String, FClass> getTypesMap()98{ // probably there could be some lazy initialization, but I decided not to deal with that.99FClass[] types = getAnnotaion().classlist();100Map<String, FClass> typesMap = new HashMap<String, FClass>(types.length);101for (FClass type : types)102{103typesMap.put(type.key(), type);104}105return typesMap;106107}108109// see ExampleWithNonprimitiveOptions instead.110// public void test()111// {112// if(!this.getClass().isAnnotationPresent(Factory.class))113// throw new RuntimeException(" Found an unnotated BasicObjectFactory subclass.");114// Factory factoryAnn = this.getClass().getAnnotation(Factory.class);115// System.out.println(" placeholder_text " + factoryAnn.placeholder_text());116// System.out.println(" default_value " +117// (factoryAnn.default_value().equals(Factory.def_default_value) ? null : factoryAnn.default_value()) );118//119// }120//121// @Factory(placeholder_text="number", default_value="test",122// classlist = {123// @FClass( key="int", description="integer", type=int.class),124// @FClass( key="boolean", description="boolean", type=boolean.class)125// } )126// public static class testOF extends BasicObjectFactory<BasicObjectFactory> {}127//128//// @Factory(placeholder_text="placehldr1")129//// public static class testOF1 extends BasicObjectFactory<BasicObjectFactory> {}130//131//132// public static void main(String[] args)133// {134// BasicObjectFactory bof = new testOF();135// bof.test();136//// new testOF1().test();137//138// }139140}141142143