
leap.lang.reflect.ReflectMethod Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package leap.lang.reflect;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import leap.lang.Classes;
import leap.lang.Strings;
public class ReflectMethod extends ReflectMember{
private final ReflectAccessor accessor;
private final int accessorIndex;
private final Method reflectedMethod;
private ReflectParameter[] parameters;
private boolean setter;
private boolean getter;
protected ReflectMethod(ReflectClass reflectClass,Method method) {
super(reflectClass,method);
this.accessor = reflectClass.getAccessor();
this.reflectedMethod = method;
this.accessorIndex = accessor == null ? -1 : accessor.getMethodIndex(method);
this.initialize();
}
public String getName() {
return reflectedMethod.getName();
}
public Method getReflectedMethod(){
return this.reflectedMethod;
}
public ReflectParameter[] getParameters(){
return parameters;
}
public Class> getReturnType(){
return reflectedMethod.getReturnType();
}
public boolean isStatic(){
return Modifier.isStatic(reflectedMethod.getModifiers());
}
public boolean isSynthetic(){
return reflectedMethod.isSynthetic();
}
public boolean isGetterMethod(){
return getter;
}
public boolean isSetterMethod(){
return setter;
}
public boolean isAnnotationPresent(Class extends Annotation> annotationClsss){
return reflectedMethod.isAnnotationPresent(annotationClsss);
}
public T getAnnotation(Class annotationClass){
return reflectedMethod.getAnnotation(annotationClass);
}
public Annotation[] getAnnotations(){
return reflectedMethod.getAnnotations();
}
public boolean hasReturnValue(){
return !Classes.isVoid(reflectedMethod.getReturnType());
}
public Object invoke(Object instance,Object... args) {
try {
if(parameters.length != args.length){
throw new IllegalArgumentException("argument's length must be " + parameters.length);
}
for(int i=0;i 0){
String[] names = Reflection.getParameterNames(reflectedMethod);
if(null == names){
names = createUnknowParameterNames(parameters.length);
}
for(int i=0;i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy