1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 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.badlogic.gwtref.client; 18 19 import java.lang.annotation.Annotation; 20 import java.util.Arrays; 21 22 /** Describes a method of a {@link Type}. 23 * @author mzechner */ 24 public class Method { 25 private static final Parameter[] EMPTY_PARAMS = new Parameter[0]; 26 final String name; 27 final CachedTypeLookup enclosingType; 28 final CachedTypeLookup returnType; 29 final boolean isAbstract; 30 final boolean isFinal; 31 final boolean isStatic; 32 final boolean isNative; 33 final boolean isDefaultAccess; 34 final boolean isPrivate; 35 final boolean isProtected; 36 final boolean isPublic; 37 final boolean isVarArgs; 38 final boolean isMethod; 39 final boolean isConstructor; 40 final Parameter[] parameters; 41 final int methodId; 42 final Annotation[] annotations; 43 44 public Method (String name, Class enclosingType, Class returnType, Parameter[] parameters, boolean isAbstract, 45 boolean isFinal, boolean isStatic, boolean isDefaultAccess, boolean isPrivate, boolean isProtected, boolean isPublic, 46 boolean isNative, boolean isVarArgs, boolean isMethod, boolean isConstructor, int methodId, Annotation[] annotations) { 47 this.name = name; 48 this.enclosingType = new CachedTypeLookup(enclosingType); 49 this.parameters = parameters != null ? parameters : EMPTY_PARAMS; 50 this.returnType = new CachedTypeLookup(returnType); 51 this.isAbstract = isAbstract; 52 this.isFinal = isFinal; 53 this.isStatic = isStatic; 54 this.isNative = isNative; 55 this.isDefaultAccess = isDefaultAccess; 56 this.isPrivate = isPrivate; 57 this.isProtected = isProtected; 58 this.isPublic = isPublic; 59 this.isVarArgs = isVarArgs; 60 this.isMethod = isMethod; 61 this.isConstructor = isConstructor; 62 this.methodId = methodId; 63 this.annotations = annotations; 64 } 65 66 /** @return the {@link Class} of the enclosing type. */ 67 public Class getEnclosingType () { 68 return enclosingType.clazz; 69 } 70 71 /** @return the {@link Class} of the return type or null. */ 72 public Class getReturnType () { 73 return returnType.clazz; 74 } 75 76 /** @return the list of parameters, can be a zero size array. */ 77 public Parameter[] getParameters () { 78 return parameters; 79 } 80 81 /** @return the name of the method. */ 82 public String getName () { 83 return name; 84 } 85 86 public boolean isAbstract () { 87 return isAbstract; 88 } 89 90 public boolean isFinal () { 91 return isFinal; 92 } 93 94 public boolean isDefaultAccess () { 95 return isDefaultAccess; 96 } 97 98 public boolean isPrivate () { 99 return isPrivate; 100 } 101 102 public boolean isProtected () { 103 return isProtected; 104 } 105 106 public boolean isPublic () { 107 return isPublic; 108 } 109 110 public boolean isNative () { 111 return isNative; 112 } 113 114 public boolean isVarArgs () { 115 return isVarArgs; 116 } 117 118 public boolean isStatic () { 119 return isStatic; 120 } 121 122 public boolean isMethod () { 123 return isMethod; 124 } 125 126 public boolean isConstructor () { 127 return isConstructor; 128 } 129 130 public Annotation[] getDeclaredAnnotations () { 131 return annotations; 132 } 133 134 /** Invokes the method on the given object. Ignores the object if this is a static method. Throws an IllegalArgumentException if 135 * the parameters do not match. 136 * @param obj the object to invoke the method on or null. 137 * @param params the parameters to pass to the method or null. 138 * @return the return value or null if the method does not return anything. */ 139 public Object invoke (Object obj, Object... params) { 140 if (parameters.length != (params != null ? params.length : 0)) throw new IllegalArgumentException("Parameter mismatch"); 141 142 return ReflectionCache.invoke(this, obj, params); 143 } 144 145 boolean match (String name, Class... types) { 146 return this.name.equals(name) && match(types); 147 } 148 149 boolean match (Class... types) { 150 if (types == null) return parameters.length == 0; 151 if (types.length != parameters.length) return false; 152 for (int i = 0; i < types.length; i++) { 153 Type t1 = parameters[i].getType(); 154 Type t2 = ReflectionCache.getType(types[i]); 155 if (t1 != t2 && !t1.isAssignableFrom(t2)) return false; 156 } 157 return true; 158 } 159 160 @Override 161 public String toString () { 162 return "Method [name=" + name + ", enclosingType=" + enclosingType + ", returnType=" + returnType + ", isAbstract=" 163 + isAbstract + ", isFinal=" + isFinal + ", isStatic=" + isStatic + ", isNative=" + isNative + ", isDefaultAccess=" 164 + isDefaultAccess + ", isPrivate=" + isPrivate + ", isProtected=" + isProtected + ", isPublic=" + isPublic 165 + ", isVarArgs=" + isVarArgs + ", isMethod=" + isMethod + ", isConstructor=" + isConstructor + ", parameters=" 166 + Arrays.toString(parameters) + "]"; 167 } 168 } 169