1 package com.lexicalscope.fluentreflection; 2 3 import java.lang.reflect.Type; 4 import java.util.List; 5 6 import org.hamcrest.Matcher; 7 8 /* 9 * Copyright 2011 Tim Wood 10 * 11 * Licensed under the Apache License, Version 2.0 (the "License"); 12 * you may not use this file except in compliance with the License. 13 * You may obtain a copy of the License at 14 * 15 * http://www.apache.org/licenses/LICENSE-2.0 16 * 17 * Unless required by applicable law or agreed to in writing, software 18 * distributed under the License is distributed on an "AS IS" BASIS, 19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 * See the License for the specific language governing permissions and 21 * limitations under the License. 22 */ 23 24 /** 25 * Reflection access to a class or an object. 26 * 27 * @author tim 28 * 29 * @param <T> the underlying type being reflected on 30 */ 31 public interface FluentAccess<T> extends FluentAnnotated { 32 /** 33 * Obtain the class being reflected 34 * 35 * @return the class being reflected 36 */ 37 Class<T> classUnderReflection(); 38 39 /** 40 * All interfaces implemented by this type 41 * 42 * @return all the interfaces 43 */ 44 List<FluentClass<?>> interfaces(); 45 46 /** 47 * Return the list of all superclasses with the immediate parent first 48 * 49 * @return list of superclasses nearest first 50 */ 51 List<FluentClass<?>> superclasses(); 52 53 /** 54 * Does this type or any of its implemented types match the given matcher? 55 * 56 * @param typeMatcher 57 * matcher on the required type 58 * 59 * @return does the type or any of its supertypes match the given matcher 60 */ 61 boolean isType(ReflectionMatcher<FluentClass<?>> typeMatcher); 62 63 /** 64 * All methods 65 * 66 * @return all the methods 67 */ 68 List<FluentMethod> methods(); 69 70 /** 71 * All methods declared by this type 72 * 73 * @return methods declared by this type 74 */ 75 List<FluentMethod> declaredMethods(); 76 77 /** 78 * Find all methods matching the supplied matcher 79 * 80 * @param methodMatcher 81 * matches the methods 82 * 83 * @return The methods matching the supplied matcher 84 */ 85 List<FluentMethod> methods(Matcher<? super FluentMethod> methodMatcher); 86 87 /** 88 * Find the first method matching the supplied matcher 89 * 90 * @param methodMatcher 91 * matches the method 92 * 93 * @return The method matching the supplied matcher 94 */ 95 FluentMethod method(Matcher<? super FluentMethod> methodMatcher); 96 97 /** 98 * Find the first method with the given name 99 * 100 * @param name 101 * the name of the method 102 * 103 * @return The method matching the name 104 */ 105 FluentMethod method(String name); 106 107 /** 108 * Call a method by name if one can be found that can be called with the given arguments 109 * 110 * @param name the name of the method 111 * @param args the arguments of the method 112 * 113 * @return the result of calling the method 114 */ 115 FluentObject<?> call(String name, Object ... args); 116 117 /** 118 * Call a method using a matcher, if one can be found that can also be called with the given arguments 119 * 120 * @param methodMatcher matches the method 121 * @param args the arguments of the method 122 * 123 * @return the result of calling the method 124 */ 125 FluentObject<?> call(Matcher<? super FluentMethod> methodMatcher, Object ... args); 126 127 /** 128 * All fields 129 * 130 * @return all the fields 131 */ 132 List<FluentField> fields(); 133 134 /** 135 * Find all fields matching the supplied matcher 136 * 137 * @param fieldMatcher 138 * matches the field 139 * 140 * @return The fields matching the supplied matcher 141 */ 142 List<FluentField> fields(ReflectionMatcher<? super FluentField> fieldMatcher); 143 144 /** 145 * All fields declared by this type 146 * 147 * @return fields declared by this type 148 */ 149 List<FluentField> declaredFields(); 150 151 /** 152 * Find field matching the supplied matcher 153 * 154 * @param fieldMatcher 155 * matches the field 156 * 157 * @return The first field matching the supplied matcher 158 */ 159 FluentField field(ReflectionMatcher<FluentMember> fieldMatcher); 160 161 /** 162 * Determines if this {@code Class} object represents a 163 * primitive type. 164 * 165 * @return true if and only if this class represents a primitive type 166 */ 167 boolean isPrimitive(); 168 169 /** 170 * Determines if this {@code Class} object represents the wrapper of a 171 * primitive type. 172 * 173 * @return true iff this class is one of the primative wrapper types 174 */ 175 boolean isUnboxable(); 176 177 /** 178 * Determines if this {@code Class} object is a primitive wrapper 179 * type, and the unwrapped type can be assigned from the parameter. 180 * Can the parameter be boxed into this type? 181 * 182 * @param from the type that maybe be assignable to this type 183 * 184 * @return true iff this class is a primitive wrapper type, and the 185 * unwrapped type can be assigned from the parameter 186 */ 187 boolean canBeBoxed(Class<?> from); 188 189 /** 190 * Determines if this {@code Class} object is a primitive 191 * type, and the wrapped type can be assigned from the parameter. 192 * Can the parameter be unboxed into this type? 193 * 194 * @param from the type that maybe be assignable to this type 195 * 196 * @return true iff this class is a primitive type, and the 197 * wrapped type can be assigned from the parameter 198 */ 199 boolean canBeUnboxed(Class<?> from); 200 201 /** 202 * If this class is a primitive type return the wrapped type 203 * 204 * @return the wrapped type or this 205 */ 206 FluentClass<T> boxedType(); 207 208 /** 209 * If this class is a primitive wrapper type return the unwrapped type 210 * 211 * @return the unwrapped type or this 212 */ 213 FluentClass<T> unboxedType(); 214 215 /** 216 * True if the given object can be assigned to a variable of the type 217 * represented by this class 218 * 219 * @param value 220 * the value that might be assigned 221 * 222 * @return true iff the value can be assigned to variables of this type 223 */ 224 boolean assignableFromObject(Object value); 225 226 /** 227 * Is the parameter assignable from this class? 228 * 229 * @param klass the class that may be assignable to 230 * 231 * @return true iff the parameter is assignable from this class 232 */ 233 boolean assignableTo(Class<?> klass); 234 235 FluentClass<?> typeArgument(int typeParameter); 236 237 /** 238 * @return the name of the class under reflection 239 */ 240 String name(); 241 242 /** 243 * @return the simple name of the class under reflection 244 */ 245 String simpleName(); 246 247 /** 248 * @return the underlying type instance 249 */ 250 Type type(); 251 }