Jesse Wilson | 109c128 | 2009-12-08 13:45:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.google.caliper; |
| 18 | |
Jesse Wilson | f062bf4 | 2010-01-13 17:12:18 -0800 | [diff] [blame^] | 19 | import java.lang.reflect.Field; |
| 20 | import java.lang.reflect.Member; |
| 21 | import java.lang.reflect.Method; |
| 22 | import java.lang.reflect.Modifier; |
| 23 | import java.lang.reflect.ParameterizedType; |
| 24 | import java.lang.reflect.Type; |
| 25 | import java.util.Arrays; |
| 26 | import java.util.Collection; |
| 27 | import java.util.Collections; |
| 28 | import java.util.EnumSet; |
| 29 | import java.util.Map; |
| 30 | import java.util.Set; |
| 31 | import java.util.TreeMap; |
Jesse Wilson | 109c128 | 2009-12-08 13:45:25 -0800 | [diff] [blame] | 32 | |
| 33 | /** |
Jesse Wilson | 1440b36 | 2009-12-15 18:54:02 -0800 | [diff] [blame] | 34 | * A parameter in a {@link SimpleBenchmark}. |
Jesse Wilson | 109c128 | 2009-12-08 13:45:25 -0800 | [diff] [blame] | 35 | */ |
| 36 | abstract class Parameter<T> { |
| 37 | |
| 38 | private final Field field; |
| 39 | |
| 40 | private Parameter(Field field) { |
| 41 | this.field = field; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Returns all properties for the given class. |
| 46 | */ |
Jesse Wilson | 1440b36 | 2009-12-15 18:54:02 -0800 | [diff] [blame] | 47 | public static Map<String, Parameter<?>> forClass(Class<? extends Benchmark> suiteClass) { |
Jesse Wilson | 109c128 | 2009-12-08 13:45:25 -0800 | [diff] [blame] | 48 | Map<String, Parameter<?>> parameters = new TreeMap<String, Parameter<?>>(); |
Jesse Wilson | f062bf4 | 2010-01-13 17:12:18 -0800 | [diff] [blame^] | 49 | for (Field field : suiteClass.getDeclaredFields()) { |
Jesse Wilson | 109c128 | 2009-12-08 13:45:25 -0800 | [diff] [blame] | 50 | if (field.isAnnotationPresent(Param.class)) { |
| 51 | field.setAccessible(true); |
Jesse Wilson | f062bf4 | 2010-01-13 17:12:18 -0800 | [diff] [blame^] | 52 | Parameter<?> parameter = forField(suiteClass, field); |
Jesse Wilson | 109c128 | 2009-12-08 13:45:25 -0800 | [diff] [blame] | 53 | parameters.put(parameter.getName(), parameter); |
| 54 | } |
| 55 | } |
| 56 | return parameters; |
| 57 | } |
| 58 | |
Jesse Wilson | f062bf4 | 2010-01-13 17:12:18 -0800 | [diff] [blame^] | 59 | private static Parameter<?> forField( |
Jesse Wilson | 1440b36 | 2009-12-15 18:54:02 -0800 | [diff] [blame] | 60 | Class<? extends Benchmark> suiteClass, final Field field) { |
Jesse Wilson | f062bf4 | 2010-01-13 17:12:18 -0800 | [diff] [blame^] | 61 | // First check for String values on the annotation itself |
| 62 | final Object[] defaults = field.getAnnotation(Param.class).value(); |
| 63 | if (defaults.length > 0) { |
| 64 | return new Parameter<Object>(field) { |
| 65 | @Override public Collection<Object> values() throws Exception { |
| 66 | return Arrays.asList(defaults); |
| 67 | } |
| 68 | }; |
| 69 | // TODO: or should we continue so we can give an error/warning if params are also give in a |
| 70 | // method or field? |
| 71 | } |
| 72 | |
| 73 | Parameter<?> result = null; |
Jesse Wilson | 109c128 | 2009-12-08 13:45:25 -0800 | [diff] [blame] | 74 | Type returnType = null; |
| 75 | Member member = null; |
| 76 | |
Jesse Wilson | f062bf4 | 2010-01-13 17:12:18 -0800 | [diff] [blame^] | 77 | // Now check for a fooValues() method |
Jesse Wilson | 109c128 | 2009-12-08 13:45:25 -0800 | [diff] [blame] | 78 | try { |
| 79 | final Method valuesMethod = suiteClass.getDeclaredMethod(field.getName() + "Values"); |
| 80 | valuesMethod.setAccessible(true); |
| 81 | member = valuesMethod; |
| 82 | returnType = valuesMethod.getGenericReturnType(); |
| 83 | result = new Parameter<Object>(field) { |
| 84 | @SuppressWarnings("unchecked") // guarded below |
Jesse Wilson | f062bf4 | 2010-01-13 17:12:18 -0800 | [diff] [blame^] | 85 | @Override public Collection<Object> values() throws Exception { |
Jesse Wilson | 109c128 | 2009-12-08 13:45:25 -0800 | [diff] [blame] | 86 | return (Collection<Object>) valuesMethod.invoke(null); |
| 87 | } |
| 88 | }; |
| 89 | } catch (NoSuchMethodException ignored) { |
| 90 | } |
| 91 | |
Jesse Wilson | f062bf4 | 2010-01-13 17:12:18 -0800 | [diff] [blame^] | 92 | // Now check for a fooValues field |
Jesse Wilson | 109c128 | 2009-12-08 13:45:25 -0800 | [diff] [blame] | 93 | try { |
| 94 | final Field valuesField = suiteClass.getDeclaredField(field.getName() + "Values"); |
| 95 | valuesField.setAccessible(true); |
| 96 | member = valuesField; |
| 97 | if (result != null) { |
| 98 | throw new ConfigurationException("Two values members defined for " + field); |
| 99 | } |
| 100 | returnType = valuesField.getGenericType(); |
| 101 | result = new Parameter<Object>(field) { |
| 102 | @SuppressWarnings("unchecked") // guarded below |
Jesse Wilson | f062bf4 | 2010-01-13 17:12:18 -0800 | [diff] [blame^] | 103 | @Override public Collection<Object> values() throws Exception { |
Jesse Wilson | 109c128 | 2009-12-08 13:45:25 -0800 | [diff] [blame] | 104 | return (Collection<Object>) valuesField.get(null); |
| 105 | } |
| 106 | }; |
| 107 | } catch (NoSuchFieldException ignored) { |
| 108 | } |
| 109 | |
Jesse Wilson | f062bf4 | 2010-01-13 17:12:18 -0800 | [diff] [blame^] | 110 | if (member != null && !Modifier.isStatic(member.getModifiers())) { |
| 111 | throw new ConfigurationException("Values member must be static " + member); |
| 112 | } |
| 113 | |
| 114 | // If there isn't a values member but the parameter is an enum, we default |
| 115 | // to EnumSet.allOf. |
| 116 | if (member == null && field.getType().isEnum()) { |
| 117 | returnType = Collection.class; |
| 118 | result = new Parameter<Object>(field) { |
| 119 | // TODO: figure out the simplest way to make this compile and be green in IDEA too |
| 120 | @SuppressWarnings({"unchecked", "RawUseOfParameterizedType", "RedundantCast"}) |
| 121 | // guarded above |
| 122 | @Override public Collection<Object> values() throws Exception { |
| 123 | Set<Enum> set = EnumSet.allOf((Class<Enum>) field.getType()); |
| 124 | return (Collection) set; |
| 125 | } |
| 126 | }; |
| 127 | } |
| 128 | |
Jesse Wilson | 109c128 | 2009-12-08 13:45:25 -0800 | [diff] [blame] | 129 | if (result == null) { |
Jesse Wilson | f062bf4 | 2010-01-13 17:12:18 -0800 | [diff] [blame^] | 130 | return new Parameter<Object>(field) { |
| 131 | @Override public Collection<Object> values() { |
| 132 | // TODO: need tests to make sure this fails properly when no cmdline params given and |
| 133 | // works properly when they are given |
| 134 | return Collections.emptySet(); |
| 135 | } |
| 136 | }; |
| 137 | } else if (!isValidReturnType(returnType)) { |
Jesse Wilson | 109c128 | 2009-12-08 13:45:25 -0800 | [diff] [blame] | 138 | throw new ConfigurationException("Invalid return type " + returnType |
| 139 | + " for values member " + member + "; must be Collection"); |
| 140 | } |
Jesse Wilson | 109c128 | 2009-12-08 13:45:25 -0800 | [diff] [blame] | 141 | return result; |
| 142 | } |
| 143 | |
Jesse Wilson | f062bf4 | 2010-01-13 17:12:18 -0800 | [diff] [blame^] | 144 | private static boolean isValidReturnType(Type returnType) { |
| 145 | if (returnType == Collection.class) { |
| 146 | return true; |
| 147 | } |
| 148 | if (returnType instanceof ParameterizedType) { |
| 149 | ParameterizedType type = (ParameterizedType) returnType; |
| 150 | if (type.getRawType() == Collection.class) { |
| 151 | return true; |
| 152 | } |
| 153 | } |
| 154 | return false; |
| 155 | } |
| 156 | |
Jesse Wilson | 109c128 | 2009-12-08 13:45:25 -0800 | [diff] [blame] | 157 | /** |
| 158 | * Sets the value of this property to the specified value for the given suite. |
| 159 | */ |
Jesse Wilson | 1440b36 | 2009-12-15 18:54:02 -0800 | [diff] [blame] | 160 | public void set(Benchmark suite, Object value) throws Exception { |
Jesse Wilson | 109c128 | 2009-12-08 13:45:25 -0800 | [diff] [blame] | 161 | field.set(suite, value); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Returns the available values of the property as specified by the suite. |
| 166 | */ |
| 167 | public abstract Collection<T> values() throws Exception; |
| 168 | |
| 169 | /** |
| 170 | * Returns the parameter's type, such as double.class. |
| 171 | */ |
| 172 | public Type getType() { |
| 173 | return field.getGenericType(); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Returns the field's name. |
| 178 | */ |
Jesse Wilson | f062bf4 | 2010-01-13 17:12:18 -0800 | [diff] [blame^] | 179 | String getName() { |
Jesse Wilson | 109c128 | 2009-12-08 13:45:25 -0800 | [diff] [blame] | 180 | return field.getName(); |
| 181 | } |
Jesse Wilson | f062bf4 | 2010-01-13 17:12:18 -0800 | [diff] [blame^] | 182 | } |